ATLAS Offline Software
Loading...
Searching...
No Matches
TRT_StrawAlignDbSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
9
10#include "TRT_StrawAlignDbSvc.h"
11#include <fstream>
12#include <iostream>
13#include <iomanip>
14#include <sstream>
15
17
18#include "GaudiKernel/IToolSvc.h"
21
22#include "Identifier/Identifier.h"
24
25ATLAS_NO_CHECK_FILE_THREAD_SAFETY; // This class uses const_cast, regFcn (callback) and DataHandle. Legacy code
26
28 ISvcLocator* pSvcLocator )
29 : base_class(name,pSvcLocator),
30 m_detStore("DetectorStore",name),
31 m_par_dxcontainerkey("/TRT/Calib/DX"),
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 /*
61 ATH_MSG_INFO (" dx container exists - reg callback ");
62 if( (m_detStore->regFcn(&TRT_StrawAlignDbSvc::IOVCallBack,this,m_dxcontainer,m_par_dxcontainerkey)).isFailure())
63 ATH_MSG_ERROR ("Could not register IOV callback for key: " << m_par_dxcontainerkey);
64 */
65 ATH_MSG_DEBUG(" dx container exists - do nothing ");
66
67 } else {
68
69 // create, record and update data handle
70 ATH_MSG_INFO ("Creating new dx container");
71 auto dxcontainer = std::make_unique<StrawDxContainer>();
72
73 // reading from file
74 if( !m_par_strawtextfile.empty() ) {
75 ATH_CHECK( this->readTextFile(dxcontainer.get(),
77 }
78
79 ATH_CHECK( m_detStore->record(std::move(dxcontainer), m_par_dxcontainerkey) );
80 }
81
82 return StatusCode::SUCCESS;
83}
84
85
87{
88 ATH_MSG_INFO ("TRT_StrawAlignDbSvc finalize method called");
89 return StatusCode::SUCCESS;
90}
91
92StatusCode TRT_StrawAlignDbSvc::writeTextFile(const std::string& filename) const
93{
94 ATH_MSG_INFO (" Write straw alignment data to text file " << filename);
95 std::ofstream outfile(filename.c_str());
96
97
98 // first reduce the container as much as possible
99 // getDxContainer()->crunch() ;
100
101 StrawDxContainer::FlatContainer packedstrawdata ;
102 const StrawDxContainer* dxcontainer = getConstDxContainer();
103 dxcontainer->getall( packedstrawdata ) ;
104
105 // now, we store the entries
106 ATH_MSG_INFO ("Number of entries in flatcontainer: "
107 << packedstrawdata.size());
108
109 for(auto & it : packedstrawdata) {
110 const TRTCond::ExpandedIdentifier& calid = it.first ;
111 // get the end point corrections. if not the right type, store zeros.
112 float dx1=dxcontainer->getDx1(calid) ;
113 float dx2=dxcontainer->getDx2(calid) ;
114 float dxerr=dxcontainer->getDxErr(calid) ;
115 outfile << calid << " "
116 << std::setprecision(5)
117 << std::setw(12) << dx1 << " "
118 << std::setw(12) << dx2 << " "
119 << std::setw(12) << dxerr << std::endl ;
120 }
121 outfile.close() ;
122 return StatusCode::SUCCESS ;
123}
124
125
126
127StatusCode TRT_StrawAlignDbSvc::readTextFile(const std::string& filename)
128{
129 return readTextFile( getDxContainer(), filename );
130}
131
132
133StatusCode TRT_StrawAlignDbSvc::readTextFile(StrawDxContainer* dxcontainer,
134 const std::string& filename)
135{
136 ATH_MSG_INFO ("Reading straw alignment data from text file " << filename);
137
138 if(!dxcontainer) {
139 ATH_MSG_WARNING (" Could not find the container ");
140 return StatusCode::FAILURE;
141 }
142 dxcontainer->clear() ;
143 std::ifstream infile(filename.c_str()) ;
145 float dx1,dx2,dxerr;
146 int nentries(0) ;
147 while ((infile >> id >> dx1 >> dx2 >> dxerr ) ) {
148 setDx(id,dx1,dx2,dxerr) ;
149 ATH_MSG_DEBUG (" read from file: dx1 " << dx1 << " dx2 " << dx2 << " dxerr " << dxerr);
150 ++nentries ;
151 }
152 size_t dxfootprint = dxcontainer->footprint() ;
153 ATH_MSG_INFO (" read " << nentries << " from file. ");
154 ATH_MSG_INFO (" dx footprints " << dxfootprint);
155 ATH_MSG_INFO (" (no compression) ");
156
157 // force a call back in the geometry
158// (const_cast<InDetDD::TRT_DetectorManager*>(m_trtman))->align().ignore() ;
159
160 return StatusCode::SUCCESS ;
161}
162
163
165{
166 ATH_MSG_INFO ("entering streamOutObjects ");
167
168 // Get Output Stream tool for writing
169 ATH_CHECK( m_streamer.retrieve() );
170
171 IAthenaOutputStreamTool* streamer=const_cast<IAthenaOutputStreamTool*>(&(*m_streamer));
172
173 ATH_CHECK( streamer->connectOutput() );
174
176 typeKeys.push_back( IAthenaOutputStreamTool::TypeKeyPair(StrawDxContainer::classname(),m_par_dxcontainerkey)) ;
177 getDxContainer()->crunch() ;
178
179 ATH_CHECK( streamer->streamObjects(typeKeys) );
180 ATH_CHECK( streamer->commitOutput() );
181
182 ATH_MSG_INFO (" Streamed out and committed " << typeKeys.size() << " objects ");
183 return StatusCode::SUCCESS;
184}
185
186
187StatusCode TRT_StrawAlignDbSvc::registerObjects(std::string tag, int run1, int event1, int run2, int event2) const
188{
189 ATH_MSG_INFO ("registerObjects with IOV ");
190 ATH_MSG_INFO ("Run/evt1 [" << run1 << "," << event1 << "]");
191 ATH_MSG_INFO ("Run/evt2 [" << run2 << "," << event2 << "]");
192
193 // get pointer to registration svc
194 SmartIF<IIOVRegistrationSvc> regsvc{service("IOVRegistrationSvc")};
195 ATH_CHECK( regsvc.isValid() );
196
197 if (StatusCode::SUCCESS==regsvc->registerIOV(StrawDxContainer::classname(),
198 m_par_dxcontainerkey,tag,run1,run2,event1,event2))
199 ATH_MSG_INFO ("Registered StrawDxContainer object with key " << m_par_dxcontainerkey);
200 else
201 ATH_MSG_ERROR ("Could not register StrawDxContainer object with key " << m_par_dxcontainerkey);
202
203 return( StatusCode::SUCCESS);
204}
205
206
208{
209 /*
210 for (std::list<std::string>::const_iterator
211 itr=keys.begin(); itr!=keys.end(); ++itr)
212 ATH_MSG_INFO (" IOVCALLBACK for key " << *itr << " number " << I);
213 */
214
215 // if constants need to be read from textfile, we use the call back routine to refill the IOV objects
217
218 return StatusCode::SUCCESS;
219}
220
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Interface to an output stream tool.
This is an interface to a tool used to register conditions objects in the Interval of Validity (IOV) ...
interface to TRT straw alignment constants
#define ATLAS_NO_CHECK_FILE_THREAD_SAFETY
This is a tool that allows streaming out of DataObjects.
virtual StatusCode streamObjects(const TypeKeyPairs &typeKeys, const std::string &outputName="")=0
std::pair< std::string, std::string > TypeKeyPair
Stream out objects.
virtual StatusCode connectOutput(const std::string &outputName="")=0
Connect to the output stream Must connectOutput BEFORE streaming Only specify "outputName" if one wan...
std::vector< TypeKeyPair > TypeKeyPairs
virtual StatusCode commitOutput(bool doCommit=false)=0
Commit the output stream after having streamed out objects Must commitOutput AFTER streaming.
TRT_StrawAlignDbSvc(const std::string &name, ISvcLocator *pSvcLocator)
constructor
virtual StatusCode readTextFile(const std::string &file)
read calibration from text file into TDS
virtual ~TRT_StrawAlignDbSvc()
destructor
StatusCode writeTextFile(const std::string &file) const
write calibration constants to flat text file
const InDetDD::TRT_DetectorManager * m_trtman
void setDx(const TRTCond::ExpandedIdentifier &id, float dx1, float dx2, float dxerr)
set endpoints for a TRTCond::ExpandedIdentifier
virtual StatusCode finalize()
tool finalize
StatusCode streamOutObjects() const
write the calibration objects to output, after cleaning
StatusCode IOVCallBack()
IOV call back for dx objects. normally this doesn't do anything.
virtual StatusCode initialize()
tool initialize
StrawDxContainer * getDxContainer() const
access to containers
StatusCode registerObjects(std::string tag, int run1, int event1, int run2, int event2) const
register calibration objects with the IoV service
const StrawDxContainer * getConstDxContainer() const
ServiceHandle< StoreGateSvc > m_detStore
ToolHandle< IAthenaOutputStreamTool > m_streamer