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 
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_ptr< reco::PFSimParticleCollection >
223  pOutputPFSimParticleCollection(new reco::PFSimParticleCollection );
224 
225  Handle<vector<SimTrack> > simTracks;
226  bool found = iEvent.getByToken(tokenSim_,simTracks);
227  if(!found) {
228 
229  ostringstream err;
230  err<<"cannot find sim tracks: "<<inputTagSim_;
231  LogError("PFSimParticleProducer")<<err.str()<<endl;
232 
233  throw cms::Exception( "MissingProduct", err.str());
234  }
235 
236 
237 
238  Handle<vector<SimVertex> > simVertices;
239  found = iEvent.getByToken(tokenSimVertices_,simVertices);
240  if(!found) {
241  LogError("PFSimParticleProducer")
242  <<"cannot find sim vertices: "<<inputTagSim_<<endl;
243  return;
244  }
245 
246 
247 
248  // for(unsigned it = 0; it<simTracks->size(); it++ ) {
249  // cout<<"\t track "<< (*simTracks)[it]<<" "
250  // <<(*simTracks)[it].momentum().vect().perp()<<" "
251  // <<(*simTracks)[it].momentum().e()<<endl;
252  // }
253 
254  mySimEvent->fill( *simTracks, *simVertices );
255 
256  if(verbose_)
257  mySimEvent->print();
258 
259  // const std::vector<FSimTrack>& fsimTracks = *(mySimEvent->tracks() );
260  for(unsigned i=0; i<mySimEvent->nTracks(); i++) {
261 
262  const FSimTrack& fst = mySimEvent->track(i);
263 
264  int motherId = -1;
265  if( ! fst.noMother() ) // a mother exist
266  motherId = fst.mother().id();
267 
268  //This is finding out the simID corresponding
269  //to the recTrack
270 // cout << "Particle " << i
271 // << " " << fst.genpartIndex()
272 // << " -------------------------------------" << endl;
273 
274  //GETTING THE TRACK ID
275  unsigned recTrackID = 99999;
276  vector<unsigned> recHitContrib; //modif
277  vector<double> recHitContribFrac; //modif
278 
279  if(mctruthMatchingInfo_){ //modif
280 
281  for(unsigned lo=0; lo<recTrackSimID.size();
282  lo++) {
283  if( i == recTrackSimID[lo] ) {
284 // cout << "Corresponding Rec Track "
285 // << lo << endl;
286  recTrackID = lo;
287  }//match track
288  }//loop rectrack
289 // if( recTrackID == 99999 )
290 // cout << "Sim Track not reconstructed pT=" <<
291 // fst.momentum().pt() << endl;
292 
293  // get the ecalBarrel rechits for MC truth matching tool
295  bool found = iEvent.getByToken(tokenEcalRecHitsEB_,
296  rhcHandle);
297  if(!found) {
298  ostringstream err;
299  err<<"could not find rechits "<< inputTagEcalRecHitsEB_;
300  LogError("PFSimParticleProducer")<<err.str()<<endl;
301 
302  throw cms::Exception( "MissingProduct", err.str());
303  }
304  else {
305  assert( rhcHandle.isValid() );
306 // cout << "PFSimParticleProducer: number of rechits="
307 // << rhcHandle->size() << endl;
308 
310  = rhcHandle.product()->begin();
312  = rhcHandle.product()->end();
313 
314  for(;it_rh!=itend_rh;++it_rh)
315  {
316  unsigned rhit_hi
317  = EBDetId(it_rh->id()).hashedIndex();
318  EBDetId detid(it_rh->id());
319 // cout << detid << " " << detid.rawId()
320 // << " " << detid.hashedIndex()
321 // << " " << it_rh->energy() << endl;
322 
323  ITM it_phit = caloHitsEBID[rhit_hi].begin();
324  ITM itend_phit = caloHitsEBID[rhit_hi].end();
325  for(;it_phit!=itend_phit;++it_phit)
326  {
327  if(i == it_phit->second)
328  {
329  //Alex (08/10/08) TO BE REMOVED, eliminating
330  //duplicated rechits
331  bool alreadyin = false;
332  for( unsigned ihit = 0; ihit < recHitContrib.size();
333  ++ihit )
334  if(detid.rawId() == recHitContrib[ihit])
335  alreadyin = true;
336 
337  if(!alreadyin){
338  double pcalofraction = 0.0;
339  if(caloHitsEBTotE[rhit_hi] != 0.0)
340  pcalofraction
341  = (it_phit->first/caloHitsEBTotE[rhit_hi])*100.0;
342 
343  //store info
344  recHitContrib.push_back(it_rh->id());
345  recHitContribFrac.push_back(pcalofraction);
346  }//selected rechits
347  }//matching
348  }//loop pcalohit
349 
350  }//loop rechits
351 
352  }//getting the rechits
353 
354  }//mctruthMatchingInfo_ //modif
355 
356 // cout << "This particule has " << recHitContrib.size()
357 // << " rechit contribution" << endl;
358 // for( unsigned ih = 0; ih < recHitContrib.size(); ++ih )
359 // cout << recHitContrib[ih]
360 // << " f=" << recHitContribFrac[ih] << " ";
361 // cout << endl;
362 
363  reco::PFSimParticle particle( fst.charge(),
364  fst.type(),
365  fst.id(),
366  motherId,
367  fst.daughters(),
368  recTrackID,
369  recHitContrib,
370  recHitContribFrac);
371 
372 
373  const FSimVertex& originVtx = fst.vertex();
374 
375  math::XYZPoint posOrig( originVtx.position().x(),
376  originVtx.position().y(),
377  originVtx.position().z() );
378 
379  math::XYZTLorentzVector momOrig( fst.momentum().px(),
380  fst.momentum().py(),
381  fst.momentum().pz(),
382  fst.momentum().e() );
384  pointOrig(-1,
386  posOrig, momOrig);
387 
388  // point 0 is origin vertex
389  particle.addPoint(pointOrig);
390 
391 
392  if( ! fst.noEndVertex() ) {
393  const FSimVertex& endVtx = fst.endVertex();
394 
395  math::XYZPoint posEnd( endVtx.position().x(),
396  endVtx.position().y(),
397  endVtx.position().z() );
398  // cout<<"end vertex : "
399  // <<endVtx.position().x()<<" "
400  // <<endVtx.position().y()<<endl;
401 
403 
405  pointEnd( -1,
407  posEnd, momEnd);
408 
409  particle.addPoint(pointEnd);
410  }
411  else { // add a dummy point
413  particle.addPoint(dummy);
414  }
415 
416 
417  if( fst.onLayer1() ) { // PS layer1
418  const RawParticle& rp = fst.layer1Entrance();
419 
420  math::XYZPoint posLayer1( rp.x(), rp.y(), rp.z() );
421  math::XYZTLorentzVector momLayer1( rp.px(), rp.py(), rp.pz(), rp.e() );
423  posLayer1, momLayer1);
424 
425  particle.addPoint( layer1Pt );
426 
427  // extrapolate to cluster depth
428  }
429  else { // add a dummy point
431  particle.addPoint(dummy);
432  }
433 
434  if( fst.onLayer2() ) { // PS layer2
435  const RawParticle& rp = fst.layer2Entrance();
436 
437  math::XYZPoint posLayer2( rp.x(), rp.y(), rp.z() );
438  math::XYZTLorentzVector momLayer2( rp.px(), rp.py(), rp.pz(), rp.e() );
440  posLayer2, momLayer2);
441 
442  particle.addPoint( layer2Pt );
443 
444  // extrapolate to cluster depth
445  }
446  else { // add a dummy point
448  particle.addPoint(dummy);
449  }
450 
451  if( fst.onEcal() ) {
452  const RawParticle& rp = fst.ecalEntrance();
453 
454  math::XYZPoint posECAL( rp.x(), rp.y(), rp.z() );
455  math::XYZTLorentzVector momECAL( rp.px(), rp.py(), rp.pz(), rp.e() );
456  reco::PFTrajectoryPoint ecalPt(-1,
458  posECAL, momECAL);
459 
460  particle.addPoint( ecalPt );
461 
462  // extrapolate to cluster depth
463  }
464  else { // add a dummy point
466  particle.addPoint(dummy);
467  }
468 
469  // add a dummy point for ECAL Shower max
471  particle.addPoint(dummy);
472 
473  if( fst.onHcal() ) {
474 
475  const RawParticle& rpin = fst.hcalEntrance();
476 
477  math::XYZPoint posHCALin( rpin.x(), rpin.y(), rpin.z() );
478  math::XYZTLorentzVector momHCALin( rpin.px(), rpin.py(), rpin.pz(),
479  rpin.e() );
480  reco::PFTrajectoryPoint hcalPtin(-1,
482  posHCALin, momHCALin);
483 
484  particle.addPoint( hcalPtin );
485 
486  const RawParticle& rpout = fst.hcalExit();
487 
488  math::XYZPoint posHCALout( rpout.x(), rpout.y(), rpout.z() );
489  math::XYZTLorentzVector momHCALout( rpout.px(), rpout.py(), rpout.pz(),
490  rpout.e() );
492  hcalPtout(-1, reco::PFTrajectoryPoint::HCALExit,
493  posHCALout, momHCALout);
494 
495  particle.addPoint( hcalPtout );
496 
497  const RawParticle& rpho = fst.hoEntrance();
498 
499  math::XYZPoint posHOEntrance( rpho.x(), rpho.y(), rpho.z() );
500  math::XYZTLorentzVector momHOEntrance( rpho.px(), rpho.py(), rpho.pz(),
501  rpho.e() );
504  posHOEntrance, momHOEntrance);
505 
506  particle.addPoint( hoPtin );
507 
508 
509 
510 
511  }
512  else { // add a dummy point
514  particle.addPoint(dummy);
515  }
516 
517  pOutputPFSimParticleCollection->push_back( particle );
518  }
519 
520  iEvent.put(pOutputPFSimParticleCollection);
521  }
522 
523 
524  LogDebug("PFSimParticleProducer")<<"STOP event: "<<iEvent.id().event()
525  <<" in run "<<iEvent.id().run()<<endl;
526 }
527 
529  std::vector<unsigned>& recTrackSimID )
530 {
531 
532  if( trackh.isValid() ) {
533 // cout << "Size=" << trackh->size() << endl;
534 
535  for(unsigned i=0;i<trackh->size(); i++) {
536 
537  reco::PFRecTrackRef ref( trackh,i );
538  const reco::PFRecTrack& PFT = *ref;
539  const reco::TrackRef trackref = PFT.trackRef();
540 
541 // double Pt = trackref->pt();
542 // double DPt = trackref->ptError();
543 // cout << " PFBlockProducer: PFrecTrack->Track Pt= "
544 // << Pt << " DPt = " << DPt << endl;
545 
546  trackingRecHit_iterator rhitbeg
547  = trackref->recHitsBegin();
548  trackingRecHit_iterator rhitend
549  = trackref->recHitsEnd();
550  for (trackingRecHit_iterator it = rhitbeg;
551  it != rhitend; it++){
552 
553  if( (*it)->isValid() ){
554 
555  const FastTrackerRecHit * rechit
556  = (const FastTrackerRecHit*) (*it);
557 
558 // cout << "rechit"
559 // << " corresponding simId "
560 // << rechit->simtrackId()
561 // << endl;
562 
563  for(unsigned int st_index=0;st_index<rechit->nSimTrackIds();++st_index){
564  recTrackSimID.push_back(rechit->simTrackId(st_index));
565  }
566  break;
567  }
568  }//loop track rechit
569  }//loop recTracks
570  }//track handle valid
571 }
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
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
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
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:79
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:120
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:75
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
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
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