CMS 3D CMS Logo

ParticleListDrawer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <iostream>
4 #include <sstream>
5 
20 
38 using namespace std;
39 using namespace reco;
40 using namespace edm;
41 
43 public:
44  explicit ParticleListDrawer(const edm::ParameterSet&);
45  ~ParticleListDrawer() override{};
46  void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
47 
48 private:
49  std::string getParticleName(int id) const;
50 
55  int maxEventsToPrint_; // Must be signed, because -1 is used for no limit
56  unsigned int nEventAnalyzed_;
61 };
62 
64  : src_(pset.getParameter<InputTag>("src")),
65  srcToken_(consumes<reco::CandidateView>(src_)),
67  maxEventsToPrint_(pset.getUntrackedParameter<int>("maxEventsToPrint", 1)),
68  nEventAnalyzed_(0),
69  printOnlyHardInteraction_(pset.getUntrackedParameter<bool>("printOnlyHardInteraction", false)),
70  printVertex_(pset.getUntrackedParameter<bool>("printVertex", false)),
71  printFlags_(pset.getUntrackedParameter<bool>("printFlags", false)),
72  useMessageLogger_(pset.getUntrackedParameter<bool>("useMessageLogger", false)) {}
73 
75  const ParticleData* pd = pdt_->particle(id);
76  if (!pd) {
77  std::ostringstream ss;
78  ss << "P" << id;
79  return ss.str();
80  } else
81  return pd->name();
82 }
83 
86  iEvent.getByToken(srcToken_, particles);
87  pdt_ = iSetup.getHandle(pdtToken_);
88 
89  if (maxEventsToPrint_ < 0 || nEventAnalyzed_ < static_cast<unsigned int>(maxEventsToPrint_)) {
90  ostringstream out;
91  char buf[256];
92 
93  out << endl << "[ParticleListDrawer] analysing particle collection " << src_.label() << endl;
94 
95  snprintf(buf,
96  sizeof(buf),
97  " idx | ID - Name |Stat| Mo1 Mo2 Da1 Da2 |nMo nDa| pt eta phi | px "
98  " py pz m |");
99  out << buf;
100  if (printVertex_) {
101  snprintf(buf, sizeof(buf), " vx vy vz |");
102  out << buf;
103  }
104  out << endl;
105 
106  int idx = -1;
107  int iMo1 = -1;
108  int iMo2 = -1;
109  int iDa1 = -1;
110  int iDa2 = -1;
111  vector<const reco::Candidate*> cands;
112  vector<const Candidate*>::const_iterator found = cands.begin();
113  for (CandidateView::const_iterator p = particles->begin(); p != particles->end(); ++p) {
114  cands.push_back(&*p);
115  }
116 
117  for (CandidateView::const_iterator p = particles->begin(); p != particles->end(); p++) {
118  if (printOnlyHardInteraction_ && p->status() != 3)
119  continue;
120 
121  // Particle Name
122  int id = p->pdgId();
123  string particleName = getParticleName(id);
124 
125  // Particle Index
126  idx = p - particles->begin();
127 
128  // Particles Mothers and Daighters
129  iMo1 = -1;
130  iMo2 = -1;
131  iDa1 = -1;
132  iDa2 = -1;
133  int nMo = p->numberOfMothers();
134  int nDa = p->numberOfDaughters();
135 
136  found = find(cands.begin(), cands.end(), p->mother(0));
137  if (found != cands.end())
138  iMo1 = found - cands.begin();
139 
140  found = find(cands.begin(), cands.end(), p->mother(nMo - 1));
141  if (found != cands.end())
142  iMo2 = found - cands.begin();
143 
144  found = find(cands.begin(), cands.end(), p->daughter(0));
145  if (found != cands.end())
146  iDa1 = found - cands.begin();
147 
148  found = find(cands.begin(), cands.end(), p->daughter(nDa - 1));
149  if (found != cands.end())
150  iDa2 = found - cands.begin();
151 
152  char buf[2400];
153  snprintf(
154  buf,
155  sizeof(buf),
156  " %4d | %5d - %10s | %2d | %4d %4d %4d %4d | %2d %2d | %7.3f %10.3f %6.3f | %10.3f %10.3f %10.3f %8.3f |",
157  idx,
158  p->pdgId(),
159  particleName.c_str(),
160  p->status(),
161  iMo1,
162  iMo2,
163  iDa1,
164  iDa2,
165  nMo,
166  nDa,
167  p->pt(),
168  p->eta(),
169  p->phi(),
170  p->px(),
171  p->py(),
172  p->pz(),
173  p->mass());
174  out << buf;
175 
176  if (printVertex_) {
177  snprintf(buf, sizeof(buf), " %10.3f %10.3f %10.3f |", p->vertex().x(), p->vertex().y(), p->vertex().z());
178  out << buf;
179  }
180 
181  if (printFlags_) {
182  const reco::GenParticle* gp = dynamic_cast<const reco::GenParticle*>(&*p);
183  if (!gp)
184  throw cms::Exception("Unsupported", "Status flags can be printed only for reco::GenParticle objects\n");
185  if (gp->isPromptFinalState())
186  out << " PromptFinalState";
187  if (gp->isDirectPromptTauDecayProductFinalState())
188  out << " DirectPromptTauDecayProductFinalState";
189  if (gp->isHardProcess())
190  out << " HardProcess";
191  if (gp->fromHardProcessFinalState())
192  out << " HardProcessFinalState";
193  if (gp->fromHardProcessBeforeFSR())
194  out << " HardProcessBeforeFSR";
195  if (gp->statusFlags().isFirstCopy())
196  out << " FirstCopy";
197  if (gp->isLastCopy())
198  out << " LastCopy";
199  if (gp->isLastCopyBeforeFSR())
200  out << " LastCopyBeforeFSR";
201  }
202 
203  out << endl;
204  }
205  nEventAnalyzed_++;
206 
207  if (useMessageLogger_)
208  LogVerbatim("ParticleListDrawer") << out.str();
209  else
210  cout << out.str();
211  }
212 }
213 
Log< level::Info, true > LogVerbatim
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
HepPDT::ParticleDataTable ParticleDataTable
std::string const & label() const
Definition: InputTag.h:36
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< reco::CandidateView > srcToken_
int iEvent
Definition: GenABIO.cc:224
ParticleListDrawer(const edm::ParameterSet &)
unsigned int nEventAnalyzed_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
HepPDT::ParticleData ParticleData
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override
std::string getParticleName(int id) const
edm::ESHandle< ParticleDataTable > pdt_
fixed size matrix
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:88
~ParticleListDrawer() override
Module to analyze the particle listing as provided by common event generators.
edm::ESGetToken< ParticleDataTable, edm::DefaultRecord > pdtToken_