ATLAS Offline Software
Loading...
Searching...
No Matches
CaloCellGroup.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <cctype>
7#include <cstdlib>
8#include <limits>
9
11 m_pos(false),
12 m_neg(false),
13 m_emb(false),
14 m_emecow(false),
15 m_emeciw(false),
16 m_hec(false),
17 m_fcal(false),
18 m_caloCellId(nullptr)
19{
20}
21
22
23bool CaloCellGroup::setDefinition(const CaloCell_Base_ID* caloCellId, const std::string& definition, MsgStream& logStr) {
24 m_caloCellId=caloCellId;
25 m_fieldBoundaries.clear();
26 m_values.clear();
27 m_emb=false;
28 m_emecow=false;
29 m_emeciw=false;
30 m_hec=false;
31 m_fcal=false;
32 m_pos=false;
33 m_neg=false;
34
35 std::vector<std::string> subdefs(2+NFIELDS);
36 std::string valuesStr;
37
38 std::size_t pos1,pos2;
39 pos1=definition.find('[');
40 if( pos1==std::string::npos) {
41 complain(logStr,definition);
42 return false;
43 }
44
45 std::size_t i,iField=0;
46 for (i=1+pos1;i<definition.size();i++) {
47 char c=definition[i];
48 if (isspace(c)) continue;
49 if (c==']') break;
50 if (c=='/') {
51 //std::cout << "Subfield " << iField << ": " << subdefs[iField] << std::endl;
52 iField++;
53 if (iField >= subdefs.size()) break;
54 }
55 else
56 subdefs[iField].push_back(c);
57 }
58
59 if (iField!=subdefs.size()-1 || i==definition.size()) {
60 logStr << MSG::ERROR << "Definition String [" << definition << "] malformed. Expect "
61 << NFIELDS << " fileds separated by '/'" << endmsg;
62 return false;
63 }
64
65 valuesStr=definition.substr(i+1);
66 //std::cout << "ValueStr " << valuesStr << std::endl;
67
68 //Interpret subcalo-string (first field)
69 std::string& subCaloStr=subdefs[0];
70 size_t len;
71 pos1=0;
72 do {
73 pos2=subCaloStr.find(',',pos1);
74 if (pos2==std::string::npos)
75 len=subCaloStr.size();
76 else {
77 len=pos2-pos1;
78 pos1=pos2+1;
79 }
80 if (subCaloStr.compare(pos1,len,"EMB")==0)
81 m_emb=true;
82 else if(subCaloStr.compare(pos1,len,"EMECIW")==0)
83 m_emeciw=true;
84 else if(subCaloStr.compare(pos1,len,"EMECOW")==0)
85 m_emecow=true;
86 else if(subCaloStr.compare(pos1,len,"HEC")==0)
87 m_hec=true;
88 else if(subCaloStr.compare(pos1,len,"FCAL")==0)
89 m_fcal=true;
90 else {
91 logStr << MSG::ERROR << "Unknown sub-calo name '"<< subCaloStr.substr(pos1,len) <<"'found. "
92 << "Allowed values are EMB, EMECIW, EMECOW, HEC and FCAL" << endmsg;
93 return false;
94 }
95 }
96 while(pos2!=std::string::npos);
97
98 //Interpret side-field (second field)
99 if (subdefs[1].size()!=1) {
100 complain(logStr,subdefs[1]);
101 return false;
102 }
103
104 if (subdefs[1][0]=='A')
105 m_pos=true;
106 else if (subdefs[1][0]=='C')
107 m_neg=true;
108 else if (subdefs[1][0]=='*')
109 m_pos=m_neg=true;
110 else {
111 logStr << MSG::ERROR << "Allowed values for side are: A,C or * (means both), found " << subdefs[1] << endmsg;
112 return false;
113 }
114 //Interpret remaining fields
115 for (std::size_t i=0;i<NFIELDS;i++) {
116 std::string& subdef=subdefs[i+2];
117 int lower=std::numeric_limits<int>::min();
118 int upper=std::numeric_limits<int>::max();
119 if (subdef.compare("*")!=0) {
120 std::size_t pos1=subdef.find('-');
121 if (pos1==std::string::npos) {
122 if (isdigit(subdef[0]))
123 lower=upper=atol(subdef.c_str());
124 else
125 logStr << MSG::ERROR << "Malformed string [" << subdef << "], expected numerical value!" << endmsg;
126 }
127 else {//Found '-'
128 std::string lStr=subdef.substr(0,pos1);
129 std::string uStr=subdef.substr(pos1+1);
130 if (!lStr.empty()) {
131 if (isdigit(lStr[0]))
132 lower=atol(lStr.c_str());
133 else
134 logStr << MSG::ERROR << "Malformed string [" << subdef << "], expected numerical value!" << endmsg;
135 }
136 if (!uStr.empty()) {
137 if (isdigit(uStr[0]))
138 upper=atol(uStr.c_str());
139 else
140 logStr << MSG::ERROR << "Malformed string [" << subdef << "], expected numerical value!" << endmsg;
141 }
142 }
143 }//end not *
144 m_fieldBoundaries.emplace_back(lower,upper);
145 }//end loop over fields
146
147
148 const char* ptr1=valuesStr.c_str();
149 char* ptr2;
150 float val;
151 //errno=0;
152 do {
153 val=strtof(ptr1,&ptr2);
154 if (ptr1!=ptr2)
155 m_values.push_back(val);
156 ptr1=const_cast<const char*>(ptr2+1);
157 }
158 while(*ptr2!='\0');
159
160 logStr << MSG::INFO << "Sussefully parsed defintion string" << endmsg;
161 return true;
162}
163
164
165
166bool CaloCellGroup::inGroup(const Identifier id) const {
167 //std::cout << "Check if in Group..";
168 if (!m_caloCellId) {
169 //throw exception;
170 //std::cout << "No Calo Cell Id" << std::endl;
171 return false;
172 };
173
174 if (!m_caloCellId->is_lar(id))
175 return false; //Not a lar cell
176
177 //std::cout << "have calo..";
178
179 if (m_caloCellId->is_em_barrel(id) && !m_emb) return false;
180 if (m_caloCellId->is_em_endcap_inner(id) && !m_emeciw) return false;
181 if (m_caloCellId->is_em_endcap_outer(id) && !m_emecow) return false;
182 if (m_caloCellId->is_hec(id) && !m_hec) return false;
183 if (m_caloCellId->is_fcal(id) && !m_fcal) return false;
184
185 //std::cout << "have subcalo..";
186
187 //Check side
188 const int pn=m_caloCellId->pos_neg(id);
189 if (pn<0 && !m_neg) return false;
190 if (pn>0 && !m_pos) return false;
191
192 //std::cout << "have side..";
193
194 const int layer=m_caloCellId->sampling(id);
195 if (layer<m_fieldBoundaries[0].first || layer>m_fieldBoundaries[0].second) return false;
196
197 //std::cout << "have layer..";
198
199 const int region=m_caloCellId->region(id);
200 if (region<m_fieldBoundaries[1].first || region>m_fieldBoundaries[1].second) return false;
201
202 //std::cout << "have region..";
203
204 const int eta=m_caloCellId->eta(id);
205 if (eta<m_fieldBoundaries[2].first || eta>m_fieldBoundaries[2].second) return false;
206
207
208 //std::cout << "have eta..";
209
210 const int phi=m_caloCellId->phi(id);
211 if (phi<m_fieldBoundaries[3].first || phi>m_fieldBoundaries[3].second) return false;
212
213 //std::cout << "have phi..match! ";
214 return true;
215}
216
217
218
219void CaloCellGroup::complain(MsgStream& logStr, const std::string& problem) {
220 logStr << MSG::ERROR << "Malformed definition string '"<<problem << "'" << endmsg;
221}
222
223
224
225
226bool CaloCellGroupList::setDefinition(const CaloCell_Base_ID* caloCellId, const std::vector<std::string>& definitions,
227 MsgStream& logStr){
228
229 m_groups.clear();
230 m_defaults.clear();
231
232 if (definitions.empty()) {
233 logStr << MSG::WARNING << "No definition string found" << endmsg;
234 return true;
235 }
236
237
238 const std::string& defaultValueStr=definitions[0];
239
240 const char* ptr1=defaultValueStr.c_str();
241 char* ptr2;
242 float val;
243 //errno=0;
244 do {
245 val=strtof(ptr1,&ptr2);
246 if (ptr1!=ptr2)
247 m_defaults.push_back(val);
248 ptr1=const_cast<const char*>(ptr2+1);
249 }
250 while(*ptr2!='\0');
251
252
253 m_groups.resize(definitions.size()-1);
254 for (size_t i=0;i<m_groups.size();i++) {
255 bool s=m_groups[i].setDefinition(caloCellId,definitions[i+1],logStr);
256 if (!s) return false;
257 if (m_groups[i].getValue().size()!=m_defaults.size()) {
258 logStr << MSG::ERROR << "Incosistent number of values!" << endmsg;
259 return false;
260 }
261 }
262 m_initialized=true;
263 return true;
264}
265
266
267const std::vector<float>& CaloCellGroupList::valuesForCell(const Identifier id) {
268 if (!m_initialized) {
269 //throw exception
270 return m_defaults;
271 }
272
273 const std::vector<float>* result=&m_defaults;
274
275 std::vector<CaloCellGroup>::const_iterator it=m_groups.begin();
276 std::vector<CaloCellGroup>::const_iterator it_e=m_groups.end();
277 for(;it!=it_e;++it) {
278 //std::cout << " G " << i++;
279 if (it->inGroup(id)) result=&(it->getValue());
280 }
281 return *result;
282}
283
285
286 std::cout << "Definitions: " << std::endl;
287 for (size_t i=0;i<m_groups.size();i++) {
288 std::cout << "Group " << i << ":" << std::endl;
289 m_groups[i].printDef();
290 }
291 std::cout << "Results: "<< std::endl;
292 for (const Identifier& id : caloCellId->cell_range()) {
293 std::cout << "Values for " << caloCellId->show_to_string(id);
294 const std::vector<float>& x=this->valuesForCell(id);
295 std::cout << " [";
296 for (std::size_t i=0;i<x.size();i++)
297 std::cout << x[i] << " ";
298 std::cout << "]" << std::endl;
299 }
300 }
301
303 std::cout << "Definitions: " << std::endl;
304 for (size_t i=0;i<m_groups.size();i++) {
305 std::cout << "Group " << i << ":" << std::endl;
306 m_groups[i].printDef();
307 }
308
309
310
311}
312
314
315 std::cout << "Values";
316 for (std::size_t i=0;i<m_values.size();i++)
317 std::cout << " " << m_values[i];
318 std::cout << " will be used for the following region:" << std::endl;
319 std::cout << "SubCalos: " <<m_emb<<m_emecow<<m_emeciw<<m_hec <<m_fcal <<std::endl;
320 std::cout << "pos=" << m_pos << " neg=" << m_neg << std::endl;
321 std::cout << "Layer [" << m_fieldBoundaries[0].first << " - " << m_fieldBoundaries[0].second << "]" << std::endl;
322 std::cout << "Region [" << m_fieldBoundaries[1].first << " - " << m_fieldBoundaries[1].second << "]" << std::endl;
323 std::cout << "Eta [" << m_fieldBoundaries[2].first << " - " << m_fieldBoundaries[2].second << "]" << std::endl;
324 std::cout << "Phi [" << m_fieldBoundaries[3].first << " - " << m_fieldBoundaries[3].second << "]" << std::endl << std::endl;
325}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define endmsg
int upper(int c)
#define x
bool setDefinition(const CaloCell_Base_ID *caloCellId, const std::vector< std::string > &definitions, MsgStream &logStr)
std::vector< CaloCellGroup > m_groups
std::vector< float > m_defaults
void dump(const CaloCell_Base_ID *caloCellId)
void printDef() const
const std::vector< float > & valuesForCell(const Identifier id)
std::vector< float > m_values
bool setDefinition(const CaloCell_Base_ID *caloCellId, const std::string &definition, MsgStream &logStr)
void printDef() const
static void complain(MsgStream &, const std::string &)
std::vector< std::pair< int, int > > m_fieldBoundaries
bool inGroup(const Identifier id) const
const CaloCell_Base_ID * m_caloCellId
Helper base class for offline cell identifiers.
std::string show_to_string(const Identifier id, const IdContext *context=0, char sep='.') const
id_range cell_range() const
Range over full set of Identifiers (LAr + Tiles)