CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ParticleDecayDrawer.cc
Go to the documentation of this file.
1 /* class ParticleDecayDrawer
2  *
3  * \author Luca Lista, INFN
4  */
12 
14 public:
16 
17 private:
18  void analyze(const edm::Event &, const edm::EventSetup &) override;
20  std::string decay(const reco::Candidate &, std::list<const reco::Candidate *> &) const;
26  std::string printP4(const reco::Candidate &) const;
28  bool accept(const reco::Candidate &, const std::list<const reco::Candidate *> &) const;
30  bool select(const reco::Candidate &) const;
32  bool hasValidDaughters(const reco::Candidate &) const;
33 };
34 
40 #include <iostream>
41 #include <sstream>
42 #include <algorithm>
43 using namespace std;
44 using namespace edm;
45 using namespace reco;
46 
48  : srcToken_(consumes<edm::View<reco::Candidate> >(cfg.getParameter<InputTag>("src"))),
49  pdtToken_(esConsumes<ParticleDataTable, edm::DefaultRecord>()),
50  printP4_(cfg.getUntrackedParameter<bool>("printP4", false)),
51  printPtEtaPhi_(cfg.getUntrackedParameter<bool>("printPtEtaPhi", false)),
52  printVertex_(cfg.getUntrackedParameter<bool>("printVertex", false)) {}
53 
54 bool ParticleDecayDrawer::accept(const reco::Candidate &c, const list<const Candidate *> &skip) const {
55  if (find(skip.begin(), skip.end(), &c) != skip.end())
56  return false;
57  return select(c);
58 }
59 
60 bool ParticleDecayDrawer::select(const reco::Candidate &c) const { return c.status() == 3; }
61 
63  size_t ndau = c.numberOfDaughters();
64  for (size_t i = 0; i < ndau; ++i)
65  if (select(*c.daughter(i)))
66  return true;
67  return false;
68 }
69 
71  pdt_ = es.getHandle(pdtToken_);
72  Handle<View<Candidate> > particles;
73  event.getByToken(srcToken_, particles);
74  list<const Candidate *> skip;
75  vector<const Candidate *> nodes, moms;
76  for (View<Candidate>::const_iterator p = particles->begin(); p != particles->end(); ++p) {
77  if (p->numberOfMothers() > 1) {
78  if (select(*p)) {
79  skip.push_back(&*p);
80  nodes.push_back(&*p);
81  for (size_t j = 0; j < p->numberOfMothers(); ++j) {
82  const Candidate *mom = p->mother(j);
83  const Candidate *grandMom;
84  while ((grandMom = mom->mother()) != nullptr)
85  mom = grandMom;
86  if (select(*mom)) {
87  moms.push_back(mom);
88  }
89  }
90  }
91  }
92  }
93  cout << "-- decay: --" << endl;
94  if (!moms.empty()) {
95  if (moms.size() > 1)
96  for (size_t m = 0; m < moms.size(); ++m) {
97  string dec = decay(*moms[m], skip);
98  if (!dec.empty())
99  cout << "{ " << dec << " } ";
100  }
101  else
102  cout << decay(*moms[0], skip);
103  }
104  if (!nodes.empty()) {
105  cout << "-> ";
106  if (nodes.size() > 1) {
107  for (size_t n = 0; n < nodes.size(); ++n) {
108  skip.remove(nodes[n]);
109  string dec = decay(*nodes[n], skip);
110  if (!dec.empty()) {
111  if (dec.find("->", 0) != string::npos)
112  cout << " ( " << dec << " )";
113  else
114  cout << " " << dec;
115  }
116  }
117  } else {
118  skip.remove(nodes[0]);
119  cout << decay(*nodes[0], skip);
120  }
121  }
122  cout << endl;
123 }
124 
126  ostringstream cout;
127  if (printP4_)
128  cout << " (" << c.px() << ", " << c.py() << ", " << c.pz() << "; " << c.energy() << ")";
129  if (printPtEtaPhi_)
130  cout << " [" << c.pt() << ": " << c.eta() << ", " << c.phi() << "]";
131  if (printVertex_)
132  cout << " {" << c.vx() << ", " << c.vy() << ", " << c.vz() << "}";
133  return cout.str();
134 }
135 
136 string ParticleDecayDrawer::decay(const Candidate &c, list<const Candidate *> &skip) const {
137  string out;
138  if (find(skip.begin(), skip.end(), &c) != skip.end())
139  return out;
140  skip.push_back(&c);
141 
142  int id = c.pdgId();
143  const ParticleData *pd = pdt_->particle(id);
144  assert(pd != nullptr);
145  out += (pd->name() + printP4(c));
146 
147  size_t validDau = 0, ndau = c.numberOfDaughters();
148  for (size_t i = 0; i < ndau; ++i)
149  if (accept(*c.daughter(i), skip))
150  ++validDau;
151  if (validDau == 0)
152  return out;
153 
154  out += " ->";
155 
156  for (size_t i = 0; i < ndau; ++i) {
157  const Candidate *d = c.daughter(i);
158  if (accept(*d, skip)) {
159  string dec = decay(*d, skip);
160  if (dec.find("->", 0) != string::npos)
161  out += (" ( " + dec + " )");
162  else
163  out += (" " + dec);
164  }
165  }
166  return out;
167 }
168 
170 
bool hasValidDaughters(const reco::Candidate &) const
has valid daughters in the chain
virtual double energy() const =0
energy
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
const edm::EventSetup & c
tuple cfg
Definition: looper.py:296
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
HepPDT::ParticleDataTable ParticleDataTable
virtual double pt() const =0
transverse momentum
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool accept(const reco::Candidate &, const std::list< const reco::Candidate * > &) const
accept candidate
std::string printP4(const reco::Candidate &) const
print 4 momenta
virtual int status() const =0
status word
virtual double pz() const =0
z coordinate of momentum vector
virtual double vx() const =0
x coordinate of vertex position
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
assert(be >=bs)
edm::ESHandle< ParticleDataTable > pdt_
virtual double vy() const =0
y coordinate of vertex position
tuple d
Definition: ztail.py:151
virtual size_type numberOfDaughters() const =0
number of daughters
std::string decay(const reco::Candidate &, std::list< const reco::Candidate * > &) const
bool select(const reco::Candidate &) const
select candidate
ParticleDecayDrawer(const edm::ParameterSet &)
bool printP4_
print parameters
void analyze(const edm::Event &, const edm::EventSetup &) override
virtual double py() const =0
y coordinate of momentum vector
HepPDT::ParticleData ParticleData
virtual double px() const =0
x coordinate of momentum vector
virtual int pdgId() const =0
PDG identifier.
edm::ESGetToken< ParticleDataTable, edm::DefaultRecord > pdtToken_
virtual double vz() const =0
z coordinate of vertex position
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
tuple cout
Definition: gather_cfg.py:144
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:157
edm::EDGetTokenT< edm::View< reco::Candidate > > srcToken_
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
virtual double phi() const =0
momentum azimuthal angle
virtual double eta() const =0
momentum pseudorapidity