ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/TreeFiller/src/FillerMCParticles.cc
Revision: 1.22
Committed: Fri Mar 11 04:03:55 2011 UTC (14 years, 1 month ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_025c_branch2, Mit_025c_branch1, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_025c_branch0, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, HEAD
Branch point for: Mit_025c_branch
Changes since 1.21: +2 -2 lines
Log Message:
various minor changes to acommodate new root and architecture

File Contents

# User Rev Content
1 bendavid 1.22 // $Id: FillerMCParticles.cc,v 1.21 2010/03/18 20:21:00 bendavid Exp $
2 bendavid 1.1
3     #include "MitProd/TreeFiller/interface/FillerMCParticles.h"
4     #include "FWCore/MessageLogger/interface/MessageLogger.h"
5     #include "DataFormats/Common/interface/Handle.h"
6 bendavid 1.8 #include "DataFormats/Common/interface/RefToPtr.h"
7 bendavid 1.16 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
8 bendavid 1.8 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
9     #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
10 loizides 1.15 #include "DataFormats/METReco/interface/METCollection.h"
11 bendavid 1.4 #include "SimDataFormats/Track/interface/SimTrack.h"
12     #include "SimDataFormats/Track/interface/SimTrackContainer.h"
13     #include "SimDataFormats/Vertex/interface/SimVertex.h"
14     #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
15 bendavid 1.1 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
16     #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
17     #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertex.h"
18     #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertexContainer.h"
19 bendavid 1.20 #include "DataFormats/TrackingRecHit/interface/InvalidTrackingRecHit.h"
20     #include "DataFormats/TrackReco/interface/HitPattern.h"
21 bendavid 1.1 #include "MitAna/DataTree/interface/Names.h"
22 loizides 1.15 #include "MitAna/DataTree/interface/MCParticleCol.h"
23 bendavid 1.20 #include "MitAna/DataTree/interface/TrackingParticleCol.h"
24 loizides 1.15 #include "MitProd/ObjectService/interface/ObjectService.h"
25 bendavid 1.1
26     using namespace std;
27     using namespace edm;
28     using namespace mithep;
29    
30     //--------------------------------------------------------------------------------------------------
31     FillerMCParticles::FillerMCParticles(const ParameterSet &cfg, const char *name, bool active) :
32     BaseFiller(cfg, name, active),
33     genActive_(Conf().getUntrackedParameter<bool>("genActive",true)),
34 bendavid 1.8 useAodGen_(Conf().getUntrackedParameter<bool>("useAodGen",true)),
35 bendavid 1.1 simActive_(Conf().getUntrackedParameter<bool>("simActive",true)),
36 bendavid 1.4 trackingActive_(Conf().getUntrackedParameter<bool>("trackingActive",false)),
37 bendavid 1.20 fillTracking_(Conf().getUntrackedParameter<bool>("fillTracking",false)),
38 loizides 1.12 genEdmName_(Conf().getUntrackedParameter<string>("genEdmName","genParticles")),
39 bendavid 1.4 simEdmName_(Conf().getUntrackedParameter<string>("simEdmName","g4SimHits")),
40 loizides 1.9 trackingEdmName_(Conf().getUntrackedParameter<string>("trackingEdmName",
41     "mergedtruth:MergedTrackTruth")),
42 loizides 1.3 genMapName_(Conf().getUntrackedParameter<string>("genMapName","GenMap")),
43     simMapName_(Conf().getUntrackedParameter<string>("simMapName","SimMap")),
44 bendavid 1.4 trackingMapName_(Conf().getUntrackedParameter<string>("trackingMapName","TrackingMap")),
45 bendavid 1.1 mitName_(Conf().getUntrackedParameter<string>("mitName",Names::gkMCPartBrn)),
46 bendavid 1.20 mitTrackingName_(Conf().getUntrackedParameter<string>("mitTrackingName",Names::gkTrackingParticleBrn)),
47 bendavid 1.1 mcParticles_(new mithep::MCParticleArr(250)),
48 bendavid 1.20 trackingParticles_(new mithep::TrackingParticleArr(250)),
49 bendavid 1.8 genMap_(genActive_?new mithep::GenParticleBarcodeMap:0),
50     aodGenMap_((genActive_&&useAodGen_)?new mithep::AODGenParticleMap:0),
51     simMap_(simActive_?new mithep::SimTrackTidMap:0),
52 bendavid 1.4 trackingMap_(trackingActive_?new mithep::TrackingParticleMap:0)
53 bendavid 1.1 {
54     // Constructor.
55     }
56    
57     //--------------------------------------------------------------------------------------------------
58     FillerMCParticles::~FillerMCParticles()
59     {
60     // Destructor.
61    
62     delete mcParticles_;
63 bendavid 1.20 delete trackingParticles_;
64 bendavid 1.1 delete genMap_;
65 bendavid 1.8 delete aodGenMap_;
66 bendavid 1.1 delete simMap_;
67 bendavid 1.4 delete trackingMap_;
68 bendavid 1.1 }
69    
70     //--------------------------------------------------------------------------------------------------
71 bendavid 1.21 void FillerMCParticles::BookDataBlock(TreeWriter &tws)
72 bendavid 1.1 {
73 loizides 1.3 // Add branch to tree and publish our maps.
74 bendavid 1.1
75 loizides 1.13 Int_t brsize = tws.GetDefaultBrSize();
76     if (brsize<128*1024)
77     brsize=128*1024;
78    
79     tws.AddBranch(mitName_,&mcParticles_,brsize);
80 loizides 1.9 OS()->add<mithep::MCParticleArr>(mcParticles_,mitName_);
81 loizides 1.2
82 bendavid 1.20 if (trackingActive_ && fillTracking_) {
83     tws.AddBranch(mitTrackingName_,&trackingParticles_,brsize);
84     OS()->add<mithep::TrackingParticleArr>(trackingParticles_,mitTrackingName_);
85     }
86    
87 loizides 1.9 if (genActive_ && !genMapName_.empty()) {
88     genMap_->SetBrName(mitName_);
89     if (genMap_)
90     OS()->add(genMap_,genMapName_);
91     }
92     if (simActive_ && !simMapName_.empty()) {
93     simMap_->SetBrName(mitName_);
94     if (simMap_)
95     OS()->add(simMap_,simMapName_);
96     }
97     if (trackingActive_ && !trackingMapName_.empty()) {
98     trackingMap_->SetBrName(mitName_);
99     if (trackingMap_)
100     OS()->add(trackingMap_,trackingMapName_);
101     }
102 bendavid 1.1 }
103    
104     //--------------------------------------------------------------------------------------------------
105     void FillerMCParticles::FillDataBlock(const edm::Event &event,
106 loizides 1.2 const edm::EventSetup &setup)
107 bendavid 1.1 {
108 loizides 1.9 // Loop over generated or simulated particles and fill their information.
109 bendavid 1.1
110 bendavid 1.7 mcParticles_->Delete();
111 bendavid 1.20
112     if (trackingActive_ && fillTracking_) {
113     trackingParticles_->Delete();
114     }
115 bendavid 1.1
116 bendavid 1.8 if (genActive_)
117 loizides 1.3 genMap_->Reset();
118 bendavid 1.1
119 bendavid 1.8 if (genActive_ && !useAodGen_) {
120 bendavid 1.1 Handle<edm::HepMCProduct> hHepMCProduct;
121     GetProduct(genEdmName_, hHepMCProduct, event);
122    
123     const HepMC::GenEvent &GenEvent = hHepMCProduct->getHepMCData();
124 loizides 1.18
125 loizides 1.9 // loop over all hepmc particles and copy their information
126 bendavid 1.1 for (HepMC::GenEvent::particle_const_iterator pgen = GenEvent.particles_begin();
127     pgen != GenEvent.particles_end(); ++pgen) {
128    
129     HepMC::GenParticle *mcPart = (*pgen);
130 loizides 1.18 if(!mcPart)
131     continue;
132 bendavid 1.1
133     mithep::MCParticle *genParticle = mcParticles_->Allocate();
134     new (genParticle) mithep::MCParticle(mcPart->momentum().x(),mcPart->momentum().y(),
135 loizides 1.11 mcPart->momentum().z(),mcPart->momentum().e(),
136     mcPart->pdg_id(),mcPart->status());
137 bendavid 1.1 genParticle->SetIsGenerated();
138     genMap_->Add(mcPart->barcode(), genParticle);
139     }
140     }
141    
142 bendavid 1.8 if (genActive_ && useAodGen_) {
143 loizides 1.9
144 bendavid 1.8 aodGenMap_->Reset();
145    
146     Handle<reco::GenParticleCollection> hGenPProduct;
147     GetProduct(genEdmName_, hGenPProduct, event);
148    
149 loizides 1.9 // element by element aligned collection of hepmc barcodes associated to
150     // the genparticles
151 bendavid 1.8 Handle<std::vector<int> > genBarcodes;
152     if (simActive_)
153     GetProduct(genEdmName_, genBarcodes, event);
154    
155     const reco::GenParticleCollection genParticles = *(hGenPProduct.product());
156    
157 loizides 1.11 // loop over all genparticles and copy their information
158 bendavid 1.8 for (reco::GenParticleCollection::const_iterator pgen = genParticles.begin();
159     pgen != genParticles.end(); ++pgen) {
160    
161     mithep::MCParticle *mcPart = mcParticles_->Allocate();
162     new (mcPart) mithep::MCParticle(pgen->px(),pgen->py(),
163 loizides 1.18 pgen->pz(),pgen->energy(),
164     pgen->pdgId(),pgen->status());
165 bendavid 1.8
166     mcPart->SetIsGenerated();
167    
168 loizides 1.9 // add hepmc barcode association, needed to merge in sim particles
169 bendavid 1.8 if (simActive_) {
170     int genBarcode = genBarcodes->at(pgen - genParticles.begin());
171     genMap_->Add(genBarcode, mcPart);
172     }
173    
174     edm::Ptr<reco::GenParticle> thePtr(hGenPProduct, pgen - genParticles.begin());
175     aodGenMap_->Add(thePtr, mcPart);
176     }
177     }
178    
179 bendavid 1.1 if (simActive_) {
180 loizides 1.3 simMap_->Reset();
181 bendavid 1.4
182     Handle<edm::SimTrackContainer> hSimTrackProduct;
183     GetProduct(simEdmName_, hSimTrackProduct, event);
184    
185     const edm::SimTrackContainer simTracks = *(hSimTrackProduct.product());
186    
187 bendavid 1.1 // loop through all simParticles
188 bendavid 1.4 for (SimTrackContainer::const_iterator iM = simTracks.begin();
189     iM != simTracks.end(); ++iM) {
190    
191 bendavid 1.8 mithep::MCParticle *outSimParticle = 0;
192 bendavid 1.1
193 bendavid 1.4 if (genActive_ && (iM->genpartIndex() >= 0) ) {
194     outSimParticle = genMap_->GetMit(iM->genpartIndex());
195 loizides 1.9 if (verbose_>1) {
196     printf("Sim px,py,pz,e = %f,%f,%f,%f\nGen px,py,pz,e = %f,%f,%f,%f\n",
197     iM->momentum().px(),iM->momentum().py(),
198     iM->momentum().pz(),iM->momentum().e(),
199     outSimParticle->Px(),outSimParticle->Py(),
200     outSimParticle->Pz(),outSimParticle->E());
201     }
202     } else {
203 bendavid 1.1 outSimParticle = mcParticles_->Allocate();
204 bendavid 1.4 new (outSimParticle) mithep::MCParticle(iM->momentum().px(),
205     iM->momentum().py(),
206     iM->momentum().pz(),
207     iM->momentum().e(),
208 loizides 1.9 iM->type(), -99);
209 bendavid 1.1 }
210    
211     outSimParticle->SetIsSimulated();
212 bendavid 1.8 simMap_->Add(iM->trackId(), outSimParticle);
213 bendavid 1.4 }
214     }
215    
216 loizides 1.10 // loop through TrackingParticles and build association map to MCParticles
217 bendavid 1.4 if (trackingActive_) {
218     trackingMap_->Reset();
219    
220     Handle<TrackingParticleCollection> hTrackingParticleProduct;
221     GetProduct(trackingEdmName_, hTrackingParticleProduct, event);
222    
223 bendavid 1.5 const TrackingParticleCollection trackingParticles = *(hTrackingParticleProduct.product());
224 bendavid 1.4
225     // loop through all TrackingParticles
226 bendavid 1.5 for (TrackingParticleCollection::const_iterator iM = trackingParticles.begin();
227 bendavid 1.4 iM != trackingParticles.end(); ++iM) {
228    
229     if ( simActive_ && iM->g4Tracks().size() ) {
230 bendavid 1.14 TrackingParticleRef theRef(hTrackingParticleProduct, iM-trackingParticles.begin());
231     const SimTrack &theSimTrack = iM->g4Tracks().at(iM->g4Tracks().size()-1);
232     mithep::MCParticle *outSimParticle = simMap_->GetMit(theSimTrack.trackId());
233     trackingMap_->Add(theRef, outSimParticle);
234 bendavid 1.20
235     if (fillTracking_) {
236     mithep::TrackingParticle *outTracking = trackingParticles_->AddNew();
237    
238     //fill associated sim tracks
239     for (std::vector<SimTrack>::const_iterator iST = iM->g4Tracks().begin();
240     iST != iM->g4Tracks().end(); ++iST) {
241     outTracking->AddMCPart(simMap_->GetMit(theSimTrack.trackId()));
242     }
243    
244     //fill hit information
245     reco::HitPattern hitPattern;
246     int nHits = 0;
247    
248     //loop through sim hits and construct intermediate reco::HitPattern
249     for (std::vector<PSimHit>::const_iterator iSimHit = iM->pSimHit_begin();
250     iSimHit != iM->pSimHit_end(); ++iSimHit) {
251    
252     unsigned int detectorIdIndex = iSimHit->detUnitId();
253     DetId detectorId = DetId(detectorIdIndex);
254    
255     InvalidTrackingRecHit tHit(detectorId,TrackingRecHit::valid);
256    
257     hitPattern.set(tHit,nHits);
258     ++nHits;
259    
260     }
261    
262     //construct hit layer bitmask as used in bambu
263     BitMask48 hits;
264     // search for all good crossed layers (with or without hit)
265     for (Int_t hi=0; hi<hitPattern.numberOfHits(); hi++) {
266     uint32_t hit = hitPattern.getHitPattern(hi);
267     if (hitPattern.getHitType(hit)<=1) {
268     if (hitPattern.trackerHitFilter(hit)) {
269     hits.SetBit(hitReader_.Layer(hit));
270     }
271     }
272     }
273    
274     outTracking->SetHits(hits);
275    
276     if (verbose_>1) {
277     printf("Hit Layer Mask: ");
278     for (Int_t biti=47; biti >= 0; --biti) {
279     printf("%i",hits.TestBit(biti));
280     }
281     printf("\n");
282     }
283    
284     }
285    
286 loizides 1.9 if (verbose_>1) {
287 bendavid 1.14 printf("trackId = %i\n",theSimTrack.trackId());
288 bendavid 1.22 printf("Tracking particle has %i SimTracks\n",(int)iM->g4Tracks().size());
289 loizides 1.9 if (iM->g4Tracks().size()>1) {
290     for (std::vector<SimTrack>::const_iterator iST = iM->g4Tracks().begin();
291     iST != iM->g4Tracks().end(); ++iST) {
292     printf("g4 trackid = %i\n",iST->trackId());
293     }
294     }
295     }
296 bendavid 1.4 }
297 bendavid 1.1 }
298     }
299    
300     mcParticles_->Trim();
301     }
302    
303     //--------------------------------------------------------------------------------------------------
304     void FillerMCParticles::ResolveLinks(const edm::Event &event,
305 loizides 1.2 const edm::EventSetup &setup)
306 bendavid 1.1 {
307 loizides 1.9 // Loop over generated and simulated particles and resolve their links.
308 bendavid 1.1
309 bendavid 1.8 if (genActive_ && !useAodGen_) {
310 bendavid 1.1
311     Handle<edm::HepMCProduct> hHepMCProduct;
312     GetProduct(genEdmName_, hHepMCProduct, event);
313    
314     const HepMC::GenEvent &GenEvent = hHepMCProduct->getHepMCData();
315    
316     for (HepMC::GenEvent::particle_const_iterator pgen = GenEvent.particles_begin();
317     pgen != GenEvent.particles_end(); ++pgen) {
318    
319     HepMC::GenParticle *mcPart = (*pgen);
320 loizides 1.18 if(!mcPart)
321     continue;
322 bendavid 1.1
323 loizides 1.11 // check if genpart has a decay vertex
324 bendavid 1.1 HepMC::GenVertex *dVertex = mcPart->end_vertex();
325 loizides 1.18 if (!dVertex)
326     continue;
327    
328 loizides 1.11 // find corresponding mithep genparticle parent in association table
329 bendavid 1.1 mithep::MCParticle *genParent = genMap_->GetMit(mcPart->barcode());
330    
331 loizides 1.11 // set decay vertex
332     // division by 10.0 is needed due to HepMC use of mm instead of cm for distance units
333 bendavid 1.1 genParent->SetVertex(dVertex->point3d().x()/10.0,
334 loizides 1.17 dVertex->point3d().y()/10.0,
335     dVertex->point3d().z()/10.0);
336 bendavid 1.1
337 loizides 1.11 // loop through daugthers
338 loizides 1.3 for (HepMC::GenVertex::particles_out_const_iterator pgenD =
339     dVertex->particles_out_const_begin();
340     pgenD != dVertex->particles_out_const_end(); ++pgenD) {
341 bendavid 1.1 HepMC::GenParticle *mcDaughter = (*pgenD);
342     mithep::MCParticle *genDaughter = genMap_->GetMit(mcDaughter->barcode());
343     genParent->AddDaughter(genDaughter);
344     if (!(genDaughter->HasMother()))
345     genDaughter->SetMother(genParent);
346     }
347     }
348     }
349    
350 loizides 1.11 // loop over aod GenParticle candidates and resolve their links
351 bendavid 1.8 if (genActive_ && useAodGen_) {
352    
353     Handle<reco::GenParticleCollection> hGenPProduct;
354     GetProduct(genEdmName_, hGenPProduct, event);
355    
356     const reco::GenParticleCollection genParticles = *(hGenPProduct.product());
357 loizides 1.11 // loop over all genparticles and copy their information
358 bendavid 1.8 for (reco::GenParticleCollection::const_iterator pgen = genParticles.begin();
359     pgen != genParticles.end(); ++pgen) {
360    
361     int nDaughters = pgen->numberOfDaughters();
362     if (nDaughters>0) {
363     edm::Ptr<reco::GenParticle> thePtr(hGenPProduct, pgen - genParticles.begin());
364     MCParticle *mcMother = aodGenMap_->GetMit(thePtr);
365     for (int i=0; i<nDaughters; ++i) {
366     const reco::Candidate *genDaughter = pgen->daughter(i);
367     MCParticle *mcDaughter = aodGenMap_->GetMit(refToPtr(pgen->daughterRef(i)));
368 loizides 1.11 // set mother decay vertex
369 bendavid 1.8 if (i==0)
370     mcMother->SetVertex(genDaughter->vx(),genDaughter->vy(),genDaughter->vz());
371 loizides 1.18
372 loizides 1.11 // set mother-daughter links
373 bendavid 1.8 mcMother->AddDaughter(mcDaughter);
374     if (!mcDaughter->HasMother())
375     mcDaughter->SetMother(mcMother);
376     }
377     }
378     }
379     }
380    
381 loizides 1.11 // loop over SimTracks and resolve links
382 bendavid 1.1 if (simActive_) {
383 bendavid 1.4 Handle<edm::SimTrackContainer> hSimTrackProduct;
384     GetProduct(simEdmName_, hSimTrackProduct, event);
385    
386     const edm::SimTrackContainer simTracks = *(hSimTrackProduct.product());
387    
388     Handle<std::vector<SimVertex> > hSimVertexProduct;
389     GetProduct(simEdmName_, hSimVertexProduct, event);
390    
391     const edm::SimVertexContainer simVertexes = *(hSimVertexProduct.product());
392    
393 bendavid 1.1 // loop through all simParticles
394 bendavid 1.4 for (SimTrackContainer::const_iterator iM = simTracks.begin();
395     iM != simTracks.end(); ++iM) {
396    
397     if (iM->vertIndex()>=0) {
398 bendavid 1.8 mithep::MCParticle *simDaughter = simMap_->GetMit(iM->trackId());
399 bendavid 1.4 const SimVertex &theVertex = simVertexes.at(iM->vertIndex());
400     if (theVertex.parentIndex()>=0) {
401 bendavid 1.8 mithep::MCParticle *simParent = simMap_->GetMit(theVertex.parentIndex());
402 loizides 1.9 simParent->SetVertex(theVertex.position().x(),
403     theVertex.position().y(),
404     theVertex.position().z());
405 bendavid 1.6 //make sure we don't double count the decay tree
406     if ( !simParent->HasDaughter(simDaughter) ) {
407     simParent->AddDaughter(simDaughter);
408     }
409     if ( !simDaughter->HasMother() ) {
410     simDaughter->SetMother(simParent);
411     }
412 bendavid 1.1 }
413 loizides 1.9 }
414     }
415     }
416 bendavid 1.4
417 loizides 1.9 if (verbose_>1 && trackingActive_) {
418     Handle<TrackingParticleCollection> hTrackingParticleProduct;
419     GetProduct(trackingEdmName_, hTrackingParticleProduct, event);
420    
421     const TrackingParticleCollection trackingParticles = *(hTrackingParticleProduct.product());
422    
423     // loop through all simParticles
424     for (TrackingParticleCollection::const_iterator iM = trackingParticles.begin();
425     iM != trackingParticles.end(); ++iM) {
426    
427     TrackingParticleRef theRef(hTrackingParticleProduct, iM-trackingParticles.begin());
428    
429     mithep::MCParticle *simParent = trackingMap_->GetMit(theRef);
430     printf("MCParticle pdg = %i, Daughter Pdgs: ", simParent->PdgId());
431     for (UInt_t i=0; i<simParent->NDaughters(); ++i) {
432     printf("%i, ", simParent->Daughter(i)->PdgId());
433 bendavid 1.1 }
434 loizides 1.9 printf("\n");
435    
436     printf("TrackingParticle pdg = %i, Daughter Pdgs: ", iM->pdgId());
437     for (TrackingVertexRefVector::iterator v= iM->decayVertices().begin();
438     v != iM->decayVertices().end(); ++v) {
439     for (TrackingParticleRefVector::iterator pd = v->get()->daughterTracks().begin();
440     pd != v->get()->daughterTracks().end(); ++pd) {
441     printf("%i, ", pd->get()->pdgId());
442     }
443     }
444     printf("\n");
445 bendavid 1.1 }
446     }
447     }