CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFSimParticleProducer.cc
Go to the documentation of this file.
2 
5 
8 
11 
12 // include files used for reconstructed tracks
13 // #include "DataFormats/TrackReco/interface/Track.h"
14 // #include "DataFormats/TrackReco/interface/TrackFwd.h"
15 // #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
16 // #include "DataFormats/TrackingRecHit/interface/TrackingRecHitFwd.h"
17 // #include "DataFormats/TrackCandidate/interface/TrackCandidate.h"
18 // #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
19 // #include "DataFormats/TrajectorySeed/interface/PropagationDirection.h"
20 
37 // #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
39 
43 
49 
60 
61 #include <set>
62 #include <sstream>
63 
64 using namespace std;
65 using namespace edm;
66 
68 {
69 
70 
71  processParticles_ =
72  iConfig.getUntrackedParameter<bool>("process_Particles",true);
73 
74 
75  inputTagSim_
76  = iConfig.getParameter<InputTag>("sim");
77 
78  //retrieving collections for MC Truth Matching
79 
80  //modif-beg
81  inputTagFamosSimHits_
82  = iConfig.getUntrackedParameter<InputTag>("famosSimHits");
83  mctruthMatchingInfo_ =
84  iConfig.getUntrackedParameter<bool>("MCTruthMatchingInfo",false);
85  //modif-end
86 
87  inputTagRecTracks_
88  = iConfig.getParameter<InputTag>("RecTracks");
89  inputTagEcalRecHitsEB_
90  = iConfig.getParameter<InputTag>("ecalRecHitsEB");
91  inputTagEcalRecHitsEE_
92  = iConfig.getParameter<InputTag>("ecalRecHitsEE");
93 
94  verbose_ =
95  iConfig.getUntrackedParameter<bool>("verbose",false);
96 
97 
98  // register products
99  produces<reco::PFSimParticleCollection>();
100 
101  particleFilter_ = iConfig.getParameter<ParameterSet>
102  ( "ParticleFilter" );
103 
104  mySimEvent = new FSimEvent( particleFilter_ );
105 }
106 
107 
108 
110 {
111  delete mySimEvent;
112 }
113 
114 
115 void
117  const edm::EventSetup & es)
118 {
119  // init Particle data table (from Pythia)
121  // edm::ESHandle < DefaultConfig::ParticleDataTable > pdt;
122  es.getData(pdt);
123  mySimEvent->initializePdt(&(*pdt));
124 }
125 
126 
128  const EventSetup& iSetup)
129 {
130  ParticleTable::Sentry ptable(mySimEvent->theTable());
131  LogDebug("PFSimParticleProducer")<<"START event: "<<iEvent.id().event()
132  <<" in run "<<iEvent.id().run()<<endl;
133 
134  //MC Truth Matching only with Famos and UnFoldedMode option to true!!
135 
136  //vector to store the trackIDs of rectracks corresponding
137  //to the simulated particle.
138  std::vector<unsigned> recTrackSimID;
139 
140  //In order to know which simparticule contribute to
141  //a given Ecal RecHit energy, we need to access
142  //the PCAloHit from FastSim.
143 
144  typedef std::pair<double, unsigned> hitSimID;
145  typedef std::list< std::pair<double, unsigned> >::iterator ITM;
146  std::vector< std::list <hitSimID> > caloHitsEBID(62000);
147  std::vector<double> caloHitsEBTotE(62000,0.0);
148 
149  if(mctruthMatchingInfo_){
150 
151  //getting the PCAloHit
153  // bool found_phit
154  // = iEvent.getByLabel("famosSimHits","EcalHitsEB",
155  // pcalohits);
156  //modif-beg
157  bool found_phit
158  = iEvent.getByLabel(inputTagFamosSimHits_,pcalohits);
159  //modif-end
160 
161  if(!found_phit) {
162  ostringstream err;
163  err<<"could not find pcaloHit "<<"famosSimHits:EcalHitsEB";
164  LogError("PFSimParticleProducer")<<err.str()<<endl;
165 
166  throw cms::Exception( "MissingProduct", err.str());
167  }
168  else {
169  assert( pcalohits.isValid() );
170 
171  // cout << "PFSimParticleProducer: number of pcalohits="
172  // << pcalohits->size() << endl;
173 
174  edm::PCaloHitContainer::const_iterator it
175  = pcalohits.product()->begin();
176  edm::PCaloHitContainer::const_iterator itend
177  = pcalohits.product()->end();
178 
179  //loop on the PCaloHit from FastSim Calorimetry
180  for(;it!=itend;++it)
181  {
182  EBDetId detid(it->id());
183 
184  // cout << detid << " " << detid.rawId()
185  // << " " << detid.hashedIndex()
186  // << " " << it->energy()
187  // << " " << it->id()
188  // << " trackId="
189  // << it->geantTrackId()
190  // << endl;
191 
192  if(it->energy() > 0.0) {
193  std::pair<double, unsigned> phitsimid
194  = make_pair(it->energy(),it->geantTrackId());
195  caloHitsEBID[detid.hashedIndex()].push_back(phitsimid);
196  caloHitsEBTotE[detid.hashedIndex()]
197  += it->energy(); //summing pcalhit energy
198  }//energy > 0
199 
200  }//loop PcaloHits
201  }//pcalohit handle access
202 
203  //Retrieving the PFRecTrack collection for
204  //Monte Carlo Truth Matching tool
206  try{
207  LogDebug("PFSimParticleProducer")<<"getting PFRecTracks"<<endl;
208  iEvent.getByLabel(inputTagRecTracks_, recTracks);
209 
210  } catch (cms::Exception& err) {
211  LogError("PFSimParticleProducer")<<err
212  <<" cannot get collection "
213  <<"particleFlowBlock"<<":"
214  <<""
215  <<endl;
216  }//pfrectrack handle access
217 
218  //getting the simID corresponding to
219  //each of the PFRecTracks
220  getSimIDs( recTracks, recTrackSimID );
221 
222  }//mctruthMatchingInfo_ //modif
223 
224  // deal with true particles
225  if( processParticles_) {
226 
227  auto_ptr< reco::PFSimParticleCollection >
228  pOutputPFSimParticleCollection(new reco::PFSimParticleCollection );
229 
230  Handle<vector<SimTrack> > simTracks;
231  bool found = iEvent.getByLabel(inputTagSim_,simTracks);
232  if(!found) {
233 
234  ostringstream err;
235  err<<"cannot find sim tracks: "<<inputTagSim_;
236  LogError("PFSimParticleProducer")<<err.str()<<endl;
237 
238  throw cms::Exception( "MissingProduct", err.str());
239  }
240 
241 
242 
243  Handle<vector<SimVertex> > simVertices;
244  found = iEvent.getByLabel(inputTagSim_,simVertices);
245  if(!found) {
246  LogError("PFSimParticleProducer")
247  <<"cannot find sim vertices: "<<inputTagSim_<<endl;
248  return;
249  }
250 
251 
252 
253  // for(unsigned it = 0; it<simTracks->size(); it++ ) {
254  // cout<<"\t track "<< (*simTracks)[it]<<" "
255  // <<(*simTracks)[it].momentum().vect().perp()<<" "
256  // <<(*simTracks)[it].momentum().e()<<endl;
257  // }
258 
259  mySimEvent->fill( *simTracks, *simVertices );
260 
261  if(verbose_)
262  mySimEvent->print();
263 
264  // const std::vector<FSimTrack>& fsimTracks = *(mySimEvent->tracks() );
265  for(unsigned i=0; i<mySimEvent->nTracks(); i++) {
266 
267  const FSimTrack& fst = mySimEvent->track(i);
268 
269  int motherId = -1;
270  if( ! fst.noMother() ) // a mother exist
271  motherId = fst.mother().id();
272 
273  //This is finding out the simID corresponding
274  //to the recTrack
275 // cout << "Particle " << i
276 // << " " << fst.genpartIndex()
277 // << " -------------------------------------" << endl;
278 
279  //GETTING THE TRACK ID
280  unsigned recTrackID = 99999;
281  vector<unsigned> recHitContrib; //modif
282  vector<double> recHitContribFrac; //modif
283 
284  if(mctruthMatchingInfo_){ //modif
285 
286  for(unsigned lo=0; lo<recTrackSimID.size();
287  lo++) {
288  if( i == recTrackSimID[lo] ) {
289 // cout << "Corresponding Rec Track "
290 // << lo << endl;
291  recTrackID = lo;
292  }//match track
293  }//loop rectrack
294 // if( recTrackID == 99999 )
295 // cout << "Sim Track not reconstructed pT=" <<
296 // fst.momentum().pt() << endl;
297 
298  // get the ecalBarrel rechits for MC truth matching tool
300  bool found = iEvent.getByLabel(inputTagEcalRecHitsEB_,
301  rhcHandle);
302  if(!found) {
303  ostringstream err;
304  err<<"could not find rechits "<< inputTagEcalRecHitsEB_;
305  LogError("PFSimParticleProducer")<<err.str()<<endl;
306 
307  throw cms::Exception( "MissingProduct", err.str());
308  }
309  else {
310  assert( rhcHandle.isValid() );
311 // cout << "PFSimParticleProducer: number of rechits="
312 // << rhcHandle->size() << endl;
313 
315  = rhcHandle.product()->begin();
317  = rhcHandle.product()->end();
318 
319  for(;it_rh!=itend_rh;++it_rh)
320  {
321  unsigned rhit_hi
322  = EBDetId(it_rh->id()).hashedIndex();
323  EBDetId detid(it_rh->id());
324 // cout << detid << " " << detid.rawId()
325 // << " " << detid.hashedIndex()
326 // << " " << it_rh->energy() << endl;
327 
328  ITM it_phit = caloHitsEBID[rhit_hi].begin();
329  ITM itend_phit = caloHitsEBID[rhit_hi].end();
330  for(;it_phit!=itend_phit;++it_phit)
331  {
332  if(i == it_phit->second)
333  {
334  //Alex (08/10/08) TO BE REMOVED, eliminating
335  //duplicated rechits
336  bool alreadyin = false;
337  for( unsigned ihit = 0; ihit < recHitContrib.size();
338  ++ihit )
339  if(detid.rawId() == recHitContrib[ihit])
340  alreadyin = true;
341 
342  if(!alreadyin){
343  double pcalofraction = 0.0;
344  if(caloHitsEBTotE[rhit_hi] != 0.0)
345  pcalofraction
346  = (it_phit->first/caloHitsEBTotE[rhit_hi])*100.0;
347 
348  //store info
349  recHitContrib.push_back(it_rh->id());
350  recHitContribFrac.push_back(pcalofraction);
351  }//selected rechits
352  }//matching
353  }//loop pcalohit
354 
355  }//loop rechits
356 
357  }//getting the rechits
358 
359  }//mctruthMatchingInfo_ //modif
360 
361 // cout << "This particule has " << recHitContrib.size()
362 // << " rechit contribution" << endl;
363 // for( unsigned ih = 0; ih < recHitContrib.size(); ++ih )
364 // cout << recHitContrib[ih]
365 // << " f=" << recHitContribFrac[ih] << " ";
366 // cout << endl;
367 
368  reco::PFSimParticle particle( fst.charge(),
369  fst.type(),
370  fst.id(),
371  motherId,
372  fst.daughters(),
373  recTrackID,
374  recHitContrib,
375  recHitContribFrac);
376 
377 
378  const FSimVertex& originVtx = fst.vertex();
379 
380  math::XYZPoint posOrig( originVtx.position().x(),
381  originVtx.position().y(),
382  originVtx.position().z() );
383 
384  math::XYZTLorentzVector momOrig( fst.momentum().px(),
385  fst.momentum().py(),
386  fst.momentum().pz(),
387  fst.momentum().e() );
389  pointOrig(-1,
391  posOrig, momOrig);
392 
393  // point 0 is origin vertex
394  particle.addPoint(pointOrig);
395 
396 
397  if( ! fst.noEndVertex() ) {
398  const FSimVertex& endVtx = fst.endVertex();
399 
400  math::XYZPoint posEnd( endVtx.position().x(),
401  endVtx.position().y(),
402  endVtx.position().z() );
403  // cout<<"end vertex : "
404  // <<endVtx.position().x()<<" "
405  // <<endVtx.position().y()<<endl;
406 
408 
410  pointEnd( -1,
412  posEnd, momEnd);
413 
414  particle.addPoint(pointEnd);
415  }
416  else { // add a dummy point
418  particle.addPoint(dummy);
419  }
420 
421 
422  if( fst.onLayer1() ) { // PS layer1
423  const RawParticle& rp = fst.layer1Entrance();
424 
425  math::XYZPoint posLayer1( rp.x(), rp.y(), rp.z() );
426  math::XYZTLorentzVector momLayer1( rp.px(), rp.py(), rp.pz(), rp.e() );
428  posLayer1, momLayer1);
429 
430  particle.addPoint( layer1Pt );
431 
432  // extrapolate to cluster depth
433  }
434  else { // add a dummy point
436  particle.addPoint(dummy);
437  }
438 
439  if( fst.onLayer2() ) { // PS layer2
440  const RawParticle& rp = fst.layer2Entrance();
441 
442  math::XYZPoint posLayer2( rp.x(), rp.y(), rp.z() );
443  math::XYZTLorentzVector momLayer2( rp.px(), rp.py(), rp.pz(), rp.e() );
445  posLayer2, momLayer2);
446 
447  particle.addPoint( layer2Pt );
448 
449  // extrapolate to cluster depth
450  }
451  else { // add a dummy point
453  particle.addPoint(dummy);
454  }
455 
456  if( fst.onEcal() ) {
457  const RawParticle& rp = fst.ecalEntrance();
458 
459  math::XYZPoint posECAL( rp.x(), rp.y(), rp.z() );
460  math::XYZTLorentzVector momECAL( rp.px(), rp.py(), rp.pz(), rp.e() );
461  reco::PFTrajectoryPoint ecalPt(-1,
463  posECAL, momECAL);
464 
465  particle.addPoint( ecalPt );
466 
467  // extrapolate to cluster depth
468  }
469  else { // add a dummy point
471  particle.addPoint(dummy);
472  }
473 
474  // add a dummy point for ECAL Shower max
476  particle.addPoint(dummy);
477 
478  if( fst.onHcal() ) {
479 
480  const RawParticle& rpin = fst.hcalEntrance();
481 
482  math::XYZPoint posHCALin( rpin.x(), rpin.y(), rpin.z() );
483  math::XYZTLorentzVector momHCALin( rpin.px(), rpin.py(), rpin.pz(),
484  rpin.e() );
485  reco::PFTrajectoryPoint hcalPtin(-1,
487  posHCALin, momHCALin);
488 
489  particle.addPoint( hcalPtin );
490 
491  const RawParticle& rpout = fst.hcalExit();
492 
493  math::XYZPoint posHCALout( rpout.x(), rpout.y(), rpout.z() );
494  math::XYZTLorentzVector momHCALout( rpout.px(), rpout.py(), rpout.pz(),
495  rpout.e() );
497  hcalPtout(-1, reco::PFTrajectoryPoint::HCALExit,
498  posHCALout, momHCALout);
499 
500  particle.addPoint( hcalPtout );
501 
502  const RawParticle& rpho = fst.hoEntrance();
503 
504  math::XYZPoint posHOEntrance( rpho.x(), rpho.y(), rpho.z() );
505  math::XYZTLorentzVector momHOEntrance( rpho.px(), rpho.py(), rpho.pz(),
506  rpho.e() );
509  posHOEntrance, momHOEntrance);
510 
511  particle.addPoint( hoPtin );
512 
513 
514 
515 
516  }
517  else { // add a dummy point
519  particle.addPoint(dummy);
520  }
521 
522  pOutputPFSimParticleCollection->push_back( particle );
523  }
524 
525  iEvent.put(pOutputPFSimParticleCollection);
526  }
527 
528 
529  LogDebug("PFSimParticleProducer")<<"STOP event: "<<iEvent.id().event()
530  <<" in run "<<iEvent.id().run()<<endl;
531 }
532 
534  std::vector<unsigned>& recTrackSimID )
535 {
536 
537  if( trackh.isValid() ) {
538 // cout << "Size=" << trackh->size() << endl;
539 
540  for(unsigned i=0;i<trackh->size(); i++) {
541 
542  reco::PFRecTrackRef ref( trackh,i );
543  const reco::PFRecTrack& PFT = *ref;
544  const reco::TrackRef trackref = PFT.trackRef();
545 
546 // double Pt = trackref->pt();
547 // double DPt = trackref->ptError();
548 // cout << " PFBlockProducer: PFrecTrack->Track Pt= "
549 // << Pt << " DPt = " << DPt << endl;
550 
551  trackingRecHit_iterator rhitbeg
552  = trackref->recHitsBegin();
553  trackingRecHit_iterator rhitend
554  = trackref->recHitsEnd();
555  for (trackingRecHit_iterator it = rhitbeg;
556  it != rhitend; it++){
557 
558  if( (*it)->isValid() ){
559 
560  const SiTrackerGSMatchedRecHit2D * rechit
561  = (const SiTrackerGSMatchedRecHit2D*) (*it);
562 
563 // cout << "rechit"
564 // << " corresponding simId "
565 // << rechit->simtrackId()
566 // << endl;
567 
568  recTrackSimID.push_back( rechit->simtrackId() );
569  break;
570  }
571  }//loop track rechit
572  }//loop recTracks
573  }//track handle valid
574 }
PFSimParticleProducer(const edm::ParameterSet &)
#define LogDebug(id)
RunNumber_t run() const
Definition: EventID.h:39
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
reconstructed track used as an input to particle flow
Definition: PFRecTrack.h:22
int i
Definition: DBlmapReader.cc:9
bool noEndVertex() const
no end vertex
float charge() const
charge
Definition: FSimTrack.h:47
const RawParticle & hoEntrance() const
The particle at HCAL exir.
Definition: FSimTrack.h:145
int onLayer2() const
Definition: FSimTrack.h:96
const FSimVertex & endVertex() const
end vertex
const RawParticle & hcalExit() const
The particle at HCAL exir.
Definition: FSimTrack.h:142
double y() const
y of vertex
Definition: RawParticle.h:271
double z() const
z of vertex
Definition: RawParticle.h:272
virtual void beginRun(const edm::Run &r, const edm::EventSetup &c) override
const XYZTLorentzVector & momentum() const
Temporary (until move of SimTrack to Mathcore) - No! Actually very useful.
Definition: FSimTrack.h:190
assert(m_qm.get())
const RawParticle & layer1Entrance() const
The particle at Preshower Layer 1.
Definition: FSimTrack.h:127
std::vector< EcalRecHit >::const_iterator const_iterator
std::vector< PFSimParticle > PFSimParticleCollection
collection of PFSimParticle objects
const std::vector< int > & daughters() const
Vector of daughter indices.
double x() const
x of vertex
Definition: RawParticle.h:270
const reco::TrackRef & trackRef() const
Definition: PFRecTrack.h:54
void getData(T &iHolder) const
Definition: EventSetup.h:78
int onEcal() const
Definition: FSimTrack.h:101
virtual void produce(edm::Event &, const edm::EventSetup &) override
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
int hashedIndex(int ieta, int iphi)
Definition: EcalPyUtils.cc:42
int iEvent
Definition: GenABIO.cc:230
Point of closest approach from beam axis (initial point in the case of PFSimParticle) ...
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
const math::XYZTLorentzVector & position() const
Temporary (until CMSSW moves to Mathcore) - No ! Actually very useful.
Definition: FSimVertex.h:49
const RawParticle & ecalEntrance() const
The particle at ECAL entrance.
Definition: FSimTrack.h:133
const FSimVertex & vertex() const
Origin vertex.
bool isValid() const
Definition: HandleBase.h:75
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
true particle for particle flow
Definition: PFSimParticle.h:19
T const * product() const
Definition: Handle.h:81
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
edm::EventID id() const
Definition: EventBase.h:60
int type() const
particle type (HEP PDT convension)
Definition: CoreSimTrack.h:25
int id() const
the index in FBaseSimEvent and other vectors
Definition: FSimTrack.h:86
const RawParticle & layer2Entrance() const
The particle at Preshower Layer 2.
Definition: FSimTrack.h:130
int onLayer1() const
Definition: FSimTrack.h:91
int onHcal() const
Definition: FSimTrack.h:106
bool noMother() const
no mother particle
const RawParticle & hcalEntrance() const
The particle at HCAL entrance.
Definition: FSimTrack.h:136
A PFTrack holds several trajectory points, which basically contain the position and momentum of a tra...
void getSimIDs(const TrackHandle &trackh, std::vector< unsigned > &recTrackSimID)
const FSimTrack & mother() const
mother
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
Definition: Run.h:41