ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/TreeFiller/src/FillerMCParticles.cc
Revision: 1.19
Committed: Mon Oct 12 21:41:47 2009 UTC (15 years, 6 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a
Changes since 1.18: +1 -2 lines
Log Message:
Removed warning

File Contents

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