NtupleWriter13/NtupleWriter13-00-01-00/src/JetTruthTrackSelectorTool.cxx

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 //#include "JetSimTools/JetTruthTrackSelectorTool.h"
00026 //#include "JetSimTools/MyTruthTrackJetInfoContainer.h"
00027 #include "NtupleWriter13/JetTruthTrackSelectorTool.h"
00028 #include "NtupleWriter13/MyTruthTrackJetInfoContainer.h"
00029 
00030 // track particles //
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   // allocate StoreGate service
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   // allocate truth service
00099   m_truthAccess = new GenAccessIO();
00100 
00101   // check
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   // message service
00118   MsgStream report(msgSvc(),name());
00119   //static std::ofstream out_e("et_incone_truth_trkjets_e.txt");
00120   //static std::ofstream out_mu("et_incone_truth_trkjets_mu.txt");
00121   //static std::ofstream out("debug_truth.txt");
00122 //   const TrackParticleContainer *track_particle_container(0);
00123 //                                      // pointer to the track particles
00124 //      StatusCode sc(m_storeGate->retrieve(track_particle_container,
00125 //                                      "TrackParticleCandidate"));
00126 //      if (sc.isFailure() || track_particle_container==0) {
00127 //              report << MSG::ERROR
00128 //                      << "Could not retrieve TrackParticleContainer"
00129 //                      << endreq;
00130 //              return sc;
00131 //      }
00132 
00133   Hep3Vector primary_vertex(0.0, 0.0, 0.0); // primary vertex of highest
00134                                                   // momentum
00135 
00136   // retrieve truth
00137   std::vector<const HepMC::GenParticle*> theParticles;
00138   IsGenInteracting theGenInteracting ;
00139   StatusCode checkOut = 
00140     m_truthAccess->getMC(theParticles, &theGenInteracting );
00141 
00142   // check
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   // get the primary vertex with the highest Et sum
00152   //if (!m_ignore_multivertices) {
00153         primary_vertex = highest_Et_vertex(theParticles);
00154         //}
00155 
00156   // create new jet collection
00157   jetcollection_t* writeCollection = new jetcollection_t();
00158 
00159   // loop on particle
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       // select particles
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           //static std::ofstream out("eta_truth_trkjets.txt");
00179           //out << particleKine.eta() <<"\t\t" << particleKine.et() << endl;
00180 
00181           //     out << particleKine.eta() << endl;
00182     
00183 //      double deltaR(1.0e99);
00184 //      Hep3Vector comp_vert;
00185 //      for (unsigned int k=0; k<track_particle_container->size(); k++) {
00186 //              HepLorentzVector p((*track_particle_container)[k]->hlv());
00187 //              if (p.deltaR(particleKine)<deltaR) {
00188 //                      deltaR = p.deltaR(particleKine);
00189 //                      comp_vert = (*track_particle_container
00190 //                                      )[k]->reconstructedVertex()->position()-
00191 //                                      (*firstParticle)->production_vertex(
00192 //                                      )->position().vect();
00193 //              }
00194 //      }
00195 //      out << deltaR << "\t" << comp_vert.x() << "\t" << comp_vert.y() << "\t"
00196 //              << comp_vert.z() << endl;
00197 
00198       
00199 
00200           //check isolation
00201           if ( m_remove_isol_leptons ){
00202 
00203               double etcone(0);
00204 
00205               //electrons
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                   //out_e << etcone << endl;
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                   //out_mu << etcone << endl;
00224                   if( etcone < m_muon_et_cut ){
00225                       continue;
00226                   }
00227                   
00228               }
00229           }
00230 
00231           
00232           // jet representing the particle
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                   //                    (*firstParticle)->production_vertex(
00240                   //                                    )->position().vect()==primary_vertex) {
00241                   delta.mag()<m_vertex_cut) {
00242                   writeCollection->push_back(new Jet(particleKine));
00243               }
00244           }
00245       }
00246   }
00247   
00248   // hand collection to StoreGate (explicit copy of each object!)
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   // clean up
00259   JetCollectionHelper::remove_object(writeCollection,
00260                                      writeCollection->begin(),
00261                                      writeCollection->end());
00262   delete writeCollection;
00263 
00264   // retrieve collection from StoreGate to satisfy Jet constructor
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   // build intermediate jet list
00276   for ( Jet::index_type theIndex=0; theIndex < readCollection->size();
00277         theIndex++ )
00278     {
00279       // jet with storable particle constituent
00280       theJets->push_back(new Jet(readCollection,theIndex));
00281     }
00282 
00283   //cout << "size: " <<  theJets->size() << endl;
00284 
00285   return checkOut;
00286 }
00287 
00288 //*****************************************************************************
00289 
00290 //::::::::::::::::::::::::::::::
00291 //:: METHOD highest_Et_vertex ::
00292 //::::::::::::::::::::::::::::::
00293 
00294 Hep3Vector JetTruthTrackSelectorTool::highest_Et_vertex(
00295                         std::vector<const HepMC::GenParticle*> theParticles) {
00296 
00298 // VARIABLES //
00300 
00301         std::vector<Hep3Vector> vertex; // vector of vertices
00302         bool vertex_flag; // auxiliary vertex flag
00303         double max_Et; // maximum transerve vertex momentum found so far
00304         Hep3Vector primary_vertex(0.0, 0.0, 0.0); // auxiliary primary vertex
00305         std::vector<double> Et_vert; // Et(vertex)
00306         std::vector<unsigned> nb_part; // number of particles from the vertex
00307         MyTruthTrackJetInfoContainer *container(
00308                 MyTruthTrackJetInfoContainer::getTruthTrackJetInfoContainer());
00309                                                 // container for vertex data
00310         container->clear();
00311 
00313 // COLLECT ALL VERTICES //
00315 
00316 //      static std::ofstream out("debug_truth.txt");
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 //              out << particleKine.et() << std::endl;
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 //                              if ((*firstParticle)->production_vertex(
00336 //                                      )->position().vect()==vertex[l]) {
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 // REJECT THOSE VERTICES FROM WHICH ONLY ONE PARTICLE EMERGES //
00362 
00363 //      vector<Hep3Vector> new_vertex;
00364 //      vector<double> new_Et_vert;
00365 //      for (unsigned k=0; k<vertex.size(); k++) {
00366 //              if (nb_part[k]>1) {
00367 //                      new_vertex.push_back(vertex[k]);
00368 //                      new_Et_vert.push_back(Et_vert[k]);
00369 //              }
00370 //      }
00371 
00373 // GET THE PRIMARY VERTEX WITH THE HIGHEST TRANSVERSE MOMENTUM //
00375 
00376 
00377 //      static ofstream outfile("debug.txt");
00378 //      outfile << vertex.size() << "\t" << new_vertex.size() << endl;
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 //:: METHOD get_energy_in_cone ::
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 // VARIABLES --
00410 //-------------
00411 
00412     double ET(0.);   // transverse energy in the cone
00413 
00414 //-------------------------------------------------------------
00415 // LOOP OVER THE TRACKS AND CALCULATE THE ENERGY IN THE CONE --
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             //if ( theParticles[k]
00434             //   ->production_vertex()->position().vect()!=vertex) {
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 }

Generated on Tue Oct 21 11:50:43 2008 for NtupleAnalysis by  doxygen 1.5.1