ATLAS Offline Software
ComTimeRec.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "ComTimeRec.h"
6 
7 #include "CLHEP/Units/SystemOfUnits.h"
8 
9 #include "Gaudi/Property.h"
10 
12 
15 
16 #include <algorithm>
17 
18 //----------------------------------------------------------------
19 
20 ComTimeRec::ComTimeRec(const std::string& name,
21  ISvcLocator* pSvcLocator) :
22  AthAlgorithm(name,pSvcLocator),m_triggerTimeTool(0)
23 {
24  declareProperty("ComTimeKey",m_comTimeKey="ComTime");
25  declareProperty("Mode",m_mode="TTRCavern");
26  declareProperty("Method",m_method="YZero"); // or "ZZero" or "Origin"
27  declareProperty("TTRKey",m_TTRKey="CosmicRecord");
28  declareProperty("SamplingPeriod",m_samplingPeriod=1./(40.08*CLHEP::megahertz));
30 }
31 
32 //----------------------------------------------------------------
33 
35 {
36 }
37 
38 //----------------------------------------------------------------
39 
41 {
42 
43  ATH_MSG_INFO( "In initialize()" );
44 
45  // This level of obfuscation is necessary because ITriggerTime, the interface class for
46  // the timing tool, does not provide the method that is needed here (which is bad design).
47  // The right thing to do is to fix the interface, at which point this is a simple retrieve.
48  // Until then, we are stuck with the old way.
49  IToolSvc *toolSvc(0);
50  CHECK( service( "ToolSvc",toolSvc ) );
51 
52  IAlgTool *trigTool(0);
53  CHECK( toolSvc->retrieveTool("CosmicTriggerTimeTool",trigTool) );
54 
55  m_triggerTimeTool = dynamic_cast<CosmicTriggerTimeTool*>(trigTool);
56  if(0==m_triggerTimeTool){
57  ATH_MSG_ERROR( " Failed to dynamic cast CosmicTriggerTimeTool " );
58  return StatusCode::FAILURE;
59  }
60 
61  return StatusCode::SUCCESS;
62 
63 }
64 
65 //----------------------------------------------------------------
66 
68 {
69 
70  ATH_MSG_DEBUG( "In execute()" );
71 
72  // Pick method of calculating trigger point.
73  if(m_mode == "TTRCavern" ) {
74 
75  // Use the Timed Track Record of the muon entering the cavern.
76 
77  if(!evtStore()->contains<TrackRecordCollection>(m_TTRKey) ){
78  ATH_MSG_ERROR( " can not retrieve TrackRecordCollection with key " << m_TTRKey );
79  // Put default ComTime into SG for.
80  ComTime *theComTime = new ComTime();
81  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
82  return StatusCode::SUCCESS;
83  }
84  // const TimedTrackRecordCollection* coll;
85  const TrackRecordCollection *coll(NULL);
86  CHECK( evtStore()->retrieve(coll, m_TTRKey) );
87  int nMuons = 0;
88  double earliestMuonTime = -1.;
89  // TimedTrackRecord* earliestMuon=NULL;
90  TrackRecord *earliestMuon=nullptr;
91 
92  for(auto it : *coll) {
93 
94  if(abs(it.GetPDGCode()) == 13 ) {
95  nMuons++;
96  if( (nMuons==1) or (it.GetTime() < earliestMuonTime) ) {
97  // earliestMuon = const_cast<TimedTrackRecord*>(*it);
98  earliestMuon = &it;
99  earliestMuonTime = it.GetTime();
100  }
101  }
102  }
103 
104  if( nMuons>=1 ) { // use the earliest muon if more than 1
105  CLHEP::Hep3Vector pos = earliestMuon->GetPosition();
106  CLHEP::Hep3Vector mom = earliestMuon->GetMomentum();
107  double energy = sqrt(mom.x()*mom.x() + mom.y()*mom.y() + mom.z()*mom.z());
108 
109  CLHEP::Hep3Vector cosThetaDir = CLHEP::Hep3Vector(mom.x()/energy, mom.y()/energy, mom.z()/energy);
110 
111  double time = earliestMuon->GetTime();
112 
113  ATH_MSG_DEBUG( " Earliest Muon xyz position " << pos.x()<<" "
114  << pos.y() << " " << pos.z() );
115  ATH_MSG_DEBUG( " cosThetaDir " << cosThetaDir.x() << " "
116  << cosThetaDir.y() << " " << cosThetaDir.z() );
117  ATH_MSG_DEBUG( " time " << time );
118 
119  // Three different methods of definining the virtrual trigger point
120  // 1) m_method = YZero : extrapolate to Y=0 plane (default)
121  // 2) m_method = ZZero : extrapolate to Z=0 plane
122  // 3) m_method = Origin : extrapolate to closest approach to (0,0,0)
123 
124 
125  double PathCritical;
126  CLHEP::Hep3Vector PointCritical;
127 
128  if( m_method == "YZero" ) { // Extrapolate to Y=0 plane. Good for cosmics.
129  double dXdY = cosThetaDir.x()/cosThetaDir.y();
130  double dZdY = cosThetaDir.z()/cosThetaDir.y();
131 
132  double XCritical = pos.x()-dXdY*pos.y();
133  double YCritical = 0.0;
134  double ZCritical = pos.z()-dZdY*pos.y();
135 
136  PointCritical = CLHEP::Hep3Vector(XCritical,YCritical,ZCritical);
137 
138  PathCritical = sqrt(pow(XCritical-pos.x(),2) + pow(YCritical-pos.y(),2) +
139  pow(ZCritical-pos.z(),2));
140  if( (YCritical-pos.y())*cosThetaDir.y() < 0 ) { // backwards extrapolation??
141  ATH_MSG_WARNING( "Backwards extrapolation?? pos.y(),cosThetaDir.y() = "
142  << pos.y()<<"," << cosThetaDir.y() );
143  PathCritical = -1.0 * PathCritical;
144  }
145  }
146  else if (m_method == "ZZero") { // Extrapolate to Z=0 plane. Good for halo.
147  double dXdZ = cosThetaDir.x()/cosThetaDir.z();
148  double dYdZ = cosThetaDir.y()/cosThetaDir.z();
149 
150  double XCritical = pos.x()-dXdZ*pos.z();
151  double YCritical = pos.y()-dYdZ*pos.z();
152  double ZCritical = 0.0;
153 
154  PointCritical = CLHEP::Hep3Vector(XCritical,YCritical,ZCritical);
155 
156  PathCritical = sqrt(pow(XCritical-pos.x(),2) + pow(YCritical-pos.y(),2) +
157  pow(ZCritical-pos.z(),2));
158  if( (ZCritical-pos.z())*cosThetaDir.z() < 0 ) { // backwards extrapolation??
159  ATH_MSG_WARNING( "Backwards extrapolation?? pos.z(),cosThetaDir.z() = "
160  << pos.z()<<"," << cosThetaDir.z() );
161  PathCritical = -1.0 * PathCritical;
162  }
163  }
164  else if (m_method == "Origin") { // Extrapolate to closest approach to (0,0,0)
165  // See arithmetic in RMcP logbook 28 Feb 2006
166  PathCritical = -1.*(pos.x()*cosThetaDir.x() + pos.y()*cosThetaDir.y() +
167  pos.z()*cosThetaDir.z());
168  PointCritical = CLHEP::Hep3Vector( pos.x() + PathCritical*cosThetaDir.x(),
169  pos.y() + PathCritical*cosThetaDir.y(),
170  pos.z() + PathCritical*cosThetaDir.z());
171  }
172  else { // This is bad ... invalid method requested
173  ATH_MSG_FATAL( "Invalid method = " << m_method );
174  return StatusCode::FAILURE;
175  }
176 
177  double TimeCritical = time + PathCritical/m_velocity;
178  double TTCTime = m_samplingPeriod * floor(TimeCritical/m_samplingPeriod);
179 
180  ATH_MSG_DEBUG( " Extrapolated muon xyz position " << PointCritical.x()<<" "
181  << PointCritical.y()<<" " << PointCritical.z());
182  ATH_MSG_DEBUG( " path " << PathCritical );
183  ATH_MSG_DEBUG( " time " << TimeCritical );
184  ATH_MSG_DEBUG( " TTCTime " << TTCTime );
185 
186  ComTime* theComTime = new ComTime(TTCTime,TimeCritical,PointCritical,cosThetaDir);
187  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
188 
189  m_triggerTimeTool->setComTime(theComTime);
190 
191  } else { // no muons entering cavern found.
192  ATH_MSG_DEBUG( " No muons entered cavern." );
193  ComTime *theComTime = new ComTime();
194  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
195  }
196  } else if(m_mode == "CollisionMode" ) {
197  // Using cosmic reco during collisions. Fire on BCIDs.
198  // Just provide a default ComTime object.
199  ComTime *theComTime = new ComTime();
200  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
201  return StatusCode::SUCCESS;
202  } else {
203  ATH_MSG_FATAL( "Invalid mode = " << m_mode );
204  return StatusCode::SUCCESS;
205  }
206  return StatusCode::SUCCESS;
207 }
208 
209 //----------------------------------------------------------------
210 
212 
213  ATH_MSG_INFO( "In finalize()" );
214 
215  return StatusCode::SUCCESS;
216 
217 }
218 
219 //----------------------------------------------------------------
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ComTimeRec::execute
StatusCode execute()
Definition: ComTimeRec.cxx:67
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ComTime.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
python.SystemOfUnits.nanosecond
int nanosecond
Definition: SystemOfUnits.py:119
ComTimeRec.h
ComTimeRec::~ComTimeRec
~ComTimeRec()
Definition: ComTimeRec.cxx:34
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
ComTimeRec::finalize
StatusCode finalize()
Definition: ComTimeRec.cxx:211
AtlasHitsVector
Definition: AtlasHitsVector.h:33
skel.it
it
Definition: skel.GENtoEVGEN.py:423
ComTime
Definition: ComTime.h:17
ComTimeRec::m_comTimeKey
std::string m_comTimeKey
Definition: ComTimeRec.h:26
ComTimeRec::initialize
StatusCode initialize()
Definition: ComTimeRec.cxx:40
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
python.SystemOfUnits.megahertz
int megahertz
Definition: SystemOfUnits.py:127
ComTimeRec::m_TTRKey
std::string m_TTRKey
Definition: ComTimeRec.h:29
TrackRecord::GetTime
double GetTime() const
Time.
Definition: TrackRecord.h:92
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
python.SystemOfUnits.millimeter
int millimeter
Definition: SystemOfUnits.py:53
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ParticleGun_FastCalo_ChargeFlip_Config.energy
energy
Definition: ParticleGun_FastCalo_ChargeFlip_Config.py:78
ComTimeRec::m_method
std::string m_method
Definition: ComTimeRec.h:28
TrackRecord::GetPosition
CLHEP::Hep3Vector GetPosition() const
Position.
Definition: TrackRecord.h:74
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
ComTimeRec::m_samplingPeriod
double m_samplingPeriod
Definition: ComTimeRec.h:30
AthAlgorithm
Definition: AthAlgorithm.h:47
TrackRecord.h
CosmicTriggerTimeTool::setComTime
void setComTime(const ComTime *comTime)
Definition: CosmicTriggerTimeTool.h:36
ComTimeRec::m_mode
std::string m_mode
Definition: ComTimeRec.h:27
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TrackRecordCollection.h
TrackRecord::GetMomentum
CLHEP::Hep3Vector GetMomentum() const
Momentum.
Definition: TrackRecord.h:80
TrackRecord
Definition: TrackRecord.h:10
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
ComTimeRec::ComTimeRec
ComTimeRec(const std::string &name, ISvcLocator *pSvcLocator)
Definition: ComTimeRec.cxx:20
ComTimeRec::m_triggerTimeTool
CosmicTriggerTimeTool * m_triggerTimeTool
Definition: ComTimeRec.h:32
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
ComTimeRec::m_velocity
double m_velocity
Definition: ComTimeRec.h:31
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CosmicTriggerTimeTool
Definition: CosmicTriggerTimeTool.h:16