SND@LHC Software
Loading...
Searching...
No Matches
ecalInf.cxx
Go to the documentation of this file.
1
7#include "ecalInf.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 ecalInf::fRefCount=0;
26
27
29{
30 for(Int_t i=0;i<fEcalStr.GetSize();i++)
31 delete (TObjString*)(fEcalStr.At(i));
32 fEcalStr.Clear();
33}
34
35ecalInf* ecalInf::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 << "ecalInf: Trying create ";
52 cerr << "instance of ecalInf with";
53 cerr << " name " << filename;
54 cerr << ", which is different from ";
55 cerr << fInf->fFileName << "." << endl;
56 return NULL;
57 }
58 }
59 fInf=new ecalInf(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 ecalInf::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 ecalInf::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 ecalInf::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 ecalInf::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//=============================================================================
139ecalInf::ecalInf(const char* filename)
140 : TObject(),
141 fVariables(new TMap(200)),
142 fEcalStr(),
143 fXPos(0.),
144 fYPos(0.),
145 fZPos(0.),
146 fNLayers(0),
147 fXSize(0),
148 fYSize(0),
149 fModuleSize(0.),
150 fLead(0.),
151 fScin(0.),
152 fTyveec(0.),
153 fThicknessLayer(0.),
154 fCellSize(0.),
155 fEcalSize(),
156 fECut(0.),
157 fHCut(0.),
158 fSemiX(0.),
159 fSemiY(0.),
160 fFastMC(-1),
161 fSuccess(1),
162 fFileName(filename)
163{
175 std::ifstream file(filename);
176 Int_t linenum;
177 Double_t val;
178 string buffer;
179 string message;
180 string variable;
181 string value;
182 TObjString* str=NULL;
183 char** err=NULL;
184 char winend[2]={13, 0};
185 int ssize=-1;
186
187 if (!file) {
188 cerr << "ecalInf: Can't open information file " << filename << "!" << endl;
189 cerr << "ecalInf: Ecal will not be constructed correctly." << endl;
190 fSuccess=0;
191 return;
192 }
193
194 linenum=0;
195 while(getline(file,buffer)) {
196 linenum++;
197 message=buffer.substr(buffer.find_first_not_of(" ")); //Skiping initial spaces
198 message=message.substr(0,message.find("#")); //Removing comments
199 // Threat windows end of strings correctly
200 message=message.substr(0,message.find(winend));
201 if (message.empty()) continue; //This was just a comment
202 variable=message.substr(0,message.find("="));
203 if (variable=="structure") {
204 while(getline(file,buffer)) {
205 linenum++;
206 if (buffer.empty()) break;
207 message=buffer.substr(buffer.find_first_not_of(" ")); //Skiping initial spaces
208 message=message.substr(0,message.find("#")); //Removing comments
209 message=message.substr(0,message.find_last_not_of(" ")+1); //Skiping ending spaces
210
211 // Threat windows end of strings correctly
212 message=message.substr(0,message.find(winend));
213
214 if (!message.empty()) {
215 if (-1==ssize)
216 ssize=message.size();
217 else
218 if (ssize!=(Int_t)message.size()) {
219 cerr << "Error in ECAL structure at line " << linenum;
220 cerr << "." << endl;
221 cerr << "Line length differs from previous one" << endl;
222 fSuccess=0;
223 file.close();
224 return;
225
226 }
227
228 str=new TObjString(message.c_str());
229 fEcalStr.Add(str);
230 }
231 }
232 break;
233 }
234 if (variable==message) {
235 cerr << "Syntax error: File " << filename << ".Line " << linenum << "." << endl;
236 fSuccess=0;
237 file.close();
238 return;
239 }
240 variable=variable.substr(0,variable.find_first_of(" "));
241 value=message.substr(message.find("=")+1);
242 value=value.substr(value.find_first_not_of(" ")); //Skiping initial spaces
243 value=value.substr(0,value.find_first_of(" "));
244/*
245 value=value.substr(0,value.find_first_not_of("1234567890-+e."));
246 val=strtod(value.c_str(),err);
247 if (err) {
248 cerr << "Syntax error after =: File " << filename << ".Line " << linenum << "." << endl;
249 fSuccess=0;
250 file.close();
251 return;
252 }
253*/
254 AddVariable(variable.c_str(), value.c_str());
255 }
256 file.close();
258}
259
260Bool_t ecalInf::ExcludeParameter(TString parname)
261{
262 if (parname.CompareTo("ecalversion")==0) return kTRUE;
263 return kFALSE;
264}
265
266/*
267void ecalInf::CheckVariables()
268{
269 FairRunAna* ana = FairRunAna::Instance();
270 if (ana==NULL)
271 {
272 return;
273 }
274 FairRuntimeDb* rtdb=ana->GetRuntimeDb();
275 CbmGeoEcalPar* par=(CbmGeoEcalPar*)(rtdb->findContainer("CbmGeoEcalPar"));
276 if (par==NULL)
277 {
278 Info("CheckVariables","No parameters container is found.");
279 return;
280 }
281 TMap* parVariables=par->GetVariables();
282 if (parVariables)
283 {
284 TObjString* key;
285 TIterator* iter=parVariables->MakeIterator();
286 while((key=(TObjString*)iter->Next())!=NULL)
287 {
288 TObjString* first=(TObjString*)parVariables->GetValue(key->String());
289 TObjString* second=(TObjString*)fVariables->GetValue(key->String());
290 if (ExcludeParameter(key->String())==kFALSE)
291 if (second==NULL)
292 {
293 Info("CheckVariables", "Parameter %s not found in .geo file, but found in parameter file.", key->String().Data());
294 } else
295 if (first->String()!=second->String())
296 {
297 Info("CheckVariables", "Parameter %s differs in .geo file and parameter file!", key->String().Data());
298 Info("CheckVariables", "%s=%s in parameter file.", key->String().Data(), first->String().Data());
299 Info("CheckVariables", "%s=%s in .geo file.", key->String().Data(), second->String().Data());
300 }
301 if (ExcludeParameter(key->String())==kTRUE)
302 AddVariable(key->String().Data(), first->String().Data());
303 }
304 }
305
306 TObjArray* parEcalStr=par->GetEcalStr();
307 if (parEcalStr)
308 {
309 TObjString* key;
310 for(Int_t i=0;i<parEcalStr->GetEntriesFast();i++)
311 {
312 TObjString* first=(TObjString*)parEcalStr->At(i);
313 TObjString* second=(TObjString*)fEcalStr.At(i);
314 if (second&&first->String()!=second->String())
315 {
316 Info("CheckVariables", "String %d in calorimeter structure differs in .geo file and in parameter file.", i);
317 Info("CheckVariables", "%s=%s in parameter file", key->String().Data(), first->String().Data());
318 Info("CheckVariables", "%s=%s in .geo file", key->String().Data(), second->String().Data());
319 }
320 }
321 }
322}
323*/
325{
326 TString stri;
327 TObjString* str=(TObjString*)fEcalStr.At(0);
328
329 fXPos=GetVariableStrict("xpos");
330 fYPos=GetVariableStrict("ypos");
331 fZPos=GetVariableStrict("zpos");
332 fNLayers=(Int_t)GetVariableStrict("nlayers");
333 fModuleSize=GetVariableStrict("modulesize");
334 fLead=GetVariableStrict("lead");
335 fScin=GetVariableStrict("scin");
336 fTyveec=GetVariableStrict("tyveec");
337 fEcalSize[2]=GetVariableStrict("ecalzsize");
338 fECut=GetVariableStrict("ecut");
339 fHCut=GetVariableStrict("hcut");
340 fFastMC=(Int_t)GetVariableStrict("fastmc");
341 fSemiX=GetVariableStrict("xsemiaxis");
342 fSemiY=GetVariableStrict("ysemiaxis");
343
344 stri=""; stri+=str->GetString().Length();
345 AddVariable("xsize", stri);
346 stri=""; stri+=fEcalStr.GetLast()+1;
347 AddVariable("ysize", stri);
348
349 fXSize=(Int_t)GetVariableStrict("xsize");
350 fYSize=(Int_t)GetVariableStrict("ysize");
354 stri=""; stri+=fEcalSize[0];
355 AddVariable("xecalsize",stri);
356 stri=""; stri+=fEcalSize[1];
357 AddVariable("yecalsize",stri);
358}
359
360//-----------------------------------------------------------------------------
362{
363 // Print out the ECAL geometry container
364
365 if (fVariables)
366 {
367 TObjString* key;
368 TIterator* iter=fVariables->MakeIterator();
369 while((key=(TObjString*)iter->Next())!=NULL)
370 {
371 TObjString* str=(TObjString*)fVariables->GetValue(key);
372 cout << key->String() << "=" << str->String() << endl;
373 }
374 }
375 TObjString* key;
376 TIterator* iter=fEcalStr.MakeIterator();
377
378 Int_t modules=0;
379 Int_t channels=0;
380 Int_t i;
381 Int_t j;
382 Int_t m[10];
383 char stri[2]={0, 0};
384 TString st;
385 for(i=0;i<10;i++) m[i]=0;
386
387 while((key=(TObjString*)iter->Next())!=NULL)
388 {
389 st=key->String();
390 cout << key->String() << endl;
391 for(i=0;i<st.Length();i++)
392 {
393 stri[0]=st[i];
394 j=atoi(stri);
395 m[j]++;
396 if (j) modules++;
397 channels+=j*j;
398 }
399 }
400 cout << "Total modules: " << modules << endl;
401 cout << "Total channels: " << channels << endl;
402 for(i=1;i<10;i++)
403 {
404 if (m[i]==0) continue;
405 cout << " Type " << i << " : modules=" << m[i] << ", channels=" << m[i]*i*i << endl;
406 }
407
408}
409
Double_t m
Bool_t ExcludeParameter(TString parname)
Definition ecalInf.cxx:260
Double_t GetModuleSize() const
Definition ecalInf.h:36
Int_t GetYSize() const
Definition ecalInf.h:46
TMap * fVariables
Definition ecalInf.h:99
Int_t fYSize
Definition ecalInf.h:116
Int_t fXSize
Definition ecalInf.h:114
Double_t GetTyveec() const
Definition ecalInf.h:41
void DumpContainer() const
Definition ecalInf.cxx:361
Int_t GetXSize() const
Definition ecalInf.h:45
Double_t fTyveec
Definition ecalInf.h:126
Double_t fECut
Definition ecalInf.h:134
TString GetStringVariable(const char *key)
Definition ecalInf.cxx:103
TString fFileName
Definition ecalInf.h:147
ecalInf()
Definition ecalInf.h:23
Double_t fThicknessLayer
Definition ecalInf.h:128
Int_t fSuccess
Definition ecalInf.h:145
Int_t fFastMC
Definition ecalInf.h:142
virtual ~ecalInf()
Definition ecalInf.cxx:28
Double_t fSemiY
Definition ecalInf.h:139
Double_t fYPos
Definition ecalInf.h:106
static Int_t fRefCount
Definition ecalInf.h:88
Double_t fXPos
Definition ecalInf.h:104
Double_t fHCut
Definition ecalInf.h:136
Double_t fEcalSize[3]
Definition ecalInf.h:132
Int_t fNLayers
Definition ecalInf.h:111
Double_t GetScin() const
Definition ecalInf.h:40
TObjArray fEcalStr
Definition ecalInf.h:102
void AddVariable(const char *key, const char *value)
Definition ecalInf.cxx:128
Double_t fSemiX
Definition ecalInf.h:138
Double_t fScin
Definition ecalInf.h:124
Double_t fLead
Definition ecalInf.h:122
Double_t GetVariableStrict(const char *key)
Definition ecalInf.cxx:82
Double_t GetLead() const
Definition ecalInf.h:39
Double_t fModuleSize
Definition ecalInf.h:119
void InitVariables()
Definition ecalInf.cxx:324
Double_t GetVariable(const char *key)
Definition ecalInf.cxx:115
Double_t fZPos
Definition ecalInf.h:108
static ecalInf * GetInstance(const char *filename)
Definition ecalInf.cxx:35
static ecalInf * fInf
Definition ecalInf.h:87
int cmp_nocase(const string &s, const string &s2)
Definition ecalInf.cxx:70