ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Morgan/src/ParticleTreeDrawer.cc
(Generate patch)

Comparing UserCode/Morgan/src/ParticleTreeDrawer.cc (file contents):
Revision 1.2 by lethuill, Thu Oct 30 16:17:40 2008 UTC vs.
Revision 1.6 by lethuill, Wed Jun 10 11:17:06 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines