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

# Content
1 // $Id: FillerMCParticles.cc,v 1.18 2009/09/25 08:42:50 loizides Exp $
2
3 #include "MitProd/TreeFiller/interface/FillerMCParticles.h"
4 #include "FWCore/MessageLogger/interface/MessageLogger.h"
5 #include "DataFormats/Common/interface/Handle.h"
6 #include "DataFormats/Common/interface/RefToPtr.h"
7 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
8 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
9 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
10 #include "DataFormats/METReco/interface/METCollection.h"
11 #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 #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 #include "MitAna/DataTree/interface/MCParticleCol.h"
21 #include "MitProd/ObjectService/interface/ObjectService.h"
22
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 useAodGen_(Conf().getUntrackedParameter<bool>("useAodGen",true)),
32 simActive_(Conf().getUntrackedParameter<bool>("simActive",true)),
33 trackingActive_(Conf().getUntrackedParameter<bool>("trackingActive",false)),
34 genEdmName_(Conf().getUntrackedParameter<string>("genEdmName","genParticles")),
35 simEdmName_(Conf().getUntrackedParameter<string>("simEdmName","g4SimHits")),
36 trackingEdmName_(Conf().getUntrackedParameter<string>("trackingEdmName",
37 "mergedtruth:MergedTrackTruth")),
38 genMapName_(Conf().getUntrackedParameter<string>("genMapName","GenMap")),
39 simMapName_(Conf().getUntrackedParameter<string>("simMapName","SimMap")),
40 trackingMapName_(Conf().getUntrackedParameter<string>("trackingMapName","TrackingMap")),
41 mitName_(Conf().getUntrackedParameter<string>("mitName",Names::gkMCPartBrn)),
42 mcParticles_(new mithep::MCParticleArr(250)),
43 genMap_(genActive_?new mithep::GenParticleBarcodeMap:0),
44 aodGenMap_((genActive_&&useAodGen_)?new mithep::AODGenParticleMap:0),
45 simMap_(simActive_?new mithep::SimTrackTidMap:0),
46 trackingMap_(trackingActive_?new mithep::TrackingParticleMap:0)
47 {
48 // Constructor.
49 }
50
51 //--------------------------------------------------------------------------------------------------
52 FillerMCParticles::~FillerMCParticles()
53 {
54 // Destructor.
55
56 delete mcParticles_;
57 delete genMap_;
58 delete aodGenMap_;
59 delete simMap_;
60 delete trackingMap_;
61 }
62
63 //--------------------------------------------------------------------------------------------------
64 void FillerMCParticles::BookDataBlock(TreeWriter &tws, const edm::EventSetup &es)
65 {
66 // Add branch to tree and publish our maps.
67
68 Int_t brsize = tws.GetDefaultBrSize();
69 if (brsize<128*1024)
70 brsize=128*1024;
71
72 tws.AddBranch(mitName_,&mcParticles_,brsize);
73 OS()->add<mithep::MCParticleArr>(mcParticles_,mitName_);
74
75 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 }
91
92 //--------------------------------------------------------------------------------------------------
93 void FillerMCParticles::FillDataBlock(const edm::Event &event,
94 const edm::EventSetup &setup)
95 {
96 // Loop over generated or simulated particles and fill their information.
97
98 mcParticles_->Delete();
99
100 if (genActive_)
101 genMap_->Reset();
102
103 if (genActive_ && !useAodGen_) {
104 Handle<edm::HepMCProduct> hHepMCProduct;
105 GetProduct(genEdmName_, hHepMCProduct, event);
106
107 const HepMC::GenEvent &GenEvent = hHepMCProduct->getHepMCData();
108
109 // loop over all hepmc particles and copy their information
110 for (HepMC::GenEvent::particle_const_iterator pgen = GenEvent.particles_begin();
111 pgen != GenEvent.particles_end(); ++pgen) {
112
113 HepMC::GenParticle *mcPart = (*pgen);
114 if(!mcPart)
115 continue;
116
117 mithep::MCParticle *genParticle = mcParticles_->Allocate();
118 new (genParticle) mithep::MCParticle(mcPart->momentum().x(),mcPart->momentum().y(),
119 mcPart->momentum().z(),mcPart->momentum().e(),
120 mcPart->pdg_id(),mcPart->status());
121 genParticle->SetIsGenerated();
122 genMap_->Add(mcPart->barcode(), genParticle);
123 }
124 }
125
126 if (genActive_ && useAodGen_) {
127
128 aodGenMap_->Reset();
129
130 Handle<reco::GenParticleCollection> hGenPProduct;
131 GetProduct(genEdmName_, hGenPProduct, event);
132
133 // element by element aligned collection of hepmc barcodes associated to
134 // the genparticles
135 Handle<std::vector<int> > genBarcodes;
136 if (simActive_)
137 GetProduct(genEdmName_, genBarcodes, event);
138
139 const reco::GenParticleCollection genParticles = *(hGenPProduct.product());
140
141 // loop over all genparticles and copy their information
142 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 pgen->pz(),pgen->energy(),
148 pgen->pdgId(),pgen->status());
149
150 mcPart->SetIsGenerated();
151
152 // add hepmc barcode association, needed to merge in sim particles
153 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 if (simActive_) {
164 simMap_->Reset();
165
166 Handle<edm::SimTrackContainer> hSimTrackProduct;
167 GetProduct(simEdmName_, hSimTrackProduct, event);
168
169 const edm::SimTrackContainer simTracks = *(hSimTrackProduct.product());
170
171 // loop through all simParticles
172 for (SimTrackContainer::const_iterator iM = simTracks.begin();
173 iM != simTracks.end(); ++iM) {
174
175 mithep::MCParticle *outSimParticle = 0;
176
177 if (genActive_ && (iM->genpartIndex() >= 0) ) {
178 outSimParticle = genMap_->GetMit(iM->genpartIndex());
179 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 outSimParticle = mcParticles_->Allocate();
188 new (outSimParticle) mithep::MCParticle(iM->momentum().px(),
189 iM->momentum().py(),
190 iM->momentum().pz(),
191 iM->momentum().e(),
192 iM->type(), -99);
193 }
194
195 outSimParticle->SetIsSimulated();
196 simMap_->Add(iM->trackId(), outSimParticle);
197 }
198 }
199
200 // loop through TrackingParticles and build association map to MCParticles
201 if (trackingActive_) {
202 trackingMap_->Reset();
203
204 Handle<TrackingParticleCollection> hTrackingParticleProduct;
205 GetProduct(trackingEdmName_, hTrackingParticleProduct, event);
206
207 const TrackingParticleCollection trackingParticles = *(hTrackingParticleProduct.product());
208
209 // loop through all TrackingParticles
210 for (TrackingParticleCollection::const_iterator iM = trackingParticles.begin();
211 iM != trackingParticles.end(); ++iM) {
212
213 if ( simActive_ && iM->g4Tracks().size() ) {
214 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 if (verbose_>1) {
219 printf("trackId = %i\n",theSimTrack.trackId());
220 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 }
229 }
230 }
231
232 mcParticles_->Trim();
233 }
234
235 //--------------------------------------------------------------------------------------------------
236 void FillerMCParticles::ResolveLinks(const edm::Event &event,
237 const edm::EventSetup &setup)
238 {
239 // Loop over generated and simulated particles and resolve their links.
240
241 if (genActive_ && !useAodGen_) {
242
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 if(!mcPart)
253 continue;
254
255 // check if genpart has a decay vertex
256 HepMC::GenVertex *dVertex = mcPart->end_vertex();
257 if (!dVertex)
258 continue;
259
260 // find corresponding mithep genparticle parent in association table
261 mithep::MCParticle *genParent = genMap_->GetMit(mcPart->barcode());
262
263 // set decay vertex
264 // division by 10.0 is needed due to HepMC use of mm instead of cm for distance units
265 genParent->SetVertex(dVertex->point3d().x()/10.0,
266 dVertex->point3d().y()/10.0,
267 dVertex->point3d().z()/10.0);
268
269 // loop through daugthers
270 for (HepMC::GenVertex::particles_out_const_iterator pgenD =
271 dVertex->particles_out_const_begin();
272 pgenD != dVertex->particles_out_const_end(); ++pgenD) {
273 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 // loop over aod GenParticle candidates and resolve their links
283 if (genActive_ && useAodGen_) {
284
285 Handle<reco::GenParticleCollection> hGenPProduct;
286 GetProduct(genEdmName_, hGenPProduct, event);
287
288 const reco::GenParticleCollection genParticles = *(hGenPProduct.product());
289 // loop over all genparticles and copy their information
290 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 // set mother decay vertex
301 if (i==0)
302 mcMother->SetVertex(genDaughter->vx(),genDaughter->vy(),genDaughter->vz());
303
304 // set mother-daughter links
305 mcMother->AddDaughter(mcDaughter);
306 if (!mcDaughter->HasMother())
307 mcDaughter->SetMother(mcMother);
308 }
309 }
310 }
311 }
312
313 // loop over SimTracks and resolve links
314 if (simActive_) {
315 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 // loop through all simParticles
326 for (SimTrackContainer::const_iterator iM = simTracks.begin();
327 iM != simTracks.end(); ++iM) {
328
329 if (iM->vertIndex()>=0) {
330 mithep::MCParticle *simDaughter = simMap_->GetMit(iM->trackId());
331 const SimVertex &theVertex = simVertexes.at(iM->vertIndex());
332 if (theVertex.parentIndex()>=0) {
333 mithep::MCParticle *simParent = simMap_->GetMit(theVertex.parentIndex());
334 simParent->SetVertex(theVertex.position().x(),
335 theVertex.position().y(),
336 theVertex.position().z());
337 //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 }
345 }
346 }
347 }
348
349 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 }
366 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 }
378 }
379 }