ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/TreeFiller/src/FillerMCParticles.cc
Revision: 1.15
Committed: Mon Jun 15 15:00:26 2009 UTC (15 years, 10 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_009c, Mit_009b
Changes since 1.14: +5 -2 lines
Log Message:
Added proper fwd defs plus split up complilation of MitAna/DataTree LinkDefs.

File Contents

# Content
1 // $Id: FillerMCParticles.cc,v 1.14 2009/05/24 12:54:32 bendavid 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 "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
8 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
9 #include "DataFormats/METReco/interface/METCollection.h"
10 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.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)
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) continue;
115
116 mithep::MCParticle *genParticle = mcParticles_->Allocate();
117 new (genParticle) mithep::MCParticle(mcPart->momentum().x(),mcPart->momentum().y(),
118 mcPart->momentum().z(),mcPart->momentum().e(),
119 mcPart->pdg_id(),mcPart->status());
120 genParticle->SetIsGenerated();
121 genMap_->Add(mcPart->barcode(), genParticle);
122 }
123 }
124
125 if (genActive_ && useAodGen_) {
126
127 aodGenMap_->Reset();
128
129 Handle<reco::GenParticleCollection> hGenPProduct;
130 GetProduct(genEdmName_, hGenPProduct, event);
131
132 // element by element aligned collection of hepmc barcodes associated to
133 // the genparticles
134 Handle<std::vector<int> > genBarcodes;
135 if (simActive_)
136 GetProduct(genEdmName_, genBarcodes, event);
137
138 const reco::GenParticleCollection genParticles = *(hGenPProduct.product());
139
140 // loop over all genparticles and copy their information
141 for (reco::GenParticleCollection::const_iterator pgen = genParticles.begin();
142 pgen != genParticles.end(); ++pgen) {
143
144 mithep::MCParticle *mcPart = mcParticles_->Allocate();
145 new (mcPart) mithep::MCParticle(pgen->px(),pgen->py(),
146 pgen->pz(),pgen->energy(),
147 pgen->pdgId(),
148 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) continue;
253
254 // check if genpart has a decay vertex
255 HepMC::GenVertex *dVertex = mcPart->end_vertex();
256 if (!dVertex) continue;
257
258 // find corresponding mithep genparticle parent in association table
259 mithep::MCParticle *genParent = genMap_->GetMit(mcPart->barcode());
260
261 // set decay vertex
262 // division by 10.0 is needed due to HepMC use of mm instead of cm for distance units
263 genParent->SetVertex(dVertex->point3d().x()/10.0,
264 dVertex->point3d().y()/10.0,
265 dVertex->point3d().z()/10.0);
266
267 // loop through daugthers
268 for (HepMC::GenVertex::particles_out_const_iterator pgenD =
269 dVertex->particles_out_const_begin();
270 pgenD != dVertex->particles_out_const_end(); ++pgenD) {
271 HepMC::GenParticle *mcDaughter = (*pgenD);
272 mithep::MCParticle *genDaughter = genMap_->GetMit(mcDaughter->barcode());
273 genParent->AddDaughter(genDaughter);
274 if (!(genDaughter->HasMother()))
275 genDaughter->SetMother(genParent);
276 }
277 }
278 }
279
280 // loop over aod GenParticle candidates and resolve their links
281 if (genActive_ && useAodGen_) {
282
283 Handle<reco::GenParticleCollection> hGenPProduct;
284 GetProduct(genEdmName_, hGenPProduct, event);
285
286 const reco::GenParticleCollection genParticles = *(hGenPProduct.product());
287
288 // loop over all genparticles and copy their information
289 for (reco::GenParticleCollection::const_iterator pgen = genParticles.begin();
290 pgen != genParticles.end(); ++pgen) {
291
292 int nDaughters = pgen->numberOfDaughters();
293
294 if (nDaughters>0) {
295
296 edm::Ptr<reco::GenParticle> thePtr(hGenPProduct, pgen - genParticles.begin());
297 MCParticle *mcMother = aodGenMap_->GetMit(thePtr);
298 for (int i=0; i<nDaughters; ++i) {
299 const reco::Candidate *genDaughter = pgen->daughter(i);
300 MCParticle *mcDaughter = aodGenMap_->GetMit(refToPtr(pgen->daughterRef(i)));
301 // set mother decay vertex
302 if (i==0)
303 mcMother->SetVertex(genDaughter->vx(),genDaughter->vy(),genDaughter->vz());
304
305 // set mother-daughter links
306 mcMother->AddDaughter(mcDaughter);
307 if (!mcDaughter->HasMother())
308 mcDaughter->SetMother(mcMother);
309
310 }
311 }
312 }
313
314 }
315
316 // loop over SimTracks and resolve links
317 if (simActive_) {
318 Handle<edm::SimTrackContainer> hSimTrackProduct;
319 GetProduct(simEdmName_, hSimTrackProduct, event);
320
321 const edm::SimTrackContainer simTracks = *(hSimTrackProduct.product());
322
323 Handle<std::vector<SimVertex> > hSimVertexProduct;
324 GetProduct(simEdmName_, hSimVertexProduct, event);
325
326 const edm::SimVertexContainer simVertexes = *(hSimVertexProduct.product());
327
328 // loop through all simParticles
329 for (SimTrackContainer::const_iterator iM = simTracks.begin();
330 iM != simTracks.end(); ++iM) {
331
332 if (iM->vertIndex()>=0) {
333 mithep::MCParticle *simDaughter = simMap_->GetMit(iM->trackId());
334 const SimVertex &theVertex = simVertexes.at(iM->vertIndex());
335 if (theVertex.parentIndex()>=0) {
336 mithep::MCParticle *simParent = simMap_->GetMit(theVertex.parentIndex());
337 simParent->SetVertex(theVertex.position().x(),
338 theVertex.position().y(),
339 theVertex.position().z());
340 //make sure we don't double count the decay tree
341 if ( !simParent->HasDaughter(simDaughter) ) {
342 simParent->AddDaughter(simDaughter);
343 }
344 if ( !simDaughter->HasMother() ) {
345 simDaughter->SetMother(simParent);
346 }
347 }
348 }
349 }
350 }
351
352 if (verbose_>1 && trackingActive_) {
353 Handle<TrackingParticleCollection> hTrackingParticleProduct;
354 GetProduct(trackingEdmName_, hTrackingParticleProduct, event);
355
356 const TrackingParticleCollection trackingParticles = *(hTrackingParticleProduct.product());
357
358 // loop through all simParticles
359 for (TrackingParticleCollection::const_iterator iM = trackingParticles.begin();
360 iM != trackingParticles.end(); ++iM) {
361
362 TrackingParticleRef theRef(hTrackingParticleProduct, iM-trackingParticles.begin());
363
364 mithep::MCParticle *simParent = trackingMap_->GetMit(theRef);
365 printf("MCParticle pdg = %i, Daughter Pdgs: ", simParent->PdgId());
366 for (UInt_t i=0; i<simParent->NDaughters(); ++i) {
367 printf("%i, ", simParent->Daughter(i)->PdgId());
368 }
369 printf("\n");
370
371 printf("TrackingParticle pdg = %i, Daughter Pdgs: ", iM->pdgId());
372 for (TrackingVertexRefVector::iterator v= iM->decayVertices().begin();
373 v != iM->decayVertices().end(); ++v) {
374 for (TrackingParticleRefVector::iterator pd = v->get()->daughterTracks().begin();
375 pd != v->get()->daughterTracks().end(); ++pd) {
376 printf("%i, ", pd->get()->pdgId());
377 }
378 }
379 printf("\n");
380 }
381 }
382 }