/
githubmirror
/
cmssw
Обзор
Документация
Войти
/
githubmirror
/
cmssw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
L1Trigger/TrackFindingTracklet/test/AnalyzerTM.cc
288 строк
13 KB
tschuh
quinnanm 1
16 апр 2025, 23:34
16 апр 2025, 23:34
e51e86a
Код
Авторство
О чём код?
#include "FWCore/Framework/interface/one/EDAnalyzer.h" #include "FWCore/Framework/interface/Run.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/EventSetup.h" #include "FWCore/Framework/interface/MakerMacros.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/ServiceRegistry/interface/Service.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Utilities/interface/EDGetToken.h" #include "FWCore/Utilities/interface/InputTag.h" #include "FWCore/Utilities/interface/Exception.h" #include "CommonTools/UtilAlgos/interface/TFileService.h" #include "DataFormats/Common/interface/Handle.h" #include "SimTracker/TrackTriggerAssociation/interface/StubAssociation.h" #include "L1Trigger/TrackTrigger/interface/Setup.h" #include "L1Trigger/TrackFindingTracklet/interface/DataFormats.h" #include "L1Trigger/TrackFindingTracklet/interface/ChannelAssignment.h" #include <TProfile.h> #include <TH1F.h> #include <vector> #include <deque> #include <map> #include <set> #include <cmath> #include <numeric> #include <sstream> namespace trklet { /*! \class trklet::AnalyzerTM * \brief Class to analyze hardware like structured TTStub Collection generated by TM module * \author Thomas Schuh * \date 2023, Jan */ class AnalyzerTM : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> { public: AnalyzerTM(const edm::ParameterSet& iConfig); void beginJob() override {} void beginRun(const edm::Run& iEvent, const edm::EventSetup& iSetup) override; void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override; void endRun(const edm::Run& iEvent, const edm::EventSetup& iSetup) override {} void endJob() override; private: // void formTracks(const tt::StreamsTrack& streamsTrack, const tt::StreamsStub& streamsStubs, std::vector<std::vector<TTStubRef>>& tracks, int channel) const; // void associate(const std::vector<std::vector<TTStubRef>>& tracks, const tt::StubAssociation* ass, std::set<TPPtr>& tps, int& sum, bool perfect = false) const; // ED input token of stubs edm::EDGetTokenT<tt::StreamsStub> edGetTokenStubs_; // ED input token of tracks edm::EDGetTokenT<tt::StreamsTrack> edGetTokenTracks_; // ED input token of TTStubRef to TPPtr association for tracking efficiency edm::EDGetTokenT<tt::StubAssociation> edGetTokenSelection_; // ED input token of TTStubRef to recontructable TPPtr association edm::EDGetTokenT<tt::StubAssociation> edGetTokenReconstructable_; // Setup token edm::ESGetToken<tt::Setup, tt::SetupRcd> esGetTokenSetup_; // DataFormats token edm::ESGetToken<DataFormats, ChannelAssignmentRcd> esGetTokenDataFormats_; // ChannelAssignment token edm::ESGetToken<ChannelAssignment, ChannelAssignmentRcd> esGetTokenChannelAssignment_; // stores, calculates and provides run-time constants const tt::Setup* setup_ = nullptr; // helper class to extract structured data from tt::Frames const DataFormats* dataFormats_ = nullptr; // helper class to assign tracklet track to channel const ChannelAssignment* channelAssignment_ = nullptr; // enables analyze of TPs bool useMCTruth_; // int nEvents_ = 0; // Histograms TProfile* prof_; TProfile* profChannel_; TH1F* hisChannel_; // printout std::stringstream log_; }; AnalyzerTM::AnalyzerTM(const edm::ParameterSet& iConfig) : useMCTruth_(iConfig.getParameter<bool>("UseMCTruth")) { usesResource("TFileService"); // book in- and output ED products const std::string& label = iConfig.getParameter<std::string>("OutputLabelTM"); const std::string& branchStubs = iConfig.getParameter<std::string>("BranchStubs"); const std::string& branchTracks = iConfig.getParameter<std::string>("BranchTracks"); edGetTokenStubs_ = consumes<tt::StreamsStub>(edm::InputTag(label, branchStubs)); edGetTokenTracks_ = consumes<tt::StreamsTrack>(edm::InputTag(label, branchTracks)); if (useMCTruth_) { const auto& inputTagSelecttion = iConfig.getParameter<edm::InputTag>("InputTagSelection"); const auto& inputTagReconstructable = iConfig.getParameter<edm::InputTag>("InputTagReconstructable"); edGetTokenSelection_ = consumes<tt::StubAssociation>(inputTagSelecttion); edGetTokenReconstructable_ = consumes<tt::StubAssociation>(inputTagReconstructable); } // book ES products esGetTokenSetup_ = esConsumes<edm::Transition::BeginRun>(); esGetTokenDataFormats_ = esConsumes<edm::Transition::BeginRun>(); esGetTokenChannelAssignment_ = esConsumes<edm::Transition::BeginRun>(); // log config log_.setf(std::ios::fixed, std::ios::floatfield); log_.precision(4); } void AnalyzerTM::beginRun(const edm::Run& iEvent, const edm::EventSetup& iSetup) { // helper class to store configurations setup_ = &iSetup.getData(esGetTokenSetup_); // helper class to extract structured data from tt::Frames dataFormats_ = &iSetup.getData(esGetTokenDataFormats_); // helper class to assign tracklet track to channel channelAssignment_ = &iSetup.getData(esGetTokenChannelAssignment_); // book histograms edm::Service<TFileService> fs; TFileDirectory dir; dir = fs->mkdir("TM"); prof_ = dir.make<TProfile>("Counts", ";", 10, 0.5, 10.5); prof_->GetXaxis()->SetBinLabel(1, "Stubs"); prof_->GetXaxis()->SetBinLabel(2, "Tracks"); prof_->GetXaxis()->SetBinLabel(4, "Matched Tracks"); prof_->GetXaxis()->SetBinLabel(5, "All Tracks"); prof_->GetXaxis()->SetBinLabel(6, "Found TPs"); prof_->GetXaxis()->SetBinLabel(7, "Found selected TPs"); prof_->GetXaxis()->SetBinLabel(9, "All TPs"); prof_->GetXaxis()->SetBinLabel(10, "Perfect TPs"); // channel occupancy constexpr int maxOcc = 180; const int numChannels = 1; hisChannel_ = dir.make<TH1F>("His Channel Occupancy", ";", maxOcc, -.5, maxOcc - .5); profChannel_ = dir.make<TProfile>("Prof Channel Occupancy", ";", numChannels, -.5, numChannels - .5); } void AnalyzerTM::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) { // read in ht products edm::Handle<tt::StreamsStub> handleStubs; iEvent.getByToken<tt::StreamsStub>(edGetTokenStubs_, handleStubs); const tt::StreamsStub& streamsStub = *handleStubs; edm::Handle<tt::StreamsTrack> handleTracks; iEvent.getByToken<tt::StreamsTrack>(edGetTokenTracks_, handleTracks); const tt::StreamsTrack& streamsTrack = *handleTracks; // read in MCTruth const tt::StubAssociation* selection = nullptr; const tt::StubAssociation* reconstructable = nullptr; if (useMCTruth_) { edm::Handle<tt::StubAssociation> handleSelection; iEvent.getByToken<tt::StubAssociation>(edGetTokenSelection_, handleSelection); selection = handleSelection.product(); prof_->Fill(9, selection->numTPs()); edm::Handle<tt::StubAssociation> handleReconstructable; iEvent.getByToken<tt::StubAssociation>(edGetTokenReconstructable_, handleReconstructable); reconstructable = handleReconstructable.product(); } // analyze ht products and associate found tracks with reconstrucable TrackingParticles std::set<TPPtr> tpPtrs; std::set<TPPtr> tpPtrsSelection; std::set<TPPtr> tpPtrsPerfect; int allMatched(0); int allTracks(0); for (int region = 0; region < setup_->numRegions(); region++) { int nStubs(0); int nTracks(0); std::vector<std::vector<TTStubRef>> tracks; formTracks(streamsTrack, streamsStub, tracks, region); nTracks += tracks.size(); nStubs += std::accumulate(tracks.begin(), tracks.end(), 0, [](int sum, const std::vector<TTStubRef>& track) { return sum + track.size(); }); allTracks += tracks.size(); if (!useMCTruth_) continue; int tmp(0); associate(tracks, selection, tpPtrsSelection, tmp); associate(tracks, selection, tpPtrsPerfect, tmp, true); associate(tracks, reconstructable, tpPtrs, allMatched); const tt::StreamTrack& stream = streamsTrack[region]; const auto end = std::find_if( stream.rbegin(), stream.rend(), [](const tt::FrameTrack& frame) { return frame.first.isNonnull(); }); const int size = std::distance(stream.begin(), end.base()) - 1; hisChannel_->Fill(size); profChannel_->Fill(1, size); prof_->Fill(1, nStubs); prof_->Fill(2, nTracks); } prof_->Fill(4, allMatched); prof_->Fill(5, allTracks); prof_->Fill(6, tpPtrs.size()); prof_->Fill(7, tpPtrsSelection.size()); prof_->Fill(10, tpPtrsPerfect.size()); nEvents_++; } void AnalyzerTM::endJob() { if (nEvents_ == 0) return; // printout summary const double totalTPs = prof_->GetBinContent(9); const double numStubs = prof_->GetBinContent(1); const double numTracks = prof_->GetBinContent(2); const double totalTracks = prof_->GetBinContent(5); const double numTracksMatched = prof_->GetBinContent(4); const double numTPsAll = prof_->GetBinContent(6); const double numTPsEff = prof_->GetBinContent(7); const double numTPsEffPerfect = prof_->GetBinContent(10); const double errStubs = prof_->GetBinError(1); const double errTracks = prof_->GetBinError(2); const double fracFake = (totalTracks - numTracksMatched) / totalTracks; const double fracDup = (numTracksMatched - numTPsAll) / totalTracks; const double eff = numTPsEff / totalTPs; const double errEff = std::sqrt(eff * (1. - eff) / totalTPs / nEvents_); const double effPerfect = numTPsEffPerfect / totalTPs; const double errEffPerfect = std::sqrt(effPerfect * (1. - effPerfect) / totalTPs / nEvents_); const std::vector<double> nums = {numStubs, numTracks}; const std::vector<double> errs = {errStubs, errTracks}; const int wNums = std::ceil(std::log10(*std::max_element(nums.begin(), nums.end()))) + 5; const int wErrs = std::ceil(std::log10(*std::max_element(errs.begin(), errs.end()))) + 5; log_ << " TM SUMMARY " << std::endl; log_ << "number of stubs per TFP = " << std::setw(wNums) << numStubs << " +- " << std::setw(wErrs) << errStubs << std::endl; log_ << "number of tracks per TFP = " << std::setw(wNums) << numTracks << " +- " << std::setw(wErrs) << errTracks << std::endl; log_ << " current tracking efficiency = " << std::setw(wNums) << effPerfect << " +- " << std::setw(wErrs) << errEffPerfect << std::endl; log_ << " max tracking efficiency = " << std::setw(wNums) << eff << " +- " << std::setw(wErrs) << errEff << std::endl; log_ << " fake rate = " << std::setw(wNums) << fracFake << std::endl; log_ << " duplicate rate = " << std::setw(wNums) << fracDup << std::endl; log_ << "============================================================="; edm::LogPrint(moduleDescription().moduleName()) << log_.str(); } // void AnalyzerTM::formTracks(const tt::StreamsTrack& streamsTrack, const tt::StreamsStub& streamsStubs, std::vector<std::vector<TTStubRef>>& tracks, int channel) const { static const int numLayers = channelAssignment_->tmNumLayers(); const int offset = channel * numLayers; const tt::StreamTrack& streamTrack = streamsTrack[channel]; const int numTracks = std::accumulate(streamTrack.begin(), streamTrack.end(), 0, [](int sum, const tt::FrameTrack& frame) { return sum + (frame.first.isNonnull() ? 1 : 0); }); tracks.reserve(numTracks); for (int frame = 0; frame < static_cast<int>(streamTrack.size()); frame++) { const tt::FrameTrack& frameTrack = streamTrack[frame]; if (frameTrack.first.isNull()) continue; std::vector<TTStubRef> ttStubRefs; ttStubRefs.reserve(numLayers); for (int layer = 0; layer < numLayers; layer++) { const tt::FrameStub& stub = streamsStubs[offset + layer][frame]; if (stub.first.isNonnull()) ttStubRefs.push_back(stub.first); } tracks.push_back(ttStubRefs); } } // void AnalyzerTM::associate(const std::vector<std::vector<TTStubRef>>& tracks, const tt::StubAssociation* ass, std::set<TPPtr>& tps, int& sum, bool perfect) const { for (const std::vector<TTStubRef>& ttStubRefs : tracks) { const std::vector<TPPtr>& tpPtrs = perfect ? ass->associateFinal(ttStubRefs) : ass->associate(ttStubRefs); if (tpPtrs.empty()) continue; sum++; std::copy(tpPtrs.begin(), tpPtrs.end(), std::inserter(tps, tps.begin())); } } } // namespace trklet DEFINE_FWK_MODULE(trklet::AnalyzerTM);