CMS 3D CMS Logo

HLTBitAnalyzer.cc
Go to the documentation of this file.
1 // File: HLTBitAnalyzer.cc
2 // Description: Example of Analysis driver originally from Jeremy Mans,
3 // Date: 13-October-2006
4 
5 #include <boost/foreach.hpp>
6 
9 
10 #include "HLTMessages.h"
11 #include "HLTBitAnalyzer.h"
12 
13 typedef std::pair<const char *, const edm::InputTag *> MissingCollectionInfo;
14 
15 template <class T>
16 static inline
17 bool getCollection(const edm::Event & event, std::vector<MissingCollectionInfo> & missing, edm::Handle<T> & handle, const edm::InputTag & name, const edm::EDGetTokenT<T> token, const char * description)
18 {
19  event.getByToken(token, handle);
20  bool valid = handle.isValid();
21  if (not valid) {
22  missing.push_back( std::make_pair(description, & name) );
23  handle.clear();
24  // std::cout << "not valid "<< description << " " << name << std::endl;
25  }
26  return valid;
27 }
28 
29 // Boiler-plate constructor definition of an analyzer module:
30 //HLTBitAnalyzer::HLTBitAnalyzer(edm::ParameterSet const& conf) {
32  hlt_analysis_(conf, consumesCollector(), *this) {
33 
34  // If your module takes parameters, here is where you would define
35  // their names and types, and access them to initialize internal
36  // variables. Example as follows:
37  std::cout << " Beginning HLTBitAnalyzer Analysis " << std::endl;
38 
39  /*
40  l1extramu_ = conf.getParameter<std::string> ("l1extramu");
41  m_l1extramu = edm::InputTag(l1extramu_, "");
42 
43  // read the L1Extra collection name, and add the instance names as needed
44  l1extramc_ = conf.getParameter<std::string> ("l1extramc");
45  m_l1extraemi = edm::InputTag(l1extramc_, "Isolated");
46  m_l1extraemn = edm::InputTag(l1extramc_, "NonIsolated");
47  m_l1extrajetc = edm::InputTag(l1extramc_, "Central");
48  m_l1extrajetf = edm::InputTag(l1extramc_, "Forward");
49  m_l1extrajet = edm::InputTag("gctInternJetProducer","Internal","ANALYSIS");
50  m_l1extrataujet = edm::InputTag(l1extramc_, "Tau");
51  m_l1extramet = edm::InputTag(l1extramc_, "MET");
52  m_l1extramht = edm::InputTag(l1extramc_, "MHT");
53  */
54 
55  mctruth_ = conf.getParameter<edm::InputTag> ("mctruth");
56  genEventInfo_ = conf.getParameter<edm::InputTag> ("genEventInfo");
57  VertexTagOffline0_ = conf.getParameter<edm::InputTag> ("OfflinePrimaryVertices0");
58  simhits_ = conf.getParameter<edm::InputTag> ("simhits");
59 
60  hltresults_ = conf.getParameter<edm::InputTag> ("hltresults");
61  l1results_ = conf.getParameter<edm::InputTag> ("l1results");
62  // gtReadoutRecord_ = conf.getParameter<edm::InputTag> ("l1GtReadoutRecord");
63  /*
64  gtObjectMap_ = conf.getParameter<edm::InputTag> ("l1GtObjectMapRecord");
65 
66  gctBitCounts_ = edm::InputTag( conf.getParameter<edm::InputTag>("l1GctHFBitCounts").label(), "" );
67  gctRingSums_ = edm::InputTag( conf.getParameter<edm::InputTag>("l1GctHFRingSums").label(), "" );
68  */
69  pileupInfo_ = edm::InputTag("addPileupInfo");
70 
71  hltresultsToken_ = consumes<edm::TriggerResults>(hltresults_);
72  genEventInfoToken_ = consumes<GenEventInfoProduct>(genEventInfo_);
73  /*
74  l1extramuToken_ = consumes<l1extra::L1MuonParticleCollection>(m_l1extramu);
75  l1extraemiToken_ = consumes<l1extra::L1EmParticleCollection>(m_l1extraemi);
76  l1extraemnToken_ = consumes<l1extra::L1EmParticleCollection>(m_l1extraemn);
77 
78  l1extrajetcToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrajetc);
79  l1extrajetfToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrajetf);
80  l1extrajetToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrajet);
81  l1extrataujetToken_ = consumes<l1extra::L1JetParticleCollection>(m_l1extrataujet);
82  l1extrametToken_ = consumes<l1extra::L1EtMissParticleCollection>(m_l1extramet);
83  l1extramhtToken_ = consumes<l1extra::L1EtMissParticleCollection>(m_l1extramht);
84  */
85 
86  l1resultsToken_ = consumes<GlobalAlgBlkBxCollection>(l1results_);
87  // gtReadoutRecordToken_ = consumes<L1GlobalTriggerReadoutRecord>(gtReadoutRecord_);
88  /*
89  gtObjectMapToken_ = consumes<L1GlobalTriggerObjectMapRecord>(gtObjectMap_);
90  gctBitCountsToken_ = consumes<L1GctHFBitCountsCollection>(gctBitCounts_);
91  gctRingSumsToken_ = consumes<L1GctHFRingEtSumsCollection>(gctRingSums_);
92  */
93 
94  mctruthToken_ = consumes<reco::CandidateView>(mctruth_);
95  VertexTagOffline0Token_ = consumes<reco::VertexCollection>(VertexTagOffline0_);
96  simtracksToken_ = consumes<std::vector<SimTrack> >(simhits_);
97  simverticesToken_ = consumes<std::vector<SimVertex> >(simhits_);
98  pileupInfoToken_ = consumes<std::vector<PileupSummaryInfo> >(pileupInfo_);
99 
100  _UseTFileService = conf.getUntrackedParameter<bool>("UseTFileService",false);
101 
102  m_file = nullptr; // set to null
103  errCnt = 0;
104 
105  // read run parameters with a default value
106  edm::ParameterSet runParameters = conf.getParameter<edm::ParameterSet>("RunParameters");
107  _HistName = runParameters.getUntrackedParameter<std::string>("HistogramFile", "test.root");
108  _isData = runParameters.getUntrackedParameter<bool>("isData",true);
109 
110  // open the tree file and initialize the tree
111  if(_UseTFileService){
113  HltTree = fs->make<TTree>("HltTree", "");
114  }else{
115  m_file = new TFile(_HistName.c_str(), "RECREATE");
116  if (m_file)
117  m_file->cd();
118  HltTree = new TTree("HltTree", "");
119  }
120 
121  // Setup the different analysis
122  hlt_analysis_.setup(conf, HltTree);
123  if (!_isData) { mct_analysis_.setup(conf, HltTree); }
124  vrt_analysisOffline0_.setup(conf, HltTree, "Offline0");
126 }
127 
128 // Boiler-plate "analyze" method declaration for an analyzer module.
130 
133  /*
134  edm::Handle<l1extra::L1EmParticleCollection> l1extemi, l1extemn;
135  edm::Handle<l1extra::L1MuonParticleCollection> l1extmu;
136  edm::Handle<l1extra::L1JetParticleCollection> l1extjetc, l1extjetf, l1extjet, l1exttaujet;
137  edm::Handle<l1extra::L1EtMissParticleCollection> l1extmet,l1extmht;
138  edm::Handle<L1GlobalTriggerReadoutRecord> l1GtRR;
139  edm::Handle<L1GlobalTriggerObjectMapRecord> l1GtOMRec;
140  edm::Handle<L1GlobalTriggerObjectMap> l1GtOM;
141  edm::Handle< L1GctHFBitCountsCollection > gctBitCounts ;
142  edm::Handle< L1GctHFRingEtSumsCollection > gctRingSums ;
143  */
148  edm::Handle<reco::VertexCollection> recoVertexsOffline0;
150 
151  // extract the collections from the event, check their validity and log which are missing
152  std::vector<MissingCollectionInfo> missing;
153 
154  getCollection( iEvent, missing, hltresults, hltresults_, hltresultsToken_, kHltresults );
155  /*
156  getCollection( iEvent, missing, l1extemi, m_l1extraemi, l1extraemiToken_, kL1extemi );
157  getCollection( iEvent, missing, l1extemn, m_l1extraemn, l1extraemnToken_, kL1extemn );
158  getCollection( iEvent, missing, l1extmu, m_l1extramu, l1extramuToken_, kL1extmu );
159  getCollection( iEvent, missing, l1extjetc, m_l1extrajetc, l1extrajetcToken_, kL1extjetc );
160  getCollection( iEvent, missing, l1extjetf, m_l1extrajetf, l1extrajetfToken_, kL1extjetf );
161  getCollection( iEvent, missing, l1extjet, m_l1extrajet, l1extrajetToken_, kL1extjet );
162  getCollection( iEvent, missing, l1exttaujet, m_l1extrataujet, l1extrataujetToken_, kL1exttaujet );
163  getCollection( iEvent, missing, l1extmet, m_l1extramet, l1extrametToken_, kL1extmet );
164  getCollection( iEvent, missing, l1extmht, m_l1extramht, l1extramhtToken_, kL1extmht );
165  */
166  getCollection( iEvent, missing, l1results, l1results_, l1resultsToken_, kL1GtRR) ;
167  // getCollection( iEvent, missing, l1GtRR, gtReadoutRecord_, gtReadoutRecordToken_, kL1GtRR );
168  /*
169  getCollection( iEvent, missing, l1GtOMRec, gtObjectMap_, gtObjectMapToken_, kL1GtOMRec );
170  getCollection( iEvent, missing, gctBitCounts, gctBitCounts_, gctBitCountsToken_, kL1GctBitCounts );
171  getCollection( iEvent, missing, gctRingSums, gctRingSums_, gctRingSumsToken_, kL1GctRingSums );
172  */
173  getCollection( iEvent, missing, mctruth, mctruth_, mctruthToken_, kMctruth );
174  getCollection( iEvent, missing, simTracks, simhits_, simtracksToken_, kSimhit );
175  getCollection( iEvent, missing, simVertices, simhits_, simverticesToken_, kSimhit );
176  getCollection( iEvent, missing, genEventInfo, genEventInfo_, genEventInfoToken_, kGenEventInfo );
177  getCollection( iEvent, missing, pupInfo, pileupInfo_, pileupInfoToken_, kPileupInfo );
178 
179  getCollection( iEvent, missing, recoVertexsOffline0, VertexTagOffline0_, VertexTagOffline0Token_, kRecoVerticesOffline0 );
180 
181  if (!_isData) {
182  ptHat=-1.;
183  if (genEventInfo.isValid()) {ptHat=genEventInfo->qScale();}
184 
185  weight = genEventInfo->weight();
186  }
187  // print missing collections
188  if (not missing.empty() and (errCnt < errMax())) {
189  errCnt++;
190  std::stringstream out;
191  out << "OpenHLT analyser - missing collections:";
192  BOOST_FOREACH(const MissingCollectionInfo & entry, missing)
193  out << "\n\t" << entry.first << ": " << entry.second->encode();
194  edm::LogPrint("OpenHLT") << out.str() << std::endl;
195  if (errCnt == errMax())
196  edm::LogWarning("OpenHLT") << "Maximum error count reached -- No more messages will be printed.";
197  }
198 
199  // run the analysis, passing required event fragments
201  hltresults,
202  /*
203  l1extemi,
204  l1extemn,
205  l1extmu,
206  l1extjetc,
207  l1extjetf,
208  l1extjet,
209  l1exttaujet,
210  l1extmet,
211  l1extmht,
212  */
213  l1results,
214  // l1GtRR,
215  /*
216  gctBitCounts,
217  gctRingSums,
218  */
219  iSetup,
220  iEvent,
221  HltTree);
222 
223  evt_header_.analyze(iEvent, HltTree);
224 
225  if (!_isData) {
227  mctruth,
228  ptHat,
229  weight,
230  simTracks,
231  simVertices,
232  pupInfo,
233  HltTree);
234  }
236  recoVertexsOffline0,
237  HltTree);
238 
239  // std::cout << " Ending Event Analysis" << std::endl;
240  // After analysis, fill the variables tree
241  if (m_file) { m_file->cd(); }
242 
243  HltTree->Fill();
244 }
245 
246 // ------------ method called when starting to processes a run ------------
247 void
249 {
250  hlt_analysis_.beginRun(run, es);
251 }
252 
253 // "endJob" is an inherited method that you may implement to do post-EOF processing and produce final output.
255 
256  if(!_UseTFileService){
257  if (m_file)
258  m_file->cd();
259 
260  HltTree->Write();
261  delete HltTree;
262  HltTree = nullptr;
263 
264  if (m_file) { // if there was a tree file...
265  m_file->Write(); // write out the branches
266  delete m_file; // close and delete the file
267  m_file = nullptr; // set to zero to clean up
268  }
269  }
270 }
271 
272 
273 // declare this class as a framework plugin
void analyze(const edm::Handle< edm::TriggerResults > &hltresults, const edm::Handle< GlobalAlgBlkBxCollection > &l1results, edm::EventSetup const &eventSetup, edm::Event const &iEvent, TTree *tree)
Definition: HLTInfo.cc:92
T getParameter(std::string const &) const
void analyze(edm::Event const &iEvent, TTree *tree)
Definition: EventHeader.cc:44
T getUntrackedParameter(std::string const &, T const &) const
edm::InputTag VertexTagOffline0_
void setup(edm::ConsumesCollector &&iC, TTree *tree)
Definition: EventHeader.cc:24
const char * kHltresults
Definition: HLTMessages.cc:34
void setup(const edm::ParameterSet &pSet, TTree *tree)
Definition: HLTInfo.cc:53
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const char * kSimhit
Definition: HLTMessages.cc:49
edm::InputTag hltresults_
void analyze(edm::Event const &e, edm::EventSetup const &iSetup) override
Definition: weight.py:1
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
double weight() const
edm::InputTag l1results_
void endJob() override
RECOVertex vrt_analysisOffline0_
edm::EDGetTokenT< GlobalAlgBlkBxCollection > l1resultsToken_
edm::InputTag genEventInfo_
int iEvent
Definition: GenABIO.cc:230
void setup(const edm::ParameterSet &pSet, TTree *tree, std::string vertexType)
Definition: RECOVertex.cc:49
double qScale() const
std::pair< const char *, const edm::InputTag * > MissingCollectionInfo
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
EventHeader evt_header_
Default analyses.
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileupInfoToken_
const char * kMctruth
Definition: HLTMessages.cc:48
static int errMax()
void analyze(edm::Handle< reco::VertexCollection > recoVertexs, TTree *tree)
Definition: RECOVertex.cc:91
void beginRun(const edm::Run &, const edm::EventSetup &)
Definition: HLTInfo.cc:30
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< GenEventInfoProduct > genEventInfoToken_
static bool getCollection(const edm::Event &event, std::vector< MissingCollectionInfo > &missing, edm::Handle< T > &handle, const edm::InputTag &name, const edm::EDGetTokenT< T > token, const char *description)
std::string _HistName
HLTMCtruth mct_analysis_
void analyze(const edm::Handle< reco::CandidateView > &mctruth, const double &pthat, const double &weight, const edm::Handle< std::vector< SimTrack > > &simTracks, const edm::Handle< std::vector< SimVertex > > &simVertices, const edm::Handle< std::vector< PileupSummaryInfo > > &PupInfo, TTree *tree)
Definition: HLTMCtruth.cc:74
const char * kL1GtRR
Definition: HLTMessages.cc:44
edm::EDGetTokenT< std::vector< SimTrack > > simtracksToken_
edm::InputTag mctruth_
const char * kRecoVerticesOffline0
Definition: HLTMessages.cc:128
const char * kGenEventInfo
Definition: HLTMessages.cc:50
edm::EDGetTokenT< reco::VertexCollection > VertexTagOffline0Token_
edm::InputTag simhits_
edm::EDGetTokenT< edm::TriggerResults > hltresultsToken_
void setup(const edm::ParameterSet &pSet, TTree *tree)
Definition: HLTMCtruth.cc:24
edm::InputTag pileupInfo_
HLTInfo hlt_analysis_
edm::EDGetTokenT< reco::CandidateView > mctruthToken_
HLTBitAnalyzer(edm::ParameterSet const &conf)
void beginRun(edm::Run const &, edm::EventSetup const &) override
edm::EDGetTokenT< std::vector< SimVertex > > simverticesToken_
Definition: event.py:1
Definition: Run.h:44
const char * kPileupInfo
Definition: HLTMessages.cc:51