Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

ElectronSelector.cpp

Go to the documentation of this file.
00001 #include "caf_util/ElectronSelector.hpp"
00002 #include "caf_util/TRefFinder.hpp"
00003 #include "cafe/Config.hpp"
00004 #include <sstream>
00005 
00006 using namespace cafe ;
00007 using namespace std ;
00008 
00009 namespace {
00010   bool moreThan(const TMBEMCluster& a, const TMBEMCluster& b) {
00011     return a.Pt() > b.Pt();    
00012   }
00013 }
00014 
00015 namespace caf_util {
00016 
00017   ElectronSelector::ElectronSelector(const char *name) : 
00018     cafe::SelectUserObjects<TMBEMCluster>(name), _nselected(0), 
00019     _event(0), _finder(0)
00020   {
00021     cafe::Config config(name);
00022     
00023     _quality = config.get("Quality", "Tight");
00024     _ver = config.get("Version", 1);
00025     _lorun = config.get("FromRun", 0);
00026     _hirun = config.get("ToRun", 10000000);
00027     _nelectrons = config.get("nElectrons", 0);
00028     _nelectronsmax = config.get("nElectronsMax", -1);
00029     _sort = config.get("Sort", 0);
00030     _emidfilepath = config.get("SelectionFile","");
00031     
00032     // lets look for the input file: emid_cuts.txt
00033     // First in the user specified path:
00034     if ( _emidfilepath != "" )
00035       _emidfilepath = FindFile(_emidfilepath);
00036     
00037     // then in the workin area
00038     if ( _emidfilepath=="" )
00039       if (gSystem->Getenv("SRT_PRIVATE_CONTEXT") && strcmp(gSystem->Getenv("SRT_PRIVATE_CONTEXT"),".") != 0)
00040         _emidfilepath = FindFile(string(gSystem->Getenv("SRT_PRIVATE_CONTEXT"))+string("/emid_cuts/support/emid_cuts.txt"));
00041     
00042     if ( _emidfilepath=="" )
00043       _emidfilepath = FindFile("./emid_cuts/support/emid_cuts.txt");
00044     
00045     if ( _emidfilepath == "" )
00046       _emidfilepath = FindFile("./emid_cuts.txt");
00047     
00048     if ( _emidfilepath == "") {
00049       err() << "ElectronSelector(" << name << ")\nError: can not find the file \"emid_cuts.txt\". Please specify the correct path in your config file:\n   "
00050             << name << ".SelectionFile: path/emid_cuts.txt\n";
00051       throw std::runtime_error("Can not find emid_cuts file");
00052     }
00053     
00054     out() << "\n======== ElectronSelector("<<name<<") ==================\nUsing ElectronID criteria from \""<<_emidfilepath<<"\"\n";
00055     _id = new emID(_emidfilepath, _quality, _ver, _lorun, _hirun, 0);
00056     out() << "===================================================\n\n";
00057 
00058   
00059   addVariable("_id") ;
00060   if (_id->cutOnIsolation()) addVariable("_iso") ;
00061   if (_id->cutOnEMFraction()) addVariable("_EMfrac") ;
00062   if (_id->cutOnpT() ) {
00063     addVariable("fX") ;
00064     addVariable("fY") ;
00065   }
00066   bool etavar = false ;
00067   if ( _id->cutOnEtaCC() || _id->cutOnEtaECmin() || _id->cutOnEtaECmax()) {
00068     addVariable("_CalDetectorEta") ;
00069     etavar = true ;
00070   }
00071 
00072   bool hmx7var = false ;
00073   if (_id->cutOnHMx7CC()) {
00074     if (!etavar) {
00075       addVariable("_CalDetectorEta") ;
00076       etavar = true ;
00077     }
00078     addVariable("_HMx7") ;
00079     hmx7var = true ;
00080   }
00081 
00082   if (_id->cutOnHMx8EC()) {
00083     if (!etavar) {
00084       addVariable("_CalDetectorEta") ;
00085       etavar = true ;
00086     }
00087     addVariable("_HMx8") ;
00088   }
00089 
00090   if (_id->cutOnHMx7()) {
00091     if (!hmx7var) {
00092       addVariable("_HMx7") ;
00093       hmx7var = true ;
00094     }
00095   }
00096 
00097   if (_id->cutOnTrkMatchChi2EOP())  {
00098     addVariable("_TrMatchChi2ProbBest") ;
00099     addVariable("_SpatialTrMatchChi2ProbBest") ;
00100   } if (_id->cutOnTrkMatchChi2()) addVariable("_SpatialTrMatchChi2ProbBest") ;
00101 
00102 
00103   if (_id->cutOnLHood()) {
00104     addVariable("_Lhood8") ;
00105     addVariable("_chptr") ;
00106   }
00107   if (_id->cutOnTrackPt()) {
00108     addVariable("_chptr") ;
00109     if (_id->cutOnTrkMatchChi2EOP() ) addVariable("_chptrBest") ;
00110     _finder = new TRefFinder() ;
00111     _finder->SetVariablesTrack(Variables("fUniqueID", "fX", "fY")) ;
00112   }
00113   _id->saveCuts() ;
00114   }
00115 
00116   std::string ElectronSelector::FindFile(std::string path) {
00117     ifstream emid_file;
00118     emid_file.open(path.c_str());
00119     
00120     if (!emid_file) {
00121       out() << "ElectronSelector("<<name()<<"): Did not find the file emid_cuts.txt in:\n  \""<< path << "\"" << endl;
00122       return "";
00123     }
00124     
00125     emid_file.close();
00126     return path;
00127   }
00128 
00129 bool ElectronSelector::selectObject(const TMBEMCluster &electron)
00130 {
00131   float trkpt = 1000.0 ;
00132   if (_finder) {
00133     const TMBTrack* trk = 0 ;
00134     //    cout << (electron.GetChargedTrackRef().GetUniqueID() & 0xffffff )
00135     //   << " " << (electron.getPtrChpSpatialRef().GetUniqueID() & 0xffffff )
00136     //   << " " << (electron.getPtrChpRef().GetUniqueID() & 0xffffff)  << endl ;
00137     if (_id->cutOnTrkMatchChi2EOP() && electron.track_match_chi2prob()>0) {
00138       trk = _finder->FindTrack(*_event, electron.getPtrChpRef()) ;
00139     } else if (electron.track_match_spatialchi2prob()>0) {
00140       //      getPtrChpSpatialRef() always 0. Bug?
00141       //      trk = _finder->FindTrack(*_event, electron.getPtrChpSpatialRef()) ;
00142       trk = _finder->FindTrack(*_event, electron.GetChargedTrackRef()) ;
00143     }
00144 
00145     if (!trk) 
00146       trkpt = -1.0 ;
00147     else 
00148       trkpt =trk->Pt() ;
00149   }
00150 
00151 
00152 
00153   bool pass = _id->select(1, 
00154                           electron.id(), 
00155                           electron.iso(), 
00156                           electron.emfrac(), 
00157                           electron.Pt(), 
00158                           electron.CalDetectorEta(), 
00159                           electron.HMx7(), 
00160                           electron.HMx8(), 
00161                           electron.track_match_spatialchi2prob(), 
00162                           electron.Lhood8(),
00163                           trkpt,
00164                           electron.track_match_chi2prob()
00165                           ) ;
00166 
00167 
00168   const vector<string>* cuts = _id->passCuts() ;
00169   if (cuts && _nelectrons > 0 ) 
00170     for (vector<string>::const_iterator it = cuts->begin();
00171          it != cuts->end(); it++) _stat.EventSelected(*it) ;
00172 
00173   if (!pass) return false ;
00174   
00175   _nselected++ ;
00176 
00177   if (debug()>1) 
00178     cout << "PROCESSOR \"" << name() 
00179          << "\" SELECTED OBJECT: pT = " << electron.Pt() 
00180          << " eta = " << electron.Eta() 
00181          << " phi = " << electron.Phi() 
00182          << " charge = " << electron.charge() 
00183          << endl ;
00184   return true ;
00185 }
00186 
00187 void ElectronSelector::finish() {
00188   if (debug() >=1 ) _id->Stat() ;
00189 }
00190 
00191   bool ElectronSelector::processEvent(cafe::Event &event)
00192   {
00193 
00194     //get pointer to statistics collector
00195     event.get("StatPointer", _stat)  ;
00196     _event = &event ;
00197     _nselected = 0 ;
00198     SelectUserObjects<TMBEMCluster>::processEvent(event);
00199 
00200     if (debug() >=2 ) 
00201       out() << " ELECTRON SELECTED: " << _nselected 
00202             << std::endl ;
00203     
00204     for (int i = 1; i <= _nelectrons; i++) {
00205       if (_nselected < i) return false ; 
00206       ostringstream st ;
00207       st << " N electrons >= "  << i;
00208       _stat.EventSelected(st.str()) ;
00209     }
00210 
00211     if (_nelectronsmax >= 0) {
00212       if (_nselected > _nelectronsmax) return false ; 
00213       ostringstream st ;
00214       st << "Number of electrons <= " <<  _nelectronsmax ;
00215       _stat.EventSelected(st.str()) ;      
00216     }
00217 
00218     return true  ;      
00219   }
00220 
00221   void ElectronSelector::before(cafe::Collection<TMBEMCluster>& from) {
00222       if(_sort) {
00223           from.sort(from.begin(),from.end(),::moreThan) ;
00224       }
00225   }
00226 
00227 }
00228 
00229 ClassImp(caf_util::ElectronSelector) ;

Generated on Tue Mar 28 10:13:03 2006 for CAF by doxygen 1.3.4