ATLAS Offline Software
ComTimeRec.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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 
50  IAlgTool *trigTool(0);
51  CHECK( toolSvc()->retrieveTool("CosmicTriggerTimeTool",trigTool) );
52 
53  m_triggerTimeTool = dynamic_cast<CosmicTriggerTimeTool*>(trigTool);
54  if(0==m_triggerTimeTool){
55  ATH_MSG_ERROR( " Failed to dynamic cast CosmicTriggerTimeTool " );
56  return StatusCode::FAILURE;
57  }
58 
59  return StatusCode::SUCCESS;
60 
61 }
62 
63 //----------------------------------------------------------------
64 
66 {
67 
68  ATH_MSG_DEBUG( "In execute()" );
69 
70  // Pick method of calculating trigger point.
71  if(m_mode == "TTRCavern" ) {
72 
73  // Use the Timed Track Record of the muon entering the cavern.
74 
75  if(!evtStore()->contains<TrackRecordCollection>(m_TTRKey) ){
76  ATH_MSG_ERROR( " can not retrieve TrackRecordCollection with key " << m_TTRKey );
77  // Put default ComTime into SG for.
78  ComTime *theComTime = new ComTime();
79  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
80  return StatusCode::SUCCESS;
81  }
82  // const TimedTrackRecordCollection* coll;
83  const TrackRecordCollection *coll(NULL);
84  CHECK( evtStore()->retrieve(coll, m_TTRKey) );
85  int nMuons = 0;
86  double earliestMuonTime = -1.;
87  const TrackRecord *earliestMuon=nullptr;
88 
89  for(const TrackRecord& r : *coll) {
90 
91  if(abs(r.GetPDGCode()) == 13 ) {
92  nMuons++;
93  if( (nMuons==1) or (r.GetTime() < earliestMuonTime) ) {
94  earliestMuon = &r;
95  earliestMuonTime = r.GetTime();
96  }
97  }
98  }
99 
100  if( nMuons>=1 ) { // use the earliest muon if more than 1
101  CLHEP::Hep3Vector pos = earliestMuon->GetPosition();
102  CLHEP::Hep3Vector mom = earliestMuon->GetMomentum();
103  double energy = sqrt(mom.x()*mom.x() + mom.y()*mom.y() + mom.z()*mom.z());
104 
105  CLHEP::Hep3Vector cosThetaDir = CLHEP::Hep3Vector(mom.x()/energy, mom.y()/energy, mom.z()/energy);
106 
107  double time = earliestMuon->GetTime();
108 
109  ATH_MSG_DEBUG( " Earliest Muon xyz position " << pos.x()<<" "
110  << pos.y() << " " << pos.z() );
111  ATH_MSG_DEBUG( " cosThetaDir " << cosThetaDir.x() << " "
112  << cosThetaDir.y() << " " << cosThetaDir.z() );
113  ATH_MSG_DEBUG( " time " << time );
114 
115  // Three different methods of definining the virtrual trigger point
116  // 1) m_method = YZero : extrapolate to Y=0 plane (default)
117  // 2) m_method = ZZero : extrapolate to Z=0 plane
118  // 3) m_method = Origin : extrapolate to closest approach to (0,0,0)
119 
120 
121  double PathCritical;
122  CLHEP::Hep3Vector PointCritical;
123 
124  if( m_method == "YZero" ) { // Extrapolate to Y=0 plane. Good for cosmics.
125  double dXdY = cosThetaDir.x()/cosThetaDir.y();
126  double dZdY = cosThetaDir.z()/cosThetaDir.y();
127 
128  double XCritical = pos.x()-dXdY*pos.y();
129  double YCritical = 0.0;
130  double ZCritical = pos.z()-dZdY*pos.y();
131 
132  PointCritical = CLHEP::Hep3Vector(XCritical,YCritical,ZCritical);
133 
134  PathCritical = sqrt(pow(XCritical-pos.x(),2) + pow(YCritical-pos.y(),2) +
135  pow(ZCritical-pos.z(),2));
136  if( (YCritical-pos.y())*cosThetaDir.y() < 0 ) { // backwards extrapolation??
137  ATH_MSG_WARNING( "Backwards extrapolation?? pos.y(),cosThetaDir.y() = "
138  << pos.y()<<"," << cosThetaDir.y() );
139  PathCritical = -1.0 * PathCritical;
140  }
141  }
142  else if (m_method == "ZZero") { // Extrapolate to Z=0 plane. Good for halo.
143  double dXdZ = cosThetaDir.x()/cosThetaDir.z();
144  double dYdZ = cosThetaDir.y()/cosThetaDir.z();
145 
146  double XCritical = pos.x()-dXdZ*pos.z();
147  double YCritical = pos.y()-dYdZ*pos.z();
148  double ZCritical = 0.0;
149 
150  PointCritical = CLHEP::Hep3Vector(XCritical,YCritical,ZCritical);
151 
152  PathCritical = sqrt(pow(XCritical-pos.x(),2) + pow(YCritical-pos.y(),2) +
153  pow(ZCritical-pos.z(),2));
154  if( (ZCritical-pos.z())*cosThetaDir.z() < 0 ) { // backwards extrapolation??
155  ATH_MSG_WARNING( "Backwards extrapolation?? pos.z(),cosThetaDir.z() = "
156  << pos.z()<<"," << cosThetaDir.z() );
157  PathCritical = -1.0 * PathCritical;
158  }
159  }
160  else if (m_method == "Origin") { // Extrapolate to closest approach to (0,0,0)
161  // See arithmetic in RMcP logbook 28 Feb 2006
162  PathCritical = -1.*(pos.x()*cosThetaDir.x() + pos.y()*cosThetaDir.y() +
163  pos.z()*cosThetaDir.z());
164  PointCritical = CLHEP::Hep3Vector( pos.x() + PathCritical*cosThetaDir.x(),
165  pos.y() + PathCritical*cosThetaDir.y(),
166  pos.z() + PathCritical*cosThetaDir.z());
167  }
168  else { // This is bad ... invalid method requested
169  ATH_MSG_FATAL( "Invalid method = " << m_method );
170  return StatusCode::FAILURE;
171  }
172 
173  double TimeCritical = time + PathCritical/m_velocity;
174  double TTCTime = m_samplingPeriod * floor(TimeCritical/m_samplingPeriod);
175 
176  ATH_MSG_DEBUG( " Extrapolated muon xyz position " << PointCritical.x()<<" "
177  << PointCritical.y()<<" " << PointCritical.z());
178  ATH_MSG_DEBUG( " path " << PathCritical );
179  ATH_MSG_DEBUG( " time " << TimeCritical );
180  ATH_MSG_DEBUG( " TTCTime " << TTCTime );
181 
182  ComTime* theComTime = new ComTime(TTCTime,TimeCritical,PointCritical,cosThetaDir);
183  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
184 
185  m_triggerTimeTool->setComTime(theComTime);
186 
187  } else { // no muons entering cavern found.
188  ATH_MSG_DEBUG( " No muons entered cavern." );
189  ComTime *theComTime = new ComTime();
190  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
191  }
192  } else if(m_mode == "CollisionMode" ) {
193  // Using cosmic reco during collisions. Fire on BCIDs.
194  // Just provide a default ComTime object.
195  ComTime *theComTime = new ComTime();
196  CHECK( evtStore()->record(theComTime, m_comTimeKey) );
197  return StatusCode::SUCCESS;
198  } else {
199  ATH_MSG_FATAL( "Invalid mode = " << m_mode );
200  return StatusCode::SUCCESS;
201  }
202  return StatusCode::SUCCESS;
203 }
204 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
beamspotman.r
def r
Definition: beamspotman.py:672
python.SystemOfUnits.megahertz
float megahertz
Definition: SystemOfUnits.py:143
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
ComTimeRec::execute
virtual StatusCode execute() override
Definition: ComTimeRec.cxx:65
ComTimeRec.h
ComTimeRec::~ComTimeRec
~ComTimeRec()
Definition: ComTimeRec.cxx:34
AtlasHitsVector
Definition: AtlasHitsVector.h:32
python.SystemOfUnits.nanosecond
float nanosecond
Definition: SystemOfUnits.py:134
ComTimeRec::initialize
virtual StatusCode initialize() override
Definition: ComTimeRec.cxx:40
ComTime
Definition: ComTime.h:17
ComTimeRec::m_comTimeKey
std::string m_comTimeKey
Definition: ComTimeRec.h:25
python.SystemOfUnits.millimeter
float millimeter
Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration # This software is distribute...
Definition: SystemOfUnits.py:62
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
ComTimeRec::m_TTRKey
std::string m_TTRKey
Definition: ComTimeRec.h:28
TrackRecord::GetTime
double GetTime() const
Time.
Definition: TrackRecord.h:106
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
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:27
TrackRecord::GetPosition
CLHEP::Hep3Vector GetPosition() const
Position.
Definition: TrackRecord.h:88
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
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Definition: AthCommonDataStore.h:145
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:29
AthAlgorithm
Definition: AthAlgorithm.h:47
TrackRecord.h
CosmicTriggerTimeTool::setComTime
void setComTime(const ComTime *comTime)
Definition: CosmicTriggerTimeTool.h:35
ComTimeRec::m_mode
std::string m_mode
Definition: ComTimeRec.h:26
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
TrackRecordCollection.h
TrackRecord::GetMomentum
CLHEP::Hep3Vector GetMomentum() const
Momentum.
Definition: TrackRecord.h:94
TrackRecord
Definition: TrackRecord.h:12
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
ComTimeRec::ComTimeRec
ComTimeRec(const std::string &name, ISvcLocator *pSvcLocator)
Definition: ComTimeRec.cxx:20
ComTimeRec::m_triggerTimeTool
CosmicTriggerTimeTool * m_triggerTimeTool
Definition: ComTimeRec.h:31
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
ComTimeRec::m_velocity
double m_velocity
Definition: ComTimeRec.h:30
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
CosmicTriggerTimeTool
Definition: CosmicTriggerTimeTool.h:15