40 {
41 ATH_MSG_DEBUG(
"PixelDistortionAlg::execute(const EventContext& ctx) const");
42
43 SG::WriteCondHandle<PixelDistortionData> writeHandle(
m_writeKey, ctx);
44 if (writeHandle.isValid()) {
45 ATH_MSG_DEBUG(
"CondHandle " << writeHandle.fullKey() <<
" is already valid.. In theory this should not be called, but may happen if multiple concurrent events are being processed out of order.");
46 return StatusCode::SUCCESS;
47 }
48
49
50 std::unique_ptr<PixelDistortionData> writeCdo(std::make_unique<PixelDistortionData>());
51
52
53
54 constexpr int nmodule_max = 2048;
55 std::unordered_map<uint32_t,std::vector<float>> distortionMap;
56 std::unordered_map<uint32_t,unsigned long long>
ids;
60 for (
int i=0;
i<nmodule_max;
i++) {
61 distortionMap[
i].push_back(0.0);
62 distortionMap[
i].push_back(0.0);
63 distortionMap[
i].push_back(0.0);
64 }
65 }
69 for (
int i=0;
i<nmodule_max;
i++) {
70 distortionMap[
i].push_back(0.1);
71 distortionMap[
i].push_back(0.1);
72 distortionMap[
i].push_back(2.0*std::atan(0.0005)/CLHEP::degree);
73 }
74 }
78 ATH_MSG_ERROR(
"Distortion filename is empty not found! No pixel distortion will be applied.");
79 return StatusCode::FAILURE;
80 }
81
82 ATH_MSG_DEBUG(
"Reading pixel distortions from file: " << file_name);
84
85 if (file_name[0] != '/') {
87 }
88 std::ifstream
input(file_name);
91 return StatusCode::FAILURE;
92 }
93
94 int distosize;
96 else distosize = 441;
97
98 while (!
input.eof()) {
99 unsigned int idmod{};
100 unsigned int hashID {};
102
105 hashID = idmod;
106 } else {
107 input >> std::hex >> idmod >> std::dec;
108 hashID =
m_pixelID->wafer_hash((Identifier)idmod);
109 }
110
111
112
113 Identifier modId =
m_pixelID->wafer_id((IdentifierHash)hashID);
115 ATH_MSG_DEBUG(
"Identifier = 0x" << std::hex << ids[hashID] << std::dec);
116
118 for (
int i = 0;
i < distosize; ++
i) {
121 distortionMap[hashID].push_back(
data);
122 }
124 }
126 }
130
131 ATHRNG::RNGWrapper* rngWrapper =
m_rndmSvc->getEngine(
this);
132 CLHEP::HepRandomEngine *rndmEngine = rngWrapper->
getEngine(ctx);
133
134 constexpr double distortionMeanR{0.12/CLHEP::meter};
135 constexpr double distortionRMSR{0.08};
136 constexpr double distortionMeanTwist{-0.0005};
137
138 for (
int i=0;
i<nmodule_max;
i++) {
139 float r1 = CLHEP::RandGaussZiggurat::shoot(rndmEngine,distortionMeanR,distortionRMSR);
140 float r2 = CLHEP::RandGaussZiggurat::shoot(rndmEngine,r1,distortionRMSR/10.);
141 float twist = CLHEP::RandGaussZiggurat::shoot(rndmEngine,distortionMeanTwist,distortionMeanTwist);
142 distortionMap[
i].push_back(r1*CLHEP::meter);
143 distortionMap[
i].push_back(r2*CLHEP::meter);
144 distortionMap[
i].push_back(2.0*std::atan(twist)/CLHEP::degree);
145 }
146 }
149 SG::ReadCondHandle<DetCondCFloat> readHandle(
m_readKey, ctx);
150 const DetCondCFloat* readCdo = *readHandle;
151 if (readCdo==nullptr) {
153 return StatusCode::FAILURE;
154 }
155
156 ATH_MSG_DEBUG(
"Size of DetCondCFloat " << readHandle.fullKey() <<
" readCdo->size()= " << readCdo->
size());
157
159 if (readCdo->
tag()==
"/Indet/PixelDist") {
161 ATH_MSG_INFO(
"Detected old version of pixel distortions data.");
162 }
163 else {
164 bool gotVersion = false;
165
166
167 std::string baseStr = "/Indet/PixelDist_v";
168 if (readCdo->
tag().compare(0,baseStr.size(),baseStr)==0) {
169 std::istringstream istr(readCdo->
tag().substr(baseStr.size()));
170 int version_tmp = 0;
171 istr >> version_tmp;
172 if (istr.eof()) {
174 gotVersion = true;
175 }
176 }
177 if (!gotVersion) {
178 ATH_MSG_WARNING(
"Unable to determine version number of pixel distortions data. Version string: " << readCdo->
tag());
179 }
180 }
181 writeCdo -> setVersion(version);
183
184 int distosize = readCdo->
size();
185
186 for (
int i=0;
i<nmodule_max;
i++) {
188 Identifier modId =
m_pixelID->wafer_id((IdentifierHash)i);
189 const float *disto = readCdo->
find(modId);
191
192 ATH_MSG_DEBUG(
"Identifier = 0x" << std::hex << ids[i] << std::dec);
194 for (int j = 0; j < distosize; ++j) {
195 distortionMap[
i].push_back(disto[j]);
196 s << disto[j] <<
" ";
197 }
199 }
200 }
201 }
202 writeCdo -> setDistortionMap(distortionMap);
203 writeCdo -> setIds(ids);
204
206 std::ofstream*
outfile =
new std::ofstream(
"output_distortion.txt");
207 for (
int i=0;
i<nmodule_max;
i++) {
208 if (!distortionMap[i].
empty()) {
210 *
outfile <<
m_pixelID->wafer_id(IdentifierHash(i)) <<
" " << distortionMap[
i].at(0) <<
" " << distortionMap[
i].at(1) <<
" " << distortionMap[
i].at(2) << std::endl;
211 }
213 *
outfile <<
i <<
" " << distortionMap[
i].at(0) <<
" " << distortionMap[
i].at(1) <<
" " << distortionMap[
i].at(2) << std::endl;
214 }
215 }
216 }
219 }
220
221 const EventIDBase
start{EventIDBase::UNDEFNUM, EventIDBase::UNDEFEVT, 0, 0, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM};
222 const EventIDBase
stop {EventIDBase::UNDEFNUM, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM-1, EventIDBase::UNDEFNUM-1, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM};
224
225 if (writeHandle.record(rangeW, std::move(writeCdo)).isFailure()) {
226 ATH_MSG_FATAL(
"Could not record PixelDistortionData " << writeHandle.key() <<
" with EventRange " << rangeW <<
" into Conditions Store");
227 return StatusCode::FAILURE;
228 }
229 ATH_MSG_DEBUG(
"recorded new CDO " << writeHandle.key() <<
" with range " << rangeW <<
" into Conditions Store");
230
231 return StatusCode::SUCCESS;
232}
#define ATH_MSG_WARNING(x)
char data[hepevt_bytes_allocation_ATLAS]
static const Attributes_t empty
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
const std::string & tag() const
const float * find(const Identifier &ident) const
value_type get_compact() const
Get the compact id.
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
Gaudi::Property< int > m_distortionVersion
Gaudi::Property< std::string > m_inputFileName
SG::ReadCondHandleKey< DetCondCFloat > m_readKey
ServiceHandle< IAthRNGSvc > m_rndmSvc
Random number service.
SG::WriteCondHandleKey< PixelDistortionData > m_writeKey
Gaudi::Property< bool > m_writeToFile
const PixelID * m_pixelID
Gaudi::Property< int > m_distortionInputSource