CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ParticleListDrawer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <iostream>
4 #include <sstream>
5 
18 
37 using namespace std;
38 using namespace reco;
39 using namespace edm;
40 
42  public:
43  explicit ParticleListDrawer(const edm::ParameterSet & );
45  void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup);
46 
47  private:
48  std::string getParticleName( int id ) const;
49 
52  int maxEventsToPrint_; // Must be signed, because -1 is used for no limit
53  unsigned int nEventAnalyzed_;
57 };
58 
60  src_(pset.getParameter<InputTag>("src")),
61  maxEventsToPrint_ (pset.getUntrackedParameter<int>("maxEventsToPrint",1)),
62  nEventAnalyzed_(0),
63  printOnlyHardInteraction_(pset.getUntrackedParameter<bool>("printOnlyHardInteraction", false)),
64  printVertex_(pset.getUntrackedParameter<bool>("printVertex", false)),
65  useMessageLogger_(pset.getUntrackedParameter<bool>("useMessageLogger", false)) {
66 }
67 
68 std::string ParticleListDrawer::getParticleName(int id) const
69 {
70  const ParticleData * pd = pdt_->particle( id );
71  if (!pd) {
72  std::ostringstream ss;
73  ss << "P" << id;
74  return ss.str();
75  } else
76  return pd->name();
77 }
78 
81  iEvent.getByLabel (src_, particles );
82  iSetup.getData( pdt_ );
83 
84  if(maxEventsToPrint_ < 0 || nEventAnalyzed_ < static_cast<unsigned int>(maxEventsToPrint_)) {
85  ostringstream out;
86  char buf[256];
87 
88  out << endl
89  << "[ParticleListDrawer] analysing particle collection " << src_.label() << endl;
90 
91  snprintf(buf, 256, " idx | ID - Name |Stat| Mo1 Mo2 Da1 Da2 |nMo nDa| pt eta phi | px py pz m |");
92  out << buf;
93  if (printVertex_) {
94  snprintf(buf, 256, " vx vy vz |");
95  out << buf;
96  }
97  out << endl;
98 
99  int idx = -1;
100  int iMo1 = -1;
101  int iMo2 = -1;
102  int iDa1 = -1;
103  int iDa2 = -1;
104  vector<const reco::Candidate *> cands;
105  vector<const Candidate *>::const_iterator found = cands.begin();
106  for(CandidateView::const_iterator p = particles->begin();
107  p != particles->end(); ++ p) {
108  cands.push_back(&*p);
109  }
110 
111  for(CandidateView::const_iterator p = particles->begin();
112  p != particles->end();
113  p ++) {
114  if (printOnlyHardInteraction_ && p->status() != 3) continue;
115 
116  // Particle Name
117  int id = p->pdgId();
118  string particleName = getParticleName(id);
119 
120  // Particle Index
121  idx = p - particles->begin();
122 
123  // Particles Mothers and Daighters
124  iMo1 = -1;
125  iMo2 = -1;
126  iDa1 = -1;
127  iDa2 = -1;
128  int nMo = p->numberOfMothers();
129  int nDa = p->numberOfDaughters();
130 
131  found = find(cands.begin(), cands.end(), p->mother(0));
132  if(found != cands.end()) iMo1 = found - cands.begin() ;
133 
134  found = find(cands.begin(), cands.end(), p->mother(nMo-1));
135  if(found != cands.end()) iMo2 = found - cands.begin() ;
136 
137  found = find(cands.begin(), cands.end(), p->daughter(0));
138  if(found != cands.end()) iDa1 = found - cands.begin() ;
139 
140  found = find(cands.begin(), cands.end(), p->daughter(nDa-1));
141  if(found != cands.end()) iDa2 = found - cands.begin() ;
142 
143  char buf[256];
144  snprintf(buf, 256,
145  " %4d | %5d - %10s | %2d | %4d %4d %4d %4d | %2d %2d | %7.3f %10.3f %6.3f | %10.3f %10.3f %10.3f %8.3f |",
146  idx,
147  p->pdgId(),
148  particleName.c_str(),
149  p->status(),
150  iMo1,iMo2,iDa1,iDa2,nMo,nDa,
151  p->pt(),
152  p->eta(),
153  p->phi(),
154  p->px(),
155  p->py(),
156  p->pz(),
157  p->mass()
158  );
159  out << buf;
160 
161  if (printVertex_) {
162  snprintf(buf, 256, " %10.3f %10.3f %10.3f |",
163  p->vertex().x(),
164  p->vertex().y(),
165  p->vertex().z());
166  out << buf;
167  }
168 
169  out << endl;
170  }
171  nEventAnalyzed_++;
172 
173  if (useMessageLogger_)
174  LogVerbatim("ParticleListDrawer") << out.str();
175  else
176  cout << out.str();
177  }
178 }
179 
181 
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
void getData(T &iHolder) const
Definition: EventSetup.h:67
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup)
int iEvent
Definition: GenABIO.cc:243
ParticleListDrawer(const edm::ParameterSet &)
unsigned int nEventAnalyzed_
HepPDT::ParticleData ParticleData
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
tuple out
Definition: dbtoconf.py:99
std::string const & label() const
Definition: InputTag.h:25
edm::ESHandle< ParticleDataTable > pdt_
std::string getParticleName(int id) const
tuple cout
Definition: gather_cfg.py:121
Module to analyze the particle listing as provided by common event generators.