CMS 3D CMS Logo

AnalysisRootpleProducer.cc
Go to the documentation of this file.
1 // Authors: F. Ambroglini, L. Fano'
4 
5 using namespace edm;
6 using namespace std;
7 using namespace reco;
8 
9 class GreaterPt{
10 public:
12  return a.pt() > b.pt();
13  }
14 };
15 
16 class GenJetSort{
17 public:
18  bool operator()(const GenJet& a, const GenJet& b) {
19  return a.pt() > b.pt();
20  }
21 };
22 
24 public:
25  bool operator()(const BasicJet& a, const BasicJet& b) {
26  return a.pt() > b.pt();
27  }
28 };
29 
31 public:
32  bool operator()(const CaloJet& a, const CaloJet& b) {
33  return a.pt() > b.pt();
34  }
35 };
36 
37 
39 
40  AnalysisTree->Fill();
41 
42  NumberMCParticles=0;
43  NumberTracks=0;
44  NumberInclusiveJet=0;
45  NumberChargedJet=0;
46  NumberTracksJet=0;
47  NumberCaloJet=0;
48 }
49 
51  EventKind = e;
52 }
53 
54 void AnalysisRootpleProducer::fillMCParticles(float p, float pt, float eta, float phi){
55  MomentumMC[NumberMCParticles]=p;
56  TransverseMomentumMC[NumberMCParticles]=pt;
57  EtaMC[NumberMCParticles]=eta;
58  PhiMC[NumberMCParticles]=phi;
59  NumberMCParticles++;
60 }
61 
62 void AnalysisRootpleProducer::fillTracks(float p, float pt, float eta, float phi){
63  MomentumTK[NumberTracks]=p;
64  TransverseMomentumTK[NumberTracks]=pt;
65  EtaTK[NumberTracks]=eta;
66  PhiTK[NumberTracks]=phi;
67  NumberTracks++;
68 }
69 
70 void AnalysisRootpleProducer::fillInclusiveJet(float p, float pt, float eta, float phi){
71  MomentumIJ[NumberInclusiveJet]=p;
72  TransverseMomentumIJ[NumberInclusiveJet]=pt;
73  EtaIJ[NumberInclusiveJet]=eta;
74  PhiIJ[NumberInclusiveJet]=phi;
75  NumberInclusiveJet++;
76 }
77 
78 void AnalysisRootpleProducer::fillChargedJet(float p, float pt, float eta, float phi){
79  MomentumCJ[NumberChargedJet]=p;
80  TransverseMomentumCJ[NumberChargedJet]=pt;
81  EtaCJ[NumberChargedJet]=eta;
82  PhiCJ[NumberChargedJet]=phi;
83  NumberChargedJet++;
84 }
85 
86 void AnalysisRootpleProducer::fillTracksJet(float p, float pt, float eta, float phi){
87  MomentumTJ[NumberTracksJet]=p;
88  TransverseMomentumTJ[NumberTracksJet]=pt;
89  EtaTJ[NumberTracksJet]=eta;
90  PhiTJ[NumberTracksJet]=phi;
91  NumberTracksJet++;
92 }
93 
94 void AnalysisRootpleProducer::fillCaloJet(float p, float pt, float eta, float phi){
95  MomentumEHJ[NumberCaloJet]=p;
96  TransverseMomentumEHJ[NumberCaloJet]=pt;
97  EtaEHJ[NumberCaloJet]=eta;
98  PhiEHJ[NumberCaloJet]=phi;
99  NumberCaloJet++;
100 }
101 
103 {
104  // flag to ignore gen-level analysis
105  onlyRECO = pset.getUntrackedParameter<bool>("OnlyRECO",false);
106 
107  // particle, track and jet collections
108  mcEventToken = mayConsume<edm::HepMCProduct>(pset.getUntrackedParameter<InputTag>("MCEvent",std::string("")));
109  genJetCollToken = mayConsume<reco::GenJetCollection>(pset.getUntrackedParameter<InputTag>("GenJetCollectionName",std::string("")));
110  chgJetCollToken = mayConsume<reco::GenJetCollection>(pset.getUntrackedParameter<InputTag>("ChgGenJetCollectionName",std::string("")));
111  tracksJetCollToken = consumes<reco::BasicJetCollection>(pset.getUntrackedParameter<InputTag>("TracksJetCollectionName",std::string("")));
112  recoCaloJetCollToken = consumes<reco::CaloJetCollection>(pset.getUntrackedParameter<InputTag>("RecoCaloJetCollectionName",std::string("")));
113  chgGenPartCollToken = mayConsume<std::vector<reco::GenParticle> >(pset.getUntrackedParameter<InputTag>("ChgGenPartCollectionName",std::string("")));
114  tracksCollToken = consumes<reco::CandidateCollection>(pset.getUntrackedParameter<InputTag>("TracksCollectionName",std::string("")));
115 
116  // trigger results
117  triggerResultsToken = consumes<edm::TriggerResults>(pset.getParameter<InputTag>("triggerResults"));
118  // hltFilterTag = pset.getParameter<InputTag>("hltFilter");
119  // triggerName = pset.getParameter<InputTag>("triggerName");
120 
121  piG = acos(-1.);
122  NumberMCParticles=0;
123  NumberTracks=0;
124  NumberInclusiveJet=0;
125  NumberChargedJet=0;
126  NumberTracksJet=0;
127  NumberCaloJet=0;
128 }
129 
131 {
132 
133  // use TFileService for output to root file
134  AnalysisTree = fs->make<TTree>("AnalysisTree","MBUE Analysis Tree ");
135 
136  AnalysisTree->Branch("EventKind",&EventKind,"EventKind/I");
137 
138  // store p, pt, eta, phi for particles and jets
139 
140  // GenParticles at hadron level
141  AnalysisTree->Branch("NumberMCParticles",&NumberMCParticles,"NumberMCParticles/I");
142  AnalysisTree->Branch("MomentumMC",MomentumMC,"MomentumMC[NumberMCParticles]/F");
143  AnalysisTree->Branch("TransverseMomentumMC",TransverseMomentumMC,"TransverseMomentumMC[NumberMCParticles]/F");
144  AnalysisTree->Branch("EtaMC",EtaMC,"EtaMC[NumberMCParticles]/F");
145  AnalysisTree->Branch("PhiMC",PhiMC,"PhiMC[NumberMCParticles]/F");
146 
147  // tracks
148  AnalysisTree->Branch("NumberTracks",&NumberTracks,"NumberTracks/I");
149  AnalysisTree->Branch("MomentumTK",MomentumTK,"MomentumTK[NumberTracks]/F");
150  AnalysisTree->Branch("TrasverseMomentumTK",TransverseMomentumTK,"TransverseMomentumTK[NumberTracks]/F");
151  AnalysisTree->Branch("EtaTK",EtaTK,"EtaTK[NumberTracks]/F");
152  AnalysisTree->Branch("PhiTK",PhiTK,"PhiTK[NumberTracks]/F");
153 
154  // GenJets
155  AnalysisTree->Branch("NumberInclusiveJet",&NumberInclusiveJet,"NumberInclusiveJet/I");
156  AnalysisTree->Branch("MomentumIJ",MomentumIJ,"MomentumIJ[NumberInclusiveJet]/F");
157  AnalysisTree->Branch("TrasverseMomentumIJ",TransverseMomentumIJ,"TransverseMomentumIJ[NumberInclusiveJet]/F");
158  AnalysisTree->Branch("EtaIJ",EtaIJ,"EtaIJ[NumberInclusiveJet]/F");
159  AnalysisTree->Branch("PhiIJ",PhiIJ,"PhiIJ[NumberInclusiveJet]/F");
160 
161  // jets from charged GenParticles
162  AnalysisTree->Branch("NumberChargedJet",&NumberChargedJet,"NumberChargedJet/I");
163  AnalysisTree->Branch("MomentumCJ",MomentumCJ,"MomentumCJ[NumberChargedJet]/F");
164  AnalysisTree->Branch("TrasverseMomentumCJ",TransverseMomentumCJ,"TransverseMomentumCJ[NumberChargedJet]/F");
165  AnalysisTree->Branch("EtaCJ",EtaCJ,"EtaCJ[NumberChargedJet]/F");
166  AnalysisTree->Branch("PhiCJ",PhiCJ,"PhiCJ[NumberChargedJet]/F");
167 
168  // jets from tracks
169  AnalysisTree->Branch("NumberTracksJet",&NumberTracksJet,"NumberTracksJet/I");
170  AnalysisTree->Branch("MomentumTJ",MomentumTJ,"MomentumTJ[NumberTracksJet]/F");
171  AnalysisTree->Branch("TrasverseMomentumTJ",TransverseMomentumTJ,"TransverseMomentumTJ[NumberTracksJet]/F");
172  AnalysisTree->Branch("EtaTJ",EtaTJ,"EtaTJ[NumberTracksJet]/F");
173  AnalysisTree->Branch("PhiTJ",PhiTJ,"PhiTJ[NumberTracksJet]/F");
174 
175  // jets from calorimeter towers
176  AnalysisTree->Branch("NumberCaloJet",&NumberCaloJet,"NumberCaloJet/I");
177  AnalysisTree->Branch("MomentumEHJ",MomentumEHJ,"MomentumEHJ[NumberCaloJet]/F");
178  AnalysisTree->Branch("TrasverseMomentumEHJ",TransverseMomentumEHJ,"TransverseMomentumEHJ[NumberCaloJet]/F");
179  AnalysisTree->Branch("EtaEHJ",EtaEHJ,"EtaEHJ[NumberCaloJet]/F");
180  AnalysisTree->Branch("PhiEHJ",PhiEHJ,"PhiEHJ[NumberCaloJet]/F");
181 
182 
183  // alternative storage method:
184  // save TClonesArrays of TLorentzVectors
185  // i.e. store 4-vectors of particles and jets
186 
187  MonteCarlo = new TClonesArray("TLorentzVector", 10000);
188  AnalysisTree->Branch("MonteCarlo", "TClonesArray", &MonteCarlo, 128000, 0);
189 
190  Track = new TClonesArray("TLorentzVector", 10000);
191  AnalysisTree->Branch("Track", "TClonesArray", &Track, 128000, 0);
192 
193  InclusiveJet = new TClonesArray("TLorentzVector", 10000);
194  AnalysisTree->Branch("InclusiveJet", "TClonesArray", &InclusiveJet, 128000, 0);
195 
196  ChargedJet = new TClonesArray("TLorentzVector", 10000);
197  AnalysisTree->Branch("ChargedJet", "TClonesArray", &ChargedJet, 128000, 0);
198 
199  TracksJet = new TClonesArray("TLorentzVector", 10000);
200  AnalysisTree->Branch("TracksJet", "TClonesArray", &TracksJet, 128000, 0);
201 
202  CalorimeterJet = new TClonesArray("TLorentzVector", 10000);
203  AnalysisTree->Branch("CalorimeterJet", "TClonesArray", &CalorimeterJet, 128000, 0);
204 
205  acceptedTriggers = new TClonesArray("TObjString", 10000);
206  AnalysisTree->Branch("acceptedTriggers", "TClonesArray", &acceptedTriggers, 128000, 0);
207 
208 }
209 
210 
212 {
213 
214  e.getByToken( triggerResultsToken, triggerResults );
215  const edm::TriggerNames & triggerNames = e.triggerNames(*triggerResults);
216 
217  acceptedTriggers->Clear();
218  unsigned int iAcceptedTriggers( 0 );
219  if ( triggerResults.product()->wasrun() )
220  {
221  //cout << "at least one path out of " << triggerResults.product()->size() << " ran? " << triggerResults.product()->wasrun() << endl;
222 
223  if ( triggerResults.product()->accept() )
224  {
225  //cout << endl << "at least one path accepted? " << triggerResults.product()->accept() << endl;
226 
227  const unsigned int n_TriggerResults( triggerResults.product()->size() );
228  for ( unsigned int itrig( 0 ); itrig < n_TriggerResults; ++itrig )
229  {
230  if ( triggerResults.product()->accept( itrig ) )
231  {
232  //cout << "path " << triggerNames.triggerName( itrig );
233  //cout << ", module index " << triggerResults.product()->index( itrig );
234  //cout << ", state (Ready = 0, Pass = 1, Fail = 2, Exception = 3) " << triggerResults.product()->state( itrig );
235  //cout << ", accept " << triggerResults.product()->accept( itrig );
236  //cout << endl;
237 
238  // save name of accepted trigger path
239  new((*acceptedTriggers)[iAcceptedTriggers]) TObjString( (triggerNames.triggerName( itrig )).c_str() );
240  ++iAcceptedTriggers;
241  }
242  }
243  }
244  }
245 
246  // gen level analysis
247  // skipped, if onlyRECO flag set to true
248 
249  if(!onlyRECO){
250 
251  e.getByToken( mcEventToken , EvtHandle );
252  e.getByToken( chgGenPartCollToken, CandHandleMC );
253  e.getByToken( chgJetCollToken , ChgGenJetsHandle );
254  e.getByToken( genJetCollToken , GenJetsHandle );
255 
256  const HepMC::GenEvent* Evt = EvtHandle->GetEvent() ;
257 
258  EventKind = Evt->signal_process_id();
259 
260  std::vector<math::XYZTLorentzVector> GenPart;
261  std::vector<GenJet> ChgGenJetContainer;
262  std::vector<GenJet> GenJetContainer;
263 
264  GenPart.clear();
265  ChgGenJetContainer.clear();
266  GenJetContainer.clear();
267  MonteCarlo->Clear();
268  InclusiveJet->Clear();
269  ChargedJet->Clear();
270 
271  // jets from charged particles at hadron level
272  if (!ChgGenJetsHandle->empty()){
273 
274  for ( GenJetCollection::const_iterator it(ChgGenJetsHandle->begin()), itEnd(ChgGenJetsHandle->end());
275  it!=itEnd; ++it)
276  {
277  ChgGenJetContainer.push_back(*it);
278  }
279 
280  std::stable_sort(ChgGenJetContainer.begin(),ChgGenJetContainer.end(),GenJetSort());
281 
282  std::vector<GenJet>::const_iterator it(ChgGenJetContainer.begin()), itEnd(ChgGenJetContainer.end());
283  for ( int iChargedJet(0); it != itEnd; ++it, ++iChargedJet)
284  {
285  fillChargedJet(it->p(),it->pt(),it->eta(),it->phi());
286  new((*ChargedJet)[iChargedJet]) TLorentzVector(it->px(), it->py(), it->pz(), it->energy());
287  }
288  }
289 
290 
291  // GenJets
292  if (!GenJetsHandle->empty()){
293 
294  for ( GenJetCollection::const_iterator it(GenJetsHandle->begin()), itEnd(GenJetsHandle->end());
295  it!=itEnd; ++it )
296  {
297  GenJetContainer.push_back(*it);
298  }
299 
300  std::stable_sort(GenJetContainer.begin(),GenJetContainer.end(),GenJetSort());
301 
302  std::vector<GenJet>::const_iterator it(GenJetContainer.begin()), itEnd(GenJetContainer.end());
303  for ( int iInclusiveJet(0); it != itEnd; ++it, ++iInclusiveJet)
304  {
305  fillInclusiveJet(it->p(),it->pt(),it->eta(),it->phi());
306  new((*InclusiveJet)[iInclusiveJet]) TLorentzVector(it->px(), it->py(), it->pz(), it->energy());
307  }
308  }
309 
310 
311  // hadron level particles
312  if (!CandHandleMC->empty()){
313 
314  for (vector<GenParticle>::const_iterator it(CandHandleMC->begin()), itEnd(CandHandleMC->end());
315  it != itEnd;it++)
316  {
317  GenPart.push_back(it->p4());
318  }
319 
320  std::stable_sort(GenPart.begin(),GenPart.end(),GreaterPt());
321 
322  std::vector<math::XYZTLorentzVector>::const_iterator it(GenPart.begin()), itEnd(GenPart.end());
323  for( int iMonteCarlo(0); it != itEnd; ++it, ++iMonteCarlo )
324  {
325  fillMCParticles(it->P(),it->Pt(),it->Eta(),it->Phi());
326  new((*MonteCarlo)[iMonteCarlo]) TLorentzVector(it->Px(), it->Py(), it->Pz(), it->E());
327  }
328  }
329 
330  }
331 
332 
333  // reco level analysis
334 
335  e.getByToken( tracksCollToken , CandHandleRECO );
336  e.getByToken( recoCaloJetCollToken, RecoCaloJetsHandle );
337  e.getByToken( tracksJetCollToken , TracksJetsHandle );
338 
339  std::vector<math::XYZTLorentzVector> Tracks;
340  std::vector<BasicJet> TracksJetContainer;
341  std::vector<CaloJet> RecoCaloJetContainer;
342 
343  Tracks.clear();
344  TracksJetContainer.clear();
345  RecoCaloJetContainer.clear();
346 
347  Track->Clear();
348  TracksJet->Clear();
349  CalorimeterJet->Clear();
350 
351  if(!RecoCaloJetsHandle->empty())
352  {
353  for(CaloJetCollection::const_iterator it(RecoCaloJetsHandle->begin()), itEnd(RecoCaloJetsHandle->end());
354  it!=itEnd;++it)
355  {
356  RecoCaloJetContainer.push_back(*it);
357  }
358  std::stable_sort(RecoCaloJetContainer.begin(),RecoCaloJetContainer.end(),CaloJetSort());
359 
360  std::vector<CaloJet>::const_iterator it(RecoCaloJetContainer.begin()), itEnd(RecoCaloJetContainer.end());
361  for( int iCalorimeterJet(0); it != itEnd; ++it, ++iCalorimeterJet)
362  {
363  fillCaloJet(it->p(),it->pt(),it->eta(),it->phi());
364  new((*CalorimeterJet)[iCalorimeterJet]) TLorentzVector(it->px(), it->py(), it->pz(), it->energy());
365  }
366  }
367 
368  if(!TracksJetsHandle->empty())
369  {
370  for(BasicJetCollection::const_iterator it(TracksJetsHandle->begin()), itEnd(TracksJetsHandle->end());
371  it!=itEnd;++it)
372  {
373  TracksJetContainer.push_back(*it);
374  }
375  std::stable_sort(TracksJetContainer.begin(),TracksJetContainer.end(),BasicJetSort());
376 
377  std::vector<BasicJet>::const_iterator it(TracksJetContainer.begin()), itEnd(TracksJetContainer.end());
378  for(int iTracksJet(0); it != itEnd; ++it, ++iTracksJet)
379  {
380  fillTracksJet(it->p(),it->pt(),it->eta(),it->phi());
381  new((*TracksJet)[iTracksJet]) TLorentzVector(it->px(), it->py(), it->pz(), it->energy());
382  }
383  }
384 
385  if(!CandHandleRECO->empty())
386  {
387  for(CandidateCollection::const_iterator it(CandHandleRECO->begin()), itEnd(CandHandleRECO->end());
388  it!=itEnd;++it)
389  {
390  Tracks.push_back(it->p4());
391  }
392  std::stable_sort(Tracks.begin(),Tracks.end(),GreaterPt());
393 
394  std::vector<math::XYZTLorentzVector>::const_iterator it( Tracks.begin()), itEnd(Tracks.end());
395  for(int iTracks(0); it != itEnd; ++it, ++iTracks)
396  {
397  fillTracks(it->P(),it->Pt(),it->Eta(),it->Phi());
398  new ((*Track)[iTracks]) TLorentzVector(it->Px(), it->Py(), it->Pz(), it->E());
399  }
400  }
401 
402  store();
403 }
404 
406 {
407 }
408 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void fillTracks(float, float, float, float)
Jets made from CaloTowers.
Definition: CaloJet.h:29
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
bool operator()(const CaloJet &a, const CaloJet &b)
void fillTracksJet(float, float, float, float)
void fillChargedJet(float, float, float, float)
double pt() const final
transverse momentum
bool operator()(const math::XYZTLorentzVector &a, const math::XYZTLorentzVector &b)
void fillInclusiveJet(float, float, float, float)
Jets made from CaloTowers.
Definition: BasicJet.h:20
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
Jets made from MC generator particles.
Definition: GenJet.h:24
void analyze(const edm::Event &, const edm::EventSetup &) override
void fillCaloJet(float, float, float, float)
std::string const & triggerName(unsigned int index) const
Definition: TriggerNames.cc:27
static std::string const triggerResults("TriggerResults")
bool operator()(const BasicJet &a, const BasicJet &b)
double b
Definition: hdecay.h:120
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:121
AnalysisRootpleProducer(const edm::ParameterSet &)
bool operator()(const GenJet &a, const GenJet &b)
void fillMCParticles(float, float, float, float)
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:301