CMS 3D CMS Logo

TrackHistoryAnalyzer.cc
Go to the documentation of this file.
1 /*
2  * TrackHistoryAnalyzer.C
3  *
4  * Created by Victor Eduardo Bazterra on 5/31/07.
5  *
6  */
7 
8 // system include files
9 #include <iostream>
10 #include <memory>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14 
15 // user include files
16 
19 
27 
30 
31 //
32 // class decleration
33 //
34 
36 public:
37  explicit TrackHistoryAnalyzer(const edm::ParameterSet &);
38  ~TrackHistoryAnalyzer() override;
39 
40 private:
41  virtual void beginRun(const edm::Run &, const edm::EventSetup &);
42  void beginJob() override;
43  void analyze(const edm::Event &, const edm::EventSetup &) override;
44 
45  // Member data
46 
48 
49  std::size_t totalTracks_;
50 
52 
53  std::string particleString(int) const;
54 
56 
58 
59  std::string vertexString(HepMC::GenVertex::particles_in_const_iterator,
60  HepMC::GenVertex::particles_in_const_iterator,
61  HepMC::GenVertex::particles_out_const_iterator,
62  HepMC::GenVertex::particles_out_const_iterator) const;
63 };
64 
66  trackProducer_ = config.getUntrackedParameter<edm::InputTag>("trackProducer");
67  consumes<edm::View<reco::Track>>(trackProducer_);
68 }
69 
71 
73  // Track collection
75  event.getByLabel(trackProducer_, trackCollection);
76 
77  // Set the classifier for a new event
78  classifier_.newEvent(event, setup);
79 
80  // Get a constant reference to the track history associated to the classifier
81  TrackHistory const &tracer = classifier_.history();
82 
83  // Loop over the track collection.
84  for (std::size_t index = 0; index < trackCollection->size(); index++) {
85  std::cout << std::endl << "History for track #" << index << " : " << std::endl;
86 
87  // Classify the track and detect for fakes
89  // Get the list of TrackingParticles associated to
90  TrackHistory::SimParticleTrail simParticles(tracer.simParticleTrail());
91 
92  // Loop over all simParticles
93  for (std::size_t hindex = 0; hindex < simParticles.size(); hindex++) {
94  std::cout << " simParticles [" << hindex << "] : " << particleString(simParticles[hindex]->pdgId())
95  << std::endl;
96  }
97 
98  // Get the list of TrackingVertexes associated to
99  TrackHistory::SimVertexTrail simVertexes(tracer.simVertexTrail());
100 
101  // Loop over all simVertexes
102  if (!simVertexes.empty()) {
103  for (std::size_t hindex = 0; hindex < simVertexes.size(); hindex++) {
104  std::cout << " simVertex [" << hindex << "] : "
105  << vertexString(simVertexes[hindex]->sourceTracks(), simVertexes[hindex]->daughterTracks())
106  << std::endl;
107  }
108  } else
109  std::cout << " simVertex no found" << std::endl;
110 
111  // Get the list of GenParticles associated to
113 
114  // Loop over all genParticles
115  for (std::size_t hindex = 0; hindex < genParticles.size(); hindex++) {
116  std::cout << " genParticles [" << hindex << "] : " << particleString(genParticles[hindex]->pdg_id())
117  << std::endl;
118  }
119 
120  // Get the list of TrackingVertexes associated to
121  TrackHistory::GenVertexTrail genVertexes(tracer.genVertexTrail());
122 
123  // Loop over all simVertexes
124  if (!genVertexes.empty()) {
125  for (std::size_t hindex = 0; hindex < genVertexes.size(); hindex++) {
126  std::cout << " genVertex [" << hindex << "] : "
127  << vertexString(genVertexes[hindex]->particles_in_const_begin(),
128  genVertexes[hindex]->particles_in_const_end(),
129  genVertexes[hindex]->particles_out_const_begin(),
130  genVertexes[hindex]->particles_out_const_end())
131  << std::endl;
132  }
133  } else
134  std::cout << " genVertex no found" << std::endl;
135  } else
136  std::cout << " fake track" << std::endl;
137 
138  std::cout << " track categories : " << classifier_;
139  std::cout << std::endl;
140  }
141 }
142 
144  // Get the particles table.
145  setup.getData(pdt_);
146 }
147 
149 
151  ParticleData const *pid;
152 
153  std::ostringstream vDescription;
154 
156 
157  if (particleType.isValid()) {
158  pid = pdt_->particle(particleType);
159  if (pid)
160  vDescription << pid->name();
161  else
162  vDescription << pdgId;
163  } else
164  vDescription << pdgId;
165 
166  return vDescription.str();
167 }
168 
170  const TrackingParticleRefVector &out) const {
171  ParticleData const *pid;
172 
173  std::ostringstream vDescription;
174 
175  for (std::size_t j = 0; j < in.size(); j++) {
176  if (!j)
177  vDescription << "(";
178 
180 
181  if (particleType.isValid()) {
182  pid = pdt_->particle(particleType);
183  if (pid)
184  vDescription << pid->name();
185  else
186  vDescription << in[j]->pdgId();
187  } else
188  vDescription << in[j]->pdgId();
189 
190  if (j == in.size() - 1)
191  vDescription << ")";
192  else
193  vDescription << ",";
194  }
195 
196  vDescription << "->";
197 
198  for (std::size_t j = 0; j < out.size(); j++) {
199  if (!j)
200  vDescription << "(";
201 
203 
204  if (particleType.isValid()) {
205  pid = pdt_->particle(particleType);
206  if (pid)
207  vDescription << pid->name();
208  else
209  vDescription << out[j]->pdgId();
210  } else
211  vDescription << out[j]->pdgId();
212 
213  if (j == out.size() - 1)
214  vDescription << ")";
215  else
216  vDescription << ",";
217  }
218 
219  return vDescription.str();
220 }
221 
222 std::string TrackHistoryAnalyzer::vertexString(HepMC::GenVertex::particles_in_const_iterator in_begin,
223  HepMC::GenVertex::particles_in_const_iterator in_end,
224  HepMC::GenVertex::particles_out_const_iterator out_begin,
225  HepMC::GenVertex::particles_out_const_iterator out_end) const {
226  ParticleData const *pid;
227 
228  std::ostringstream vDescription;
229 
230  std::size_t j = 0;
231 
232  HepMC::GenVertex::particles_in_const_iterator in, itmp;
233 
234  for (in = in_begin; in != in_end; in++, j++) {
235  if (!j)
236  vDescription << "(";
237 
238  HepPDT::ParticleID particleType((*in)->pdg_id());
239 
240  if (particleType.isValid()) {
241  pid = pdt_->particle(particleType);
242  if (pid)
243  vDescription << pid->name();
244  else
245  vDescription << (*in)->pdg_id();
246  } else
247  vDescription << (*in)->pdg_id();
248 
249  itmp = in;
250 
251  if (++itmp == in_end)
252  vDescription << ")";
253  else
254  vDescription << ",";
255  }
256 
257  vDescription << "->";
258  j = 0;
259 
260  HepMC::GenVertex::particles_out_const_iterator out, otmp;
261 
262  for (out = out_begin; out != out_end; out++, j++) {
263  if (!j)
264  vDescription << "(";
265 
266  HepPDT::ParticleID particleType((*out)->pdg_id());
267 
268  if (particleType.isValid()) {
269  pid = pdt_->particle(particleType);
270  if (pid)
271  vDescription << pid->name();
272  else
273  vDescription << (*out)->pdg_id();
274  } else
275  vDescription << (*out)->pdg_id();
276 
277  otmp = out;
278 
279  if (++otmp == out_end)
280  vDescription << ")";
281  else
282  vDescription << ",";
283  }
284 
285  return vDescription.str();
286 }
287 
T getUntrackedParameter(std::string const &, T const &) const
std::string vertexString(const TrackingParticleRefVector &, const TrackingParticleRefVector &) const
SimVertexTrail const & simVertexTrail() const
Return all the simulated vertices in the history.
Definition: HistoryBase.h:52
void analyze(const edm::Event &, const edm::EventSetup &) override
std::string particleString(int) const
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
TrackHistory const & history() const
Returns a reference to the track history used in the classification.
Definition: config.py:1
std::vector< const HepMC::GenParticle * > GenParticleTrail
HepMC::GenParticle trail type.
Definition: HistoryBase.h:15
bool getData(T &iHolder) const
Definition: EventSetup.h:111
TrackClassifier const & evaluate(reco::TrackBaseRef const &)
Classify the RecoTrack in categories.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
TrackHistoryAnalyzer(const edm::ParameterSet &)
This class traces the simulated and generated history of a given track.
Definition: TrackHistory.h:17
std::vector< const HepMC::GenVertex * > GenVertexTrail
GenVertex trail type.
Definition: HistoryBase.h:24
void newEvent(edm::Event const &, edm::EventSetup const &)
Pre-process event information (for accessing reconstraction information)
HepPDT::ParticleData ParticleData
bool is(Category category) const
Returns track flag for a given category.
Get track history and classify it in function of their .
SimParticleTrail const & simParticleTrail() const
Return all the simulated particle in the history.
Definition: HistoryBase.h:55
edm::ESHandle< ParticleDataTable > pdt_
size_type size() const
Size of the RefVector.
Definition: RefVector.h:107
GenVertexTrail const & genVertexTrail() const
Return all generated vertex in the history.
Definition: HistoryBase.h:58
std::vector< TrackingVertexRef > SimVertexTrail
SimVertex trail type.
Definition: HistoryBase.h:33
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
GenParticleTrail const & genParticleTrail() const
Return all generated particle (HepMC::GenParticle) in the history.
Definition: HistoryBase.h:61
Definition: event.py:1
Definition: Run.h:45
std::vector< TrackingParticleRef > SimParticleTrail
SimParticle trail type.
Definition: HistoryBase.h:30