CMS 3D CMS Logo

MuScleFitPlotter.cc
Go to the documentation of this file.
1 // \class MuScleFitPlotter
2 // Plotter for simulated,generated and reco info of muons
3 //
4 // \author C.Mariotti, S.Bolognesi - INFN Torino / T.Dorigo, M.De Mattia - INFN Padova
5 // revised S. Casasso, E. Migliore - UniTo & INFN Torino
6 //
7 // ----------------------------------------------------------------------------------
8 
9 #include "MuScleFitPlotter.h"
10 #include "Histograms.h"
11 #include "MuScleFitUtils.h"
16 //#include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
26 #include <CLHEP/Vector/LorentzVector.h>
27 
28 #include "TFile.h"
29 #include "TTree.h"
30 #include "TMinuit.h"
31 #include <vector>
32 
33 // Constructor
34 // ----------
36  outputFile = new TFile(theGenInfoRootFileName.c_str(), "RECREATE");
37  fillHistoMap();
38 }
39 
41  outputFile->cd();
42  writeHistoMap();
43  outputFile->Close();
44 }
45 
46 // Find and store in histograms the generated resonance and muons
47 // --------------------------------------------------------------
49  // bool prova = false;
50  //Loop on generated particles
51  std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> muFromRes;
53 
54  int mothersFound[] = {0, 0, 0, 0, 0, 0};
55 
56  for (reco::GenParticleCollection::const_iterator mcIter = genParticles.begin(); mcIter != genParticles.end();
57  ++mcIter) {
58  int status = mcIter->status();
59  int pdgId = std::abs(mcIter->pdgId());
60  //Check if it's a resonance
61  if (status == 2 &&
62  (pdgId == 23 || pdgId == 443 || pdgId == 100443 || pdgId == 553 || pdgId == 100553 || pdgId == 200553)) {
63  genRes = mcIter->p4();
64  // std::cout << "mother's mother = " << mcIter->mother()->pdgId() << std::endl;
65  if (pdgId == 23)
66  mapHisto["hGenResZ"]->Fill(genRes);
67  else if (pdgId == 443 || pdgId == 100443)
68  mapHisto["hGenResJPsi"]->Fill(genRes);
69  else if (pdgId == 553 || pdgId == 100553 || pdgId == 200553)
70  mapHisto["hGenResUpsilon1S"]->Fill(genRes);
71  }
72  //Check if it's a muon from a resonance
73  if (status == 1 && pdgId == 13 && !PATmuons) {
74  int momPdgId = std::abs(mcIter->mother()->pdgId());
75  if (momPdgId == 23 || momPdgId == 443 || momPdgId == 100443 || momPdgId == 553 || momPdgId == 100553 ||
76  momPdgId == 200553) {
77  if (momPdgId == 23)
78  mothersFound[0] = 1;
79  if (momPdgId == 443 || momPdgId == 100443)
80  mothersFound[5] = 1;
81  if (momPdgId == 553 || momPdgId == 100553 || momPdgId == 200553)
82  mothersFound[3] = 1;
83  mapHisto["hGenMu"]->Fill(mcIter->p4());
84  std::cout << "genmu " << mcIter->p4() << std::endl;
85  if (mcIter->charge() > 0) {
86  muFromRes.first = mcIter->p4();
87  // prova = true;
88  } else
89  muFromRes.second = mcIter->p4();
90  }
91  } //if PATmuons you don't have the info of the mother !!! Here I assume is a JPsi
92  if (status == 1 && pdgId == 13 && PATmuons) {
93  mothersFound[5] = 1;
94  mapHisto["hGenMu"]->Fill(mcIter->p4());
95  std::cout << "genmu " << mcIter->p4() << std::endl;
96  if (mcIter->charge() > 0) {
97  muFromRes.first = mcIter->p4();
98  // prova = true;
99  } else
100  muFromRes.second = mcIter->p4();
101  }
102  }
103  // if(!prova)
104  // std::cout<<"hgenmumu not found"<<std::endl;
105 
106  if (mothersFound[0] == 1) {
107  mapHisto["hGenMuMuZ"]->Fill(muFromRes.first + muFromRes.second);
108  mapHisto["hGenResVSMuZ"]->Fill(muFromRes.first, genRes, 1);
109  mapHisto["hGenResVSMuZ"]->Fill(muFromRes.second, genRes, -1);
110  }
111  if (mothersFound[3] == 1) {
112  mapHisto["hGenMuMuUpsilon1S"]->Fill(muFromRes.first + muFromRes.second);
113  mapHisto["hGenResVSMuUpsilon1S"]->Fill(muFromRes.first, genRes, 1);
114  mapHisto["hGenResVSMuUpsilon1S"]->Fill(muFromRes.second, genRes, -1);
115  }
116  if (mothersFound[5] == 1) {
117  mapHisto["hGenMuMuJPsi"]->Fill(muFromRes.first + muFromRes.second);
118  mapHisto["hGenResVSMuJPsi"]->Fill(muFromRes.first, genRes, 1);
119  mapHisto["hGenResVSMuJPsi"]->Fill(muFromRes.second, genRes, -1);
120  }
121 
122  mapHisto["hGenResVsSelf"]->Fill(genRes, genRes, 1);
123 }
124 
125 // Find and store in histograms the generated resonance and muons
126 // --------------------------------------------------------------
127 void MuScleFitPlotter::fillGen(const edm::HepMCProduct& evtMC, bool sherpaFlag_) {
128  //Loop on generated particles
129  const HepMC::GenEvent* Evt = evtMC.GetEvent();
130  std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> muFromRes;
132 
133  int mothersFound[] = {0, 0, 0, 0, 0, 0};
134 
135  if (sherpaFlag_) {
136  for (HepMC::GenEvent::particle_const_iterator part = Evt->particles_begin(); part != Evt->particles_end(); part++) {
137  if (std::abs((*part)->pdg_id()) == 13 && (*part)->status() == 1) { //looks for muon in the final state
138  bool fromRes = false;
139  for (HepMC::GenVertex::particle_iterator mother = (*part)->production_vertex()->particles_begin(
140  HepMC::ancestors); //loops on the mother of the final state muons
141  mother != (*part)->production_vertex()->particles_end(HepMC::ancestors);
142  ++mother) {
143  unsigned int motherPdgId = (*mother)->pdg_id();
144  if (motherPdgId == 13 && (*mother)->status() == 3)
145  fromRes = true;
146  }
147  if (fromRes) {
148  if ((*part)->pdg_id() == 13) {
149  muFromRes.first = (lorentzVector(
150  (*part)->momentum().px(), (*part)->momentum().py(), (*part)->momentum().pz(), (*part)->momentum().e()));
151  } else if ((*part)->pdg_id() == -13) {
152  muFromRes.second = (lorentzVector(
153  (*part)->momentum().px(), (*part)->momentum().py(), (*part)->momentum().pz(), (*part)->momentum().e()));
154  }
155  }
156  }
157  }
158  mapHisto["hGenResZ"]->Fill(muFromRes.first + muFromRes.second);
159  } else {
160  for (HepMC::GenEvent::particle_const_iterator part = Evt->particles_begin(); part != Evt->particles_end(); part++) {
161  int status = (*part)->status();
162  int pdgId = std::abs((*part)->pdg_id());
163  //std::cout<<"PDG ID "<< (*part)->pdg_id() <<" status "<< (*part)->status()
164  //<<" pt "<<(*part)->momentum().perp()<< " eta "<<(*part)->momentum().eta()<<std::endl ;
165  //Check if it's a resonance
166  // For sherpa the resonance is not saved. The muons from the resonance can be identified
167  // by having as mother a muon of status 3.
168 
169  if (pdgId == 13 && status == 1) {
170  if (status == 2 &&
171  (pdgId == 23 || pdgId == 443 || pdgId == 100443 || pdgId == 553 || pdgId == 100553 || pdgId == 200553)) {
173  (*part)->momentum().px(), (*part)->momentum().py(), (*part)->momentum().pz(), (*part)->momentum().e());
174 
175  if (pdgId == 23)
176  mapHisto["hGenResZ"]->Fill(genRes);
177  if (pdgId == 443)
178  mapHisto["hGenResJPsi"]->Fill(genRes);
179  if (pdgId == 553) {
180  // std::cout << "genRes mass = " << CLHEP::HepLorentzVector(genRes.x(),genRes.y(),genRes.z(),genRes.t()).m() << std::endl;
181  mapHisto["hGenResUpsilon1S"]->Fill(genRes);
182  }
183  }
184 
185  //Check if it's a muon from a resonance
186  if (pdgId == 13 && status == 1) {
187  bool fromRes = false;
188  for (HepMC::GenVertex::particle_iterator mother =
189  (*part)->production_vertex()->particles_begin(HepMC::ancestors);
190  mother != (*part)->production_vertex()->particles_end(HepMC::ancestors);
191  ++mother) {
192  int motherPdgId = (*mother)->pdg_id();
193  if (motherPdgId == 23 || motherPdgId == 443 || motherPdgId == 100443 || motherPdgId == 553 ||
194  motherPdgId == 100553 || motherPdgId == 200553) {
195  fromRes = true;
196  if (motherPdgId == 23)
197  mothersFound[0] = 1;
198  if (motherPdgId == 443)
199  mothersFound[3] = 1;
200  if (motherPdgId == 553)
201  mothersFound[5] = 1;
202  }
203  }
204 
205  if (fromRes) {
206  mapHisto["hGenMu"]->Fill(reco::Particle::LorentzVector(
207  (*part)->momentum().px(), (*part)->momentum().py(), (*part)->momentum().pz(), (*part)->momentum().e()));
208  mapHisto["hGenMuVSEta"]->Fill(reco::Particle::LorentzVector(
209  (*part)->momentum().px(), (*part)->momentum().py(), (*part)->momentum().pz(), (*part)->momentum().e()));
210  if ((*part)->pdg_id() == -13)
211  muFromRes.first = (reco::Particle::LorentzVector((*part)->momentum().px(),
212  (*part)->momentum().py(),
213  (*part)->momentum().pz(),
214  (*part)->momentum().e()));
215  else
216  muFromRes.second = (reco::Particle::LorentzVector((*part)->momentum().px(),
217  (*part)->momentum().py(),
218  (*part)->momentum().pz(),
219  (*part)->momentum().e()));
220  }
221  }
222  }
223  }
224  }
225  if (mothersFound[0] == 1) {
226  mapHisto["hGenMuMuZ"]->Fill(muFromRes.first + muFromRes.second);
227  mapHisto["hGenResVSMuZ"]->Fill(muFromRes.first, genRes, 1);
228  mapHisto["hGenResVSMuZ"]->Fill(muFromRes.second, genRes, -1);
229  }
230  if (mothersFound[3] == 1) {
231  mapHisto["hGenMuMuUpsilon1S"]->Fill(muFromRes.first + muFromRes.second);
232  mapHisto["hGenResVSMuUpsilon1S"]->Fill(muFromRes.first, genRes, 1);
233  mapHisto["hGenResVSMuUpsilon1S"]->Fill(muFromRes.second, genRes, -1);
234  }
235  if (mothersFound[5] == 1) {
236  mapHisto["hGenMuMuJPsi"]->Fill(muFromRes.first + muFromRes.second);
237  mapHisto["hGenResVSMuJPsi"]->Fill(muFromRes.first, genRes, 1);
238  mapHisto["hGenResVSMuJPsi"]->Fill(muFromRes.second, genRes, -1);
239  }
240  mapHisto["hGenResVsSelf"]->Fill(genRes, genRes, 1);
241 }
242 
243 // Find and store in histograms the simulated resonance and muons
244 // --------------------------------------------------------------
246  std::vector<SimTrack> simMuons;
247 
248  //Loop on simulated tracks
249  for (edm::SimTrackContainer::const_iterator simTrack = simTracks->begin(); simTrack != simTracks->end(); ++simTrack) {
250  // Select the muons from all the simulated tracks
251  if (std::abs((*simTrack).type()) == 13) {
252  simMuons.push_back(*simTrack);
253  mapHisto["hSimMu"]->Fill((*simTrack).momentum());
254  }
255  }
256  mapHisto["hSimMu"]->Fill(simMuons.size());
257 
258  // Recombine all the possible Z from simulated muons
259  if (simMuons.size() >= 2) {
260  for (std::vector<SimTrack>::const_iterator imu = simMuons.begin(); imu != simMuons.end(); ++imu) {
261  for (std::vector<SimTrack>::const_iterator imu2 = imu + 1; imu2 != simMuons.end(); ++imu2) {
262  if (imu == imu2)
263  continue;
264 
265  // Try all the pairs with opposite charge
266  if (((*imu).charge() * (*imu2).charge()) < 0) {
267  reco::Particle::LorentzVector Z = (*imu).momentum() + (*imu2).momentum();
268  mapHisto["hSimMuPMuM"]->Fill(Z);
269  }
270  }
271  }
272 
273  // Plots for the best possible simulated resonance
274  std::pair<SimTrack, SimTrack> simMuFromBestRes = MuScleFitUtils::findBestSimuRes(simMuons);
275  reco::Particle::LorentzVector bestSimZ = (simMuFromBestRes.first).momentum() + (simMuFromBestRes.second).momentum();
276  mapHisto["hSimBestRes"]->Fill(bestSimZ);
277  if (std::abs(simMuFromBestRes.first.momentum().eta()) < 2.5 &&
278  std::abs(simMuFromBestRes.second.momentum().eta()) < 2.5 && simMuFromBestRes.first.momentum().pt() > 2.5 &&
279  simMuFromBestRes.second.momentum().pt() > 2.5) {
280  mapHisto["hSimBestResVSMu"]->Fill(
281  simMuFromBestRes.first.momentum(), bestSimZ, int(simMuFromBestRes.first.charge()));
282  mapHisto["hSimBestResVSMu"]->Fill(
283  simMuFromBestRes.second.momentum(), bestSimZ, int(simMuFromBestRes.second.charge()));
284  }
285  }
286 }
287 
288 // Find and store in histograms the RIGHT simulated resonance and muons
289 // --------------------------------------------------------------
291  std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> simMuFromRes =
292  MuScleFitUtils::findSimMuFromRes(evtMC, simTracks);
293  //Fill resonance info
294  reco::Particle::LorentzVector rightSimRes = (simMuFromRes.first) + (simMuFromRes.second);
295  mapHisto["hSimRightRes"]->Fill(rightSimRes);
296  /*if ((std::abs(simMuFromRes.first.Eta())<2.5 && std::abs(simMuFromRes.second.Eta())<2.5)
297  && simMuFromRes.first.Pt()>2.5 && simMuFromRes.second.Pt()>2.5) {
298  }*/
299 }
300 
301 // // Find and store in histograms the reconstructed resonance and muons
302 // // --------------------------------------------------------------
303 // void MuScleFitPlotter::fillRec(std::vector<reco::LeafCandidate>& muons)
304 // {
305 // for(std::vector<reco::LeafCandidate>::const_iterator mu1 = muons.begin(); mu1!=muons.end(); mu1++){
306 // mapHisto["hRecMu"]->Fill(mu1->p4());
307 // mapHisto["hRecMuVSEta"]->Fill(mu1->p4());
308 // for(std::vector<reco::LeafCandidate>::const_iterator mu2 = muons.begin(); mu2!=muons.end(); mu2++){
309 // if (mu1->charge()<0 || mu2->charge()>0)
310 // continue;
311 // reco::Particle::LorentzVector Res (mu1->p4()+mu2->p4());
312 // mapHisto["hRecMuPMuM"]->Fill(Res);
313 // }
314 // }
315 // mapHisto["hRecMu"]->Fill(muons.size());
316 // }
317 
319 void MuScleFitPlotter::fillRec(std::vector<MuScleFitMuon>& muons) {
320  for (std::vector<MuScleFitMuon>::const_iterator mu1 = muons.begin(); mu1 != muons.end(); mu1++) {
321  mapHisto["hRecMu"]->Fill(mu1->p4());
322  mapHisto["hRecMuVSEta"]->Fill(mu1->p4());
323  for (std::vector<MuScleFitMuon>::const_iterator mu2 = muons.begin(); mu2 != muons.end(); mu2++) {
324  if (mu1->charge() < 0 || mu2->charge() > 0)
325  continue;
326  reco::Particle::LorentzVector Res(mu1->p4() + mu2->p4());
327  mapHisto["hRecMuPMuM"]->Fill(Res);
328  }
329  }
330  mapHisto["hRecMu"]->Fill(muons.size());
331 }
332 
335  const std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> >& savedPairs) {
336  std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> >::const_iterator muonPair =
337  savedPairs.begin();
338  for (; muonPair != savedPairs.end(); ++muonPair) {
339  mapHisto["hRecMu"]->Fill(muonPair->first);
340  mapHisto["hRecMuVSEta"]->Fill(muonPair->first);
341  mapHisto["hRecMu"]->Fill(muonPair->second);
342  mapHisto["hRecMuVSEta"]->Fill(muonPair->second);
343  reco::Particle::LorentzVector Res(muonPair->first + muonPair->second);
344  mapHisto["hRecMuPMuM"]->Fill(Res);
345  mapHisto["hRecMu"]->Fill(savedPairs.size());
346  }
347 }
348 
355  const std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> >& genPairs) {
356  std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> >::const_iterator genPair =
357  genPairs.begin();
358  for (; genPair != genPairs.end(); ++genPair) {
359  reco::Particle::LorentzVector genRes(genPair->first + genPair->second);
360  mapHisto["hGenResZ"]->Fill(genRes);
361  mapHisto["hGenMu"]->Fill(genPair->first);
362  mapHisto["hGenMuVSEta"]->Fill(genPair->first);
363  mapHisto["hGenMuMuZ"]->Fill(genRes);
364  mapHisto["hGenResVSMuZ"]->Fill(genPair->first, genRes, 1);
365  mapHisto["hGenResVSMuZ"]->Fill(genPair->second, genRes, -1);
366  mapHisto["hGenMuMuUpsilon1S"]->Fill(genRes);
367  mapHisto["hGenResVSMuUpsilon1S"]->Fill(genPair->first, genRes, 1);
368  mapHisto["hGenResVSMuUpsilon1S"]->Fill(genPair->second, genRes, -1);
369  mapHisto["hGenMuMuJPsi"]->Fill(genRes);
370  mapHisto["hGenResVSMuJPsi"]->Fill(genPair->first, genRes, 1);
371  mapHisto["hGenResVSMuJPsi"]->Fill(genPair->second, genRes, -1);
372  mapHisto["hGenResVsSelf"]->Fill(genRes, genRes, 1);
373  }
374 }
375 
376 // Histogram booking
377 // -----------------
379  // Generated Z and muons
380  // ---------------------
381  mapHisto["hGenResJPsi"] = new HParticle("hGenResJPsi", 3., 3.1);
382  mapHisto["hGenResUpsilon1S"] = new HParticle("hGenResUpsilon1S", 9., 11.);
383  mapHisto["hGenResZ"] = new HParticle("hGenResZ", 60., 120.);
384  mapHisto["hGenMu"] = new HParticle("hGenMu");
385  mapHisto["hGenMuVSEta"] = new HPartVSEta("hGenMuVSEta");
386 
387  mapHisto["hGenMuMuJPsi"] = new HParticle("hGenMuMuJPsi", 3., 3.1);
388  mapHisto["hGenResVSMuJPsi"] = new HMassVSPart("hGenResVSMuJPsi", 3., 3.1);
389  mapHisto["hGenMuMuUpsilon1S"] = new HParticle("hGenMuMuUpsilon1S", 9., 11.);
390  mapHisto["hGenResVSMuUpsilon1S"] = new HMassVSPart("hGenResVSMuUpsilon1S", 9., 11.);
391  mapHisto["hGenMuMuZ"] = new HParticle("hGenMuMuZ", 60., 120.);
392  mapHisto["hGenResVSMuZ"] = new HMassVSPart("hGenResVSMuZ", 60., 120.);
393 
394  mapHisto["hGenResVsSelf"] = new HMassVSPart("hGenResVsSelf");
395 
396  // Simulated resonance and muons
397  // -----------------------------
398  mapHisto["hSimMu"] = new HParticle("hSimMu");
399 
400  mapHisto["hSimMuPMuM"] = new HParticle("hSimMuPMuM");
401 
402  mapHisto["hSimBestMu"] = new HParticle("hSimBestMu");
403  mapHisto["hSimBestRes"] = new HParticle("hSimBestRes");
404  mapHisto["hSimBestResVSMu"] = new HMassVSPart("hSimBestResVSMu");
405 
406  mapHisto["hSimRightRes"] = new HParticle("hSimRightZ");
407 
408  // Reconstructed resonance and muons
409  // -----------------------------
410  mapHisto["hRecMu"] = new HParticle("hRecMu");
411  mapHisto["hRecMuVSEta"] = new HPartVSEta("hRecMuVSEta");
412  mapHisto["hRecMuPMuM"] = new HParticle("hRecMuPMuM");
413 }
414 
415 // Histogram saving
416 // -----------------
418  outputFile->cd();
419  for (std::map<std::string, Histograms*>::const_iterator histo = mapHisto.begin(); histo != mapHisto.end(); histo++) {
420  (*histo).second->Write();
421  }
422 }
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
std::map< std::string, Histograms * > mapHisto
static std::pair< lorentzVector, lorentzVector > findSimMuFromRes(const edm::Handle< edm::HepMCProduct > &evtMC, const edm::Handle< edm::SimTrackContainer > &simTracks)
void fillGenSim(edm::Handle< edm::HepMCProduct > evtMC, edm::Handle< edm::SimTrackContainer > simTracks)
void fillTreeRec(const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &savedPairs)
Used when running on the root tree containing preselected muon pairs.
virtual ~MuScleFitPlotter()
MuScleFitPlotter(std::string)
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
static std::pair< SimTrack, SimTrack > findBestSimuRes(const std::vector< SimTrack > &simMuons)
void fillRec(std::vector< MuScleFitMuon > &muons)
Used when running on the root tree containing preselected muon pairs.
void fillSim(edm::Handle< edm::SimTrackContainer > simTracks)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:34
part
Definition: HCALResponse.h:20
void fillTreeGen(const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &genPairs)
void fillGen(const reco::GenParticleCollection &genParticles, bool=false)
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21