HiggsAnalysis/HiggsAnalysis-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 "HiggsAnalysis/JetTruthTrackSelectorTool.h"
00028 #include "HiggsAnalysis/MyTruthTrackJetInfoContainer.h"
00029 #include "HiggsAnalysis/Vertex.h"
00030 
00031 // track particles //
00032 #include "Particle/TrackParticleContainer.h"
00033 
00034 #include <string>
00035 #include <vector>
00036 #include <cmath>
00037 #include <fstream>
00038 
00039 using namespace Rec;
00040 using namespace std;
00041 using namespace MPIHiggsAnalysis;
00042 
00043 JetTruthTrackSelectorTool::JetTruthTrackSelectorTool(const std::string& name,
00044                                      const std::string& type,
00045                                      const IInterface* parent)
00046   : JetAlgToolBase(name,type,parent)
00047     , m_ptMin(0.*MeV)
00048     , m_etaMax(5.)
00049     , m_includeMuons(true)
00050 {
00051   declareProperty("MinPt", m_ptMin);
00052   declareProperty("MaxEta", m_etaMax);
00053   declareProperty("IncludeMuons", m_includeMuons);
00054   declareProperty("OutputCollectionName",m_outputCollectionName);
00055 
00056   m_ignore_multivertices = false;
00057   declareProperty("ignoreMultivertices", m_ignore_multivertices);
00058 
00059   m_Et_min_vert = 1000*MeV;
00060   declareProperty("MinEtVertex", m_Et_min_vert);
00061 
00062   m_vertex_cut = 0.1*mm;
00063   declareProperty("VertexCut", m_vertex_cut);
00064 
00065   m_remove_isol_leptons = false;
00066   declareProperty("removeIsolLeptons", m_remove_isol_leptons);
00067 
00068   m_electron_cone   = 0.;
00069   m_electron_et_cut = 0.;
00070   m_muon_cone       = 0.;
00071   m_muon_et_cut     = 0.;
00072   m_inner_cone      = 0.01;
00073 
00074   declareProperty("electronCone",  m_electron_cone);
00075   declareProperty("electronEtCut", m_electron_et_cut);
00076   declareProperty("muonCone",      m_muon_cone);
00077   declareProperty("muonEtCut",     m_muon_et_cut);
00078   declareProperty("innerCone",     m_inner_cone);
00079 
00080 }
00081 
00082 JetTruthTrackSelectorTool::~JetTruthTrackSelectorTool()
00083 { }
00084 
00085 
00086 StatusCode JetTruthTrackSelectorTool::initialize()
00087 {
00088   MsgStream report(msgSvc(),name());
00089 
00090   // allocate StoreGate service
00091   StatusCode checkOut = service("StoreGateSvc",m_storeGate);
00092   if ( checkOut.isFailure() )
00093     {
00094       report << MSG::ERROR
00095              << "cannot allocate StoreGate service"
00096              << endreq;
00097       return checkOut;
00098     }
00099 
00100   // allocate truth service
00101   m_truthAccess = new GenAccessIO();
00102 
00103   // check
00104   if ( m_truthAccess == 0 )
00105     {
00106       report << MSG::ERROR
00107              << "cannot allocate GenAccessIO service."
00108              << endreq;
00109       return StatusCode::FAILURE;
00110     }
00111 
00112   return checkOut;
00113 }
00114 
00115 
00116 StatusCode JetTruthTrackSelectorTool::execute(JetAlgToolBase::jetcollection_t* 
00117                                       theJets)
00118 {
00119   // message service
00120   MsgStream report(msgSvc(),name());
00121   //static std::ofstream out_e("et_incone_truth_trkjets_e.txt");
00122   //static std::ofstream out_mu("et_incone_truth_trkjets_mu.txt");
00123   //static std::ofstream out("debug_truth.txt");
00124 //   const TrackParticleContainer *track_particle_container(0);
00125 //                                      // pointer to the track particles
00126 //      StatusCode sc(m_storeGate->retrieve(track_particle_container,
00127 //                                      "TrackParticleCandidate"));
00128 //      if (sc.isFailure() || track_particle_container==0) {
00129 //              report << MSG::ERROR
00130 //                      << "Could not retrieve TrackParticleContainer"
00131 //                      << endreq;
00132 //              return sc;
00133 //      }
00134 
00135   Hep3Vector primary_vertex(0.0, 0.0, 0.0); // primary vertex of highest
00136                                                   // momentum
00137 
00138   // retrieve truth
00139   std::vector<const HepMC::GenParticle*> theParticles;
00140   IsGenInteracting theGenInteracting ;
00141   StatusCode checkOut = 
00142     m_truthAccess->getMC(theParticles, &theGenInteracting );
00143 
00144   // check
00145   if ( theParticles.size() == 0 || checkOut.isFailure() )
00146     {
00147       report << MSG::WARNING
00148              << "no truth particles found in this event"
00149              << endreq;
00150       return StatusCode::SUCCESS;
00151     }
00152 
00153   // get the primary vertex with the highest Et sum
00154   //if (!m_ignore_multivertices) {
00155         primary_vertex = highest_Et_vertex(theParticles);
00156         //}
00157 
00158   // create new jet collection
00159   jetcollection_t* writeCollection = new jetcollection_t();
00160 
00161   // loop on particle
00162   std::vector<const HepMC::GenParticle*>::iterator 
00163     firstParticle = theParticles.begin();
00164   std::vector<const HepMC::GenParticle*>::iterator 
00165     lastParticle = theParticles.end();
00166 
00167   
00168   for( ; firstParticle != lastParticle; firstParticle++ )
00169   {
00170       // select particles
00171       HepLorentzVector particleKine = (*firstParticle)->momentum();
00172       HepPDT::ParticleID IDEN((*firstParticle)->pdg_id());
00173       
00174       if ( particleKine.perp() > m_ptMin                   &&
00175            fabs(particleKine.pseudoRapidity()) < m_etaMax  &&
00176            ( abs((*firstParticle)->pdg_id()) != 13 || m_includeMuons ) &&
00177            IDEN.threeCharge()!=0 && (*firstParticle)->production_vertex()!=0)
00178       {
00179       
00180           //static std::ofstream out("eta_truth_trkjets.txt");
00181           //out << particleKine.eta() <<"\t\t" << particleKine.et() << endl;
00182 
00183           //     out << particleKine.eta() << endl;
00184     
00185 //      double deltaR(1.0e99);
00186 //      Hep3Vector comp_vert;
00187 //      for (unsigned int k=0; k<track_particle_container->size(); k++) {
00188 //              HepLorentzVector p((*track_particle_container)[k]->hlv());
00189 //              if (p.deltaR(particleKine)<deltaR) {
00190 //                      deltaR = p.deltaR(particleKine);
00191 //                      comp_vert = (*track_particle_container
00192 //                                      )[k]->reconstructedVertex()->position()-
00193 //                                      (*firstParticle)->production_vertex(
00194 //                                      )->position().vect();
00195 //              }
00196 //      }
00197 //      out << deltaR << "\t" << comp_vert.x() << "\t" << comp_vert.y() << "\t"
00198 //              << comp_vert.z() << endl;
00199 
00200       
00201 
00202           //check isolation
00203           if ( m_remove_isol_leptons ){
00204 
00205               double etcone(0);
00206 
00207               //electrons
00208               if(abs((*firstParticle)->pdg_id()) == 11){
00209                   etcone = get_energy_in_cone( m_electron_cone, particleKine,
00210                                                (*firstParticle)->production_vertex()->position().vect(),
00211                                                theParticles
00212                                                ); 
00213                   
00214                   //out_e << etcone << endl;
00215                   if( etcone < m_electron_et_cut ){
00216                       continue;
00217                   }
00218                   
00219               }
00220               else if(abs((*firstParticle)->pdg_id()) == 13){
00221                   etcone = get_energy_in_cone( m_muon_cone, particleKine,
00222                                                (*firstParticle)->production_vertex()->position().vect(),
00223                                                theParticles
00224                                                );
00225                   //out_mu << etcone << endl;
00226                   if( etcone < m_muon_et_cut ){
00227                       continue;
00228                   }
00229                   
00230               }
00231           }
00232 
00233           
00234           // jet representing the particle
00235           if (m_ignore_multivertices) {
00236               writeCollection->push_back(new Jet(particleKine));
00237           } else {
00238               Hep3Vector delta((*firstParticle)->production_vertex(
00239                                         )->position().vect()-primary_vertex);
00240               if ((*firstParticle)->production_vertex()!=0 &&
00241                   //                    (*firstParticle)->production_vertex(
00242                   //                                    )->position().vect()==primary_vertex) {
00243                   delta.mag()<m_vertex_cut) {
00244                   writeCollection->push_back(new Jet(particleKine));
00245               }
00246           }
00247       }
00248   }
00249   
00250   // hand collection to StoreGate (explicit copy of each object!)
00251   if ( ! JetCollectionHelper::record_jets(m_storeGate,writeCollection,
00252                                           m_outputCollectionName) )
00253     {
00254       report << MSG::ERROR
00255              << "cannot convert initial jet list to JetCollection"
00256              << endreq;
00257       return StatusCode::FAILURE;
00258     }
00259 
00260   // clean up
00261   JetCollectionHelper::remove_object(writeCollection,
00262                                      writeCollection->begin(),
00263                                      writeCollection->end());
00264   delete writeCollection;
00265 
00266   // retrieve collection from StoreGate to satisfy Jet constructor
00267   const INavigable4MomentumCollection* readCollection;
00268   checkOut = m_storeGate->retrieve(readCollection,m_outputCollectionName);
00269   if ( checkOut.isFailure() )
00270     {
00271       report << MSG::ERROR
00272              << "cannot read back initial JetCollection"
00273              << endreq;
00274       return checkOut;
00275     }
00276 
00277   // build intermediate jet list
00278   for ( Jet::index_type theIndex=0; theIndex < readCollection->size();
00279         theIndex++ )
00280     {
00281       // jet with storable particle constituent
00282       theJets->push_back(new Jet(readCollection,theIndex));
00283     }
00284 
00285   //cout << "size: " <<  theJets->size() << endl;
00286 
00287   return checkOut;
00288 }
00289 
00290 //*****************************************************************************
00291 
00292 //::::::::::::::::::::::::::::::
00293 //:: METHOD highest_Et_vertex ::
00294 //::::::::::::::::::::::::::::::
00295 
00296 Hep3Vector JetTruthTrackSelectorTool::highest_Et_vertex(
00297                         std::vector<const HepMC::GenParticle*> theParticles) {
00298 
00300 // VARIABLES //
00302 
00303     std::vector<Hep3Vector> vertex; // vector of vertices
00304     bool vertex_flag; // auxiliary vertex flag
00305     double max_Et; // maximum transerve vertex momentum found so far
00306     Hep3Vector primary_vertex(0.0, 0.0, 0.0); // auxiliary primary vertex
00307     std::vector<double> Et_vert; // Et(vertex)
00308     std::vector<unsigned> nb_part; // number of particles from the vertex
00309     std::vector<MPIHiggsAnalysis::Vertex> vertices;
00310     
00311     MyTruthTrackJetInfoContainer 
00312         *container(MyTruthTrackJetInfoContainer::getTruthTrackJetInfoContainer());
00313     // container for vertex data
00314     container->clear();
00315     container->setVertexCut(m_vertex_cut);
00316 
00317 
00319 // COLLECT ALL VERTICES //
00321 
00322 //      static std::ofstream out("debug_truth.txt");
00323     std::vector<const HepMC::GenParticle*>::iterator 
00324         firstParticle(theParticles.begin());
00325     std::vector<const HepMC::GenParticle*>::iterator 
00326         lastParticle(theParticles.end());
00327    
00328     for ( ; firstParticle != lastParticle; firstParticle++) {
00329         
00330         HepLorentzVector particleKine = (*firstParticle)->momentum();
00331         HepPDT::ParticleID IDEN((*firstParticle)->pdg_id());
00332        
00333         if (particleKine.perp()>m_Et_min_vert &&
00334             fabs(particleKine.pseudoRapidity())<m_etaMax &&
00335             (abs((*firstParticle)->pdg_id())!=13 || m_includeMuons)
00336             && IDEN.threeCharge()!=0
00337             && (*firstParticle)->production_vertex()!=0) {
00338             //          out << particleKine.et() << std::endl;
00339             
00340             vertex_flag = false;
00341             
00342             for (unsigned int l=0; l<vertex.size(); l++) {
00343                 
00344                 Hep3Vector delta( (*firstParticle)->production_vertex()
00345                                   ->position().vect()-vertex[l] );
00346                 if (delta.mag()<m_vertex_cut) {
00347                     Et_vert[l] += particleKine.et();
00348                     nb_part[l]++;
00349                     vertices[l].setEt( vertices[l].Et()+particleKine.et() );
00350                     vertices[l].setNumberOfTracks( vertices[l].numberOfTracks()+1 );
00351                     vertex_flag = true;
00352                     break;
00353                 }
00354             }
00355             if (!vertex_flag) {
00356                 vertex.push_back(
00357                                  (*firstParticle)->production_vertex(
00358                                                         )->position().vect());
00359                 Et_vert.push_back(particleKine.et());
00360                 nb_part.push_back(1);
00361                 
00362                 vertices.push_back(
00363                             MPIHiggsAnalysis::Vertex( (*firstParticle)->production_vertex()
00364                                                        ->position().vect(),
00365                                                        Hep3Vector(0.,0.,0.), 0., 0, 
00366                                                        particleKine.et(), 1) );
00367             
00368             }
00369         }
00370     }
00371 
00372     if (vertex.size()==0) { 
00373         return primary_vertex;
00374     }
00375 
00377 // REJECT THOSE VERTICES FROM WHICH ONLY ONE PARTICLE EMERGES //
00379 
00380 //      vector<Hep3Vector> new_vertex;
00381 //      vector<double> new_Et_vert;
00382 //      for (unsigned k=0; k<vertex.size(); k++) {
00383 //              if (nb_part[k]>1) {
00384 //                      new_vertex.push_back(vertex[k]);
00385 //                      new_Et_vert.push_back(Et_vert[k]);
00386 //              }
00387 //      }
00388 
00390 // GET THE PRIMARY VERTEX WITH THE HIGHEST TRANSVERSE MOMENTUM //
00392 
00393 
00394 //      static ofstream outfile("debug.txt");
00395 //      outfile << vertex.size() << "\t" << new_vertex.size() << endl;
00396 
00397     max_Et = Et_vert[0];
00398     primary_vertex = vertex[0];
00399     for (unsigned int k=1; k<vertex.size(); k++) {
00400         if (Et_vert[k]>max_Et) {
00401             max_Et = Et_vert[k];
00402             primary_vertex = vertex[k];
00403         }
00404     }
00405     
00406     container->setVertex(vertex);
00407     container->setEt(Et_vert);
00408     container->setNumberOfTracks(nb_part);
00409 
00410     container->setVertices(vertices);
00411     container->sortVerticesAscendingInEt();
00412 
00413     return primary_vertex;
00414 
00415 }
00416 //*****************************************************************************
00417 
00418 //:::::::::::::::::::::::::::::::
00419 //:: METHOD get_energy_in_cone ::
00420 //:::::::::::::::::::::::::::::::
00421 
00422 double 
00423 JetTruthTrackSelectorTool::get_energy_in_cone(const double & cone,
00424                                          const HepLorentzVector & p, 
00425                                          const Hep3Vector & vertex,
00426                                          vector<const HepMC::GenParticle*> theParticles) {
00427 
00428 //-------------
00429 // VARIABLES --
00430 //-------------
00431 
00432     double ET(0.);   // transverse energy in the cone
00433 
00434 //-------------------------------------------------------------
00435 // LOOP OVER THE TRACKS AND CALCULATE THE ENERGY IN THE CONE --
00436 //-------------------------------------------------------------
00437  
00438     for (unsigned int k=0; k<theParticles.size(); k++ ) {
00439      
00440         if(theParticles[k]->production_vertex()==NULL){
00441             continue;
00442         }
00443         
00444         if(theParticles[k]->momentum().eta()>m_etaMax){
00445             continue;
00446         }
00447 
00448         if (!m_ignore_multivertices){
00449             
00450             Hep3Vector delta = theParticles[k]
00451                 ->production_vertex()->position().vect()-vertex;
00452 
00453             //if ( theParticles[k]
00454             //   ->production_vertex()->position().vect()!=vertex) {
00455             if (delta.mag()>m_vertex_cut){
00456                 continue; 
00457             }
00458         }
00459     
00460         if ( p.deltaR(theParticles[k]->momentum()) < cone &&
00461              p.deltaR(theParticles[k]->momentum()) > m_inner_cone) {
00462             
00463             ET = ET+theParticles[k]->momentum().et();
00464         }
00465         
00466     }
00467 
00468 
00469     return ET;
00470     
00471 }
00472 
00473 

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