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 
120  // init Particle data table (from Pythia)
122  // edm::ESHandle < DefaultConfig::ParticleDataTable > pdt;
123  es.getData(pdt);
125  mySimEvent->initializePdt(&(*pdt));
126 }
127 
128 
130  const EventSetup& iSetup)
131 {
132 
133  LogDebug("PFSimParticleProducer")<<"START event: "<<iEvent.id().event()
134  <<" in run "<<iEvent.id().run()<<endl;
135 
136  //MC Truth Matching only with Famos and UnFoldedMode option to true!!
137 
138  //vector to store the trackIDs of rectracks corresponding
139  //to the simulated particle.
140  std::vector<unsigned> recTrackSimID;
141 
142  //In order to know which simparticule contribute to
143  //a given Ecal RecHit energy, we need to access
144  //the PCAloHit from FastSim.
145 
146  typedef std::pair<double, unsigned> hitSimID;
147  typedef std::list< std::pair<double, unsigned> >::iterator ITM;
148  std::vector< std::list <hitSimID> > caloHitsEBID(62000);
149  std::vector<double> caloHitsEBTotE(62000,0.0);
150 
151  if(mctruthMatchingInfo_){
152 
153  //getting the PCAloHit
155  // bool found_phit
156  // = iEvent.getByLabel("famosSimHits","EcalHitsEB",
157  // pcalohits);
158  //modif-beg
159  bool found_phit
160  = iEvent.getByLabel(inputTagFamosSimHits_,pcalohits);
161  //modif-end
162 
163  if(!found_phit) {
164  ostringstream err;
165  err<<"could not find pcaloHit "<<"famosSimHits:EcalHitsEB";
166  LogError("PFSimParticleProducer")<<err.str()<<endl;
167 
168  throw cms::Exception( "MissingProduct", err.str());
169  }
170  else {
171  assert( pcalohits.isValid() );
172 
173  // cout << "PFSimParticleProducer: number of pcalohits="
174  // << pcalohits->size() << endl;
175 
176  edm::PCaloHitContainer::const_iterator it
177  = pcalohits.product()->begin();
178  edm::PCaloHitContainer::const_iterator itend
179  = pcalohits.product()->end();
180 
181  //loop on the PCaloHit from FastSim Calorimetry
182  for(;it!=itend;++it)
183  {
184  EBDetId detid(it->id());
185 
186  // cout << detid << " " << detid.rawId()
187  // << " " << detid.hashedIndex()
188  // << " " << it->energy()
189  // << " " << it->id()
190  // << " trackId="
191  // << it->geantTrackId()
192  // << endl;
193 
194  if(it->energy() > 0.0) {
195  std::pair<double, unsigned> phitsimid
196  = make_pair(it->energy(),it->geantTrackId());
197  caloHitsEBID[detid.hashedIndex()].push_back(phitsimid);
198  caloHitsEBTotE[detid.hashedIndex()]
199  += it->energy(); //summing pcalhit energy
200  }//energy > 0
201 
202  }//loop PcaloHits
203  }//pcalohit handle access
204 
205  //Retrieving the PFRecTrack collection for
206  //Monte Carlo Truth Matching tool
208  try{
209  LogDebug("PFSimParticleProducer")<<"getting PFRecTracks"<<endl;
210  iEvent.getByLabel(inputTagRecTracks_, recTracks);
211 
212  } catch (cms::Exception& err) {
213  LogError("PFSimParticleProducer")<<err
214  <<" cannot get collection "
215  <<"particleFlowBlock"<<":"
216  <<""
217  <<endl;
218  }//pfrectrack handle access
219 
220  //getting the simID corresponding to
221  //each of the PFRecTracks
222  getSimIDs( recTracks, recTrackSimID );
223 
224  }//mctruthMatchingInfo_ //modif
225 
226  // deal with true particles
227  if( processParticles_) {
228 
229  auto_ptr< reco::PFSimParticleCollection >
230  pOutputPFSimParticleCollection(new reco::PFSimParticleCollection );
231 
232  Handle<vector<SimTrack> > simTracks;
233  bool found = iEvent.getByLabel(inputTagSim_,simTracks);
234  if(!found) {
235 
236  ostringstream err;
237  err<<"cannot find sim tracks: "<<inputTagSim_;
238  LogError("PFSimParticleProducer")<<err.str()<<endl;
239 
240  throw cms::Exception( "MissingProduct", err.str());
241  }
242 
243 
244 
245  Handle<vector<SimVertex> > simVertices;
246  found = iEvent.getByLabel(inputTagSim_,simVertices);
247  if(!found) {
248  LogError("PFSimParticleProducer")
249  <<"cannot find sim vertices: "<<inputTagSim_<<endl;
250  return;
251  }
252 
253 
254 
255  // for(unsigned it = 0; it<simTracks->size(); it++ ) {
256  // cout<<"\t track "<< (*simTracks)[it]<<" "
257  // <<(*simTracks)[it].momentum().vect().perp()<<" "
258  // <<(*simTracks)[it].momentum().e()<<endl;
259  // }
260 
261  mySimEvent->fill( *simTracks, *simVertices );
262 
263  if(verbose_)
264  mySimEvent->print();
265 
266  // const std::vector<FSimTrack>& fsimTracks = *(mySimEvent->tracks() );
267  for(unsigned i=0; i<mySimEvent->nTracks(); i++) {
268 
269  const FSimTrack& fst = mySimEvent->track(i);
270 
271  int motherId = -1;
272  if( ! fst.noMother() ) // a mother exist
273  motherId = fst.mother().id();
274 
275  //This is finding out the simID corresponding
276  //to the recTrack
277 // cout << "Particle " << i
278 // << " " << fst.genpartIndex()
279 // << " -------------------------------------" << endl;
280 
281  //GETTING THE TRACK ID
282  unsigned recTrackID = 99999;
283  vector<unsigned> recHitContrib; //modif
284  vector<double> recHitContribFrac; //modif
285 
286  if(mctruthMatchingInfo_){ //modif
287 
288  for(unsigned lo=0; lo<recTrackSimID.size();
289  lo++) {
290  if( i == recTrackSimID[lo] ) {
291 // cout << "Corresponding Rec Track "
292 // << lo << endl;
293  recTrackID = lo;
294  }//match track
295  }//loop rectrack
296 // if( recTrackID == 99999 )
297 // cout << "Sim Track not reconstructed pT=" <<
298 // fst.momentum().pt() << endl;
299 
300  // get the ecalBarrel rechits for MC truth matching tool
302  bool found = iEvent.getByLabel(inputTagEcalRecHitsEB_,
303  rhcHandle);
304  if(!found) {
305  ostringstream err;
306  err<<"could not find rechits "<< inputTagEcalRecHitsEB_;
307  LogError("PFSimParticleProducer")<<err.str()<<endl;
308 
309  throw cms::Exception( "MissingProduct", err.str());
310  }
311  else {
312  assert( rhcHandle.isValid() );
313 // cout << "PFSimParticleProducer: number of rechits="
314 // << rhcHandle->size() << endl;
315 
317  = rhcHandle.product()->begin();
319  = rhcHandle.product()->end();
320 
321  for(;it_rh!=itend_rh;++it_rh)
322  {
323  unsigned rhit_hi
324  = EBDetId(it_rh->id()).hashedIndex();
325  EBDetId detid(it_rh->id());
326 // cout << detid << " " << detid.rawId()
327 // << " " << detid.hashedIndex()
328 // << " " << it_rh->energy() << endl;
329 
330  ITM it_phit = caloHitsEBID[rhit_hi].begin();
331  ITM itend_phit = caloHitsEBID[rhit_hi].end();
332  for(;it_phit!=itend_phit;++it_phit)
333  {
334  if(i == it_phit->second)
335  {
336  //Alex (08/10/08) TO BE REMOVED, eliminating
337  //duplicated rechits
338  bool alreadyin = false;
339  for( unsigned ihit = 0; ihit < recHitContrib.size();
340  ++ihit )
341  if(detid.rawId() == recHitContrib[ihit])
342  alreadyin = true;
343 
344  if(!alreadyin){
345  double pcalofraction = 0.0;
346  if(caloHitsEBTotE[rhit_hi] != 0.0)
347  pcalofraction
348  = (it_phit->first/caloHitsEBTotE[rhit_hi])*100.0;
349 
350  //store info
351  recHitContrib.push_back(it_rh->id());
352  recHitContribFrac.push_back(pcalofraction);
353  }//selected rechits
354  }//matching
355  }//loop pcalohit
356 
357  }//loop rechits
358 
359  }//getting the rechits
360 
361  }//mctruthMatchingInfo_ //modif
362 
363 // cout << "This particule has " << recHitContrib.size()
364 // << " rechit contribution" << endl;
365 // for( unsigned ih = 0; ih < recHitContrib.size(); ++ih )
366 // cout << recHitContrib[ih]
367 // << " f=" << recHitContribFrac[ih] << " ";
368 // cout << endl;
369 
370  reco::PFSimParticle particle( fst.charge(),
371  fst.type(),
372  fst.id(),
373  motherId,
374  fst.daughters(),
375  recTrackID,
376  recHitContrib,
377  recHitContribFrac);
378 
379 
380  const FSimVertex& originVtx = fst.vertex();
381 
382  math::XYZPoint posOrig( originVtx.position().x(),
383  originVtx.position().y(),
384  originVtx.position().z() );
385 
386  math::XYZTLorentzVector momOrig( fst.momentum().px(),
387  fst.momentum().py(),
388  fst.momentum().pz(),
389  fst.momentum().e() );
391  pointOrig(-1,
393  posOrig, momOrig);
394 
395  // point 0 is origin vertex
396  particle.addPoint(pointOrig);
397 
398 
399  if( ! fst.noEndVertex() ) {
400  const FSimVertex& endVtx = fst.endVertex();
401 
402  math::XYZPoint posEnd( endVtx.position().x(),
403  endVtx.position().y(),
404  endVtx.position().z() );
405  // cout<<"end vertex : "
406  // <<endVtx.position().x()<<" "
407  // <<endVtx.position().y()<<endl;
408 
410 
412  pointEnd( -1,
414  posEnd, momEnd);
415 
416  particle.addPoint(pointEnd);
417  }
418  else { // add a dummy point
420  particle.addPoint(dummy);
421  }
422 
423 
424  if( fst.onLayer1() ) { // PS layer1
425  const RawParticle& rp = fst.layer1Entrance();
426 
427  math::XYZPoint posLayer1( rp.x(), rp.y(), rp.z() );
428  math::XYZTLorentzVector momLayer1( rp.px(), rp.py(), rp.pz(), rp.e() );
430  posLayer1, momLayer1);
431 
432  particle.addPoint( layer1Pt );
433 
434  // extrapolate to cluster depth
435  }
436  else { // add a dummy point
438  particle.addPoint(dummy);
439  }
440 
441  if( fst.onLayer2() ) { // PS layer2
442  const RawParticle& rp = fst.layer2Entrance();
443 
444  math::XYZPoint posLayer2( rp.x(), rp.y(), rp.z() );
445  math::XYZTLorentzVector momLayer2( rp.px(), rp.py(), rp.pz(), rp.e() );
447  posLayer2, momLayer2);
448 
449  particle.addPoint( layer2Pt );
450 
451  // extrapolate to cluster depth
452  }
453  else { // add a dummy point
455  particle.addPoint(dummy);
456  }
457 
458  if( fst.onEcal() ) {
459  const RawParticle& rp = fst.ecalEntrance();
460 
461  math::XYZPoint posECAL( rp.x(), rp.y(), rp.z() );
462  math::XYZTLorentzVector momECAL( rp.px(), rp.py(), rp.pz(), rp.e() );
463  reco::PFTrajectoryPoint ecalPt(-1,
465  posECAL, momECAL);
466 
467  particle.addPoint( ecalPt );
468 
469  // extrapolate to cluster depth
470  }
471  else { // add a dummy point
473  particle.addPoint(dummy);
474  }
475 
476  // add a dummy point for ECAL Shower max
478  particle.addPoint(dummy);
479 
480  if( fst.onHcal() ) {
481 
482  const RawParticle& rpin = fst.hcalEntrance();
483 
484  math::XYZPoint posHCALin( rpin.x(), rpin.y(), rpin.z() );
485  math::XYZTLorentzVector momHCALin( rpin.px(), rpin.py(), rpin.pz(),
486  rpin.e() );
487  reco::PFTrajectoryPoint hcalPtin(-1,
489  posHCALin, momHCALin);
490 
491  particle.addPoint( hcalPtin );
492 
493 
494  // const RawParticle& rpout = fst.hcalExit();
495 
496  // math::XYZPoint posHCALout( rpout.x(), rpout.y(), rpout.z() );
497  // math::XYZTLorentzVector momHCALout( rpout.px(), rpout.py(), rpout.pz(),
498  // rpout.e() );
499  // reco::PFTrajectoryPoint
500  // hcalPtout(0, reco::PFTrajectoryPoint::HCALEntrance,
501  // posHCAL, momHCAL);
502 
503  // particle.addPoint( hcalPtout );
504  }
505  else { // add a dummy point
507  particle.addPoint(dummy);
508  }
509 
510  pOutputPFSimParticleCollection->push_back( particle );
511  }
512 
513  iEvent.put(pOutputPFSimParticleCollection);
514  }
515 
516 
517  LogDebug("PFSimParticleProducer")<<"STOP event: "<<iEvent.id().event()
518  <<" in run "<<iEvent.id().run()<<endl;
519 }
520 
522  std::vector<unsigned>& recTrackSimID )
523 {
524 
525  if( trackh.isValid() ) {
526 // cout << "Size=" << trackh->size() << endl;
527 
528  for(unsigned i=0;i<trackh->size(); i++) {
529 
530  reco::PFRecTrackRef ref( trackh,i );
531  const reco::PFRecTrack& PFT = *ref;
532  const reco::TrackRef trackref = PFT.trackRef();
533 
534 // double Pt = trackref->pt();
535 // double DPt = trackref->ptError();
536 // cout << " PFBlockProducer: PFrecTrack->Track Pt= "
537 // << Pt << " DPt = " << DPt << endl;
538 
539  trackingRecHit_iterator rhitbeg
540  = trackref->recHitsBegin();
541  trackingRecHit_iterator rhitend
542  = trackref->recHitsEnd();
543  for (trackingRecHit_iterator it = rhitbeg;
544  it != rhitend; it++){
545 
546  if( it->get()->isValid() ){
547 
548  const SiTrackerGSMatchedRecHit2D * rechit
549  = (const SiTrackerGSMatchedRecHit2D*) (it->get());
550 
551 // cout << "rechit"
552 // << " corresponding simId "
553 // << rechit->simtrackId()
554 // << endl;
555 
556  recTrackSimID.push_back( rechit->simtrackId() );
557  break;
558  }
559  }//loop track rechit
560  }//loop recTracks
561  }//track handle valid
562 }
PFSimParticleProducer(const edm::ParameterSet &)
#define LogDebug(id)
RunNumber_t run() const
Definition: EventID.h:42
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:44
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
int onLayer2() const
Definition: FSimTrack.h:96
const FSimVertex & endVertex() const
end vertex
double y() const
y of vertex
Definition: RawParticle.h:270
double z() const
z of vertex
Definition: RawParticle.h:271
const XYZTLorentzVector & momentum() const
Temporary (until move of SimTrack to Mathcore) - No! Actually very useful.
Definition: FSimTrack.h:168
const RawParticle & layer1Entrance() const
The particle at Preshower Layer 1.
Definition: FSimTrack.h:117
std::vector< T >::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:269
const reco::TrackRef & trackRef() const
Definition: PFRecTrack.h:54
void getData(T &iHolder) const
Definition: EventSetup.h:67
int onEcal() const
Definition: FSimTrack.h:101
virtual void beginRun(edm::Run &r, const edm::EventSetup &c)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:30
int hashedIndex(int ieta, int iphi)
Definition: EcalPyUtils.cc:42
int iEvent
Definition: GenABIO.cc:243
virtual void produce(edm::Event &, const edm::EventSetup &)
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:84
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:123
std::list< std::pair< double, unsigned > >::iterator ITM
const FSimVertex & vertex() const
Origin vertex.
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:355
true particle for particle flow
Definition: PFSimParticle.h:19
static ParticleTable * instance()
Definition: ParticleTable.h:15
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
T const * product() const
Definition: Handle.h:74
edm::EventID id() const
Definition: EventBase.h:56
int type() const
particle type (HEP PDT convension)
Definition: CoreSimTrack.h:40
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:120
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:126
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
Definition: Run.h:32