CMS 3D CMS Logo

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