CMS 3D CMS Logo

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