NtupleWriter14/src/JetTrackSelectorTool.cxx

00001 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00002 // 17.07.2007, AUTHOR: OLIVER KORTNER
00003 // Modified: 29.07.2007 by O. Kortner, options for a special treatment of
00004 //                                     multivertex events.
00005 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00006 
00007 //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00008 //:: IMPLEMENTATION OF METHODS DEFINED IN THE CLASS JetTrackSelectorTool ::
00009 //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00010 
00011 //::::::::::::::::::
00012 //:: HEADER FILES ::
00013 //::::::::::::::::::
00014 
00015 // standard C++ //
00016 #include <iostream>
00017 #include <fstream>
00018 
00019 // Gaudi //
00020 #include "GaudiKernel/Service.h"
00021 #include "GaudiKernel/MsgStream.h"
00022 
00023 // track particles //
00024 #include "Particle/TrackParticleContainer.h"
00025 #include "egammaEvent/ElectronContainer.h"
00026 #include "egammaEvent/egammaParamDefs.h"
00027 #include "muonEvent/MuonContainer.h"
00028 
00029 // jet reconstruction //
00030 #include "JetUtils/JetCollectionHelper.h"
00031 #include "JetEvent/Jet.h"
00032 #include "JetEvent/JetCollection.h"
00033 #include "JetRec/JetAlgToolBase.h"
00034 
00035 //#include "JetRecTools/JetTrackSelectorTool.h"
00036 #include "NtupleWriter14/JetTrackSelectorTool.h"
00037 //#include "TrkEventPrimitives/FitQuality.h"
00038 
00039 //::::::::::::::::::::::::
00040 //:: NAMESPACE SETTINGS ::
00041 //::::::::::::::::::::::::
00042 
00043 using namespace std;
00044 using namespace Rec;
00045 
00046 //*****************************************************************************
00047 
00048 //:::::::::::::::::
00049 //:: CONSTRUCTOR ::
00050 //:::::::::::::::::
00051 
00052 JetTrackSelectorTool::JetTrackSelectorTool(const std::string & name,
00053         const std::string & type,
00054         const IInterface * parent) : JetAlgToolBase(name, type, parent) {
00055 
00057 // JOB OPTIONS //
00059 
00060         m_pt_min = 0.0; // no cut on the transverse momentum of a track by
00061                         // default
00062         declareProperty("pTMin", m_pt_min);
00063 
00064         m_output_collection_name = string("TrackParticlesForJets");
00065         declareProperty("outputCollectionName", m_output_collection_name);
00066 
00067         m_track_particle_container_name = string("TrackParticleCandidate");
00068         declareProperty("TrackParticleContainer",
00069                                         m_track_particle_container_name);
00070 
00071         m_ignore_multivertices = false;
00072         declareProperty("ignoreMultivertices", m_ignore_multivertices);
00073 
00074         m_primary_vertex_container = string("VxPrimaryCandidate");
00075         declareProperty("vertexContainer", m_primary_vertex_container);
00076 
00077         m_vert_coll_svc_name = string("MPIHiggsAnalysis::VertexCollectionSvc");
00078         declareProperty("VertexCollectionSvc", m_vert_coll_svc_name);
00079 
00080         m_electron_container = string("ElectronCollection");
00081         declareProperty("electronContainer", m_electron_container);
00082 
00083         m_muon_container = string("StacoMuonCollection");
00084         declareProperty("muonContainer", m_muon_container);
00085 
00086         m_remove_isol_leptons = false;
00087         declareProperty("removeIsolLeptons", m_remove_isol_leptons);
00088 
00089         m_electron_cone   = 0.;
00090         m_electron_et_cut = 0.;
00091         m_muon_cone       = 0.;
00092         m_muon_et_cut     = 0.;
00093         m_inner_cone      = 0.01;
00094 
00095         declareProperty("electronCone",  m_electron_cone);
00096         declareProperty("electronEtCut", m_electron_et_cut);
00097         declareProperty("muonCone",      m_muon_cone);
00098         declareProperty("muonEtCut",     m_muon_et_cut);
00099         declareProperty("innerCone",     m_inner_cone);
00100         
00101 
00103 // RESET POINTERS //
00105 
00106         m_storeGate = 0;
00107         m_log = 0;
00108         m_vert_coll = 0;
00109 
00110 }
00111 
00112 //*****************************************************************************
00113 
00114 //::::::::::::::::
00115 //:: DESTRUCTOR ::
00116 //::::::::::::::::
00117 
00118 JetTrackSelectorTool::~JetTrackSelectorTool(void) {}
00119 
00120 //*****************************************************************************
00121 
00122 //:::::::::::::::::::::::
00123 //:: METHOD initialize ::
00124 //:::::::::::::::::::::::
00125 
00126 StatusCode JetTrackSelectorTool::initialize(void) {
00127 
00129 // VARIABLES //
00131 
00132         m_log = new MsgStream(msgSvc(), name()); // message service
00133 
00135 // MESSAGE //
00137 
00138         *m_log << MSG::INFO << "Initializing tool..." << endreq;
00139 
00141 // SET POINTERS //
00143 
00144 // store gate //
00145         StatusCode sc(service("StoreGateSvc", m_storeGate));
00146         if (!sc.isSuccess()) {
00147                 *m_log << MSG::FATAL << "Cannot retrieve StoreGateSvc!"
00148                         << endreq;
00149                 return sc;
00150         }
00151 
00152 // VertexCollectionSvc //
00153         sc = service(m_vert_coll_svc_name, m_vert_coll);
00154         if (!sc.isSuccess()) {
00155                 *m_log << MSG::FATAL << "Cannot retrieve "
00156                         << m_vert_coll_svc_name << "!" << endreq;
00157                 return sc;
00158         }
00159 
00160         if(m_remove_isol_leptons){
00161             *m_log << MSG::INFO 
00162                    << "Ignore tracks wich overlap with an isolated lepton" 
00163                    << endreq
00164                    << "Isolation: " << endreq
00165                    << "  electron: etcone(" <<  m_electron_cone << ") < " 
00166                    <<  m_electron_et_cut << endreq
00167                    << "  muon:     etcone(" <<  m_muon_cone << ") < " 
00168                    <<  m_muon_et_cut << endreq;
00169         }
00170 
00171         return StatusCode::SUCCESS;
00172 
00173 }
00174 
00175 //*****************************************************************************
00176 
00177 //::::::::::::::::::::
00178 //:: METHOD execute ::
00179 //::::::::::::::::::::
00180 
00181 StatusCode JetTrackSelectorTool::execute(
00182                                 JetAlgToolBase::jetcollection_t * theJets) {
00183 
00185 // VARIABLES //
00187 
00188     // static std::ofstream out_e("et_incone_trkjets_e.txt");
00189     //static std::ofstream out_mu("et_incone_trkjets_mu.txt");
00190 
00191         const TrackParticleContainer *track_particle_container(0);
00192                                         // pointer to the track particles
00193 
00194         const ElectronContainer *electron_container(0); 
00195                                         // container with reconstructed electrons
00196 
00197         const Analysis::MuonContainer *muon_container(0); 
00198                                         // container with reconstructed muons
00199 
00200         jetcollection_t* writeCollection(0); // output jet collection
00201         Hep3Vector primary_vertex(0.0, 0.0, 0.0); // primary vertex of highest
00202                                                   // momentum
00203 
00204 
00206 // GET THE PRIMARY VERTEX OF HIGHEST TRANSVERSE MOMENTUM, IF REQUESTED //
00208 
00209         if (!m_ignore_multivertices) {
00210                 primary_vertex = highest_pt_vertex();
00211         }
00212 
00213 
00215 // GET THE POINTER TO THE PARTICLE CONTAINER //
00217 
00218         StatusCode sc(m_storeGate->retrieve(track_particle_container,
00219                                         m_track_particle_container_name));
00220         if (sc.isFailure() || track_particle_container==0) {
00221                 *m_log << MSG::WARNING
00222                         << "No track particle container found in store gate!"
00223                         << endreq;
00224                 return sc;
00225         }
00226         *m_log << MSG::DEBUG
00227                 << "Track particle container retrieved successfully!"
00228                 << endreq;
00229 
00230 
00231         sc = m_storeGate->retrieve(electron_container, m_electron_container);
00232         if(sc.isFailure() || electron_container==0) {
00233             *m_log << MSG::WARNING
00234                    << "No electron container found in the store gate!"
00235                    << endreq;
00236             return false;
00237         }
00238         *m_log << MSG::DEBUG << "Electron container retrieved successfully!"
00239                << endreq;
00240 
00241 
00242         sc = m_storeGate->retrieve(muon_container, m_muon_container);
00243         if(sc.isFailure() || muon_container==0) {
00244             *m_log << MSG::WARNING
00245                    << "No muon container found in the store gate!"
00246                    << endreq;
00247             return false;
00248         }
00249         *m_log << MSG::DEBUG << "Muon container retrieved successfully!"
00250                << endreq;
00251 
00252     
00254 // CREATE A NEW JET COLLECTION (TO BE USED AS THE INPUT OF THE JET RECO.) //
00256 
00257         writeCollection = new jetcollection_t();
00258                                
00259         // build vectors containing the isolated leptons                         //
00260         // isolation is done by cutting on the et sum of the tracks in the cone  //
00261 
00262         //ElectronContainer *isol_electron_container = new ElectronContainer(); 
00263         vector<const Analysis::Electron*>       isol_electron_vector(0);
00264         vector<const Analysis::Muon*>           isol_muon_vector(0);
00265 
00266         if ( m_remove_isol_leptons ){
00267 
00268             //electrons
00269             for (unsigned int iE=0; iE<electron_container->size(); iE++) {
00270 
00271                 if ((*electron_container)[iE]
00272                     ->trackParticle()->reconstructedVertex() == NULL){
00273                     continue;
00274                 }
00275 
00276                 if (((*electron_container)[iE]->isem(egammaPID::ElectronLoose))!=0 ){
00277                   continue;
00278                 } 
00279 
00280                 //if (((*electron_container)[iE]->isem() & 0x7)!=0 ){
00281                 //continue;
00282                 //} 
00283  
00284                 // if ((*electron_container)[iE]->isEM()!=0 ){
00285 //                   continue;
00286 //                 } 
00287 
00288 
00289                 if ((*electron_container)[iE]->author()!=
00290                     egammaParameters::AuthorElectron &&
00291                     (*electron_container)[iE]->author()!=
00292                     egammaParameters::AuthorSofte) {
00293                     continue;
00294                 }
00295 
00296                 
00297                 if ((*electron_container)[iE]->trackParticle()==0) {  
00298                     continue;
00299                 }
00300 
00301                 if (!m_ignore_multivertices){
00302                 
00303                     if ((*electron_container)[iE]
00304                         ->trackParticle()->reconstructedVertex()->recVertex().position() != primary_vertex){
00305                     
00306                         continue; 
00307                     }
00308                 }
00309 
00310                 HepLorentzVector p((*electron_container)[iE]->hlv());
00311  
00312                 double etcone = get_energy_in_cone( m_electron_cone, p,
00313                                                     (*electron_container)[iE]->trackParticle()
00314                                                     ->reconstructedVertex()->recVertex().position(),
00315                                                     track_particle_container
00316                                                     );
00317                 //out_e << etcone << endl;
00318                 //Electron *electron = const_cast<Electron*>((*electron_container)[iE]);
00319             
00320                 if( etcone < m_electron_et_cut ){
00321                     //isol_electron_vector.push_back(electron);
00322                     isol_electron_vector.push_back((*electron_container)[iE]);
00323                 }
00324                 
00325             }
00326 
00327             //muons
00328             for (unsigned int iM=0; iM<muon_container->size(); iM++) {
00329 
00330                 // if (!(*muon_container)[iM]->hasCombinedMuonTrackParticle() || !(*muon_container)[iM]->isHighPt() 
00331 //                     || !(*muon_container)[iM]->bestMatch()) {
00332 //                     continue;
00333 //                 }
00334 
00335                 if( (*muon_container)[iM]->track()->reconstructedVertex()==NULL ){
00336                     continue;
00337                 } 
00338 
00339                 if (!m_ignore_multivertices){
00340                 
00341                   //   if( (*muon_container)[iM]->track()->reconstructedVertex()==NULL ){
00342 //                         continue;
00343 //                     } 
00344                     
00345                     if ((*muon_container)[iM]
00346                         ->track()->reconstructedVertex()->recVertex().position() != primary_vertex){
00347                     
00348                         continue; 
00349                     }
00350                 }
00351 
00352                 HepLorentzVector p((*muon_container)[iM]->hlv());
00353  
00354                 double etcone = get_energy_in_cone( m_muon_cone, p,
00355                                                     (*muon_container)[iM]->track()
00356                                                     ->reconstructedVertex()->recVertex().position(),
00357                                                     track_particle_container
00358                                                     );
00359                 
00360                 //out_mu << etcone << endl;
00361                 //Analysis::Muon *muon = const_cast<Analysis::Muon*>((*muon_container)[iM]);
00362             
00363                 if( etcone < m_muon_et_cut ){
00364                     //isol_muon_vector.push_back(muon);
00365                     isol_muon_vector.push_back((*muon_container)[iM]);
00366                 }
00367                 
00368             }
00369 
00370         }
00371 
00372         // loop over track particles and add them to the jet collection //
00373         // if they pass all the cuts                                       //
00374        
00375         //static std::ofstream out("eta_trkjets.txt");
00376         for (unsigned int k=0; k<track_particle_container->size(); k++) {
00377             
00378             if((*track_particle_container)[k]->reconstructedVertex() == NULL){
00379                     continue;
00380             }
00381 
00382             HepLorentzVector p((*track_particle_container)[k]->hlv());
00383             //const Trk::FitQuality *fq = (*track_particle_container)[k]->fitQuality();
00384             
00385             //out << p.eta() << "\t\t" <<  p.et() << endl;
00386                 //<< fq->chiSquared() << "\t\t" << fq->numberDoF()  << endl;
00387             
00388             
00389             //check pt
00390             if (p.perp()<m_pt_min) {
00391                 continue;
00392             }
00393               
00394             //check vertex
00395             if (!m_ignore_multivertices){
00396                 
00397                 if ((*track_particle_container)[k]
00398                     ->reconstructedVertex()->recVertex().position() != primary_vertex){
00399                 
00400                     continue;
00401                 }
00402             } 
00403 
00404            
00405             //check overlap with isol leptons
00406             if ( m_remove_isol_leptons ){
00407                 
00408                 bool overlap(false);
00409                 
00410                 //electrons
00411                 for (unsigned int iE=0; iE<isol_electron_vector.size(); iE++) {
00412                  
00413                     if( (*track_particle_container)[k] 
00414                         == isol_electron_vector[iE]->trackParticle() ){
00415                        
00416                         overlap = true;
00417                         break;
00418                     }
00419                 } 
00420                 
00421                 if(overlap){
00422                     continue;
00423                 }
00424 
00425                 overlap = false;
00426                 
00427                 //muons
00428                 for (unsigned int iM=0; iM<isol_muon_vector.size(); iM++) {
00429                  
00430                     if( (*track_particle_container)[k] 
00431                         == isol_muon_vector[iM]->inDetTrackParticle() ){
00432                         
00433                         overlap = true;
00434                         break;
00435                     }
00436                 } 
00437 
00438                 if(overlap){
00439                     continue;
00440                 }
00441 
00442             } //m_remove_isol_leptons
00443             
00444             
00445             writeCollection->push_back(new Jet(p));
00446 
00447         } //for track_particle_container 
00448 
00449 
00450 // record the collection in the store gate //
00451         if (!JetCollectionHelper::record_jets(m_storeGate, writeCollection,
00452                                                 m_output_collection_name)) {
00453                 *m_log << MSG::ERROR
00454                         << "Cannot convert initial list to JetCollection!"
00455                         << endreq;
00456                 return StatusCode::FAILURE;
00457         }
00458 
00459 // free memory //
00460         JetCollectionHelper::remove_object(writeCollection,
00461                                                 writeCollection->begin(),
00462                                                 writeCollection->end());
00463         delete writeCollection;
00464 
00465 // retrieve collection from StoreGate to satisfy Jet constructor //
00466         const INavigable4MomentumCollection *readCollection;
00467         sc = m_storeGate->retrieve(readCollection, m_output_collection_name);
00468         if (sc.isFailure()) {
00469                 *m_log << MSG::ERROR
00470                         << "Cannot read back initial jet collection!"
00471                         << endreq;
00472                 return sc;
00473         }
00474 
00475 // build intermediate jet list //
00476         for (Jet::index_type theIndex=0; theIndex < readCollection->size();
00477                                                                 theIndex++) {
00478                 theJets->push_back(new Jet(readCollection, theIndex));
00479         }
00480 
00481 
00482         return StatusCode::SUCCESS;
00483 
00484 }
00485 
00486 //*****************************************************************************
00487 
00488 //::::::::::::::::::::::::::::::
00489 //:: METHOD highest_pt_vertex ::
00490 //::::::::::::::::::::::::::::::
00491 
00492 Hep3Vector JetTrackSelectorTool::highest_pt_vertex(void) const {
00493 
00494         return m_vert_coll->getVertices()[
00495                                 m_vert_coll->getVertices().size()-1].position();
00496 
00497 // ///////////////
00498 // // VARIABLES //
00499 // ///////////////
00500 // 
00501 //      const Rec::TrackParticleContainer *track_particle_container(0);
00502 //                                      // pointer to the track particles
00503 //      const VxContainer *vertex_container(0); // pointer to the vertices
00504 //      double max_Et; // maximum transerve vertex momentum found so far
00505 //      Hep3Vector primary_vertex(0.0, 0.0, 0.0); // auxiliary primary vertex
00506 //      vector<MPIHiggsAnalysis::Vertex> vertices(m_vert_coll->getVertices());
00507 //                                                              // vertices
00508 //      static ofstream out("debug_jets.txt");
00509 // 
00510 // ///////////////////////////////////////////
00511 // // GET A POINTER TO THE VERTEX CONTAINER //
00512 // ///////////////////////////////////////////
00513 // 
00514 //      StatusCode sc(m_storeGate->retrieve(vertex_container,
00515 //                                              m_primary_vertex_container));
00516 //      if (sc.isFailure() || vertex_container==0) {
00517 //              *m_log << MSG::ERROR
00518 //                      << "No vertex container "
00519 //                      << m_primary_vertex_container
00520 //                      << " found in the store gate!"
00521 //                      << endreq;
00522 //              return primary_vertex;
00523 //      }
00524 // 
00525 //      *m_log << MSG::DEBUG
00526 //              << "Vertex container "
00527 //              << m_primary_vertex_container
00528 //              << " retrieved successfully!"
00529 //              << endreq;
00530 // 
00531 //      if (vertex_container->size()==0) {
00532 //              return primary_vertex;
00533 //      }
00534 // 
00535 // /////////////////////////////////////////////////////////////////////
00536 // // CALCULATE SUM OF TRANSVERSE ENERGIES EMERGING FROM THE VERTICES //
00537 // /////////////////////////////////////////////////////////////////////
00538 // 
00539 // // loop over the vertices //
00540 //      sc = (m_storeGate->retrieve(track_particle_container,
00541 //                                      m_track_particle_container_name));
00542 //      if (sc.isFailure() || track_particle_container==0) {
00543 //              *m_log << MSG::WARNING
00544 //                      << "No track particle container found in store gate!"
00545 //                      << endreq;
00546 //              return primary_vertex;
00547 //      }
00548 //      *m_log << MSG::DEBUG
00549 //              << "Track particle container retrieved successfully!"
00550 //              << endreq;
00551 // 
00552 //      vector<double> Et_vert(vertex_container->size(), 0.0); // Et(vertex)
00553 //      for (unsigned int k=0; k<track_particle_container->size(); k++) {
00554 //              for (unsigned int l=0; l<vertex_container->size(); l++) {
00555 //                      Trk::RecVertex vert((*vertex_container)[l]->recVertex());
00556 //                      if ((*track_particle_container
00557 //                              )[k]->reconstructedVertex()->position()==
00558 //                              vert.position()) {
00559 //                              Et_vert[l] = Et_vert[l]+
00560 //                                      (*track_particle_container)[k]->hlv(
00561 //                                                              ).et();
00562 //                      }
00563 //              }
00564 //      }
00565 // 
00566 // /////////////////////////////////////////////////////////////////
00567 // // GET THE PRIMARY VERTEX WITH THE HIGHEST TRANSVERSE MOMENTUM //
00568 // /////////////////////////////////////////////////////////////////
00569 //      
00570 //      if (Et_vert.size()>0) {
00571 //              max_Et = Et_vert[0];
00572 //      } else {
00573 //              max_Et = 0.0;
00574 //      }
00575 //      primary_vertex = ((*vertex_container)[0]->recVertex()).position();
00576 // 
00577 // // loop over the vertices //
00578 //      for (unsigned int k=1; k<vertex_container->size(); k++) {
00579 //              if (Et_vert[k]>max_Et) {
00580 //                      max_Et = Et_vert[k];
00581 //                      primary_vertex = ((*vertex_container)[k]->recVertex()
00582 //                                                              ).position();
00583 //              }
00584 //      }
00585 // 
00586 //      out << vertex_container->size()<< "\t"
00587 //              << m_vert_coll->getVertices().size() << "\t"
00588 //              << max_Et << "\t"
00589 //              << m_vert_coll->getVertices()[
00590 //                              m_vert_coll->getVertices().size()-1].Et()
00591 //              << "\t"
00592 //              << primary_vertex << "\t"
00593 //              << m_vert_coll->getVertices()[
00594 //                              m_vert_coll->getVertices().size()-1].position()
00595 //              << endl;
00596 // 
00597 //      return primary_vertex;
00598 
00599 }
00600 
00601 //*****************************************************************************
00602 
00603 //:::::::::::::::::::::::::::::::
00604 //:: METHOD get_energy_in_cone ::
00605 //:::::::::::::::::::::::::::::::
00606 
00607 double 
00608 JetTrackSelectorTool::get_energy_in_cone(const double & cone,
00609                                          const HepLorentzVector & p, 
00610                                          const Hep3Vector & vertex,
00611                                          const TrackParticleContainer *track_particle_container) {
00612 //-------------
00613 // VARIABLES --
00614 //-------------
00615 
00616     double ET(0.);   // transverse energy in the cone
00617 
00618 //-------------------------------------------------------------
00619 // LOOP OVER THE TRACKS AND CALCULATE THE ENERGY IN THE CONE --
00620 //-------------------------------------------------------------
00621 
00622     for (unsigned int k=0; k<track_particle_container->size(); k++) {
00623 
00624         if (!m_ignore_multivertices){
00625             
00626             if ( (*track_particle_container)[k]
00627                  ->reconstructedVertex()->recVertex().position()!=vertex) {
00628                 
00629                 continue; 
00630             }
00631         }
00632     
00633         if ( p.deltaR((*track_particle_container)[k]->hlv()) < cone && 
00634              p.deltaR((*track_particle_container)[k]->hlv()) > m_inner_cone) {
00635     
00636             ET = ET+(*track_particle_container)[k]->hlv().et();
00637         }
00638         
00639     }
00640     
00641 
00642     return ET;
00643     
00644 }
00645 
00646 

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