All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
MCTruthJetEnergy.cc
Go to the documentation of this file.
1 #include "MCTruthJetEnergy.h"
2 
3 //#include <iostream>
4 //#include <vector>
5 #include <string>
6 #include <sstream>
7 #include <set>
8 
9 #include "UTIL/LCRelationNavigator.h"
10 #include <EVENT/LCCollection.h>
11 #include <EVENT/MCParticle.h>
12 #include <EVENT/ReconstructedParticle.h>
13 #include <gearimpl/Vector3D.h>
14 
15 #ifdef MARLIN_USE_AIDA
16 #include <marlin/AIDAProcessor.h>
17 #endif
18 
19 using namespace lcio ;
20 using namespace marlin ;
21 
23 
24 
25 MCTruthJetEnergy::MCTruthJetEnergy() : Processor("MCTruthJetEnergy") {
26 
27  // modify processor description
28  _description = "MCTruthJetEnergy calculates the MC truth jet energy of the MCParticles "
29  "associated to the reconstructed particles in the jet" ;
30 
31 
32  // register steering parameters: name, description, class-variable, default value
33 
34  StringVec jCols ;
35  jCols.push_back("FTSelected_2Jets") ;
36  jCols.push_back("FTSelected_3Jets") ;
37  jCols.push_back("FTSelected_4Jets") ;
38  jCols.push_back("FTSelected_5Jets") ;
39  jCols.push_back("FTSelected_6Jets") ;
40 
41 
42  registerInputCollections( LCIO::RECONSTRUCTEDPARTICLE,
43  "JetCollectionNames" ,
44  "Names of the Jet collections" ,
45  _jetcolNames ,
46  jCols ) ;
47 
48  registerInputCollection( LCIO::LCRELATION,
49  "MCTruthRelationName" ,
50  "Name of the ReconstructedParticle-MCParticle relation collection" ,
51  _relName ,
52  std::string("RecoMCTruthLink") ) ;
53 
54 
55 }
56 
57 
59 
60  // usually a good idea to
61  printParameters() ;
62 
63  _nRun = 0 ;
64  _nEvt = 0 ;
65 
66 
67 #ifdef MARLIN_USE_AIDA
68 
69  // create histograms for jet energy resolution
70 
71  int nCol = _jetcolNames.size() ;
72 
73  _jetEnergyHists.resize( nCol ) ;
74 
75  for(int i=0 ; i < nCol ; i++ ) {
76 
77  std::stringstream str ;
78  str << _jetcolNames[i] << " _E " ;
79 
80  _jetEnergyHists[ i ] = AIDAProcessor::histogramFactory(this)->
81  createHistogram1D( str.str().c_str() , "jet energy / Gev ] ", 50 , 0. , 500. ) ;
82 
83  // createHistogram1D( str.str().c_str() , "jet energy resolution [ sigma_E/sqrt(E/Gev) ] ", 100 , ) ;
84  }
85 
86  _jetEnergyTruthReco = AIDAProcessor::histogramFactory(this)->
87  createHistogram2D( "EJetTruthVsRec ", " jet energy Reconstructed vs MCTruth",
88  500 , 0. , 300. ,
89  500 , 0. , 300. ) ;
90 
91 
92 #endif
93 
94 }
95 
96 void MCTruthJetEnergy::processRunHeader( LCRunHeader* /*run*/) {
97 
98  _nRun++ ;
99 }
100 
101 void MCTruthJetEnergy::processEvent( LCEvent * evt ) {
102 
103 
104  int nCol = _jetcolNames.size() ;
105 
106  for( int i=0 ; i < nCol ; ++i ){ // loop over jet collections
107 
108  LCCollection* jetcol = 0 ;
109 
110  try {
111 
112  jetcol = evt->getCollection( _jetcolNames[i] ) ;
113  }
114 
115  catch( lcio::DataNotAvailableException& ){
116 
117  streamlog_out( WARNING ) << " collection " << _jetcolNames[i] << " not found ! " << std::endl ;
118 
119  continue ; // try next collection name
120  }
121 
122 
123  LCCollection* relcol = evt->getCollection( _relName ) ;
124 
125 
126  int nJETS = jetcol->getNumberOfElements() ;
127 
128  streamlog_out(DEBUG1) << " found " << nJETS << " jets in event "
129  << evt->getEventNumber() << " in run " << evt->getRunNumber()
130  << std::endl ;
131 
132 
133  LCRelationNavigator rel(relcol) ;
134 
135 
136  // loop over jets in this collection
137 
138  FloatVec jetEnergies ;
139 
140  for(int j=0; j< nJETS ; j++){
141 
142  ReconstructedParticle* jet = dynamic_cast<ReconstructedParticle*>( jetcol->getElementAt( j ) ) ;
143 
144 
145  streamlog_out(DEBUG0) << " jet energy = " << jet->getEnergy() << std::endl ;
146 
147  int nPart = jet->getParticles().size();
148 
149 
150  // save all MCParticles used in this jet in a set (to avoid double counting)
151  std::set< MCParticle* > mcpset;
152  std::set< MCParticle* >::iterator mcpsetiter;
153 
154  for(int k=0; k< nPart ; k++){
155 
156  const LCObjectVec& mcpvec = rel.getRelatedToObjects( jet->getParticles()[k] ) ;
157 
158 
159  if( mcpvec.size() > 0 && mcpvec[0] != 0 ){
160 
161  MCParticle* mcp = dynamic_cast<MCParticle*>( mcpvec[0] ) ;
162 
163  if( mcp != 0 )
164  mcpset.insert( mcp );
165  }
166  else {
167 
168  ReconstructedParticle* p = dynamic_cast<ReconstructedParticle*>( jet->getParticles()[k] ) ;
169 
170  gear::Vector3D v( p->getMomentum()[0] , p->getMomentum()[1] , p->getMomentum()[2] ) ;
171 
172  streamlog_out(DEBUG) << " found broken MCParticle link for particle [E:"
173  << p->getEnergy() << "]"
174  << " theta: " << ( v.theta() * 180. / 3.141592 )
175  << " charge: " << p->getCharge()
176  << " nDaught: " << p->getParticles().size()
177  << std::endl ;
178 
179 
180  }
181  }
182 
183  double mcJetEnergy = 0;
184 
185  for ( mcpsetiter = mcpset.begin( ); mcpsetiter != mcpset.end( ); mcpsetiter++ ){
186 
187  mcJetEnergy += (*mcpsetiter)->getEnergy();
188  }
189 
190  streamlog_out(DEBUG0) << "MC particle jet energy:" << mcJetEnergy << std::endl ;
191 
192  jetEnergies.push_back( mcJetEnergy ) ;
193 
194  }
195 
196  // save the mc truth jet energies in the collection (same order as particles/jets )
197 
198  jetcol->parameters().setValues( "MCTruthJetEnergies", jetEnergies ) ;
199 
200  } // end loop over jet collections
201 
202 
203  if (_nEvt % 100 == 0){
204 
205  streamlog_out(MESSAGE2) << "processed event/run " << evt->getEventNumber()
206  << " / " << evt->getRunNumber()
207  << std::endl ;
208  }
209 
210 
211  _nEvt ++ ;
212 }
213 
214 
215 
216 void MCTruthJetEnergy::check( LCEvent * evt ) {
217 
218 #ifdef MARLIN_USE_AIDA
219 
220  // hMCPEnergy =
221  // AIDAProcessor::histogramFactory(this)->
222  // createCloud1D( "hMCPEnergy", "energy of the MCParticles", 100 ) ;
223 
224  int nCol = _jetcolNames.size() ;
225 
226  for( int i=0 ; i < nCol ; ++i ) { // loop over jet collections
227 
228  LCCollection* jetcol = 0 ;
229 
230  try {
231 
232  jetcol = evt->getCollection( _jetcolNames[i] ) ;
233  }
234 
235  catch( lcio::DataNotAvailableException& ){
236 
237  streamlog_out( WARNING ) << " collection " << _jetcolNames[i] << " not found ! " << std::endl ;
238 
239  continue ; // try next collection name
240  }
241 
242  // AIDA::IHistogram1D* _eHist = _jetEnergyHists[ i ] ;
243 
244 
245  FloatVec jetEnergies ;
246  jetcol->getParameters().getFloatVals( "MCTruthJetEnergies" , jetEnergies) ;
247 
248  int nJETS = jetcol->getNumberOfElements() ;
249 
250  if( jetEnergies.size() != unsigned( nJETS ) ){
251 
252  streamlog_out( WARNING ) << " wrong number of jet energy values in 'MCTruthJetEnergies' : "
253  << jetEnergies.size() << " with " << nJETS << " jets in the collection ! "
254  << std::endl ;
255 
256  continue ;
257  }
258 
259  for(int j=0; j< nJETS ; j++){
260 
261  ReconstructedParticle* jet = dynamic_cast<ReconstructedParticle*>( jetcol->getElementAt( j ) ) ;
262 
263 
264  _jetEnergyHists[ i ]->fill( jet->getEnergy() ) ;
265 
266  _jetEnergyTruthReco->fill( jetEnergies[j] , jet->getEnergy() ) ;
267 
268  }
269 
270 
271  }
272 #endif
273 
274 
275 }
276 
277 
279 
280 }
virtual void check(LCEvent *evt)
CLHEP::Hep3Vector Vector3D
std::string _relName
virtual void processEvent(LCEvent *evt)
Called for every event - the working horse.
static const float k
Calculates the true jet energy from MCParticles.
MCTruthJetEnergy aMCTruthJetEnergy
StringVec _jetcolNames
Input collection name.
virtual void end()
Called after data processing for clean up.
virtual void processRunHeader(LCRunHeader *run)
Called for every run.
virtual void init()
Called at the begin of the job before anything is read.
std::vector< std::string > StringVec
Definition: SiStripClus.h:56