00001
00002 #include "GaudiKernel/Service.h"
00003 #include "GaudiKernel/MsgStream.h"
00004
00005 #include "StoreGate/StoreGateSvc.h"
00006
00007 #include "NavFourMom/INavigable4MomentumCollection.h"
00008
00009 #include "HepMC/GenParticle.h"
00010
00011 #include "TruthHelper/IsGenInteracting.h"
00012 #include "TruthHelper/GenAccessIO.h"
00013
00014 #include "CLHEP/Vector/LorentzVector.h"
00015 #include "CLHEP/Units/SystemOfUnits.h"
00016 #include "CLHEP/HepPDT/ParticleID.hh"
00017 #include "CLHEP/Vector/ThreeVector.h"
00018
00019 #include "JetUtils/JetCollectionHelper.h"
00020
00021 #include "JetEvent/Jet.h"
00022 #include "JetEvent/JetCollection.h"
00023
00024 #include "JetRec/JetAlgToolBase.h"
00025
00026
00027 #include "NtupleWriter13/JetTruthTrackSelectorTool.h"
00028 #include "NtupleWriter13/MyTruthTrackJetInfoContainer.h"
00029
00030
00031 #include "Particle/TrackParticleContainer.h"
00032
00033 #include <string>
00034 #include <vector>
00035 #include <cmath>
00036 #include <fstream>
00037
00038 using namespace Rec;
00039 using namespace std;
00040
00041 JetTruthTrackSelectorTool::JetTruthTrackSelectorTool(const std::string& name,
00042 const std::string& type,
00043 const IInterface* parent)
00044 : JetAlgToolBase(name,type,parent)
00045 , m_ptMin(0.*MeV)
00046 , m_etaMax(5.)
00047 , m_includeMuons(true)
00048 {
00049 declareProperty("MinPt", m_ptMin);
00050 declareProperty("MaxEta", m_etaMax);
00051 declareProperty("IncludeMuons", m_includeMuons);
00052 declareProperty("OutputCollectionName",m_outputCollectionName);
00053
00054 m_ignore_multivertices = false;
00055 declareProperty("ignoreMultivertices", m_ignore_multivertices);
00056
00057 m_Et_min_vert = 1000*MeV;
00058 declareProperty("MinEtVertex", m_Et_min_vert);
00059
00060 m_vertex_cut = 0.1*mm;
00061 declareProperty("VertexCut", m_vertex_cut);
00062
00063 m_remove_isol_leptons = false;
00064 declareProperty("removeIsolLeptons", m_remove_isol_leptons);
00065
00066 m_electron_cone = 0.;
00067 m_electron_et_cut = 0.;
00068 m_muon_cone = 0.;
00069 m_muon_et_cut = 0.;
00070 m_inner_cone = 0.01;
00071
00072 declareProperty("electronCone", m_electron_cone);
00073 declareProperty("electronEtCut", m_electron_et_cut);
00074 declareProperty("muonCone", m_muon_cone);
00075 declareProperty("muonEtCut", m_muon_et_cut);
00076 declareProperty("innerCone", m_inner_cone);
00077
00078 }
00079
00080 JetTruthTrackSelectorTool::~JetTruthTrackSelectorTool()
00081 { }
00082
00083
00084 StatusCode JetTruthTrackSelectorTool::initialize()
00085 {
00086 MsgStream report(msgSvc(),name());
00087
00088
00089 StatusCode checkOut = service("StoreGateSvc",m_storeGate);
00090 if ( checkOut.isFailure() )
00091 {
00092 report << MSG::ERROR
00093 << "cannot allocate StoreGate service"
00094 << endreq;
00095 return checkOut;
00096 }
00097
00098
00099 m_truthAccess = new GenAccessIO();
00100
00101
00102 if ( m_truthAccess == 0 )
00103 {
00104 report << MSG::ERROR
00105 << "cannot allocate GenAccessIO service."
00106 << endreq;
00107 return StatusCode::FAILURE;
00108 }
00109
00110 return checkOut;
00111 }
00112
00113
00114 StatusCode JetTruthTrackSelectorTool::execute(JetAlgToolBase::jetcollection_t*
00115 theJets)
00116 {
00117
00118 MsgStream report(msgSvc(),name());
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 Hep3Vector primary_vertex(0.0, 0.0, 0.0);
00134
00135
00136
00137 std::vector<const HepMC::GenParticle*> theParticles;
00138 IsGenInteracting theGenInteracting ;
00139 StatusCode checkOut =
00140 m_truthAccess->getMC(theParticles, &theGenInteracting );
00141
00142
00143 if ( theParticles.size() == 0 || checkOut.isFailure() )
00144 {
00145 report << MSG::WARNING
00146 << "no truth particles found in this event"
00147 << endreq;
00148 return StatusCode::SUCCESS;
00149 }
00150
00151
00152
00153 primary_vertex = highest_Et_vertex(theParticles);
00154
00155
00156
00157 jetcollection_t* writeCollection = new jetcollection_t();
00158
00159
00160 std::vector<const HepMC::GenParticle*>::iterator
00161 firstParticle = theParticles.begin();
00162 std::vector<const HepMC::GenParticle*>::iterator
00163 lastParticle = theParticles.end();
00164
00165
00166 for( ; firstParticle != lastParticle; firstParticle++ )
00167 {
00168
00169 HepLorentzVector particleKine = (*firstParticle)->momentum();
00170 HepPDT::ParticleID IDEN((*firstParticle)->pdg_id());
00171
00172 if ( particleKine.perp() > m_ptMin &&
00173 fabs(particleKine.pseudoRapidity()) < m_etaMax &&
00174 ( abs((*firstParticle)->pdg_id()) != 13 || m_includeMuons ) &&
00175 IDEN.threeCharge()!=0 && (*firstParticle)->production_vertex()!=0)
00176 {
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 if ( m_remove_isol_leptons ){
00202
00203 double etcone(0);
00204
00205
00206 if(abs((*firstParticle)->pdg_id()) == 11){
00207 etcone = get_energy_in_cone( m_electron_cone, particleKine,
00208 (*firstParticle)->production_vertex()->position().vect(),
00209 theParticles
00210 );
00211
00212
00213 if( etcone < m_electron_et_cut ){
00214 continue;
00215 }
00216
00217 }
00218 else if(abs((*firstParticle)->pdg_id()) == 13){
00219 etcone = get_energy_in_cone( m_muon_cone, particleKine,
00220 (*firstParticle)->production_vertex()->position().vect(),
00221 theParticles
00222 );
00223
00224 if( etcone < m_muon_et_cut ){
00225 continue;
00226 }
00227
00228 }
00229 }
00230
00231
00232
00233 if (m_ignore_multivertices) {
00234 writeCollection->push_back(new Jet(particleKine));
00235 } else {
00236 Hep3Vector delta((*firstParticle)->production_vertex(
00237 )->position().vect()-primary_vertex);
00238 if ((*firstParticle)->production_vertex()!=0 &&
00239
00240
00241 delta.mag()<m_vertex_cut) {
00242 writeCollection->push_back(new Jet(particleKine));
00243 }
00244 }
00245 }
00246 }
00247
00248
00249 if ( ! JetCollectionHelper::record_jets(m_storeGate,writeCollection,
00250 m_outputCollectionName) )
00251 {
00252 report << MSG::ERROR
00253 << "cannot convert initial jet list to JetCollection"
00254 << endreq;
00255 return StatusCode::FAILURE;
00256 }
00257
00258
00259 JetCollectionHelper::remove_object(writeCollection,
00260 writeCollection->begin(),
00261 writeCollection->end());
00262 delete writeCollection;
00263
00264
00265 const INavigable4MomentumCollection* readCollection;
00266 checkOut = m_storeGate->retrieve(readCollection,m_outputCollectionName);
00267 if ( checkOut.isFailure() )
00268 {
00269 report << MSG::ERROR
00270 << "cannot read back initial JetCollection"
00271 << endreq;
00272 return checkOut;
00273 }
00274
00275
00276 for ( Jet::index_type theIndex=0; theIndex < readCollection->size();
00277 theIndex++ )
00278 {
00279
00280 theJets->push_back(new Jet(readCollection,theIndex));
00281 }
00282
00283
00284
00285 return checkOut;
00286 }
00287
00288
00289
00290
00291
00292
00293
00294 Hep3Vector JetTruthTrackSelectorTool::highest_Et_vertex(
00295 std::vector<const HepMC::GenParticle*> theParticles) {
00296
00298
00300
00301 std::vector<Hep3Vector> vertex;
00302 bool vertex_flag;
00303 double max_Et;
00304 Hep3Vector primary_vertex(0.0, 0.0, 0.0);
00305 std::vector<double> Et_vert;
00306 std::vector<unsigned> nb_part;
00307 MyTruthTrackJetInfoContainer *container(
00308 MyTruthTrackJetInfoContainer::getTruthTrackJetInfoContainer());
00309
00310 container->clear();
00311
00313
00315
00316
00317 std::vector<const HepMC::GenParticle*>::iterator firstParticle(
00318 theParticles.begin());
00319 std::vector<const HepMC::GenParticle*>::iterator lastParticle(
00320 theParticles.end());
00321 for ( ; firstParticle != lastParticle; firstParticle++) {
00322 HepLorentzVector particleKine = (*firstParticle)->momentum();
00323 HepPDT::ParticleID IDEN((*firstParticle)->pdg_id());
00324 if (particleKine.perp()>m_Et_min_vert &&
00325 fabs(particleKine.pseudoRapidity())<m_etaMax &&
00326 (abs((*firstParticle)->pdg_id())!=13 || m_includeMuons)
00327 && IDEN.threeCharge()!=0
00328 && (*firstParticle)->production_vertex()!=0) {
00329
00330 vertex_flag = false;
00331 for (unsigned int l=0; l<vertex.size(); l++) {
00332 Hep3Vector delta(
00333 (*firstParticle)->production_vertex(
00334 )->position().vect()-vertex[l]);
00335
00336
00337 if (delta.mag()<m_vertex_cut) {
00338 Et_vert[l] = Et_vert[l]+
00339 particleKine.et();
00340 nb_part[l]++;
00341 vertex_flag = true;
00342 break;
00343 }
00344 }
00345 if (!vertex_flag) {
00346 vertex.push_back(
00347 (*firstParticle)->production_vertex(
00348 )->position().vect());
00349 Et_vert.push_back(particleKine.et());
00350 nb_part.push_back(1);
00351 }
00352 }
00353 }
00354
00355 if (vertex.size()==0) {
00356 return primary_vertex;
00357 }
00358
00360
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00373
00375
00376
00377
00378
00379
00380 max_Et = Et_vert[0];
00381 primary_vertex = vertex[0];
00382 for (unsigned int k=1; k<vertex.size(); k++) {
00383 if (Et_vert[k]>max_Et) {
00384 max_Et = Et_vert[k];
00385 primary_vertex = vertex[k];
00386 }
00387 }
00388
00389 container->setVertex(vertex);
00390 container->setEt(Et_vert);
00391 container->setNumberOfTracks(nb_part);
00392
00393 return primary_vertex;
00394
00395 }
00396
00397
00398
00399
00400
00401
00402 double
00403 JetTruthTrackSelectorTool::get_energy_in_cone(const double & cone,
00404 const HepLorentzVector & p,
00405 const Hep3Vector & vertex,
00406 vector<const HepMC::GenParticle*> theParticles) {
00407
00408
00409
00410
00411
00412 double ET(0.);
00413
00414
00415
00416
00417
00418 for (unsigned int k=0; k<theParticles.size(); k++ ) {
00419
00420 if(theParticles[k]->production_vertex()==NULL){
00421 continue;
00422 }
00423
00424 if(theParticles[k]->momentum().eta()>m_etaMax){
00425 continue;
00426 }
00427
00428 if (!m_ignore_multivertices){
00429
00430 Hep3Vector delta = theParticles[k]
00431 ->production_vertex()->position().vect()-vertex;
00432
00433
00434
00435 if (delta.mag()>m_vertex_cut){
00436 continue;
00437 }
00438 }
00439
00440 if ( p.deltaR(theParticles[k]->momentum()) < cone &&
00441 p.deltaR(theParticles[k]->momentum()) > m_inner_cone) {
00442
00443 ET = ET+theParticles[k]->momentum().et();
00444 }
00445
00446 }
00447
00448
00449 return ET;
00450
00451 }