CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BasicGenParticleValidation.cc
Go to the documentation of this file.
1 /*class BasicGenParticleValidation
2  *
3  * Class to fill dqm monitor elements from existing EDM file
4  *
5  * $Date: 2011/12/29 10:53:11 $
6  * $Revision: 1.3 $
7  */
8 
10 
11 #include "CLHEP/Units/defs.h"
12 #include "CLHEP/Units/PhysicalConstants.h"
13 
14 using namespace edm;
15 
17  _wmanager(iPSet),
18  hepmcCollection_(iPSet.getParameter<edm::InputTag>("hepmcCollection")),
19  genparticleCollection_(iPSet.getParameter<edm::InputTag>("genparticleCollection")),
20  genjetCollection_(iPSet.getParameter<edm::InputTag>("genjetsCollection")),
21  matchPr_(iPSet.getParameter<double>("matchingPrecision")),
22  verbosity_(iPSet.getUntrackedParameter<unsigned int>("verbosity",0))
23 {
24  dbe = 0;
26 }
27 
29 
31 {
32  if(dbe){
34  dbe->setCurrentFolder("Generator/GenParticles");
35 
37 
38  // Number of analyzed events
39  nEvt = dbe->book1D("nEvt", "n analyzed Events", 1, 0., 1.);
40 
42  genPMultiplicity = dbe->book1D("genPMultiplicty", "Log(No. all GenParticles)", 50, -1, 5); //Log
43  //difference in HepMC and reco multiplicity
44  genMatched = dbe->book1D("genMatched", "Difference reco - matched", 50, -25, 25);
45  //multiple matching
46  multipleMatching = dbe->book1D("multipleMatching", "multiple reco HepMC matching", 50, 0, 50);
47  //momentum difference of matched particles
48  matchedResolution = dbe->book1D("matchedResolution", "log10(momentum difference of matched particles)", 70, -10., -3.);
49 
50  // GenJet general distributions
51  genJetMult = dbe->book1D("genJetMult", "GenJet multiplicity", 50, 0, 50);
52  genJetEnergy = dbe->book1D("genJetEnergy", "Log10(GenJet energy)", 60, -1, 5);
53  genJetPt = dbe->book1D("genJetPt", "Log10(GenJet pt)", 60, -1, 5);
54  genJetEta = dbe->book1D("genJetEta", "GenJet eta", 220, -11, 11);
55  genJetPhi = dbe->book1D("genJetPhi", "GenJet phi", 360, -180, 180);
56  genJetDeltaEtaMin = dbe->book1D("genJetDeltaEtaMin", "GenJet minimum rapidity gap", 30, 0, 30);
57 
58  genJetPto1 = dbe->book1D("genJetPto1", "GenJet multiplicity above 1 GeV", 50, 0, 50);
59  genJetPto10 = dbe->book1D("genJetPto10", "GenJet multiplicity above 10 GeV", 50, 0, 50);
60  genJetPto100 = dbe->book1D("genJetPto100", "GenJet multiplicity above 100 GeV", 50, 0, 50);
61  genJetCentral = dbe->book1D("genJetCentral", "GenJet multiplicity |eta|.lt.2.5", 50, 0, 50);
62 
63  genJetTotPt = dbe->book1D("genJetTotPt", "Log10(GenJet total pt)", 100, -5, 5);
64 
65  }
66  return;
67 }
68 
71 {
73  iSetup.getData( fPDGTable );
74  return;
75 }
76 void BasicGenParticleValidation::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){return;}
78 {
79 
80  unsigned int initSize = 1000;
81 
84  iEvent.getByLabel(hepmcCollection_, evt);
85 
86  //Get HepMC EVENT
87  HepMC::GenEvent *myGenEvent = new HepMC::GenEvent(*(evt->GetEvent()));
88 
89  double weight = _wmanager.weight(iEvent);
90 
91  nEvt->Fill(0.5, weight);
92 
93  std::vector<const HepMC::GenParticle*> hepmcGPCollection;
94  std::vector<int> barcodeList;
95  hepmcGPCollection.reserve(initSize);
96  barcodeList.reserve(initSize);
97 
98  //Looping through HepMC::GenParticle collection to search for status 1 particles
99  for (HepMC::GenEvent::particle_const_iterator iter = myGenEvent->particles_begin(); iter != myGenEvent->particles_end(); ++iter){
100  if ( (*iter)->status() == 1) {
101  hepmcGPCollection.push_back(*iter);
102  barcodeList.push_back((*iter)->barcode());
103  if ( verbosity_ > 0 ) {
104  std::cout << "HepMC " << std::setw(14) << std::fixed << (*iter)->pdg_id() << std::setw(14) << std::fixed << (*iter)->momentum().px() << std::setw(14) << std::fixed
105  << (*iter)->momentum().py() << std::setw(14) << std::fixed << (*iter)->momentum().pz() << std::endl;
106  }
107  }
108  }
109 
110 
111  // Gather information on the reco::GenParticle collection
113  iEvent.getByLabel(genparticleCollection_, genParticles );
114 
115  std::vector<const reco::GenParticle*> particles;
116  particles.reserve(initSize);
117  for (reco::GenParticleCollection::const_iterator iter=genParticles->begin();iter!=genParticles->end();++iter){
118  if ( (*iter).status() == 1) {
119  particles.push_back(&*iter);
120  if ( verbosity_ > 0 ) {
121  std::cout << "reco " << std::setw(14) << std::fixed << (*iter).pdgId() << std::setw(14) << std::fixed << (*iter).px()
122  << std::setw(14) << std::fixed << (*iter).py() << std::setw(14) << std::fixed << (*iter).pz() << std::endl;
123  }
124  }
125  }
126 
127  unsigned int nReco = particles.size();
128  unsigned int nHepMC = hepmcGPCollection.size();
129 
130  genPMultiplicity->Fill(std::log10(nReco), weight);
131 
132  // Define vector containing index of hepmc corresponding to the reco::GenParticle
133  std::vector<int> hepmcMatchIndex;
134  hepmcMatchIndex.reserve(initSize);
135 
136  // Matching procedure
137 
138  // Check array size consistency
139 
140  if ( nReco != nHepMC ) {
141  edm::LogWarning("CollectionSizeInconsistency") << "Collection size inconsistency: HepMC::GenParticle = " << nHepMC << " reco::GenParticle = " << nReco;
142  }
143 
144  // Match each HepMC with a reco
145 
146  for ( unsigned int i = 0; i < nReco; ++i ){
147  for ( unsigned int j = 0; j < nHepMC; ++j ){
148  if ( matchParticles( hepmcGPCollection[j], particles[i] ) ) {
149  hepmcMatchIndex.push_back((int)j);
150  if ( hepmcGPCollection[j]->momentum().rho() != 0. ) {
151  double reso = 1.-particles[i]->p()/hepmcGPCollection[j]->momentum().rho();
152  if ( verbosity_ > 0 ) {
153  std::cout << "Matching momentum: reco = " << particles[i]->p() << " HepMC = "
154  << hepmcGPCollection[j]->momentum().rho() << " resoultion = " << reso << std::endl;
155  }
156  matchedResolution->Fill(std::log10(std::fabs(reso)),weight); }
157  continue;
158  }
159  }
160  }
161 
162  // Check unicity of matching
163 
164  unsigned int nMatched = hepmcMatchIndex.size();
165 
166  if ( nMatched != nReco ) {
167  edm::LogWarning("IncorrectMatching") << "Incorrect number of matched indexes: GenParticle = " << nReco << " matched indexes = " << nMatched;
168  }
169  genMatched->Fill(int(nReco-nMatched),weight);
170 
171  unsigned int nWrMatch = 0;
172 
173  for ( unsigned int i = 0; i < nMatched; ++i ){
174  for (unsigned int j = i+1; j < nMatched; ++j ){
175  if ( hepmcMatchIndex[i] == hepmcMatchIndex[j] ) {
176  int theIndex = hepmcMatchIndex[i];
177  edm::LogWarning("DuplicatedMatching") << "Multiple matching occurencies for GenParticle barcode = " << barcodeList[theIndex];
178  nWrMatch++;
179  }
180  }
181  }
182  multipleMatching->Fill(int(nWrMatch),weight);
183 
184  // Gather information in the GenJet collection
186  iEvent.getByLabel(genjetCollection_, genJets );
187 
188  int nJets = 0;
189  int nJetso1 = 0;
190  int nJetso10 = 0;
191  int nJetso100 = 0;
192  int nJetsCentral = 0;
193  double totPt = 0.;
194 
195  std::vector<double> jetEta;
196  jetEta.reserve(initSize);
197 
198  for (reco::GenJetCollection::const_iterator iter=genJets->begin();iter!=genJets->end();++iter){
199  nJets++;
200  double pt = (*iter).pt();
201  totPt += pt;
202  if (pt > 1.) nJetso1++;
203  if (pt > 10.) nJetso10++;
204  if (pt > 100.) nJetso100++;
205  double eta = (*iter).eta();
206  if ( std::fabs(eta) < 2.5 ) nJetsCentral++;
207  jetEta.push_back(eta);
208 
209  genJetEnergy->Fill(std::log10((*iter).energy()),weight);
210  genJetPt->Fill(std::log10(pt),weight);
211  genJetEta->Fill(eta,weight);
212  genJetPhi->Fill((*iter).phi()/CLHEP::degree,weight);
213  }
214 
215  genJetMult->Fill(nJets,weight);
216  genJetPto1->Fill(nJetso1,weight);
217  genJetPto10->Fill(nJetso10,weight);
218  genJetPto100->Fill(nJetso100,weight);
219  genJetCentral->Fill(nJetsCentral,weight);
220 
221  genJetTotPt->Fill(std::log10(totPt),weight);
222 
223  double deltaEta = 999.;
224  if ( jetEta.size() > 1 ) {
225  for (unsigned int i = 0; i < jetEta.size(); i++){
226  for (unsigned int j = i+1; j < jetEta.size(); j++){
227  deltaEta = std::min(deltaEta,std::fabs(jetEta[i]-jetEta[j]));
228  }
229  }
230  }
231 
232  genJetDeltaEtaMin->Fill(deltaEta,weight);
233 
234  delete myGenEvent;
235 }//analyze
236 
238 
239  bool state = false;
240 
241  if ( hepmcP->pdg_id() != recoP->pdgId() ) return state;
242  if ( std::fabs(hepmcP->momentum().px()-recoP->px()) < std::fabs(matchPr_*hepmcP->momentum().px()) &&
243  std::fabs(hepmcP->momentum().py()-recoP->py()) < std::fabs(matchPr_*hepmcP->momentum().py()) &&
244  std::fabs(hepmcP->momentum().pz()-recoP->pz()) < std::fabs(matchPr_*hepmcP->momentum().pz())) {
245  state = true; }
246 
247  return state;
248 
249 }
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
int i
Definition: DBlmapReader.cc:9
edm::ESHandle< HepPDT::ParticleDataTable > fPDGTable
PDT table.
DQMStore * dbe
ME&#39;s &quot;container&quot;.
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:722
virtual int pdgId() const GCC11_FINAL
PDG identifier.
virtual void analyze(const edm::Event &, const edm::EventSetup &)
Definition: DDAxes.h:10
#define min(a, b)
Definition: mlp_lapack.h:161
bool matchParticles(const HepMC::GenParticle *&, const reco::GenParticle *&)
T eta() const
virtual double pz() const GCC11_FINAL
z coordinate of momentum vector
virtual double py() const GCC11_FINAL
y coordinate of momentum vector
void getData(T &iHolder) const
Definition: EventSetup.h:67
void Fill(long long x)
int iEvent
Definition: GenABIO.cc:243
virtual double px() const GCC11_FINAL
x coordinate of momentum vector
int j
Definition: DBlmapReader.cc:9
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
char state
Definition: procUtils.cc:75
tuple cout
Definition: gather_cfg.py:121
BasicGenParticleValidation(const edm::ParameterSet &)
int weight
Definition: histoStyle.py:50
double weight(const edm::Event &)
virtual void endRun(const edm::Run &, const edm::EventSetup &)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:434
Definition: Run.h:36