Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:22

0001 // File: HLTBitAnalyzer.cc
0002 // Description:  Example of Analysis driver originally from Jeremy Mans,
0003 // Date:  13-October-2006
0004 
0005 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007 
0008 #include "HLTMessages.h"
0009 #include "HLTBitAnalyzer.h"
0010 
0011 typedef std::pair<const char*, const edm::InputTag*> MissingCollectionInfo;
0012 
0013 template <class T>
0014 static inline bool getCollection(const edm::Event& event,
0015                                  std::vector<MissingCollectionInfo>& missing,
0016                                  edm::Handle<T>& handle,
0017                                  const edm::InputTag& name,
0018                                  const edm::EDGetTokenT<T> token,
0019                                  const char* description) {
0020   event.getByToken(token, handle);
0021   bool valid = handle.isValid();
0022   if (not valid) {
0023     missing.push_back(std::make_pair(description, &name));
0024     handle.clear();
0025     //  std::cout << "not valid "<< description << " " << name << std::endl;
0026   }
0027   return valid;
0028 }
0029 
0030 // Boiler-plate constructor definition of an analyzer module:
0031 //HLTBitAnalyzer::HLTBitAnalyzer(edm::ParameterSet const& conf) {
0032 HLTBitAnalyzer::HLTBitAnalyzer(edm::ParameterSet const& conf) : hlt_analysis_(conf, consumesCollector(), *this) {
0033   // If your module takes parameters, here is where you would define
0034   // their names and types, and access them to initialize internal
0035   // variables. Example as follows:
0036   std::cout << " Beginning HLTBitAnalyzer Analysis " << std::endl;
0037 
0038   /*
0039   l1extramu_        = conf.getParameter<std::string>   ("l1extramu");
0040   m_l1extramu       = edm::InputTag(l1extramu_, "");
0041 
0042   // read the L1Extra collection name, and add the instance names as needed
0043   l1extramc_        = conf.getParameter<std::string>   ("l1extramc");
0044   m_l1extraemi      = edm::InputTag(l1extramc_, "Isolated");
0045   m_l1extraemn      = edm::InputTag(l1extramc_, "NonIsolated");
0046   m_l1extrajetc     = edm::InputTag(l1extramc_, "Central");
0047   m_l1extrajetf     = edm::InputTag(l1extramc_, "Forward");
0048   m_l1extrajet      = edm::InputTag("gctInternJetProducer","Internal","ANALYSIS");
0049   m_l1extrataujet   = edm::InputTag(l1extramc_, "Tau");
0050   m_l1extramet      = edm::InputTag(l1extramc_, "MET");
0051   m_l1extramht      = edm::InputTag(l1extramc_, "MHT");
0052   */
0053 
0054   mctruth_ = conf.getParameter<edm::InputTag>("mctruth");
0055   genEventInfo_ = conf.getParameter<edm::InputTag>("genEventInfo");
0056   VertexTagOffline0_ = conf.getParameter<edm::InputTag>("OfflinePrimaryVertices0");
0057   simhits_ = conf.getParameter<edm::InputTag>("simhits");
0058 
0059   hltresults_ = conf.getParameter<edm::InputTag>("hltresults");
0060   l1results_ = conf.getParameter<edm::InputTag>("l1results");
0061   //  gtReadoutRecord_  = conf.getParameter<edm::InputTag> ("l1GtReadoutRecord");
0062   /*
0063   gtObjectMap_      = conf.getParameter<edm::InputTag> ("l1GtObjectMapRecord");
0064 
0065   gctBitCounts_        = edm::InputTag( conf.getParameter<edm::InputTag>("l1GctHFBitCounts").label(), "" );
0066   gctRingSums_         = edm::InputTag( conf.getParameter<edm::InputTag>("l1GctHFRingSums").label(), "" );
0067   */
0068   pileupInfo_ = edm::InputTag("addPileupInfo");
0069 
0070   hltresultsToken_ = consumes<edm::TriggerResults>(hltresults_);
0071   genEventInfoToken_ = consumes<GenEventInfoProduct>(genEventInfo_);
0072   /*
0073   l1extramuToken_ = consumes<l1extra::L1MuonParticleCollection>(m_l1extramu);
0074   l1extraemiToken_ = consumes<l1extra::L1EmParticleCollection>(m_l1extraemi);
0075   l1extraemnToken_ = consumes<l1extra::L1EmParticleCollection>(m_l1extraemn);
0076   
0077   l1extrajetcToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrajetc);
0078   l1extrajetfToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrajetf);
0079   l1extrajetToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrajet);
0080   l1extrataujetToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrataujet);
0081   l1extrametToken_ = consumes<l1extra::L1EtMissParticleCollection>(m_l1extramet);
0082   l1extramhtToken_ = consumes<l1extra::L1EtMissParticleCollection>(m_l1extramht);
0083   */
0084 
0085   l1resultsToken_ = consumes<GlobalAlgBlkBxCollection>(l1results_);
0086   //  gtReadoutRecordToken_ = consumes<L1GlobalTriggerReadoutRecord>(gtReadoutRecord_);
0087   /*
0088   gtObjectMapToken_ = consumes<L1GlobalTriggerObjectMapRecord>(gtObjectMap_);
0089   gctBitCountsToken_ = consumes<L1GctHFBitCountsCollection>(gctBitCounts_);
0090   gctRingSumsToken_ = consumes<L1GctHFRingEtSumsCollection>(gctRingSums_);
0091   */
0092 
0093   mctruthToken_ = consumes<reco::CandidateView>(mctruth_);
0094   VertexTagOffline0Token_ = consumes<reco::VertexCollection>(VertexTagOffline0_);
0095   simtracksToken_ = consumes<std::vector<SimTrack> >(simhits_);
0096   simverticesToken_ = consumes<std::vector<SimVertex> >(simhits_);
0097   pileupInfoToken_ = consumes<std::vector<PileupSummaryInfo> >(pileupInfo_);
0098 
0099   _UseTFileService = conf.getUntrackedParameter<bool>("UseTFileService", false);
0100 
0101   m_file = nullptr;  // set to null
0102   errCnt = 0;
0103 
0104   // read run parameters with a default value
0105   edm::ParameterSet runParameters = conf.getParameter<edm::ParameterSet>("RunParameters");
0106   _HistName = runParameters.getUntrackedParameter<std::string>("HistogramFile", "test.root");
0107   _isData = runParameters.getUntrackedParameter<bool>("isData", true);
0108 
0109   // open the tree file and initialize the tree
0110   if (_UseTFileService) {
0111     edm::Service<TFileService> fs;
0112     HltTree = fs->make<TTree>("HltTree", "");
0113   } else {
0114     m_file = new TFile(_HistName.c_str(), "RECREATE");
0115     if (m_file)
0116       m_file->cd();
0117     HltTree = new TTree("HltTree", "");
0118   }
0119 
0120   // Setup the different analysis
0121   hlt_analysis_.setup(conf, HltTree);
0122   if (!_isData) {
0123     mct_analysis_.setup(conf, HltTree);
0124   }
0125   vrt_analysisOffline0_.setup(conf, HltTree, "Offline0");
0126   evt_header_.setup(consumesCollector(), HltTree);
0127 }
0128 
0129 // Boiler-plate "analyze" method declaration for an analyzer module.
0130 void HLTBitAnalyzer::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
0131   edm::Handle<edm::TriggerResults> hltresults;
0132   edm::Handle<GlobalAlgBlkBxCollection> l1results;
0133   /*
0134   edm::Handle<l1extra::L1EmParticleCollection>      l1extemi, l1extemn;
0135   edm::Handle<l1extra::L1MuonParticleCollection>    l1extmu;
0136   edm::Handle<l1extra::L1JetParticleCollection>     l1extjetc, l1extjetf, l1extjet, l1exttaujet;
0137   edm::Handle<l1extra::L1EtMissParticleCollection>  l1extmet,l1extmht;
0138   edm::Handle<L1GlobalTriggerReadoutRecord>         l1GtRR;
0139   edm::Handle<L1GlobalTriggerObjectMapRecord>       l1GtOMRec;
0140   edm::Handle<L1GlobalTriggerObjectMap>             l1GtOM;
0141   edm::Handle< L1GctHFBitCountsCollection >         gctBitCounts ;
0142   edm::Handle< L1GctHFRingEtSumsCollection >        gctRingSums ;
0143   */
0144   edm::Handle<reco::CandidateView> mctruth;
0145   edm::Handle<GenEventInfoProduct> genEventInfo;
0146   edm::Handle<std::vector<SimTrack> > simTracks;
0147   edm::Handle<std::vector<SimVertex> > simVertices;
0148   edm::Handle<reco::VertexCollection> recoVertexsOffline0;
0149   edm::Handle<std::vector<PileupSummaryInfo> > pupInfo;
0150 
0151   // extract the collections from the event, check their validity and log which are missing
0152   std::vector<MissingCollectionInfo> missing;
0153 
0154   getCollection(iEvent, missing, hltresults, hltresults_, hltresultsToken_, kHltresults);
0155   /*
0156   getCollection( iEvent, missing, l1extemi,        m_l1extraemi,       l1extraemiToken_,      kL1extemi );
0157   getCollection( iEvent, missing, l1extemn,        m_l1extraemn,       l1extraemnToken_,      kL1extemn );
0158   getCollection( iEvent, missing, l1extmu,         m_l1extramu,        l1extramuToken_,       kL1extmu );
0159   getCollection( iEvent, missing, l1extjetc,       m_l1extrajetc,      l1extrajetcToken_,     kL1extjetc );
0160   getCollection( iEvent, missing, l1extjetf,       m_l1extrajetf,      l1extrajetfToken_,     kL1extjetf );
0161   getCollection( iEvent, missing, l1extjet,        m_l1extrajet,       l1extrajetToken_,      kL1extjet );
0162   getCollection( iEvent, missing, l1exttaujet,     m_l1extrataujet,    l1extrataujetToken_,   kL1exttaujet );
0163   getCollection( iEvent, missing, l1extmet,        m_l1extramet,       l1extrametToken_,      kL1extmet );
0164   getCollection( iEvent, missing, l1extmht,        m_l1extramht,       l1extramhtToken_,      kL1extmht );
0165   */
0166   getCollection(iEvent, missing, l1results, l1results_, l1resultsToken_, kL1GtRR);
0167   //  getCollection( iEvent, missing, l1GtRR,          gtReadoutRecord_,   gtReadoutRecordToken_, kL1GtRR );
0168   /*
0169   getCollection( iEvent, missing, l1GtOMRec,       gtObjectMap_,       gtObjectMapToken_,     kL1GtOMRec );
0170   getCollection( iEvent, missing, gctBitCounts,    gctBitCounts_,      gctBitCountsToken_,    kL1GctBitCounts );
0171   getCollection( iEvent, missing, gctRingSums,     gctRingSums_,       gctRingSumsToken_,     kL1GctRingSums );
0172   */
0173   getCollection(iEvent, missing, mctruth, mctruth_, mctruthToken_, kMctruth);
0174   getCollection(iEvent, missing, simTracks, simhits_, simtracksToken_, kSimhit);
0175   getCollection(iEvent, missing, simVertices, simhits_, simverticesToken_, kSimhit);
0176   getCollection(iEvent, missing, genEventInfo, genEventInfo_, genEventInfoToken_, kGenEventInfo);
0177   getCollection(iEvent, missing, pupInfo, pileupInfo_, pileupInfoToken_, kPileupInfo);
0178 
0179   getCollection(
0180       iEvent, missing, recoVertexsOffline0, VertexTagOffline0_, VertexTagOffline0Token_, kRecoVerticesOffline0);
0181 
0182   if (!_isData) {
0183     ptHat = -1.;
0184     if (genEventInfo.isValid()) {
0185       ptHat = genEventInfo->qScale();
0186     }
0187 
0188     weight = genEventInfo->weight();
0189   }
0190   // print missing collections
0191   if (not missing.empty() and (errCnt < errMax())) {
0192     errCnt++;
0193     std::stringstream out;
0194     out << "OpenHLT analyser - missing collections:";
0195     for (auto const& entry : missing)
0196       out << "\n\t" << entry.first << ": " << entry.second->encode();
0197     edm::LogPrint("OpenHLT") << out.str() << std::endl;
0198     if (errCnt == errMax())
0199       edm::LogWarning("OpenHLT") << "Maximum error count reached -- No more messages will be printed.";
0200   }
0201 
0202   // run the analysis, passing required event fragments
0203   hlt_analysis_.analyze(hltresults,
0204                         /*
0205     l1extemi,
0206     l1extemn,
0207     l1extmu,
0208     l1extjetc,
0209     l1extjetf,
0210     l1extjet,
0211     l1exttaujet,
0212     l1extmet,
0213     l1extmht,
0214     */
0215                         l1results,
0216                         //    l1GtRR,
0217                         /*
0218     gctBitCounts,
0219     gctRingSums,
0220     */
0221                         iSetup,
0222                         iEvent,
0223                         HltTree);
0224 
0225   evt_header_.analyze(iEvent, HltTree);
0226 
0227   if (!_isData) {
0228     mct_analysis_.analyze(mctruth, ptHat, weight, simTracks, simVertices, pupInfo, HltTree);
0229   }
0230   vrt_analysisOffline0_.analyze(recoVertexsOffline0, HltTree);
0231 
0232   // std::cout << " Ending Event Analysis" << std::endl;
0233   // After analysis, fill the variables tree
0234   if (m_file) {
0235     m_file->cd();
0236   }
0237 
0238   HltTree->Fill();
0239 }
0240 
0241 // ------------ method called when starting to processes a run  ------------
0242 void HLTBitAnalyzer::beginRun(edm::Run const& run, edm::EventSetup const& es) { hlt_analysis_.beginRun(run, es); }
0243 
0244 // "endJob" is an inherited method that you may implement to do post-EOF processing and produce final output.
0245 void HLTBitAnalyzer::endJob() {
0246   if (!_UseTFileService) {
0247     if (m_file)
0248       m_file->cd();
0249 
0250     HltTree->Write();
0251     delete HltTree;
0252     HltTree = nullptr;
0253 
0254     if (m_file) {        // if there was a tree file...
0255       m_file->Write();   // write out the branches
0256       delete m_file;     // close and delete the file
0257       m_file = nullptr;  // set to zero to clean up
0258     }
0259   }
0260 }
0261 
0262 // declare this class as a framework plugin
0263 #include "FWCore/Framework/interface/MakerMacros.h"
0264 DEFINE_FWK_MODULE(HLTBitAnalyzer);