1 |
|
#include "UserCode/Morgan/interface/ParticleTreeDrawer.h" |
2 |
|
|
3 |
– |
#include "FWCore/ParameterSet/interface/ParameterSet.h" |
4 |
– |
#include "FWCore/Framework/interface/Event.h" |
5 |
– |
//#include "FWCore/Framework/interface/Handle.h" |
6 |
– |
#include "FWCore/Framework/interface/EventSetup.h" |
7 |
– |
#include "DataFormats/HepMCCandidate/interface/GenParticleCandidate.h" |
8 |
– |
#include "FWCore/Utilities/interface/EDMException.h" |
9 |
– |
#include <iostream> |
10 |
– |
#include <sstream> |
11 |
– |
|
3 |
|
using namespace std; |
4 |
|
using namespace edm; |
5 |
|
using namespace reco; |
6 |
|
using namespace HepMC; |
7 |
|
|
8 |
|
|
9 |
< |
ParticleTreeDrawer::ParticleTreeDrawer() : |
10 |
< |
src_("genParticleCandidates"), printP4_(false), printPtEtaPhi_(false), printVertex_(false), printStatus_(false) |
11 |
< |
{} |
12 |
< |
|
13 |
< |
void ParticleTreeDrawer::analyze( const Event & event, const EventSetup & es ) |
9 |
> |
ParticleTreeDrawer::ParticleTreeDrawer(const edm::ParameterSet & cfg, const edm::ParameterSet & producers) : |
10 |
> |
src_( producers.getParameter<InputTag>( "genParticlesProducer" ) ), |
11 |
> |
printP4_( cfg.getUntrackedParameter<bool>( "mcTreePrintP4", false ) ), |
12 |
> |
printPtEtaPhi_( cfg.getUntrackedParameter<bool>( "mcTreePrintPtEtaPhi", false ) ), |
13 |
> |
printVertex_( cfg.getUntrackedParameter<bool>( "mcTreePrintVertex", false ) ), |
14 |
> |
printStatus_( cfg.getUntrackedParameter<bool>( "mcTreePrintStatus", false ) ), |
15 |
> |
printIndex_( cfg.getUntrackedParameter<bool>( "mcTreePrintIndex", false ) ), |
16 |
> |
status_( cfg.getUntrackedParameter<vint>( "mcTreeStatus", vint() ) ) |
17 |
|
{ |
24 |
– |
/* |
25 |
– |
es.getData( pdt_ ); |
26 |
– |
Handle<CandidateCollection> particles; |
27 |
– |
event.getByLabel( "genParticleCandidates", particles ); |
28 |
– |
for( CandidateCollection::const_iterator p = particles->begin(); |
29 |
– |
p != particles->end(); ++ p ) |
30 |
– |
{ |
31 |
– |
if ( p->mother() == 0 ) |
32 |
– |
{ |
33 |
– |
cout << "-- decay: --" << endl; |
34 |
– |
printDecay( * p, "" ); |
35 |
– |
} |
36 |
– |
} |
37 |
– |
*/ |
18 |
|
} |
19 |
|
|
20 |
< |
void ParticleTreeDrawer::printP4( const reco::Candidate & c ) const |
21 |
< |
{ |
22 |
< |
/* |
23 |
< |
if ( printP4_ ) |
44 |
< |
cout << " (" << c.px() << ", " << c.py() << ", " << c.pz() << "; " << c.energy() << ")"; |
45 |
< |
if ( printPtEtaPhi_ ) |
46 |
< |
cout << " [" << c.pt() << ": " << c.eta() << ", " << c.phi() << "]"; |
47 |
< |
if ( printVertex_ ) |
48 |
< |
cout << " {" << c.vx() << ", " << c.vy() << ", " << c.vz() << "}"; |
49 |
< |
if ( printStatus_ ) |
50 |
< |
cout << "{status: " << c.status() << "}"; |
51 |
< |
*/ |
20 |
> |
|
21 |
> |
bool ParticleTreeDrawer::accept( const reco::Candidate & c ) const { |
22 |
> |
if ( status_.size() == 0 ) return true; |
23 |
> |
return find( status_.begin(), status_.end(), c.status() ) != status_.end(); |
24 |
|
} |
25 |
|
|
54 |
– |
void ParticleTreeDrawer::printDecay( const reco::Candidate & c, const std::string & pre ) const |
55 |
– |
{ |
56 |
– |
/* |
57 |
– |
int id = c.pdgId(); |
58 |
– |
unsigned int ndau = c.numberOfDaughters(); |
59 |
– |
const ParticleData * pd = pdt_->particle( id ); |
60 |
– |
assert( pd != 0 ); |
26 |
|
|
27 |
< |
cout << pd->name(); |
28 |
< |
printP4( c ); |
29 |
< |
cout << endl; |
27 |
> |
bool ParticleTreeDrawer::hasValidDaughters( const reco::Candidate & c ) const { |
28 |
> |
size_t ndau = c.numberOfDaughters(); |
29 |
> |
for( size_t i = 0; i < ndau; ++ i ) |
30 |
> |
if ( accept( * c.daughter( i ) ) ) |
31 |
> |
return true; |
32 |
> |
return false; |
33 |
> |
} |
34 |
|
|
66 |
– |
if ( ndau == 0 ) |
67 |
– |
return; |
35 |
|
|
36 |
< |
bool lastLevel = true; |
37 |
< |
for( size_t i = 0; i < ndau; ++ i ) |
38 |
< |
if ( c.daughter( i )->numberOfDaughters() != 0 ) |
39 |
< |
{ |
40 |
< |
lastLevel = false; |
41 |
< |
break; |
42 |
< |
} |
43 |
< |
|
44 |
< |
if ( lastLevel ) |
45 |
< |
{ |
46 |
< |
cout << pre << "+-> "; |
47 |
< |
for( size_t i = 0; i < ndau; ++ i ) |
48 |
< |
{ |
49 |
< |
const GenParticleCandidate * d = |
50 |
< |
dynamic_cast<const GenParticleCandidate *>( c.daughter( i ) ); |
51 |
< |
assert( d != 0 ); |
85 |
< |
const ParticleData * pd = pdt_->particle( d->pdgId() ); |
86 |
< |
assert( pd != 0 ); |
87 |
< |
cout << pd->name(); |
88 |
< |
printP4( * d ); |
89 |
< |
if ( i != ndau - 1 ) |
90 |
< |
cout << " "; |
91 |
< |
} |
92 |
< |
cout << endl; |
93 |
< |
return; |
36 |
> |
void ParticleTreeDrawer::analyze( const Event & event, const EventSetup & es ) { |
37 |
> |
es.getData( pdt_ ); |
38 |
> |
Handle<View<Candidate> > particles; |
39 |
> |
event.getByLabel( src_, particles ); |
40 |
> |
cands_.clear(); |
41 |
> |
for( View<Candidate>::const_iterator p = particles->begin(); |
42 |
> |
p != particles->end(); ++ p ) { |
43 |
> |
cands_.push_back( & * p ); |
44 |
> |
} |
45 |
> |
for( View<Candidate>::const_iterator p = particles->begin(); |
46 |
> |
p != particles->end(); ++ p ) { |
47 |
> |
if ( accept( * p ) ) { |
48 |
> |
if ( p->mother() == 0 ) { |
49 |
> |
cout << "-- decay tree: --" << endl; |
50 |
> |
printDecay( * p, "" ); |
51 |
> |
} |
52 |
|
} |
53 |
+ |
} |
54 |
+ |
} |
55 |
+ |
|
56 |
|
|
57 |
< |
for( size_t i = 0; i < ndau; ++i ) |
58 |
< |
{ |
59 |
< |
const GenParticleCandidate * d = |
60 |
< |
dynamic_cast<const GenParticleCandidate *>( c.daughter( i ) ); |
61 |
< |
cout << pre << "+-> "; |
62 |
< |
string prepre( pre ); |
63 |
< |
if ( i == ndau - 1 ) |
64 |
< |
prepre += " "; |
65 |
< |
else |
66 |
< |
prepre += "| "; |
67 |
< |
printDecay( * d, prepre ); |
57 |
> |
|
58 |
> |
void ParticleTreeDrawer::printInfo( const Candidate & c ) const { |
59 |
> |
if ( printP4_ ) cout << " (" << c.px() << ", " << c.py() << ", " << c.pz() << "; " << c.energy() << ")"; |
60 |
> |
if ( printPtEtaPhi_ ) cout << " [" << c.pt() << ": " << c.eta() << ", " << c.phi() << "]"; |
61 |
> |
if ( printVertex_ ) cout << " {" << c.vx() << ", " << c.vy() << ", " << c.vz() << "}"; |
62 |
> |
if ( printStatus_ ) cout << "{status: " << c.status() << "}"; |
63 |
> |
if ( printIndex_ ) { |
64 |
> |
int idx = -1; |
65 |
> |
vector<const Candidate *>::const_iterator found = find( cands_.begin(), cands_.end(), & c ); |
66 |
> |
if ( found != cands_.end() ) { |
67 |
> |
idx = found - cands_.begin(); |
68 |
> |
cout << " <idx: " << idx << ">"; |
69 |
|
} |
70 |
< |
*/ |
70 |
> |
} |
71 |
|
} |
72 |
|
|
73 |
< |
//#include "FWCore/Framework/interface/MakerMacros.h" |
73 |
> |
void ParticleTreeDrawer::printDecay( const Candidate & c, const string & pre ) const { |
74 |
> |
int id = c.pdgId(); |
75 |
> |
const ParticleData * pd = pdt_->particle( id ); |
76 |
> |
cout << (pd != 0? pd->name():"???"); |
77 |
> |
//cout << endl; |
78 |
> |
printInfo( c ); |
79 |
> |
cout << endl; |
80 |
> |
|
81 |
> |
size_t ndau = c.numberOfDaughters(), validDau = 0; |
82 |
> |
for( size_t i = 0; i < ndau; ++ i ) |
83 |
> |
if ( accept( * c.daughter( i ) ) ) |
84 |
> |
++ validDau; |
85 |
> |
if ( validDau == 0 ) return; |
86 |
> |
|
87 |
> |
bool lastLevel = true; |
88 |
> |
for( size_t i = 0; i < ndau; ++ i ) { |
89 |
> |
if ( hasValidDaughters( * c.daughter( i ) ) ) { |
90 |
> |
lastLevel = false; |
91 |
> |
break; |
92 |
> |
} |
93 |
> |
} |
94 |
> |
|
95 |
> |
if ( lastLevel ) { |
96 |
> |
cout << pre << "+-> "; |
97 |
> |
size_t vd = 0; |
98 |
> |
for( size_t i = 0; i < ndau; ++ i ) { |
99 |
> |
const Candidate * d = c.daughter( i ); |
100 |
> |
if ( accept( * d ) ) { |
101 |
> |
const ParticleData * pd = pdt_->particle( d->pdgId() ); |
102 |
> |
cout << (pd != 0? pd->name():"???") << endl; |
103 |
> |
printInfo( * d ); |
104 |
> |
if ( vd != validDau - 1 ) |
105 |
> |
cout << " "; |
106 |
> |
vd ++; |
107 |
> |
} |
108 |
> |
} |
109 |
> |
cout << endl; |
110 |
> |
return; |
111 |
> |
} |
112 |
|
|
113 |
< |
//DEFINE_FWK_MODULE( ParticleTreeDrawer ); |
113 |
> |
for( size_t i = 0; i < ndau; ++i ) { |
114 |
> |
const Candidate * d = c.daughter( i ); |
115 |
> |
assert( d != 0 ); |
116 |
> |
if ( accept( * d ) ) { |
117 |
> |
cout << pre << "+-> "; |
118 |
> |
string prepre( pre ); |
119 |
> |
if ( i == ndau - 1 ) prepre += " "; |
120 |
> |
else prepre += "| "; |
121 |
> |
printDecay( * d, prepre ); |
122 |
> |
} |
123 |
> |
} |
124 |
> |
} |
125 |
|
|