ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/HbbAnalysis/plugins/MCTreeMaker.cc
Revision: 1.1
Committed: Wed Apr 18 12:56:47 2012 UTC (13 years ago) by amagnan
Content type: text/plain
Branch: MAIN
Log Message:
add MCTree maker

File Contents

# User Rev Content
1 amagnan 1.1 #include "TLorentzVector.h"
2     #include <limits>
3    
4     #include "DataFormats/Common/interface/Handle.h"
5     #include "DataFormats/Common/interface/ValueMap.h"
6    
7     #include "DataFormats/Candidate/interface/Candidate.h"
8     #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
9     #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
10     #include "DataFormats/JetReco/interface/GenJet.h"
11     #include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
12     #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
13     #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
14     #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
15    
16    
17     #include "FWCore/ServiceRegistry/interface/Service.h"
18     #include "FWCore/Framework/interface/Run.h"
19     #include "FWCore/Framework/interface/ESHandle.h"
20     #include "CommonTools/UtilAlgos/interface/TFileService.h"
21    
22     #include "UserCode/HbbAnalysis/plugins/MCTreeMaker.hh"
23     #include "boost/regex.hpp"
24     #include "boost/lexical_cast.hpp"
25    
26     MCTreeMaker::MCTreeMaker(const edm::ParameterSet & pset):
27     debug_(pset.getParameter<int>("DEBUG")),
28     doGen_(pset.getParameter<bool>("DOGEN")),
29     genParticleSrc_(pset.getParameter<edm::InputTag>("GenParticles")),
30     genJetSrc_(pset.getParameter<edm::InputTag>("GenJets")),
31     partonJetSrc_(pset.getParameter<edm::InputTag>("PartonJets")),
32     status1ToKeep_(pset.getParameter<std::vector<std::string> >("Status1ToKeep")),
33     status2ToKeep_(pset.getParameter<std::vector<std::string> >("Status2ToKeep")),
34     status3ToKeep_(pset.getParameter<std::vector<std::string> >("Status3ToKeep")),
35     event_(0),
36     tree_(0)
37     //processVec_(pset.getParameter<std::vector<std::string> >("ProcessVec"))
38     {//constructor
39     for (unsigned i = 0; i < status1ToKeep_.size(); ++i){
40     status1ToKeepRegex_.push_back(boost::regex(status1ToKeep_[i]));
41     }
42     for (unsigned i = 0; i < status2ToKeep_.size(); ++i){
43     status2ToKeepRegex_.push_back(boost::regex(status2ToKeep_[i]));
44     }
45     for (unsigned i = 0; i < status3ToKeep_.size(); ++i){
46     status3ToKeepRegex_.push_back(boost::regex(status3ToKeep_[i]));
47     }
48     }//constructor
49    
50     MCTreeMaker::~MCTreeMaker()
51     {//destructor
52    
53     }//destructor
54    
55    
56    
57     void MCTreeMaker::beginJob(){//beginJob
58    
59    
60     edm::Service<TFileService> lFileService;
61    
62     TFileDirectory lDir = lFileService->mkdir("MCAnalysis");
63    
64     tree_ = lDir.make<TTree>("Tree","Tree");
65     tree_->Branch("MCEvent","HbbAnalysis::MCEvent",&event_);
66     event_ = new HbbAnalysis::MCEvent();
67    
68     }//beginJob
69    
70    
71    
72     void MCTreeMaker::endJob(){//endJob
73    
74     //tree_->Write();
75     //delete tree_;
76     //delete event_;
77    
78     }//endJob
79    
80     void MCTreeMaker::beginRun(const edm::Run& aRun, const edm::EventSetup& aEvtSetup){//beginRun
81    
82     }
83    
84     void MCTreeMaker::analyze(const edm::Event& aEvt, const edm::EventSetup& aEvtSetup){//analyze
85    
86     if (debug_) std::cout << "Processing event #" << aEvt.id().event() << std::endl;
87    
88    
89     static bool firstEvent = true;
90    
91     event_->Clear();
92     event_->event(aEvt.id().event());
93    
94     event_->run(aEvt.run());
95     //used in jet method....
96     edm::Handle<reco::GenParticleCollection> lGenParticles;
97    
98     // edm::Handle<edm::HepMCProduct> mcProd;
99     // edm::Handle<GenRunInfoProduct> lRunProd;
100     // edm::Handle<GenEventInfoProduct> genEventInfoHandle;
101    
102     // try {
103     // aEvt.getByLabel("generator", mcProd );
104     // } catch(cms::Exception& e) {
105     // std::cout << "AMM: Collection HepMCProduct not available! Exception : " << e.what() << ". " << std::endl;
106     // }
107     // try {
108     // aEvt.getRun().getByLabel("generator",lRunProd);
109     // } catch(cms::Exception& e) {
110     // std::cout << "AMM: Collection GenInfoProduct not available! Exception : " << e.what() << ". " << std::endl;
111     // }
112     // try {
113     // aEvt.getByLabel("generator",genEventInfoHandle);
114     // } catch(cms::Exception& e) {
115     // std::cout << "AMM: Collection GenEventInfoProduct not available! Exception : " << e.what() << ". " << std::endl;
116     // }
117    
118     // HbbGenInfo(mcProd,lRunProd,genEventInfoHandle);
119    
120     edm::Handle<reco::GenJetCollection> lGenJets;
121     try {
122     aEvt.getByLabel(genJetSrc_,lGenJets);
123     if (debug_) std::cout << "** ngenJets = " << lGenJets->size() << std::endl;
124     } catch(cms::Exception& e) {
125     std::cout << "AMM: Collection GenJets not available! Exception : " << e.what() << ". " << std::endl;
126     }
127    
128     //save components
129     if (lGenJets.isValid()) HbbGenJets(lGenJets,event_->genjets(),true);
130    
131     edm::Handle<reco::GenJetCollection> lPartonJets;
132     try {
133     aEvt.getByLabel(partonJetSrc_,lPartonJets);
134     if (debug_) std::cout << "** npartonJets = " << lPartonJets->size() << std::endl;
135     } catch(cms::Exception& e) {
136     std::cout << "AMM: Collection PartonJets not available! Exception : " << e.what() << ". " << std::endl;
137     }
138    
139     //save components
140     if (lPartonJets.isValid()) HbbGenJets(lPartonJets,event_->partonjets(),true);
141    
142     edm::Handle<LHEEventProduct> lLHEEvt;
143     try {
144     aEvt.getByType(lLHEEvt);
145     } catch(cms::Exception& e) {
146     std::cout << "AMM: Collection of LHEEventProduct not available! Exception : " << e.what() << ". " << std::endl;
147     }
148    
149     if (lLHEEvt.isValid()){
150     HbbLHEInfo(lLHEEvt,event_->lheParticles());
151     }
152    
153     try {
154     aEvt.getByLabel(genParticleSrc_,lGenParticles);
155     if (debug_) std::cout << "** ngenParticles = " << lGenParticles->size() << std::endl;
156     } catch(cms::Exception& e) {
157     std::cout << "AMM: Collection genParticles not available! Exception : " << e.what() << ". " << std::endl;
158     }
159    
160     //if (doGen_) HbbParticles(lGenParticles,event_->particles());
161     HbbParticles(lGenParticles,event_->particles(), event_->p4total());
162    
163    
164     tree_->Fill();
165     firstEvent = false;
166    
167     }//analyze
168    
169    
170    
171     void MCTreeMaker::HbbParticles(const edm::Handle<reco::GenParticleCollection> & aCol,
172     std::vector<HbbAnalysis::GenParticle> & aVec,
173     HbbAnalysis::P4Total & p4total)
174     {//genparticles
175    
176    
177     const reco::GenParticle *ptrToFirst = 0;
178     if (aCol->size() > 0) ptrToFirst = &((*aCol)[0]);
179     //add genParticle inside vector
180     p4total.comE = 0.0;
181     p4total.sumPx = 0.0;
182     p4total.sumPy = 0.0;
183     p4total.sumPz = 0.0;
184     p4total.sumE = 0.0;
185     for (unsigned int mccount = 0;mccount<aCol->size();++mccount)
186     {//loop on particles
187     const reco::GenParticle & p = (*aCol)[mccount];
188    
189     if (p.status() == 3 && p.pdgId() == 2212){
190     p4total.comE += p.energy();
191     }
192     if (p.status() == 1){
193     p4total.sumPx += p.px();
194     p4total.sumPy += p.py();
195     p4total.sumPz += p.pz();
196     p4total.sumE += p.energy();
197     }
198    
199     bool keep = false;
200     if (p.status() == 1){
201     for (unsigned i = 0; i < status1ToKeepRegex_.size(); ++i){
202     //if (boost::regex_match(boost::lexical_cast<std::string>(p.pdgId()),status1ToKeepRegex_[i]) && p.pt() > 5.0) keep = true;
203     if (boost::regex_match(boost::lexical_cast<std::string>(p.pdgId()),status1ToKeepRegex_[i])) keep = true;
204     //if (abs(p.pdgId()) <= 16) keep = true; //Keep all status 1 leptons, regardless of matches passed through config
205     }
206     }
207     if (p.status() == 2){
208     for (unsigned i = 0; i < status2ToKeepRegex_.size(); ++i){
209     if (boost::regex_match(boost::lexical_cast<std::string>(p.pdgId()),status2ToKeepRegex_[i])) keep = true;
210     }
211     }
212     if (p.status() == 3){
213     for (unsigned i = 0; i < status3ToKeepRegex_.size(); ++i){
214     if (boost::regex_match(boost::lexical_cast<std::string>(p.pdgId()),status3ToKeepRegex_[i])) keep = true;
215     }
216     }
217    
218     if (keep){
219    
220     HbbAnalysis::FourMomentum lGenFourVec;
221     HbbAnalysis::GenVars lGen;
222     lGenFourVec.px = p.px();
223     lGenFourVec.py = p.py();
224     lGenFourVec.pz = p.pz();
225     lGenFourVec.e = p.energy();
226     lGen.charge = p.charge();
227     lGen.pdgId = p.pdgId();
228     lGen.status = p.status();
229     lGen.index = mccount;
230     lGen.vertex.vx = p.vx();
231     lGen.vertex.vy = p.vy();
232     lGen.vertex.vz = p.vz();
233    
234     HbbAnalysis::GenParticle lObj(lGenFourVec,lGen);
235    
236     for (unsigned i = 0; i < p.numberOfMothers(); ++i){
237     lObj.setParent((reco::GenParticle*)p.mother(i) - ptrToFirst);
238     //std::cout << ((reco::GenParticle*)p.mother(i) - ptrToFirst) << " " << p.mother(i) << "\t";
239     }
240     //std::cout << std::endl;
241     //std::cout << "Daughters: ";
242     for (unsigned i = 0; i < p.numberOfDaughters(); ++i){
243     lObj.setChild((reco::GenParticle*)p.daughter(i) - ptrToFirst);
244     //std::cout << ((reco::GenParticle*)p.daughter(i) - ptrToFirst) << " " << p.daughter(i) << "\t";
245     }
246     aVec.push_back(lObj);
247     //if (mccount < 50) std::cout << "------Particle " << mccount << std::endl;
248     //if (mccount < 50) lObj.Print();
249     //std::cout << std::endl;
250     }
251     /*
252     unsigned int nMum = p.numberOfMothers();
253     //std::cout << mccount << " index = " << const_cast<reco::GenParticle *>(&p)-lBegin << " has " << nMum << " mother(s)." << std::endl;
254    
255     if ( (!doGen_ &&
256     (p.status()==3 ||
257     (p.status()==2 && (abs(p.pdgId())==4 || abs(p.pdgId())==5)) ||
258     (p.status()==1 && ( (p.pt()>5 && abs(p.pdgId()) > 16) || abs(p.pdgId()) <= 16) )
259     )) || doGen_){
260     for(unsigned int mum=0;mum<nMum;++mum){//loop on mothers
261     const reco::Candidate * pMother = p.mother(mum);
262     //std::cout << "-- mother index = " << pMother-lBegin << std::endl;
263     for (unsigned int mccount2 = 0;mccount2<mccount;++mccount2)
264     {//loop on particles
265     const reco::Candidate & p2 = (*aCol)[mccount2];
266     if (pMother == &p2) {
267     lObj.setParent(mccount2);
268     break;
269     }
270     }
271     }//loop on mothers
272     */
273     }//loop on particles
274    
275    
276     if (debug_>1){
277     for (unsigned int imc(0); imc < aVec.size(); ++imc){
278     HbbAnalysis::GenParticle & p = aVec.at(imc);
279     p.Print();
280     }
281     }
282    
283    
284     }//genparticles
285    
286     void MCTreeMaker::HbbGenJets(const edm::Handle<reco::GenJetCollection> & aCol,
287     std::vector<HbbAnalysis::GenJet> & aVec,
288     const bool aSaveConstituants)
289     {
290    
291     if (aCol.isValid()){//valid
292     if (aCol->size() > 0) {//non empty
293    
294     unsigned int iEle = 0;
295     for (std::vector<reco::GenJet>::const_iterator iter = aCol->begin();
296     iter != aCol->end();
297     ++iter,++iEle)
298     {//loop on element
299     //if (debug_ > 1) std::cout << "**** Ele #" << iEle << ", pT,eta,phi = " << (*iter).pt() << " " << (*iter).eta() << " " << (*iter).phi() << std::endl;
300    
301     HbbAnalysis::FourMomentum lGenJetFourVec;
302     HbbAnalysis::GenJetVars lGenJet;
303     lGenJetFourVec.px = (*iter).px();
304     lGenJetFourVec.py = (*iter).py();
305     lGenJetFourVec.pz = (*iter).pz();
306     lGenJetFourVec.e = (*iter).energy();
307     lGenJet.mass = (*iter).mass();
308     lGenJet.nConstituents = (*iter).getGenConstituents().size();
309     lGenJet.flavour = 0; //Flavour will be set if constituents are added
310    
311    
312     //only status 1 saved !!!!
313     //get flavour from list of constituants.
314     //std::vector <const reco::GenParticle*> lConst = (*iter).getGenConstituents();
315     //unsigned int nb=0, nc=0;
316     // for (unsigned int iMC(0); iMC<lConst.size(); ++iMC){
317     // const reco::GenParticle* lPart = lConst[iMC];
318     // //std::cout << lPart->pdgId() << " " << fabs(lPart->pdgId()) << " " << abs(lPart->pdgId()) << std::endl;
319     // if (fabs(lPart->pdgId()) == 5){
320     // ++nb;
321     // break;
322     // }
323     // if (fabs(lPart->pdgId()) == 4) ++nc;
324     // }
325     // if (nb > 0) lGenJet.pdgId = 5;
326     // else if (nc > 0) lGenJet.pdgId = 4;
327     // else lGenJet.pdgId = 3;
328    
329     HbbAnalysis::GenJet lObj(lGenJetFourVec, lGenJet);
330     if (aSaveConstituants){
331     for (unsigned int iC(0); iC<(*iter).getGenConstituents().size(); ++iC){
332     const reco::GenParticle* p = (*iter).getGenConstituent(iC);
333     HbbAnalysis::FourMomentum lGenFourVec;
334     HbbAnalysis::GenVars lGen;
335     lGenFourVec.px = p->px();
336     lGenFourVec.py = p->py();
337     lGenFourVec.pz = p->pz();
338     lGenFourVec.e = p->energy();
339     lGen.charge = p->charge();
340     lGen.pdgId = p->pdgId();
341     lGen.status = p->status();
342     lGen.index = -1; //We don't know this now
343     lGen.vertex.vx = p->vx();
344     lGen.vertex.vy = p->vy();
345     lGen.vertex.vz = p->vz();
346     lObj.addConstituent(HbbAnalysis::GenParticle(lGenFourVec,lGen));
347     }
348     }
349     //std::cout << "--------------------------------------------" << std::endl;
350     //std::cout << "GenJet: " << std::endl;
351     //std::cout << "--------------------------------------------" << std::endl;
352     //lObj.Print();
353     aVec.push_back(lObj);
354     }
355     }
356     }
357     }
358    
359    
360    
361    
362     void MCTreeMaker::HbbLHEInfo(const edm::Handle<LHEEventProduct> & aLHEEvt,
363     std::vector<HbbAnalysis::GenParticle> & aVec) {
364    
365     const lhef::HEPEUP lhepeup = aLHEEvt->hepeup();
366    
367     //4-momentum (px,py,pz,E)+mass
368     const std::vector<lhef::HEPEUP::FiveVector> lpup = lhepeup.PUP;
369     //pdgid int
370     const std::vector<int> lidup = lhepeup.IDUP;
371     //status int
372     const std::vector<int> listup = lhepeup.ISTUP;
373     //mother: pair<int,int>
374     const std::vector<std::pair<int,int> > lmothup = lhepeup.MOTHUP;
375    
376    
377     //add genParticle inside vector
378     for (unsigned int imc = 0;imc<lpup.size();++imc)
379     {//loop on particles
380    
381     HbbAnalysis::FourMomentum lFourVec;
382     HbbAnalysis::GenVars lMC;
383     lMC.index = imc;
384    
385     TLorentzVector lVec((lpup[imc])[0],(lpup[imc])[1],(lpup[imc])[2],(lpup[imc])[3]);
386    
387     lFourVec.e = lVec.E();
388     lFourVec.px = lVec.Px();
389     lFourVec.py = lVec.Py();
390     lFourVec.pz = lVec.Pz();
391     /*
392     // Special case if pT = 0 (otherwise ROOT complains)
393     if (fabs(0.0 - lVec.Pt()) < std::numeric_limits<double>::epsilon()){
394     if (lVec.Pz() >= 0) {
395     lMC.eta = 1E10;
396     lMC.y = 1E10;
397     } else {
398     lMC.eta = -1E10;
399     lMC.y = -1E10;
400     }
401     } else {
402     lMC.eta = lVec.Eta();
403     lMC.y = lVec.Rapidity();
404     }
405     lMC.phi = lVec.Phi();
406     */
407     lMC.pdgId = lidup[imc];
408     lMC.status = listup[imc];
409    
410     HbbAnalysis::GenParticle lObj(lFourVec,lMC);
411     lObj.setParent(lmothup[imc].first);
412     lObj.setParent(lmothup[imc].second);
413     aVec.push_back(lObj);
414    
415     }//loop on particles
416    
417     if (debug_>1){
418     for (unsigned int imc(0); imc < aVec.size(); ++imc){
419     //HbbAnalysis::GenParticle & p = aVec.at(imc);
420     //p.print();
421     }
422     }
423     }
424    
425     #include "FWCore/Framework/interface/MakerMacros.h"
426     DEFINE_FWK_MODULE(MCTreeMaker);
427    
428