ATLAS Offline Software
CaloCellGroup.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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 
23 bool 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  if (subCaloStr.compare(pos1,len,"EMB")==0)
79  m_emb=true;
80  else if(subCaloStr.compare(pos1,len,"EMECIW")==0)
81  m_emeciw=true;
82  else if(subCaloStr.compare(pos1,len,"EMECOW")==0)
83  m_emecow=true;
84  else if(subCaloStr.compare(pos1,len,"HEC")==0)
85  m_hec=true;
86  else if(subCaloStr.compare(pos1,len,"FCAL")==0)
87  m_fcal=true;
88  else {
89  logStr << MSG::ERROR << "Unknown sub-calo name '"<< subCaloStr.substr(pos1,len) <<"'found. "
90  << "Allowed values are EMB, EMECIW, EMECOW, HEC and FCAL" << endmsg;
91  return false;
92  }
93  pos1=pos2+1;
94  }
95  while(pos2!=std::string::npos);
96 
97  //Interpret side-field (second field)
98  if (subdefs[1].size()!=1) {
99  complain(logStr,subdefs[1]);
100  return false;
101  }
102 
103  if (subdefs[1][0]=='A')
104  m_pos=true;
105  else if (subdefs[1][0]=='C')
106  m_neg=true;
107  else if (subdefs[1][0]=='*')
108  m_pos=m_neg=true;
109  else {
110  logStr << MSG::ERROR << "Allowed values for side are: A,C or * (means both), found " << subdefs[1] << endmsg;
111  return false;
112  }
113  //Interpret remaining fields
114  for (std::size_t i=0;i<NFIELDS;i++) {
115  std::string& subdef=subdefs[i+2];
116  int lower=std::numeric_limits<int>::min();
118  if (subdef.compare("*")!=0) {
119  std::size_t pos1=subdef.find('-');
120  if (pos1==std::string::npos) {
121  if (isdigit(subdef[0]))
122  lower=upper=atol(subdef.c_str());
123  else
124  logStr << MSG::ERROR << "Malformed string [" << subdef << "], expected numerical value!" << endmsg;
125  }
126  else {//Found '-'
127  std::string lStr=subdef.substr(0,pos1);
128  std::string uStr=subdef.substr(pos1+1);
129  if (!lStr.empty()) {
130  if (isdigit(lStr[0]))
131  lower=atol(lStr.c_str());
132  else
133  logStr << MSG::ERROR << "Malformed string [" << subdef << "], expected numerical value!" << endmsg;
134  }
135  if (!uStr.empty()) {
136  if (isdigit(uStr[0]))
137  upper=atol(uStr.c_str());
138  else
139  logStr << MSG::ERROR << "Malformed string [" << subdef << "], expected numerical value!" << endmsg;
140  }
141  }
142  }//end not *
143  m_fieldBoundaries.emplace_back(lower,upper);
144  }//end loop over fields
145 
146 
147  const char* ptr1=valuesStr.c_str();
148  char* ptr2;
149  float val;
150  //errno=0;
151  do {
152  val=strtof(ptr1,&ptr2);
153  if (ptr1!=ptr2)
154  m_values.push_back(val);
155  ptr1=const_cast<const char*>(ptr2+1);
156  }
157  while(*ptr2!='\0');
158 
159  logStr << MSG::INFO << "Sussefully parsed defintion string" << endmsg;
160  return true;
161 }
162 
163 
164 
165 bool CaloCellGroup::inGroup(const Identifier id) const {
166  //std::cout << "Check if in Group..";
167  if (!m_caloCellId) {
168  //throw exception;
169  //std::cout << "No Calo Cell Id" << std::endl;
170  return false;
171  };
172 
173  if (!m_caloCellId->is_lar(id))
174  return false; //Not a lar cell
175 
176  //std::cout << "have calo..";
177 
178  if (m_caloCellId->is_em_barrel(id) && !m_emb) return false;
179  if (m_caloCellId->is_em_endcap_inner(id) && !m_emeciw) return false;
180  if (m_caloCellId->is_em_endcap_outer(id) && !m_emecow) return false;
181  if (m_caloCellId->is_hec(id) && !m_hec) return false;
182  if (m_caloCellId->is_fcal(id) && !m_fcal) return false;
183 
184  //std::cout << "have subcalo..";
185 
186  //Check side
187  const int pn=m_caloCellId->pos_neg(id);
188  if (pn<0 && !m_neg) return false;
189  if (pn>0 && !m_pos) return false;
190 
191  //std::cout << "have side..";
192 
193  const int layer=m_caloCellId->sampling(id);
194  if (layer<m_fieldBoundaries[0].first || layer>m_fieldBoundaries[0].second) return false;
195 
196  //std::cout << "have layer..";
197 
198  const int region=m_caloCellId->region(id);
199  if (region<m_fieldBoundaries[1].first || region>m_fieldBoundaries[1].second) return false;
200 
201  //std::cout << "have region..";
202 
203  const int eta=m_caloCellId->eta(id);
204  if (eta<m_fieldBoundaries[2].first || eta>m_fieldBoundaries[2].second) return false;
205 
206 
207  //std::cout << "have eta..";
208 
209  const int phi=m_caloCellId->phi(id);
210  if (phi<m_fieldBoundaries[3].first || phi>m_fieldBoundaries[3].second) return false;
211 
212  //std::cout << "have phi..match! ";
213  return true;
214 }
215 
216 
217 
218 void CaloCellGroup::complain(MsgStream& logStr, const std::string& problem) {
219  logStr << MSG::ERROR << "Malformed definition string '"<<problem << "'" << endmsg;
220 }
221 
222 
223 
224 
225 bool CaloCellGroupList::setDefinition(const CaloCell_Base_ID* caloCellId, const std::vector<std::string>& definitions,
226  MsgStream& logStr){
227 
228  m_groups.clear();
229  m_defaults.clear();
230 
231  if (definitions.empty()) {
232  logStr << MSG::WARNING << "No definition string found" << endmsg;
233  return true;
234  }
235 
236 
237  const std::string& defaultValueStr=definitions[0];
238 
239  const char* ptr1=defaultValueStr.c_str();
240  char* ptr2;
241  float val;
242  //errno=0;
243  do {
244  val=strtof(ptr1,&ptr2);
245  if (ptr1!=ptr2)
246  m_defaults.push_back(val);
247  ptr1=const_cast<const char*>(ptr2+1);
248  }
249  while(*ptr2!='\0');
250 
251 
252  m_groups.resize(definitions.size()-1);
253  for (size_t i=0;i<m_groups.size();i++) {
254  bool s=m_groups[i].setDefinition(caloCellId,definitions[i+1],logStr);
255  if (!s) return false;
256  if (m_groups[i].getValue().size()!=m_defaults.size()) {
257  logStr << MSG::ERROR << "Incosistent number of values!" << endmsg;
258  return false;
259  }
260  }
261  m_initialized=true;
262  return true;
263 }
264 
265 
266 const std::vector<float>& CaloCellGroupList::valuesForCell(const Identifier id) {
267  if (!m_initialized) {
268  //throw exception
269  return m_defaults;
270  }
271 
272  const std::vector<float>* result=&m_defaults;
273 
274  std::vector<CaloCellGroup>::const_iterator it=m_groups.begin();
275  std::vector<CaloCellGroup>::const_iterator it_e=m_groups.end();
276  for(;it!=it_e;++it) {
277  //std::cout << " G " << i++;
278  if (it->inGroup(id)) result=&(it->getValue());
279  }
280  return *result;
281 }
282 
283 void CaloCellGroupList::dump(const CaloCell_Base_ID* caloCellId) {
284 
285  std::cout << "Definitions: " << std::endl;
286  for (size_t i=0;i<m_groups.size();i++) {
287  std::cout << "Group " << i << ":" << std::endl;
288  m_groups[i].printDef();
289  }
290  std::cout << "Results: "<< std::endl;
291  for (const Identifier& id : caloCellId->cell_range()) {
292  std::cout << "Values for " << caloCellId->show_to_string(id);
293  const std::vector<float>& x=this->valuesForCell(id);
294  std::cout << " [";
295  for (std::size_t i=0;i<x.size();i++)
296  std::cout << x[i] << " ";
297  std::cout << "]" << std::endl;
298  }
299  }
300 
302  std::cout << "Definitions: " << std::endl;
303  for (size_t i=0;i<m_groups.size();i++) {
304  std::cout << "Group " << i << ":" << std::endl;
305  m_groups[i].printDef();
306  }
307 
308 
309 
310 }
311 
313 
314  std::cout << "Values";
315  for (std::size_t i=0;i<m_values.size();i++)
316  std::cout << " " << m_values[i];
317  std::cout << " will be used for the following region:" << std::endl;
318  std::cout << "SubCalos: " <<m_emb<<m_emecow<<m_emeciw<<m_hec <<m_fcal <<std::endl;
319  std::cout << "pos=" << m_pos << " neg=" << m_neg << std::endl;
320  std::cout << "Layer [" << m_fieldBoundaries[0].first << " - " << m_fieldBoundaries[0].second << "]" << std::endl;
321  std::cout << "Region [" << m_fieldBoundaries[1].first << " - " << m_fieldBoundaries[1].second << "]" << std::endl;
322  std::cout << "Eta [" << m_fieldBoundaries[2].first << " - " << m_fieldBoundaries[2].second << "]" << std::endl;
323  std::cout << "Phi [" << m_fieldBoundaries[3].first << " - " << m_fieldBoundaries[3].second << "]" << std::endl << std::endl;
324 }
CaloCell_Base_ID::is_em_endcap_outer
bool is_em_endcap_outer(const Identifier id) const
test if the id belongs to the EM Endcap outer wheel
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
CaloCellGroup::m_fieldBoundaries
std::vector< std::pair< int, int > > m_fieldBoundaries
Definition: CaloCellGroup.h:52
CaloCellGroup::m_emecow
bool m_emecow
Definition: CaloCellGroup.h:51
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
get_generator_info.result
result
Definition: get_generator_info.py:21
CaloCell_Base_ID::region
int region(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
max
#define max(a, b)
Definition: cfImp.cxx:41
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:64
AtlasDetectorID::is_lar
bool is_lar(Identifier id) const
Definition: AtlasDetectorID.h:689
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
CaloCellGroupList::dump
void dump(const CaloCell_Base_ID *caloCellId)
Definition: CaloCellGroup.cxx:283
CaloCell_Base_ID::pos_neg
int pos_neg(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
skel.it
it
Definition: skel.GENtoEVGEN.py:423
CaloCellGroup::NFIELDS
@ NFIELDS
Definition: CaloCellGroup.h:33
upper
int upper(int c)
Definition: LArBadChannelParser.cxx:49
x
#define x
CaloCell_Base_ID::is_hec
bool is_hec(const Identifier id) const
test if the id belongs to the HEC
python.ZdcRecConfig.pn
pn
Definition: ZdcRecConfig.py:357
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
CaloCellGroup::m_caloCellId
const CaloCell_Base_ID * m_caloCellId
Definition: CaloCellGroup.h:55
CaloCellGroup::m_emb
bool m_emb
Definition: CaloCellGroup.h:51
lumiFormat.i
int i
Definition: lumiFormat.py:92
CaloCell_Base_ID::is_fcal
bool is_fcal(const Identifier id) const
test if the id belongs to the FCAL - true also for MiniFCAL
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
CaloCell_Base_ID::sampling
int sampling(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
CaloCell_Base_ID::is_em_endcap_inner
bool is_em_endcap_inner(const Identifier id) const
test if the id belongs to the EM Endcap inner wheel
CaloCell_Base_ID::eta
int eta(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
CaloCellGroupList::m_initialized
bool m_initialized
Definition: CaloCellGroup.h:77
CaloCellGroup::m_emeciw
bool m_emeciw
Definition: CaloCellGroup.h:51
CaloCellGroup::printDef
void printDef() const
Definition: CaloCellGroup.cxx:312
CaloCell_Base_ID::cell_range
id_range cell_range(void) const
Range over full set of Identifiers (LAr + Tiles)
min
#define min(a, b)
Definition: cfImp.cxx:40
CaloCell_Base_ID::is_em_barrel
bool is_em_barrel(const Identifier id) const
test if the id belongs to the EM barrel
CaloCellGroup::setDefinition
bool setDefinition(const CaloCell_Base_ID *caloCellId, const std::string &definition, MsgStream &logStr)
Definition: CaloCellGroup.cxx:23
CaloCellGroup::m_hec
bool m_hec
Definition: CaloCellGroup.h:51
CaloCellGroup::m_neg
bool m_neg
Definition: CaloCellGroup.h:50
CaloCell_Base_ID::phi
int phi(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
CaloCellGroup::m_pos
bool m_pos
Definition: CaloCellGroup.h:50
CaloCellGroupList::m_groups
std::vector< CaloCellGroup > m_groups
Definition: CaloCellGroup.h:75
CaloCellGroup.h
AtlasDetectorID::show_to_string
std::string show_to_string(Identifier id, const IdContext *context=0, char sep='.') const
or provide the printout in string form
Definition: AtlasDetectorID.cxx:574
CaloCellGroup::inGroup
bool inGroup(const Identifier id) const
Definition: CaloCellGroup.cxx:165
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
AsgConfigHelper::strtof
bool strtof(const std::string &input, T &f)
Definition: AsgEGammaConfigHelper.cxx:47
DeMoScan.first
bool first
Definition: DeMoScan.py:534
CaloCellGroup::m_values
std::vector< float > m_values
Definition: CaloCellGroup.h:53
CaloCellGroupList::m_defaults
std::vector< float > m_defaults
Definition: CaloCellGroup.h:76
CaloCellGroupList::printDef
void printDef() const
Definition: CaloCellGroup.cxx:301
CaloCell_Base_ID
Helper base class for offline cell identifiers.
Definition: CaloCell_Base_ID.h:41
CaloCellGroupList::valuesForCell
const std::vector< float > & valuesForCell(const Identifier id)
Definition: CaloCellGroup.cxx:266
CaloCellGroupList::setDefinition
bool setDefinition(const CaloCell_Base_ID *caloCellId, const std::vector< std::string > &definitions, MsgStream &logStr)
Definition: CaloCellGroup.cxx:225
python.compressB64.c
def c
Definition: compressB64.py:93
CaloCellGroup::m_fcal
bool m_fcal
Definition: CaloCellGroup.h:51
CaloCellGroup::CaloCellGroup
CaloCellGroup()
Definition: CaloCellGroup.cxx:10
CaloCellGroup::complain
static void complain(MsgStream &, const std::string &)
Definition: CaloCellGroup.cxx:218