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 
48 
58 
59 #include <set>
60 #include <sstream>
61 
62 using namespace std;
63 using namespace edm;
64 
66 {
67 
68 
69  processParticles_ =
70  iConfig.getUntrackedParameter<bool>("process_Particles",true);
71 
72 
73  inputTagSim_ = iConfig.getParameter<InputTag>("sim");
74  tokenSim_ = consumes<std::vector<SimTrack> >(inputTagSim_);
75  tokenSimVertices_ = consumes<std::vector<SimVertex> >(inputTagSim_);
76 
77  //retrieving collections for MC Truth Matching
78 
79  //modif-beg
80  inputTagFastSimProducer_ = iConfig.getUntrackedParameter<InputTag>("fastSimProducer");
81  tokenFastSimProducer_ = consumes<edm::PCaloHitContainer>(inputTagFastSimProducer_);
82  mctruthMatchingInfo_ =
83  iConfig.getUntrackedParameter<bool>("MCTruthMatchingInfo",false);
84  //modif-end
85 
86  inputTagRecTracks_ = iConfig.getParameter<InputTag>("RecTracks");
87  tokenRecTracks_ = consumes<reco::PFRecTrackCollection>(inputTagRecTracks_);
88 
89  inputTagEcalRecHitsEB_ = iConfig.getParameter<InputTag>("ecalRecHitsEB");
90  tokenEcalRecHitsEB_ = consumes<EcalRecHitCollection>(inputTagEcalRecHitsEB_);
91  inputTagEcalRecHitsEE_ = iConfig.getParameter<InputTag>("ecalRecHitsEE");
92  tokenEcalRecHitsEE_
93  = consumes<EcalRecHitCollection>(inputTagEcalRecHitsEE_);
94 
95  verbose_ =
96  iConfig.getUntrackedParameter<bool>("verbose",false);
97 
98 
99  // register products
100  produces<reco::PFSimParticleCollection>();
101 
102  particleFilter_ = iConfig.getParameter<ParameterSet>
103  ( "ParticleFilter" );
104 
105  mySimEvent = new FSimEvent( particleFilter_ );
106 }
107 
108 
109 
111 {
112  delete mySimEvent;
113 }
114 
116  const EventSetup& iSetup)
117 {
118  // init Particle data table (from Pythia)
120  // edm::ESHandle < DefaultConfig::ParticleDataTable > pdt;
121  iSetup.getData(pdt);
122  mySimEvent->initializePdt(&(*pdt));
123 
124  LogDebug("PFSimParticleProducer")<<"START event: "<<iEvent.id().event()
125  <<" in run "<<iEvent.id().run()<<endl;
126 
127  //MC Truth Matching only with Famos and UnFoldedMode option to true!!
128 
129  //vector to store the trackIDs of rectracks corresponding
130  //to the simulated particle.
131  std::vector<unsigned> recTrackSimID;
132 
133  //In order to know which simparticule contribute to
134  //a given Ecal RecHit energy, we need to access
135  //the PCAloHit from FastSim.
136 
137  typedef std::pair<double, unsigned> hitSimID;
138  typedef std::list< std::pair<double, unsigned> >::iterator ITM;
139  std::vector< std::list <hitSimID> > caloHitsEBID(62000);
140  std::vector<double> caloHitsEBTotE(62000,0.0);
141 
142  if(mctruthMatchingInfo_){
143 
144  //getting the PCAloHit
146  // bool found_phit
147  // = iEvent.getByLabel("fastSimProducer","EcalHitsEB",
148  // pcalohits);
149  //modif-beg
150  bool found_phit
151  = iEvent.getByToken(tokenFastSimProducer_,pcalohits);
152  //modif-end
153 
154  if(!found_phit) {
155  ostringstream err;
156  err<<"could not find pcaloHit "<<"fastSimProducer:EcalHitsEB";
157  LogError("PFSimParticleProducer")<<err.str()<<endl;
158 
159  throw cms::Exception( "MissingProduct", err.str());
160  }
161  else {
162  assert( pcalohits.isValid() );
163 
164  edm::PCaloHitContainer::const_iterator it
165  = pcalohits.product()->begin();
166  edm::PCaloHitContainer::const_iterator itend
167  = pcalohits.product()->end();
168 
169  //loop on the PCaloHit from FastSim Calorimetry
170  for(;it!=itend;++it)
171  {
172  EBDetId detid(it->id());
173 
174  if(it->energy() > 0.0) {
175  std::pair<double, unsigned> phitsimid
176  = make_pair(it->energy(),it->geantTrackId());
177  caloHitsEBID[detid.hashedIndex()].push_back(phitsimid);
178  caloHitsEBTotE[detid.hashedIndex()]
179  += it->energy(); //summing pcalhit energy
180  }//energy > 0
181 
182  }//loop PcaloHits
183  }//pcalohit handle access
184 
185  //Retrieving the PFRecTrack collection for
186  //Monte Carlo Truth Matching tool
188  try{
189  LogDebug("PFSimParticleProducer")<<"getting PFRecTracks"<<endl;
190  iEvent.getByToken(tokenRecTracks_, recTracks);
191 
192  } catch (cms::Exception& err) {
193  LogError("PFSimParticleProducer")<<err
194  <<" cannot get collection "
195  <<"particleFlowBlock"<<":"
196  <<""
197  <<endl;
198  }//pfrectrack handle access
199 
200  //getting the simID corresponding to
201  //each of the PFRecTracks
202  getSimIDs( recTracks, recTrackSimID );
203 
204  }//mctruthMatchingInfo_ //modif
205 
206  // deal with true particles
207  if( processParticles_) {
208 
209  auto pOutputPFSimParticleCollection = std::make_unique<reco::PFSimParticleCollection>();
210 
212  bool found = iEvent.getByToken(tokenSim_,simTracks);
213  if(!found) {
214 
215  ostringstream err;
216  err<<"cannot find sim tracks: "<<inputTagSim_;
217  LogError("PFSimParticleProducer")<<err.str()<<endl;
218 
219  throw cms::Exception( "MissingProduct", err.str());
220  }
221 
222 
223 
225  found = iEvent.getByToken(tokenSimVertices_,simVertices);
226  if(!found) {
227  LogError("PFSimParticleProducer")
228  <<"cannot find sim vertices: "<<inputTagSim_<<endl;
229  return;
230  }
231 
232  mySimEvent->fill( *simTracks, *simVertices );
233 
234  if(verbose_)
235  mySimEvent->print();
236 
237  for(unsigned i=0; i<mySimEvent->nTracks(); i++) {
238 
239  const FSimTrack& fst = mySimEvent->track(i);
240 
241  int motherId = -1;
242  if( ! fst.noMother() ) // a mother exist
243  motherId = fst.mother().id();
244 
245  //This is finding out the simID corresponding
246  //to the recTrack
247 
248  //GETTING THE TRACK ID
249  unsigned recTrackID = 99999;
250  vector<unsigned> recHitContrib; //modif
251  vector<double> recHitContribFrac; //modif
252 
253  if(mctruthMatchingInfo_){ //modif
254 
255  for(unsigned lo=0; lo<recTrackSimID.size();
256  lo++) {
257  if( i == recTrackSimID[lo] ) {
258 
259  recTrackID = lo;
260  }//match track
261  }//loop rectrack
262 
263  // get the ecalBarrel rechits for MC truth matching tool
265  bool found = iEvent.getByToken(tokenEcalRecHitsEB_,
266  rhcHandle);
267  if(!found) {
268  ostringstream err;
269  err<<"could not find rechits "<< inputTagEcalRecHitsEB_;
270  LogError("PFSimParticleProducer")<<err.str()<<endl;
271 
272  throw cms::Exception( "MissingProduct", err.str());
273  }
274  else {
275  assert( rhcHandle.isValid() );
276 
278  = rhcHandle.product()->begin();
280  = rhcHandle.product()->end();
281 
282  for(;it_rh!=itend_rh;++it_rh)
283  {
284  unsigned rhit_hi
285  = EBDetId(it_rh->id()).hashedIndex();
286  EBDetId detid(it_rh->id());
287 
288  ITM it_phit = caloHitsEBID[rhit_hi].begin();
289  ITM itend_phit = caloHitsEBID[rhit_hi].end();
290  for(;it_phit!=itend_phit;++it_phit)
291  {
292  if(i == it_phit->second)
293  {
294  //Alex (08/10/08) TO BE REMOVED, eliminating
295  //duplicated rechits
296  bool alreadyin = false;
297  for( unsigned ihit = 0; ihit < recHitContrib.size();
298  ++ihit )
299  if(detid.rawId() == recHitContrib[ihit])
300  alreadyin = true;
301 
302  if(!alreadyin){
303  double pcalofraction = 0.0;
304  if(caloHitsEBTotE[rhit_hi] != 0.0)
305  pcalofraction
306  = (it_phit->first/caloHitsEBTotE[rhit_hi])*100.0;
307 
308  //store info
309  recHitContrib.push_back(it_rh->id());
310  recHitContribFrac.push_back(pcalofraction);
311  }//selected rechits
312  }//matching
313  }//loop pcalohit
314 
315  }//loop rechits
316 
317  }//getting the rechits
318 
319  }//mctruthMatchingInfo_ //modif
320 
321  reco::PFSimParticle particle( fst.charge(),
322  fst.type(),
323  fst.id(),
324  motherId,
325  fst.daughters(),
326  recTrackID,
327  recHitContrib,
328  recHitContribFrac);
329 
330 
331  const FSimVertex& originVtx = fst.vertex();
332 
333  math::XYZPoint posOrig( originVtx.position().x(),
334  originVtx.position().y(),
335  originVtx.position().z() );
336 
337  math::XYZTLorentzVector momOrig( fst.momentum().px(),
338  fst.momentum().py(),
339  fst.momentum().pz(),
340  fst.momentum().e() );
342  pointOrig(-1,
344  posOrig, momOrig);
345 
346  // point 0 is origin vertex
347  particle.addPoint(pointOrig);
348 
349 
350  if( ! fst.noEndVertex() ) {
351  const FSimVertex& endVtx = fst.endVertex();
352 
353  math::XYZPoint posEnd( endVtx.position().x(),
354  endVtx.position().y(),
355  endVtx.position().z() );
356 
358 
360  pointEnd( -1,
362  posEnd, momEnd);
363 
364  particle.addPoint(pointEnd);
365  }
366  else { // add a dummy point
368  particle.addPoint(dummy);
369  }
370 
371 
372  if( fst.onLayer1() ) { // PS layer1
373  const RawParticle& rp = fst.layer1Entrance();
374 
375  math::XYZPoint posLayer1( rp.x(), rp.y(), rp.z() );
376  math::XYZTLorentzVector momLayer1( rp.px(), rp.py(), rp.pz(), rp.e() );
378  posLayer1, momLayer1);
379 
380  particle.addPoint( layer1Pt );
381 
382  // extrapolate to cluster depth
383  }
384  else { // add a dummy point
386  particle.addPoint(dummy);
387  }
388 
389  if( fst.onLayer2() ) { // PS layer2
390  const RawParticle& rp = fst.layer2Entrance();
391 
392  math::XYZPoint posLayer2( rp.x(), rp.y(), rp.z() );
393  math::XYZTLorentzVector momLayer2( rp.px(), rp.py(), rp.pz(), rp.e() );
395  posLayer2, momLayer2);
396 
397  particle.addPoint( layer2Pt );
398 
399  // extrapolate to cluster depth
400  }
401  else { // add a dummy point
403  particle.addPoint(dummy);
404  }
405 
406  if( fst.onEcal() ) {
407  const RawParticle& rp = fst.ecalEntrance();
408 
409  math::XYZPoint posECAL( rp.x(), rp.y(), rp.z() );
410  math::XYZTLorentzVector momECAL( rp.px(), rp.py(), rp.pz(), rp.e() );
411  reco::PFTrajectoryPoint ecalPt(-1,
413  posECAL, momECAL);
414 
415  particle.addPoint( ecalPt );
416 
417  // extrapolate to cluster depth
418  }
419  else { // add a dummy point
421  particle.addPoint(dummy);
422  }
423 
424  // add a dummy point for ECAL Shower max
426  particle.addPoint(dummy);
427 
428  if( fst.onHcal() ) {
429 
430  const RawParticle& rpin = fst.hcalEntrance();
431 
432  math::XYZPoint posHCALin( rpin.x(), rpin.y(), rpin.z() );
433  math::XYZTLorentzVector momHCALin( rpin.px(), rpin.py(), rpin.pz(),
434  rpin.e() );
435  reco::PFTrajectoryPoint hcalPtin(-1,
437  posHCALin, momHCALin);
438 
439  particle.addPoint( hcalPtin );
440 
441  const RawParticle& rpout = fst.hcalExit();
442 
443  math::XYZPoint posHCALout( rpout.x(), rpout.y(), rpout.z() );
444  math::XYZTLorentzVector momHCALout( rpout.px(), rpout.py(), rpout.pz(),
445  rpout.e() );
447  hcalPtout(-1, reco::PFTrajectoryPoint::HCALExit,
448  posHCALout, momHCALout);
449 
450  particle.addPoint( hcalPtout );
451 
452  const RawParticle& rpho = fst.hoEntrance();
453 
454  math::XYZPoint posHOEntrance( rpho.x(), rpho.y(), rpho.z() );
455  math::XYZTLorentzVector momHOEntrance( rpho.px(), rpho.py(), rpho.pz(),
456  rpho.e() );
459  posHOEntrance, momHOEntrance);
460 
461  particle.addPoint( hoPtin );
462 
463 
464 
465 
466  }
467  else { // add a dummy point
469  particle.addPoint(dummy);
470  }
471 
472  pOutputPFSimParticleCollection->push_back( particle );
473  }
474 
475  iEvent.put(std::move(pOutputPFSimParticleCollection));
476  }
477 
478 
479  LogDebug("PFSimParticleProducer")<<"STOP event: "<<iEvent.id().event()
480  <<" in run "<<iEvent.id().run()<<endl;
481 }
482 
484  std::vector<unsigned>& recTrackSimID )
485 {
486 
487  if( trackh.isValid() ) {
488 
489  for(unsigned i=0;i<trackh->size(); i++) {
490 
491  const reco::PFRecTrackRef ref( trackh,i );
492 
493  for(auto const& hit : ref->trackRef()->recHits())
494  {
495  if( hit->isValid() ) {
496 
497  auto rechit = dynamic_cast<const FastTrackerRecHit*>(hit);
498 
499  for(unsigned int st_index=0;st_index<rechit->nSimTrackIds();++st_index){
500  recTrackSimID.push_back(rechit->simTrackId(st_index));
501  }
502  break;
503  }
504  }//loop track rechit
505  }//loop recTracks
506  }//track handle valid
507 }
PFSimParticleProducer(const edm::ParameterSet &)
#define LogDebug(id)
RunNumber_t run() const
Definition: EventID.h:39
double pz() const
z of the momentum
Definition: RawParticle.h:321
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
bool noEndVertex() const
no end vertex
float charge() const
charge
Definition: FSimTrack.h:51
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
const RawParticle & hoEntrance() const
The particle at HCAL exir.
Definition: FSimTrack.h:150
int onLayer2() const
Definition: FSimTrack.h:101
const FSimVertex & endVertex() const
end vertex
const RawParticle & hcalExit() const
The particle at HCAL exir.
Definition: FSimTrack.h:147
double y() const
y of vertex
Definition: RawParticle.h:302
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
double z() const
z of vertex
Definition: RawParticle.h:303
const XYZTLorentzVector & momentum() const
Temporary (until move of SimTrack to Mathcore) - No! Actually very useful.
Definition: FSimTrack.h:204
const RawParticle & layer1Entrance() const
The particle at Preshower Layer 1.
Definition: FSimTrack.h:132
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:301
int onEcal() const
Definition: FSimTrack.h:106
void produce(edm::Event &, const edm::EventSetup &) override
bool getData(T &iHolder) const
Definition: EventSetup.h:111
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:224
double e() const
energy of the momentum
Definition: RawParticle.h:324
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
const RawParticle & ecalEntrance() const
The particle at ECAL entrance.
Definition: FSimTrack.h:138
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:74
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
edm::EventID id() const
Definition: EventBase.h:59
int type() const
particle type (HEP PDT convension)
Definition: CoreSimTrack.h:22
HLT enums.
int id() const
the index in FBaseSimEvent and other vectors
Definition: FSimTrack.h:91
const RawParticle & layer2Entrance() const
The particle at Preshower Layer 2.
Definition: FSimTrack.h:135
int onLayer1() const
Definition: FSimTrack.h:96
int onHcal() const
Definition: FSimTrack.h:111
bool noMother() const
no mother particle
const RawParticle & hcalEntrance() const
The particle at HCAL entrance.
Definition: FSimTrack.h:141
double px() const
x of the momentum
Definition: RawParticle.h:315
A PFTrack holds several trajectory points, which basically contain the position and momentum of a tra...
const FSimVertex vertex() const
Origin vertex.
double py() const
y of the momentum
Definition: RawParticle.h:318
void getSimIDs(const TrackHandle &trackh, std::vector< unsigned > &recTrackSimID)
const FSimTrack & mother() const
mother
def move(src, dest)
Definition: eostools.py:511
const_iterator begin() const