TimeOfFlight  1.32.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TOFEstimators.cc
Go to the documentation of this file.
1 #include "TOFEstimators.h"
2 #include "TOFUtils.h"
3 
4 #include "EVENT/LCCollection.h"
5 #include "UTIL/PIDHandler.h"
6 
7 #include "marlin/Global.h"
10 #include "marlinutil/GeometryUtil.h"
11 #include "MarlinTrk/Factory.h"
12 #include "EVENT/SimTrackerHit.h"
13 #include "UTIL/LCRelationNavigator.h"
14 #include "CLHEP/Random/Randomize.h"
15 
16 using namespace TOFUtils;
17 using std::vector;
18 using std::string;
19 using EVENT::LCCollection;
20 using EVENT::ReconstructedParticle;
21 using EVENT::TrackerHit;
22 using EVENT::Track;
23 using EVENT::SimTrackerHit;
24 using EVENT::Cluster;
25 using EVENT::CalorimeterHit;
26 using EVENT::TrackState;
27 using EVENT::LCObject;
28 using UTIL::LCRelationNavigator;
29 using CLHEP::RandGauss;
31 
33 
34 
35 TOFEstimators::TOFEstimators() : marlin::Processor("TOFEstimators") {
36  _description = "TOFEstimators processor computes time-of-flight of the chosen ReconstructedParticle to the \
37  specified end point (SET hit or Ecal surface). To be used for a further particle ID";
38 
39  registerInputCollection(LCIO::RECONSTRUCTEDPARTICLE,
40  "ReconstructedParticleCollection",
41  "Name of the ReconstructedParticle collection",
43  std::string("PandoraPFOs") );
44 
45  registerProcessorParameter("ExtrapolateToEcal",
46  "If true, track is extrapolated to the Ecal surface for track length calculation, \
47  time of flight estimated using Ecal hits. If false, track length calculated to the last tracker hit.\
48  Time of flight estimated using SET hit if exists.",
50  bool(true));
51 
52  registerProcessorParameter("TofMethod",
53  "name of the algorithm which estimates time of flight\
54  to the Ecal surface based on Ecal hits time information.\
55  Available options are: closest, frankAvg, frankFit.\
56  In case of _extrapolateToEcal==false is ignored",
57  _tofMethod,
58  std::string("closest") );
59 
60  registerProcessorParameter("TimeResolution",
61  "Time resolution of individual SET strips or Ecal hits in ps",
63  double(0.));
64 
65  registerProcessorParameter("MaxEcalLayer",
66  "Time of flight is calculated using Ecal hits only up to MaxLayer",
68  int(10) );
69 
70 }
71 
72 
74 
75  if(_tofMethod != "closest" && _tofMethod != "frankAvg" && _tofMethod != "frankFit"){
76  throw EVENT::Exception( "Invalid steering parameter for TofMethod is passed: " + _tofMethod + "\n Available options are: closest, frankAvg, frankFit" );
77  }
78 
80 
81  _outputParNames = {"timeOfFlight"};
82  _bField = MarlinUtil::getBzAtOrigin();
84  // internally we use time resolution in nanoseconds
86 }
87 
88 
89 void TOFEstimators::processEvent(EVENT::LCEvent * evt){
90  RandGauss::setTheSeed( marlin::Global::EVENTSEEDER->getSeed(this) );
91  ++_nEvent;
92  streamlog_out(DEBUG9)<<std::endl<<"==========Event========== "<<_nEvent<<std::endl;
93 
94  LCCollection* pfos = evt->getCollection(_pfoCollectionName);
95  LCCollection* setRelations = evt->getCollection("SETSpacePointRelations");
96 
97  LCRelationNavigator navigatorSET = LCRelationNavigator( setRelations );
98 
99  PIDHandler pidHandler( pfos );
100  int algoID = pidHandler.addAlgorithm( name(), _outputParNames );
101 
102 
103  for (int i=0; i<pfos->getNumberOfElements(); ++i){
104  streamlog_out(DEBUG9)<<std::endl<<"Starting to analyze "<<i+1<<" PFO"<<std::endl;
105  ReconstructedParticle* pfo = static_cast <ReconstructedParticle*> ( pfos->getElementAt(i) );
106 
107  int nClusters = pfo->getClusters().size();
108  int nTracks = pfo->getTracks().size();
109 
110  if( nClusters != 1 || nTracks != 1){
111  // Analyze only simple pfos. Otherwise write dummy zeros
112  vector<float> results{0.};
113  pidHandler.setParticleID(pfo , 0, 0, 0., algoID, results);
114  continue;
115  }
116  Track* track = pfo->getTracks()[0];
117  Cluster* cluster = pfo->getClusters()[0];
118 
119  double timeOfFlight = 0.;
120  if( _extrapolateToEcal ){
121  if (_tofMethod == "closest"){
122  timeOfFlight = getTofClosest(cluster, track, _timeResolution);
123  }
124  else if (_tofMethod == "frankAvg"){
126  timeOfFlight = getTofFrankAvg(frankHits, track, _timeResolution);
127  }
128  else if (_tofMethod == "frankFit"){
130  timeOfFlight = getTofFrankFit(frankHits, track, _timeResolution);
131  }
132  }
133  else{
134  //define tof as an average time between two SET strips
135  //if no SET hits found, tof alreasy is 0, just skip
136  TrackerHit* hitSET = getSETHit(track, _tpcOuterR);
137  if ( hitSET != nullptr ){
138  const vector<LCObject*>& simHitsSET = navigatorSET.getRelatedToObjects( hitSET );
139  if ( simHitsSET.size() >= 2 ){
140  //It must be always 2, but just in case...
141  if (simHitsSET.size() > 2) streamlog_out(WARNING)<<"Found more than two SET strip hits! Writing TOF as an average of the first two elements in the array."<<std::endl;
142 
143  SimTrackerHit* simHitSETFront = static_cast <SimTrackerHit*>( simHitsSET[0] );
144  SimTrackerHit* simHitSETBack = static_cast <SimTrackerHit*>( simHitsSET[1] );
145  double timeFront = RandGauss::shoot(simHitSETFront->getTime(), _timeResolution);
146  double timeBack = RandGauss::shoot(simHitSETBack->getTime(), _timeResolution);
147  timeOfFlight = (timeFront + timeBack)/2.;
148  }
149  else if (simHitsSET.size() == 1){
150  streamlog_out(WARNING)<<"Found only one SET strip hit! Writing TOF from a single strip."<<std::endl;
151  SimTrackerHit* simHitSET = static_cast <SimTrackerHit*>(simHitsSET[0]);
152  timeOfFlight = RandGauss::shoot(simHitSET->getTime(), _timeResolution);
153  }
154  else{
155  // this happens very rarily (0.1%). When >1 simHits associated with a single strip none simHits are written by the DDSpacePointBuilder.
156  streamlog_out(WARNING)<<"Found NO simHits associated with the found SET hit! Writing TOF as 0."<<std::endl;
157  }
158  }
159  }
160  vector<float> results{float(timeOfFlight)};
161  pidHandler.setParticleID(pfo , 0, 0, 0., algoID, results);
162  streamlog_out(DEBUG9)<<"Final results for the "<<i+1<<" PFO"<<std::endl;
163  streamlog_out(DEBUG9)<<"time-of-flight: "<< float(timeOfFlight)<<" ns"<<std::endl;
164  streamlog_out(DEBUG9)<<std::endl<<std::endl;
165  }
166 }
double getTofFrankAvg(std::vector< EVENT::CalorimeterHit * > selectedHits, EVENT::Track *track, double timeResolution)
Get the time-of-flight using average of the Frank ECal hits.
Definition: TOFUtils.cc:114
double getTofClosest(EVENT::Cluster *cluster, EVENT::Track *track, double timeResolution)
Get the time-of-flight using the closest ECal hit.
Definition: TOFUtils.cc:90
TOFEstimators()
Default constructor.
std::string _pfoCollectionName
Stores ReconstructedParticleCollection steering parameter.
Definition: TOFEstimators.h:50
T endl(T...args)
TOFEstimators aTOFEstimators
int _maxEcalLayer
Stores MaxEcalLayer steering parameter.
Definition: TOFEstimators.h:66
EVENT::TrackerHit * getSETHit(EVENT::Track *track, double tpcOuterR)
Returns SET hit.
Definition: TOFUtils.cc:53
int _nEvent
Stores current event number.
Definition: TOFEstimators.h:70
double getTPCOuterR()
Returns TPC outer radius from the DD4hep detector geometry.
Definition: TOFUtils.cc:45
STL class.
Marlin processor that calculates time-of-flight for charged particles.
Definition: TOFEstimators.h:13
static ProcessorEventSeeder * EVENTSEEDER
void processEvent(EVENT::LCEvent *evt)
Called for every event - the working horse.
double _bField
Stores z component of the magnetic field at the origin in Tesla.
Definition: TOFEstimators.h:78
std::string _tofMethod
Stores TofMethod steering parameter.
Definition: TOFEstimators.h:58
bool _extrapolateToEcal
Stores ExtrapolateToEcal steering parameter.
Definition: TOFEstimators.h:54
T size(T...args)
STL class.
double getTofFrankFit(std::vector< EVENT::CalorimeterHit * > selectedHits, EVENT::Track *track, double timeResolution)
Get the time-of-flight using fit of the Frank ECal hits.
Definition: TOFUtils.cc:131
void registerProcessor(Processor *proc)
std::vector< EVENT::CalorimeterHit * > selectFrankEcalHits(EVENT::Cluster *cluster, EVENT::Track *track, int maxEcalLayer, double bField)
Get a subset of the cluster calorimeter hits.
Definition: TOFUtils.cc:63
void init()
Called at the begin of the job before anything is read.
std::vector< std::string > _outputParNames
Stores names of the output parameter - &quot;timeOfFlight&quot;.
Definition: TOFEstimators.h:74
double _timeResolution
Stores TimeResolution steering parameter.
Definition: TOFEstimators.h:62
double _tpcOuterR
Stores outer TPC radius in mm.
Definition: TOFEstimators.h:82