SND@LHC Software
Loading...
Searching...
No Matches
hcalInf.cxx
Go to the documentation of this file.
1
7#include "hcalInf.h"
8
9#include "FairRunAna.h"
10#include "FairRuntimeDb.h"
11
12#include "TSystem.h"
13#include "TMap.h"
14
15#include <iostream>
16#include <fstream>
17#include <string>
18
19using std::cout;
20using std::cerr;
21using std::endl;
22using std::string;
23
25Int_t hcalInf::fRefCount=0;
26
27
29{
30 for(Int_t i=0;i<fHcalStr.GetSize();i++)
31 delete (TObjString*)(fHcalStr.At(i));
32 fHcalStr.Clear();
33}
34
35hcalInf* hcalInf::GetInstance(const char* filename)
36{
37 if (filename==NULL)
38 {
39 if (fInf!=NULL)
40 fRefCount++;
41 return fInf;
42 }
43 TString newname=gSystem->Getenv("VMCWORKDIR");
44 newname+="/geometry/";
45 newname+=filename;
46 if (fInf!=NULL) {
47 if (fInf->fFileName==newname) {
48 fRefCount++;
49 return fInf;
50 } else {
51 cerr << "hcalInf: Trying create ";
52 cerr << "instance of hcalInf with";
53 cerr << " name " << filename;
54 cerr << ", which is different from ";
55 cerr << fInf->fFileName << "." << endl;
56 return NULL;
57 }
58 }
59 fInf=new hcalInf(newname);
60 //Is something wrong?
61 if (fInf->fSuccess==0)
62 {
63 delete fInf;
64 return NULL;
65 }
66 fRefCount++;
67 return fInf;
68}
69
70int cmp_nocase(const string &s, const string &s2 )
71{
72 string::const_iterator p=s.begin();
73 string::const_iterator p2=s2.begin();
74 while(p!=s.end()&&p2!=s2.end()) {
75 if (toupper(*p)!=toupper(*p2)) return (toupper(*p)<toupper(*p2))?-1:1;
76 ++p;
77 ++p2;
78 }
79 return(s2.size()==s.size())?0:(s.size()<s2.size())?-1:1; // size is unsigned
80}
81
82Double_t hcalInf::GetVariableStrict(const char* key)
83{
84 TObjString* value=(TObjString*)fVariables->GetValue(key);
85 if (value==NULL)
86 {
87 cerr << "Can't find variable named \"" << key << "\"";
88 Fatal("GetVariableStrict","Exiting...");
89 }
90 Double_t val;
91 char* err=NULL;
92 val=strtod(value->GetString(),&err);
93 if (err[0]!='\0')
94 {
95 cerr << "Can't convert variable named \"" << key ;
96 cerr << "\" to floating point. Value is \"";
97 cerr << value->GetString() << "\"." << endl;
98 Fatal("GetVariableStrict","Exiting...");
99 }
100 return val;
101}
102
103TString hcalInf::GetStringVariable(const char* key)
104{
105 TObjString* value=(TObjString*)fVariables->GetValue(key);
106 if (value==NULL)
107 {
108 Fatal("GetStringVariable","Can't find variable named %s.", key);
109 }
110 return value->GetString();
111}
112
113
114
115Double_t hcalInf::GetVariable(const char* key)
116{
117 TObjString* value=(TObjString*)fVariables->GetValue(key);
118 if (value==NULL)
119 return -1111;
120 Double_t val;
121 char* err=NULL;
122 val=strtod(value->GetString(),&err);
123 if (err[0]!='\0')
124 return -1111;
125 return val;
126}
127
128void hcalInf::AddVariable(const char* key, const char* value)
129{
130 TObjString* skey=(TObjString*)fVariables->FindObject(key);
131 //Value for this key already exists!!!
132 if (skey!=NULL) return;
133 skey=new TObjString(key);
134 skey->String().ToLower();
135 TObjString* svalue=new TObjString(value);
136 fVariables->Add(skey, svalue);
137}
138//=============================================================================
139hcalInf::hcalInf(const char* filename)
140 : TObject(),
141 fVariables(new TMap(200)),
142 fHcalStr(),
143 fXPos(0.),
144 fYPos(0.),
145 fZPos(0.),
146 fNLayers(0),
147 fN1Layers(0),
148 fXSize(0),
149 fYSize(0),
150 fModuleSize(0.),
151 fAbsorber(0.),
152 fScin(0.),
153 fTyveec(0.),
154 fThicknessLayer(0.),
155 fCellSize(0.),
156 fHcalSize(),
157 fECut(0.),
158 fHCut(0.),
159 fSemiX(0.),
160 fSemiY(0.),
161 fFastMC(-1),
162 fSuccess(1),
163 fFileName(filename)
164{
176 std::ifstream file(filename);
177 Int_t linenum;
178 Double_t val;
179 string buffer;
180 string message;
181 string variable;
182 string value;
183 TObjString* str=NULL;
184 char** err=NULL;
185 char winend[2]={13, 0};
186 int ssize=-1;
187
188 if (!file) {
189 cerr << "hcalInf: Can't open information file " << filename << "!" << endl;
190 cerr << "hcalInf: Hcal will not be constructed correctly." << endl;
191 fSuccess=0;
192 return;
193 }
194
195 linenum=0;
196 while(getline(file,buffer)) {
197 linenum++;
198 message=buffer.substr(buffer.find_first_not_of(" ")); //Skiping initial spaces
199 message=message.substr(0,message.find("#")); //Removing comments
200 // Threat windows end of strings correctly
201 message=message.substr(0,message.find(winend));
202 if (message.empty()) continue; //This was just a comment
203 variable=message.substr(0,message.find("="));
204 if (variable=="structure") {
205 while(getline(file,buffer)) {
206 linenum++;
207 if (buffer.empty()) break;
208 message=buffer.substr(buffer.find_first_not_of(" ")); //Skiping initial spaces
209 message=message.substr(0,message.find("#")); //Removing comments
210 message=message.substr(0,message.find_last_not_of(" ")+1); //Skiping ending spaces
211
212 // Threat windows end of strings correctly
213 message=message.substr(0,message.find(winend));
214
215 if (!message.empty()) {
216 if (-1==ssize)
217 ssize=message.size();
218 else
219 if (ssize!=(Int_t)message.size()) {
220 cerr << "Error in HCAL structure at line " << linenum;
221 cerr << "." << endl;
222 cerr << "Line length differs from previous one" << endl;
223 fSuccess=0;
224 file.close();
225 return;
226
227 }
228
229 str=new TObjString(message.c_str());
230 fHcalStr.Add(str);
231 }
232 }
233 break;
234 }
235 if (variable==message) {
236 cerr << "Syntax error: File " << filename << ".Line " << linenum << "." << endl;
237 fSuccess=0;
238 file.close();
239 return;
240 }
241 variable=variable.substr(0,variable.find_first_of(" "));
242 value=message.substr(message.find("=")+1);
243 value=value.substr(value.find_first_not_of(" ")); //Skiping initial spaces
244 value=value.substr(0,value.find_first_of(" "));
245/*
246 value=value.substr(0,value.find_first_not_of("1234567890-+e."));
247 val=strtod(value.c_str(),err);
248 if (err) {
249 cerr << "Syntax error after =: File " << filename << ".Line " << linenum << "." << endl;
250 fSuccess=0;
251 file.close();
252 return;
253 }
254*/
255 AddVariable(variable.c_str(), value.c_str());
256 }
257 file.close();
259}
260
261Bool_t hcalInf::ExcludeParameter(TString parname)
262{
263 if (parname.CompareTo("hcalversion")==0) return kTRUE;
264 return kFALSE;
265}
266
267/*
268void hcalInf::CheckVariables()
269{
270 FairRunAna* ana = FairRunAna::Instance();
271 if (ana==NULL)
272 {
273 return;
274 }
275 FairRuntimeDb* rtdb=ana->GetRuntimeDb();
276 CbmGeoHcalPar* par=(CbmGeoHcalPar*)(rtdb->findContainer("CbmGeoHcalPar"));
277 if (par==NULL)
278 {
279 Info("CheckVariables","No parameters container is found.");
280 return;
281 }
282 TMap* parVariables=par->GetVariables();
283 if (parVariables)
284 {
285 TObjString* key;
286 TIterator* iter=parVariables->MakeIterator();
287 while((key=(TObjString*)iter->Next())!=NULL)
288 {
289 TObjString* first=(TObjString*)parVariables->GetValue(key->String());
290 TObjString* second=(TObjString*)fVariables->GetValue(key->String());
291 if (ExcludeParameter(key->String())==kFALSE)
292 if (second==NULL)
293 {
294 Info("CheckVariables", "Parameter %s not found in .geo file, but found in parameter file.", key->String().Data());
295 } else
296 if (first->String()!=second->String())
297 {
298 Info("CheckVariables", "Parameter %s differs in .geo file and parameter file!", key->String().Data());
299 Info("CheckVariables", "%s=%s in parameter file.", key->String().Data(), first->String().Data());
300 Info("CheckVariables", "%s=%s in .geo file.", key->String().Data(), second->String().Data());
301 }
302 if (ExcludeParameter(key->String())==kTRUE)
303 AddVariable(key->String().Data(), first->String().Data());
304 }
305 }
306
307 TObjArray* parHcalStr=par->GetHcalStr();
308 if (parHcalStr)
309 {
310 TObjString* key;
311 for(Int_t i=0;i<parHcalStr->GetEntriesFast();i++)
312 {
313 TObjString* first=(TObjString*)parHcalStr->At(i);
314 TObjString* second=(TObjString*)fHcalStr.At(i);
315 if (second&&first->String()!=second->String())
316 {
317 Info("CheckVariables", "String %d in calorimeter structure differs in .geo file and in parameter file.", i);
318 Info("CheckVariables", "%s=%s in parameter file", key->String().Data(), first->String().Data());
319 Info("CheckVariables", "%s=%s in .geo file", key->String().Data(), second->String().Data());
320 }
321 }
322 }
323}
324*/
326{
327 TString stri;
328 TObjString* str=(TObjString*)fHcalStr.At(0);
329
330 fXPos=GetVariableStrict("xpos");
331 fYPos=GetVariableStrict("ypos");
332 fZPos=GetVariableStrict("zpos");
333 fNLayers=(Int_t)GetVariableStrict("nlayers");
334 fN1Layers=(Int_t)GetVariableStrict("n1layers");
335 fModuleSize=GetVariableStrict("modulesize");
336 fAbsorber=GetVariableStrict("absorber");
337 fScin=GetVariableStrict("scin");
338 fTyveec=GetVariableStrict("tyveec");
339 fHcalSize[2]=GetVariableStrict("hcalzsize");
340 fECut=GetVariableStrict("ecut");
341 fHCut=GetVariableStrict("hcut");
342 fFastMC=(Int_t)GetVariableStrict("fastmc");
343 fSemiX=GetVariableStrict("xsemiaxis");
344 fSemiY=GetVariableStrict("ysemiaxis");
345
346 stri=""; stri+=str->GetString().Length();
347 AddVariable("xsize", stri);
348 stri=""; stri+=fHcalStr.GetLast()+1;
349 AddVariable("ysize", stri);
350
351 fXSize=(Int_t)GetVariableStrict("xsize");
352 fYSize=(Int_t)GetVariableStrict("ysize");
356 stri=""; stri+=fHcalSize[0];
357 AddVariable("xhcalsize",stri);
358 stri=""; stri+=fHcalSize[1];
359 AddVariable("yhcalsize",stri);
360}
361
362//-----------------------------------------------------------------------------
364{
365 // Print out the HCAL geometry container
366
367 if (fVariables)
368 {
369 TObjString* key;
370 TIterator* iter=fVariables->MakeIterator();
371 while((key=(TObjString*)iter->Next())!=NULL)
372 {
373 TObjString* str=(TObjString*)fVariables->GetValue(key);
374 cout << key->String() << "=" << str->String() << endl;
375 }
376 }
377 TObjString* key;
378 TIterator* iter=fHcalStr.MakeIterator();
379
380 Int_t modules=0;
381 Int_t channels=0;
382 Int_t i;
383 Int_t j;
384 Int_t m[10];
385 char stri[2]={0, 0};
386 TString st;
387 for(i=0;i<10;i++) m[i]=0;
388
389 while((key=(TObjString*)iter->Next())!=NULL)
390 {
391 st=key->String();
392 cout << key->String() << endl;
393 for(i=0;i<st.Length();i++)
394 {
395 stri[0]=st[i];
396 j=atoi(stri);
397 m[j]++;
398 if (j) modules++;
399 channels+=j*j;
400 }
401 }
402 cout << "Total modules: " << modules << endl;
403 cout << "Total channels: " << channels << endl;
404 for(i=1;i<10;i++)
405 {
406 if (m[i]==0) continue;
407 cout << " Type " << i << " : modules=" << m[i] << ", channels=" << m[i]*i*i << endl;
408 }
409
410}
411
Double_t m
Double_t fXPos
Definition hcalInf.h:105
void InitVariables()
Definition hcalInf.cxx:325
Double_t fScin
Definition hcalInf.h:127
Int_t fNLayers
Definition hcalInf.h:112
Double_t GetAbsorber() const
Definition hcalInf.h:40
TObjArray fHcalStr
Definition hcalInf.h:103
Double_t fSemiY
Definition hcalInf.h:142
Double_t GetScin() const
Definition hcalInf.h:41
void DumpContainer() const
Definition hcalInf.cxx:363
Int_t fFastMC
Definition hcalInf.h:145
hcalInf()
Definition hcalInf.h:23
virtual ~hcalInf()
Definition hcalInf.cxx:28
Int_t fYSize
Definition hcalInf.h:119
Double_t fThicknessLayer
Definition hcalInf.h:131
Int_t fN1Layers
Definition hcalInf.h:114
Double_t fAbsorber
Definition hcalInf.h:125
void AddVariable(const char *key, const char *value)
Definition hcalInf.cxx:128
Double_t fYPos
Definition hcalInf.h:107
Double_t GetVariable(const char *key)
Definition hcalInf.cxx:115
static hcalInf * GetInstance(const char *filename)
Definition hcalInf.cxx:35
Double_t GetVariableStrict(const char *key)
Definition hcalInf.cxx:82
Double_t GetTyveec() const
Definition hcalInf.h:42
Double_t GetModuleSize() const
Definition hcalInf.h:36
Double_t fHCut
Definition hcalInf.h:139
Bool_t ExcludeParameter(TString parname)
Definition hcalInf.cxx:261
Double_t fModuleSize
Definition hcalInf.h:122
Int_t fXSize
Definition hcalInf.h:117
static hcalInf * fInf
Definition hcalInf.h:88
TMap * fVariables
Definition hcalInf.h:100
Double_t fECut
Definition hcalInf.h:137
Int_t GetYSize() const
Definition hcalInf.h:47
Int_t GetXSize() const
Definition hcalInf.h:46
TString fFileName
Definition hcalInf.h:150
Double_t fZPos
Definition hcalInf.h:109
Double_t fTyveec
Definition hcalInf.h:129
static Int_t fRefCount
Definition hcalInf.h:89
Double_t fHcalSize[3]
Definition hcalInf.h:135
Double_t fSemiX
Definition hcalInf.h:141
TString GetStringVariable(const char *key)
Definition hcalInf.cxx:103
Int_t fSuccess
Definition hcalInf.h:148
int cmp_nocase(const string &s, const string &s2)
Definition hcalInf.cxx:70