ATLAS Offline Software
TRT_StrawAlignDbSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
10 #include "TRT_StrawAlignDbSvc.h"
11 #include <fstream>
12 #include <iostream>
13 #include <iomanip>
14 #include <sstream>
15 
16 #include "StoreGate/StoreGateSvc.h"
17 
18 #include "GaudiKernel/IToolSvc.h"
21 
22 #include "Identifier/Identifier.h"
24 
25 ATLAS_NO_CHECK_FILE_THREAD_SAFETY; // This class uses const_cast, regFcn (callback) and DataHandle. Legacy code
26 
28  ISvcLocator* pSvcLocator )
29  : AthService(name,pSvcLocator),
30  m_detStore("DetectorStore",name),
31  m_par_dxcontainerkey("/TRT/Calib/DX"),
32  m_par_strawtextfile(""),
33  m_par_forcecallback(false),
34  m_trtid(nullptr),
35  m_trtman(nullptr),
36  m_streamer("AthenaOutputStreamTool/CondStream1")
37 {
38  declareProperty("StrawTextFile",m_par_strawtextfile);
39  declareProperty("ForceCallback",m_par_forcecallback);
40  declareProperty("StreamTool",m_streamer);
41  declareProperty("DetectorStore",m_detStore);
42 }
43 
44 
46 = default;
47 
48 
50 {
51  ATH_MSG_INFO ("TRT_StrawAlignDbSvc initialize method called");
52 
53  ATH_CHECK( m_detStore->retrieve(m_trtid,"TRT_ID") );
54  ATH_CHECK( m_detStore->retrieve(m_trtman,"TRT") );
55 
56 
57  bool dxcontainerexists = m_detStore->StoreGateSvc::contains<StrawDxContainer>(m_par_dxcontainerkey) ;
58 
59  if( dxcontainerexists ) {
60  ATH_MSG_INFO (" dx container exists - reg callback ");
62  ATH_MSG_ERROR ("Could not register IOV callback for key: " << m_par_dxcontainerkey);
63 
64  } else {
65 
66  // create, record and update data handle
67  ATH_MSG_INFO ("Creating new dx container");
68  auto dxcontainer = std::make_unique<StrawDxContainer>();
69 
70  // reading from file
71  if( !m_par_strawtextfile.empty() ) {
72  ATH_CHECK( this->readTextFile(dxcontainer.get(),
74  }
75 
76  ATH_CHECK( m_detStore->record(std::move(dxcontainer), m_par_dxcontainerkey) );
77  }
78 
79  return StatusCode::SUCCESS;
80 }
81 
82 
84 {
85  ATH_MSG_INFO ("TRT_StrawAlignDbSvc finalize method called");
86  return StatusCode::SUCCESS;
87 }
88 
90 {
91  ATH_MSG_INFO (" Write straw alignment data to text file " << filename);
92  std::ofstream outfile(filename.c_str());
93 
94 
95  // first reduce the container as much as possible
96  // getDxContainer()->crunch() ;
97 
98  StrawDxContainer::FlatContainer packedstrawdata ;
99  const StrawDxContainer* dxcontainer = getConstDxContainer();
100  dxcontainer->getall( packedstrawdata ) ;
101 
102  // now, we store the entries
103  ATH_MSG_INFO ("Number of entries in flatcontainer: "
104  << packedstrawdata.size());
105 
106  for(auto & it : packedstrawdata) {
107  const TRTCond::ExpandedIdentifier& calid = it.first ;
108  // get the end point corrections. if not the right type, store zeros.
109  float dx1=dxcontainer->getDx1(calid) ;
110  float dx2=dxcontainer->getDx2(calid) ;
111  float dxerr=dxcontainer->getDxErr(calid) ;
112  outfile << calid << " "
113  << std::setprecision(5)
114  << std::setw(12) << dx1 << " "
115  << std::setw(12) << dx2 << " "
116  << std::setw(12) << dxerr << std::endl ;
117  }
118  outfile.close() ;
119  return StatusCode::SUCCESS ;
120 }
121 
122 
123 
125 {
126  return readTextFile( getDxContainer(), filename );
127 }
128 
129 
131  const std::string& filename)
132 {
133  ATH_MSG_INFO ("Reading straw alignment data from text file " << filename);
134 
135  if(!dxcontainer) {
136  ATH_MSG_WARNING (" Could not find the container ");
137  return StatusCode::FAILURE;
138  }
139  dxcontainer->clear() ;
140  std::ifstream infile(filename.c_str()) ;
142  float dx1,dx2,dxerr;
143  int nentries(0) ;
144  while ((infile >> id >> dx1 >> dx2 >> dxerr ) ) {
145  setDx(id,dx1,dx2,dxerr) ;
146  ATH_MSG_DEBUG (" read from file: dx1 " << dx1 << " dx2 " << dx2 << " dxerr " << dxerr);
147  ++nentries ;
148  }
149  size_t dxfootprint = dxcontainer->footprint() ;
150  ATH_MSG_INFO (" read " << nentries << " from file. ");
151  ATH_MSG_INFO (" dx footprints " << dxfootprint);
152  ATH_MSG_INFO (" (no compression) ");
153 
154  // force a call back in the geometry
155  int i(0);
156  std::list<std::string> keys ;
157  (const_cast<InDetDD::TRT_DetectorManager*>(m_trtman))->align(i,keys).ignore() ;
158 
159  return StatusCode::SUCCESS ;
160 }
161 
162 
164 {
165  ATH_MSG_INFO ("entering streamOutObjects ");
166 
167  // Get Output Stream tool for writing
168  ATH_CHECK( m_streamer.retrieve() );
169 
170  IAthenaOutputStreamTool* streamer=const_cast<IAthenaOutputStreamTool*>(&(*m_streamer));
171 
172  ATH_CHECK( streamer->connectOutput() );
173 
176  getDxContainer()->crunch() ;
177 
178  ATH_CHECK( streamer->streamObjects(typeKeys) );
179  ATH_CHECK( streamer->commitOutput() );
180 
181  ATH_MSG_INFO (" Streamed out and committed " << typeKeys.size() << " objects ");
182  return StatusCode::SUCCESS;
183 }
184 
185 
186 StatusCode TRT_StrawAlignDbSvc::registerObjects(std::string tag, int run1, int event1, int run2, int event2) const
187 {
188  ATH_MSG_INFO ("registerObjects with IOV ");
189  ATH_MSG_INFO ("Run/evt1 [" << run1 << "," << event1 << "]");
190  ATH_MSG_INFO ("Run/evt2 [" << run2 << "," << event2 << "]");
191 
192  // get pointer to registration svc
193  IIOVRegistrationSvc* regsvc;
194  ATH_CHECK( service("IOVRegistrationSvc",regsvc) );
195 
196  if (StatusCode::SUCCESS==regsvc->registerIOV(StrawDxContainer::classname(),
197  m_par_dxcontainerkey,tag,run1,run2,event1,event2))
198  ATH_MSG_INFO ("Registered StrawDxContainer object with key " << m_par_dxcontainerkey);
199  else
200  ATH_MSG_ERROR ("Could not register StrawDxContainer object with key " << m_par_dxcontainerkey);
201 
202  return( StatusCode::SUCCESS);
203 }
204 
205 
207 {
208  for (std::list<std::string>::const_iterator
209  itr=keys.begin(); itr!=keys.end(); ++itr)
210  ATH_MSG_INFO (" IOVCALLBACK for key " << *itr << " number " << I);
211 
212  // if constants need to be read from textfile, we use the call back routine to refill the IOV objects
214 
215  return StatusCode::SUCCESS;
216 }
217 
TRT_StrawAlignDbSvc::m_trtman
const InDetDD::TRT_DetectorManager * m_trtman
id helper
Definition: TRT_StrawAlignDbSvc.h:102
TRT_StrawAlignDbSvc::getConstDxContainer
const StrawDxContainer * getConstDxContainer() const
Definition: TRT_StrawAlignDbSvc.h:121
IAthenaOutputStreamTool::TypeKeyPairs
std::vector< TypeKeyPair > TypeKeyPairs
Definition: IAthenaOutputStreamTool.h:99
TRT_StrawAlignDbSvc::m_streamer
ToolHandle< IAthenaOutputStreamTool > m_streamer
persistifiable container
Definition: TRT_StrawAlignDbSvc.h:107
run.infile
string infile
Definition: run.py:13
TRT_StrawAlignDbSvc::setDx
void setDx(const TRTCond::ExpandedIdentifier &id, float dx1, float dx2, float dxerr)
set endpoints for a TRTCond::ExpandedIdentifier
Definition: TRT_StrawAlignDbSvc.h:145
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TRT_DetectorManager.h
TRT_StrawAlignDbSvc::IOVCallBack
StatusCode IOVCallBack(IOVSVC_CALLBACK_ARGS)
IOV call back for dx objects. normally this doesn't do anything.
Definition: TRT_StrawAlignDbSvc.cxx:206
TRT_StrawAlignDbSvc.h
interface to TRT straw alignment constants
TRT_StrawAlignDbSvc::m_par_forcecallback
bool m_par_forcecallback
input text file
Definition: TRT_StrawAlignDbSvc.h:100
IAthenaOutputStreamTool
This is a tool that allows streaming out of DataObjects. This has been factorized out from AthenaOutp...
Definition: IAthenaOutputStreamTool.h:70
skel.it
it
Definition: skel.GENtoEVGEN.py:423
IIOVRegistrationSvc.h
This is an interface to a tool used to register conditions objects in the Interval of Validity (IOV) ...
IOVSVC_CALLBACK_ARGS_P
#define IOVSVC_CALLBACK_ARGS_P(I, K)
short hand for IOVSvc call back argument list, to be used when access to formal arguments is needed,...
Definition: IOVSvcDefs.h:42
IIOVRegistrationSvc::registerIOV
virtual StatusCode registerIOV(const std::string &typeName) const =0
Register IOV DB for an object given its typeName - run/LB numbers interval or times interval and tag ...
TRTCond::NestedContainerBase< NestingLevel, NestedContainer< NestingLevel+1, StrawDx, StrawDxTrait >, StrawDx, StrawDxTrait >::FlatContainer
std::vector< std::pair< ExpandedIdentifier, const StrawDx * > > FlatContainer
fill vector with all entries in the container. needed to dump to Peter's flat file format
Definition: NestedContainer.h:346
TRT_StrawAlignDbSvc::readTextFile
virtual StatusCode readTextFile(const std::string &file)
read calibration from text file into TDS
Definition: TRT_StrawAlignDbSvc.cxx:124
TRT_StrawAlignDbSvc::writeTextFile
StatusCode writeTextFile(const std::string &file) const
write calibration constants to flat text file
Definition: TRT_StrawAlignDbSvc.cxx:89
IAthenaOutputStreamTool.h
Interface to an output stream tool.
TRT_StrawAlignDbSvc::TRT_StrawAlignDbSvc
TRT_StrawAlignDbSvc(const std::string &name, ISvcLocator *pSvcLocator)
constructor
Definition: TRT_StrawAlignDbSvc.cxx:27
PlotCalibFromCool.nentries
nentries
Definition: PlotCalibFromCool.py:798
TRT_StrawAlignDbSvc::m_dxcontainer
const DataHandle< StrawDxContainer > m_dxcontainer
detector manager
Definition: TRT_StrawAlignDbSvc.h:105
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TRT_StrawAlignDbSvc::~TRT_StrawAlignDbSvc
virtual ~TRT_StrawAlignDbSvc()
destructor
TRT_StrawAlignDbSvc::registerObjects
StatusCode registerObjects(std::string tag, int run1, int event1, int run2, int event2) const
register calibration objects with the IoV service
Definition: TRT_StrawAlignDbSvc.cxx:186
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
maskDeadModules.run1
run1
Definition: maskDeadModules.py:40
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
AthService
Definition: AthService.h:32
TRT_StrawAlignDbSvc::m_detStore
ServiceHandle< StoreGateSvc > m_detStore
Definition: TRT_StrawAlignDbSvc.h:97
IAthenaOutputStreamTool::connectOutput
virtual StatusCode connectOutput(const std::string &outputName="")=0
Connect to the output stream Must connectOutput BEFORE streaming Only specify "outputName" if one wan...
IAthenaOutputStreamTool::TypeKeyPair
std::pair< std::string, std::string > TypeKeyPair
Stream out objects.
Definition: IAthenaOutputStreamTool.h:98
IIOVRegistrationSvc
This is an interface to a service used to register conditions objects in the Interval of Validity (IO...
Definition: IIOVRegistrationSvc.h:52
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ReadCellNoiseFromCoolCompare.run2
run2
Definition: ReadCellNoiseFromCoolCompare.py:53
TRT_StrawAlignDbSvc::m_par_strawtextfile
std::string m_par_strawtextfile
folder name
Definition: TRT_StrawAlignDbSvc.h:99
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:194
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TRTCond::StrawDxContainer
Definition: StrawDxContainer.h:109
TRTCond::StrawDxContainerTemplate::getDx1
float getDx1(const ExpandedIdentifier &id) const
access to unpacked dx
Definition: StrawDxContainer.h:61
IAthenaOutputStreamTool::commitOutput
virtual StatusCode commitOutput(bool doCommit=false)=0
Commit the output stream after having streamed out objects Must commitOutput AFTER streaming.
InDetDD::TRT_DetectorManager
The Detector Manager for all TRT Detector elements, it acts as the interface to the detector elements...
Definition: TRT_DetectorManager.h:69
TRTCond::StrawDxContainerTemplate< ExpandedIdentifier::DETECTOR >::classname
static const char * classname()
classname used to talk to iovservice
Definition: StrawDxContainer.h:100
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TRT_StrawAlignDbSvc::getDxContainer
StrawDxContainer * getDxContainer() const
access to containers
Definition: TRT_StrawAlignDbSvc.h:115
TRT_StrawAlignDbSvc::streamOutObjects
StatusCode streamOutObjects() const
write the calibration objects to output, after cleaning
Definition: TRT_StrawAlignDbSvc.cxx:163
TRTCond::StrawDxContainerTemplate::getDxErr
float getDxErr(const ExpandedIdentifier &id) const
access to unpacked dx error
Definition: StrawDxContainer.h:70
TRT_StrawAlignDbSvc::m_trtid
const TRT_ID * m_trtid
force callback to manager
Definition: TRT_StrawAlignDbSvc.h:101
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
TRTCond::NestedContainerBase::clear
void clear()
clear entire container
Definition: NestedContainer.h:238
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
I
#define I(x, y, z)
Definition: MD5.cxx:116
TRTCond::NestedContainerBase::getall
void getall(FlatContainer &entries) const
Definition: NestedContainer.h:347
TRT_StrawAlignDbSvc::finalize
virtual StatusCode finalize()
tool finalize
Definition: TRT_StrawAlignDbSvc.cxx:83
TRTCond::ExpandedIdentifier
Identifier for TRT detector elements in the conditions code.
Definition: InnerDetector/InDetConditions/TRT_ConditionsData/TRT_ConditionsData/ExpandedIdentifier.h:30
IAthenaOutputStreamTool::streamObjects
virtual StatusCode streamObjects(const TypeKeyPairs &typeKeys, const std::string &outputName="")=0
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
StoreGateSvc.h
TRTCond::StrawDxContainerTemplate::getDx2
float getDx2(const ExpandedIdentifier &id) const
Definition: StrawDxContainer.h:65
TRTCond::NestedContainerBase::footprint
size_t footprint() const
return the memory allocated by the container and its daughters
Definition: NestedContainer.h:282
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
Definition: TRT_StrawAlignDbSvc.cxx:25
TRT_StrawAlignDbSvc::initialize
virtual StatusCode initialize()
tool initialize
Definition: TRT_StrawAlignDbSvc.cxx:49
TRT_StrawAlignDbSvc::m_par_dxcontainerkey
std::string m_par_dxcontainerkey
Definition: TRT_StrawAlignDbSvc.h:98
TRTCond::NestedContainerBase::crunch
void crunch()
reduce the footprint as much as possible by removing the extra space allocated by the vectors
Definition: NestedContainer.h:292