00001 #include "caf_util/VertexSelector.hpp"
00002 #include "cafe/Config.hpp"
00003
00004 #include <string>
00005 #include <sstream>
00006
00007 using namespace std ;
00008 using namespace cafe ;
00009
00010 namespace {
00011 bool lessThan(const TMBPrimaryVertex& a, const TMBPrimaryVertex& b) {
00012 return a.probMB() < b.probMB();
00013 }
00014 }
00015
00016 namespace caf_util {
00017
00018 VertexSelector::VertexSelector(const char *name) :
00019 cafe::SelectUserObjects<TMBPrimaryVertex>(name),
00020 _nselected(0),
00021 _muvars("_hasCentral", "_chptr"),
00022 _emvars()
00023 {
00024
00025 cafe::Config config(name);
00026
00027 _zmax = config.get("zmax", 60) ;
00028 _ntracks = config.get("ntracks", 3) ;
00029 _dz_muon = config.get("dz_muon", 0) ;
00030 _muonBranch = config.get("muonBranch", "") ;
00031
00032 _dz_electron = config.get("dz_electron", 0) ;
00033 _electronBranch = config.get("electronBranch", "") ;
00034 _useEOPetrack = config.get("UseEOPTrackMatchElectron", false) ;
00035 if (_useEOPetrack) {
00036 _emvars.add("_SpatialTrMatchChi2Prob") ;
00037 _emvars.add("_chptrBest") ;
00038 } else {
00039 _emvars.add("_SpatialTrMatchChi2ProbBest") ;
00040 _emvars.add("_chptr") ;
00041 }
00042
00043 _sort = config.get("Sort", 0);
00044 _nvsmax = config.get("nPVMax", -1);
00045
00046 _finder.SetVariablesTrack(Variables("fUniqueID", "_z")) ;
00047 addVariable("_VertexZ") ;
00048 addVariable("_ntrack") ;
00049 };
00050
00051 void VertexSelector::begin() {
00052 _finder.setDebug(debug()) ;
00053 }
00054
00055 bool VertexSelector::processEvent(cafe::Event &event)
00056 {
00057
00058 event.get("StatPointer", _stat) ;
00059 _event = &event ;
00060 _nselected = 0 ;
00061 SelectUserObjects<TMBPrimaryVertex>::processEvent(event) ;
00062
00063 if (_nselected == 0) return false ;
00064
00065 if (_nvsmax >= 0) {
00066 if (_nselected > _nvsmax) return false ;
00067 ostringstream st ;
00068 st << "Number of vertices <= " << _nvsmax ;
00069 _stat.EventSelected(st.str()) ;
00070 }
00071
00072 return true ;
00073 };
00074
00075 bool VertexSelector::selectObject(const TMBPrimaryVertex &vertex)
00076 {
00077 if (fabs(vertex.vz()) >= _zmax) return false ;
00078 {
00079 ostringstream st ;
00080 st << "Vertex Z < " << _zmax << " cm" ;
00081 _stat.EventSelected(st.str()) ;
00082 }
00083
00084 if (fabs(vertex.ntrack()) < _ntracks) return false ;
00085 {
00086 ostringstream st ;
00087 st << "N tracks for vertex >= " << _ntracks ;
00088 _stat.EventSelected(st.str()) ;
00089 }
00090
00091 if (_dz_muon > 0) {
00092 Collection<TMBMuon> from(_event->getCollection<TMBMuon>(_muonBranch.c_str(),_muvars));
00093 if (from.size() == 0) {
00094 out() << "VERTEX SELECTOR WARNING in dz(muon,vertex) cut! Branch "
00095 << _muonBranch << " is empty or is not existing! "
00096 << endl ;
00097 return false ;
00098 }
00099
00100 Collection<TMBMuon>::const_iterator it = from.begin();
00101 for(; it != from.end(); ++it) {
00102
00103 if (debug()>2)
00104 cout << "PROCESSOR \"" << name()
00105 << "\" READ MUON: pT = " << it->Pt()
00106 << " eta = " << it->Eta()
00107 << " phi = " << it->Phi()
00108 << endl ;
00109
00110 if (!it->hasCentral()) {
00111
00112 err() << "VERTEX SELECTOR WARNING! "
00113 << "You should use only muon with central track for the cut "
00114 << "dz(muon,vertex)."
00115 << endl ;
00116 continue ;
00117 }
00118 const TMBTrack* trk = _finder.FindTrack(*_event, it->GetChargedTrackRef()) ;
00119 if (!trk) {
00120 err() << "VERTEX SELECTOR ERROR!"
00121 << " Track pointer from the muon is 0!"
00122 << endl ;
00123 continue ;
00124 }
00125
00126 if (debug()>2)
00127 cout << " MUON TRACK: pT = " << trk->Pt()
00128 << " eta = " << trk->Eta()
00129 << " phi = " << trk->Phi()
00130 << endl ;
00131
00132 if (fabs(vertex.vz() - trk->z()) < _dz_muon) break ;
00133 }
00134 if (!(it != from.end())) return false ;
00135
00136 ostringstream st ;
00137 st << "dZ(muon, vertex) < " << (float) _dz_muon << " cm" ;
00138 _stat.EventSelected(st.str()) ;
00139 }
00140
00141 if (_dz_electron > 0) {
00142 Collection<TMBEMCluster> from(_event->getCollection<TMBEMCluster>(_electronBranch.c_str(),_emvars));
00143
00144 if (from.size() == 0) {
00145 out() << "VERTEX SELECTOR WARNING in dz(electron,vertex) cut! Branch "
00146 << _electronBranch << " is empty or is not existing! "
00147 << endl ;
00148 return false ;
00149 }
00150
00151 Collection<TMBEMCluster>::const_iterator it = from.begin();
00152 for(; it != from.end(); ++it) {
00153
00154 if (debug()>2)
00155 cout << "PROCESSOR \"" << name()
00156 << "\" READ ELECTRON: pT = " << it->Pt()
00157 << " eta = " << it->Eta()
00158 << " phi = " << it->Phi()
00159 << endl ;
00160
00161 const TMBTrack* trk = 0 ;
00162 if ( (_useEOPetrack && !it->has_track_match(0.0) ) ||
00163 (!_useEOPetrack && !it->has_spatial_track_match(0.0))) {
00164 err() << "VERTEX SELECTOR WARNING! "
00165 << "You should use only track matched electron for the cut "
00166 << "dz(electron,vertex)."
00167 << endl ;
00168 continue ;
00169 }
00170
00171 if (_useEOPetrack)
00172 trk = _finder.FindTrack(*_event, it->getPtrChpRef()) ;
00173 else
00174 trk = _finder.FindTrack(*_event, it->GetChargedTrackRef()) ;
00175
00176 if (!trk) {
00177 err() << "VERTEX SELECTOR WARNING! "
00178 << "No track was found for track matched electron!"
00179 << endl ;
00180 continue ;
00181 }
00182
00183 if (debug()>2)
00184 cout << " ELECTRON TRACK: pT = " << trk->Pt()
00185 << " eta = " << trk->Eta()
00186 << " phi = " << trk->Phi()
00187 << endl ;
00188
00189 if (fabs(vertex.vz() - trk->z()) < _dz_electron) break ;
00190 }
00191 if (!(it != from.end())) return false ;
00192
00193 ostringstream st ;
00194 st << "dZ(electron, vertex) < " << (float) _dz_electron << " cm" ;
00195 _stat.EventSelected(st.str()) ;
00196 }
00197
00198 _nselected++ ;
00199
00200 return true ;
00201
00202 }
00203
00204 void VertexSelector::before(cafe::Collection<TMBPrimaryVertex>& from) {
00205 if(_sort) {
00206 from.sort(from.begin(),from.end(),::lessThan) ;
00207 }
00208 }
00209
00210 }
00211 ClassImp(caf_util::VertexSelector) ;
00212