1 |
/** |
2 |
* \file src/CaloLevel1.cpp |
3 |
* \author Emiliano Mocchiutti |
4 |
* |
5 |
**/ |
6 |
#include <CaloLevel1.h> |
7 |
// |
8 |
ClassImp(CaloStrip); |
9 |
ClassImp(CaloLevel1); |
10 |
|
11 |
/** |
12 |
* CaloStrip default constructor |
13 |
**/ |
14 |
CaloStrip::CaloStrip() { |
15 |
c1 = 0; |
16 |
this->Clear(); |
17 |
}; |
18 |
|
19 |
/** |
20 |
* CaloStrip default constructor |
21 |
**/ |
22 |
CaloStrip::CaloStrip(Bool_t mechalig) { |
23 |
c1 = 0; |
24 |
this->Clear(); |
25 |
if ( mechalig ){ |
26 |
ismech = true; |
27 |
UXal = MECHCTX; |
28 |
UYal = MECHCTY; |
29 |
UZal = MECHCTZ; |
30 |
} else { |
31 |
ismech = false; |
32 |
UseStandardAlig(); |
33 |
}; |
34 |
}; |
35 |
|
36 |
/** |
37 |
* CaloStrip default constructor |
38 |
**/ |
39 |
CaloStrip::CaloStrip(CaloLevel1 *calo) { |
40 |
c1 = calo->GetCaloLevel1(); |
41 |
this->Clear(); |
42 |
ismech = false; |
43 |
UseStandardAlig(); |
44 |
}; |
45 |
|
46 |
/** |
47 |
* CaloStrip default constructor |
48 |
**/ |
49 |
CaloStrip::CaloStrip(CaloLevel1 *calo, Bool_t mechalig) { |
50 |
c1 = calo->GetCaloLevel1(); |
51 |
this->Clear(); |
52 |
if ( mechalig ){ |
53 |
ismech = true; |
54 |
UXal = MECHCTX; |
55 |
UYal = MECHCTY; |
56 |
UZal = MECHCTZ; |
57 |
} else { |
58 |
ismech = false; |
59 |
UseStandardAlig(); |
60 |
}; |
61 |
}; |
62 |
|
63 |
/** |
64 |
* Clear variables |
65 |
**/ |
66 |
void CaloStrip::Clear() { |
67 |
fE = 0.; |
68 |
fX = 0.; |
69 |
fY = 0.; |
70 |
fZ = 0.; |
71 |
fView = 0; |
72 |
fPlane = 0; |
73 |
fStrip = 0; |
74 |
}; |
75 |
|
76 |
/** |
77 |
* Connect to the DB and retrieve alignement parameters |
78 |
**/ |
79 |
void CaloStrip::UseStandardAlig(){ |
80 |
// |
81 |
ismech = false; |
82 |
// |
83 |
stringstream aligfile; |
84 |
Int_t error = 0; |
85 |
FILE *f = 0; |
86 |
ifstream badfile; |
87 |
GL_PARAM *glparam = new GL_PARAM(); |
88 |
// |
89 |
TString host = "mysql://localhost/pamelaprod"; |
90 |
TString user = "anonymous"; |
91 |
TString psw = ""; |
92 |
const char *pamdbhost=gSystem->Getenv("PAM_DBHOST"); |
93 |
const char *pamdbuser=gSystem->Getenv("PAM_DBUSER"); |
94 |
const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW"); |
95 |
if ( !pamdbhost ) pamdbhost = ""; |
96 |
if ( !pamdbuser ) pamdbuser = ""; |
97 |
if ( !pamdbpsw ) pamdbpsw = ""; |
98 |
if ( strcmp(pamdbhost,"") ) host = pamdbhost; |
99 |
if ( strcmp(pamdbuser,"") ) user = pamdbuser; |
100 |
if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw; |
101 |
TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); |
102 |
// |
103 |
UXal = 0.; |
104 |
UYal = 0.; |
105 |
UZal = 0.; |
106 |
// |
107 |
if ( dbc ){ |
108 |
// |
109 |
// determine where I can find calorimeter ADC to MIP conversion file |
110 |
// |
111 |
printf(" Querying DB for calorimeter parameters files...\n"); |
112 |
// |
113 |
// |
114 |
// |
115 |
error = 0; |
116 |
error = glparam->Query_GL_PARAM(1000,102,dbc); |
117 |
if ( error >= 0 ){ |
118 |
// |
119 |
aligfile.str(""); |
120 |
aligfile << glparam->PATH.Data() << "/"; |
121 |
aligfile << glparam->NAME.Data(); |
122 |
// |
123 |
printf("\n Using parameter file: \n %s \n",aligfile.str().c_str()); |
124 |
f = fopen(aligfile.str().c_str(),"rb"); |
125 |
if ( f ){ |
126 |
// |
127 |
fread(&UXal,sizeof(UXal),1,f); |
128 |
fread(&UYal,sizeof(UYal),1,f); |
129 |
fread(&UZal,sizeof(UZal),1,f); |
130 |
// |
131 |
fclose(f); |
132 |
}; |
133 |
// |
134 |
}; |
135 |
dbc->Close(); |
136 |
}; |
137 |
if ( !UXal ){ |
138 |
// |
139 |
printf(" No able to query DB for calorimeter parameters files\n Using hard-coded parameters \n"); |
140 |
UXal = CTX; |
141 |
UYal = CTY; |
142 |
UZal = CTZ; |
143 |
}; |
144 |
// |
145 |
}; |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
/** |
153 |
* Given a strip returns its position in the PAMELA reference system |
154 |
**/ |
155 |
void CaloStrip::Set(Int_t view, Int_t plane, Int_t strip) { |
156 |
// |
157 |
this->Clear(); |
158 |
// |
159 |
if ( view < 0 || view > 1 ){ |
160 |
printf(" ERROR: 0 =< view =< 1 \n"); |
161 |
return; |
162 |
}; |
163 |
if ( plane < 0 || plane > 21 ){ |
164 |
printf(" ERROR: 0 =< plane =< 21 \n"); |
165 |
return; |
166 |
}; |
167 |
if ( strip < 0 || strip > 95 ){ |
168 |
printf(" ERROR: 0 =< strip =< 95 \n"); |
169 |
return; |
170 |
}; |
171 |
// |
172 |
Float_t lShift = 0.; |
173 |
Float_t lPos = 0.; |
174 |
extern struct shift shift_; |
175 |
// |
176 |
// Find MIPs for that strip |
177 |
// |
178 |
if ( c1 ) fE = c1->GetEstrip(view, plane, strip); |
179 |
// |
180 |
fView = view + 1; |
181 |
fPlane = plane + 1; |
182 |
fStrip = strip + 1; |
183 |
if ( fPlane%2 ){ |
184 |
lShift = -0.5; |
185 |
} else { |
186 |
lShift = 0.5; |
187 |
}; |
188 |
// |
189 |
if ( fView == 2 ) lShift = - lShift; |
190 |
// |
191 |
shift_.shift = lShift; |
192 |
// |
193 |
Float_t zplane[22]; |
194 |
zplane[0] = 0.; |
195 |
Int_t ii = 0; |
196 |
for ( Int_t i = 2; i < 23; i++){ |
197 |
ii = i-1; |
198 |
if ( i%2 ){ |
199 |
zplane[ii] = zplane[ii-1] - 10.09; |
200 |
} else { |
201 |
zplane[ii] = zplane[ii-1] - 8.09; |
202 |
}; |
203 |
}; |
204 |
// |
205 |
millim_(&fStrip,&lPos); |
206 |
// |
207 |
if ( fView == 1 ){ |
208 |
// |
209 |
// X view |
210 |
// |
211 |
fX = (lPos - UXal)/10.; |
212 |
fY = 0.; |
213 |
fZ = (zplane[fPlane-1] - 5.81 + UZal)/10.; |
214 |
} else { |
215 |
// |
216 |
// Y view |
217 |
// |
218 |
fX = 0; |
219 |
fY = (lPos - UYal)/10.; |
220 |
fZ = (zplane[fPlane-1] + UZal)/10.; |
221 |
}; |
222 |
// |
223 |
}; |
224 |
|
225 |
/** |
226 |
* Given a point in the space (PAMELA ref system) returns the closest strip |
227 |
**/ |
228 |
void CaloStrip::Set(Float_t X, Float_t Y, Float_t Z) { |
229 |
// |
230 |
fX = X; |
231 |
fY = Y; |
232 |
fZ = Z; |
233 |
// |
234 |
Float_t zplane[22]; |
235 |
zplane[0] = 0.; |
236 |
Int_t ii = 0; |
237 |
for ( Int_t i = 2; i < 23; i++){ |
238 |
ii = i-1; |
239 |
if ( i%2 ){ |
240 |
zplane[ii] = zplane[ii-1] - 10.09; |
241 |
} else { |
242 |
zplane[ii] = zplane[ii-1] - 8.09; |
243 |
}; |
244 |
}; |
245 |
// |
246 |
Float_t dzx[22]; |
247 |
Float_t dzy[22]; |
248 |
for ( Int_t i=0; i < 22; i++){ |
249 |
dzx[i] = fabs(fZ*10. - (zplane[i] - 5.81 + UZal)); |
250 |
dzy[i] = fabs(fZ*10. - (zplane[i] + UZal)); |
251 |
}; |
252 |
// |
253 |
Float_t minx = TMath::MinElement(22,dzx); |
254 |
Float_t miny = TMath::MinElement(22,dzy); |
255 |
// |
256 |
// find view |
257 |
// |
258 |
if ( minx < miny ){ |
259 |
fView = 1; |
260 |
} else { |
261 |
fView = 2; |
262 |
}; |
263 |
// |
264 |
// find plane |
265 |
// |
266 |
Float_t pos = 0.; |
267 |
// |
268 |
for ( Int_t i=0; i < 22; i++){ |
269 |
if ( fView == 1 ){ |
270 |
if ( dzx[i] == minx ){ |
271 |
fPlane = i+1; |
272 |
pos = fX*10. + UXal; |
273 |
}; |
274 |
} else { |
275 |
if ( dzy[i] == miny ){ |
276 |
fPlane = i+1; |
277 |
pos = fY*10. + UYal; |
278 |
}; |
279 |
}; |
280 |
}; |
281 |
// |
282 |
// find strip |
283 |
// |
284 |
Float_t dxy[96]; |
285 |
Float_t stpos = 0.; |
286 |
// |
287 |
CaloStrip *ca = new CaloStrip(); |
288 |
// |
289 |
for ( Int_t i=0; i < 96; i++){ |
290 |
ca->Set(fView-1,fPlane-1,i); |
291 |
if ( fView == 1 ){ |
292 |
stpos = ca->GetX()*10. + UXal; |
293 |
} else { |
294 |
stpos = ca->GetY()*10. + UYal; |
295 |
}; |
296 |
dxy[i] = fabs(pos - stpos); |
297 |
}; |
298 |
// |
299 |
delete ca; |
300 |
// |
301 |
Float_t mins = TMath::MinElement(96,dxy); |
302 |
// |
303 |
for ( Int_t i=0; i < 96; i++){ |
304 |
if ( dxy[i] == mins ) fStrip = i+1; |
305 |
}; |
306 |
}; |
307 |
|
308 |
/** |
309 |
* CaloLevel1 constructor |
310 |
**/ |
311 |
CaloLevel1::CaloLevel1() { |
312 |
// |
313 |
estrip = TArrayI(0,NULL); |
314 |
// |
315 |
this->Clear(); |
316 |
// |
317 |
}; |
318 |
|
319 |
/** |
320 |
* Clear the CaloLevel1 object |
321 |
**/ |
322 |
void CaloLevel1::Clear() { |
323 |
// |
324 |
istrip = 0; |
325 |
estrip.Reset(); |
326 |
// |
327 |
}; |
328 |
|
329 |
/** |
330 |
* Returns the detected energy for the given strip once loaded the event |
331 |
**/ |
332 |
Float_t CaloLevel1::GetEstrip(Int_t sview, Int_t splane, Int_t sstrip){ |
333 |
Int_t view = -1; |
334 |
Int_t plane = -1; |
335 |
Int_t strip = -1; |
336 |
Float_t mip = 0.; |
337 |
// |
338 |
if ( istrip == 0 ) return(0.); |
339 |
// |
340 |
for (Int_t i = 0; i<istrip; i++ ){ |
341 |
// |
342 |
mip = DecodeEstrip(i,view,plane,strip); |
343 |
// |
344 |
if ( view == sview && splane == plane && sstrip == strip ) return(mip); |
345 |
// |
346 |
// entry are ordered by strip, plane and view number. Go out if you pass the input strip |
347 |
// |
348 |
if ( view == sview && plane == splane && strip > sstrip ) return(0.); |
349 |
if ( view == sview && plane > splane ) return(0.); |
350 |
if ( view > sview ) return(0.); |
351 |
// |
352 |
}; |
353 |
return(0.); |
354 |
}; |
355 |
|
356 |
/** |
357 |
* Given estrip entry returns energy plus view, plane and strip numbers |
358 |
**/ |
359 |
Float_t CaloLevel1::DecodeEstrip(Int_t entry, Int_t &view, Int_t &plane, Int_t &strip){ |
360 |
// |
361 |
if ( entry>istrip ) return(0.); |
362 |
// |
363 |
// printf(" num lim %f \n",std::numeric_limits<Float_t>::max()); |
364 |
// printf(" estrip.At(%i) = %i \n",entry,estrip.At(entry)); |
365 |
// |
366 |
Int_t eval = 0; |
367 |
if ( estrip.At(entry) > 0. ){ |
368 |
view = 0; |
369 |
eval = estrip.At(entry); |
370 |
} else { |
371 |
view = 1; |
372 |
eval = -estrip.At(entry); |
373 |
}; |
374 |
// |
375 |
Int_t fbi = 0; |
376 |
fbi = (Int_t)truncf((Float_t)(eval/1000000000)); |
377 |
// |
378 |
Int_t plom = 0; |
379 |
plom = (Int_t)truncf((Float_t)((eval-fbi*1000000000)/10000000)); |
380 |
// |
381 |
Float_t tim = 100000.; |
382 |
plane = plom; |
383 |
if ( fbi == 1 ) tim = 10000.; |
384 |
if ( plom > 21 ){ |
385 |
plane = plom - 22; |
386 |
if ( fbi == 1 ){ |
387 |
tim = 1000.; |
388 |
} else { |
389 |
tim = 100.; |
390 |
}; |
391 |
}; |
392 |
if ( plom > 43 ){ |
393 |
plane = plom - 44; |
394 |
tim = 10.; |
395 |
}; |
396 |
if ( plom > 65 ){ |
397 |
plane = plom - 66; |
398 |
tim = 1.; |
399 |
}; |
400 |
// |
401 |
strip = (Int_t)truncf((Float_t)((eval - fbi*1000000000 -plom*10000000)/100000)); |
402 |
// |
403 |
Float_t mip = ((Float_t)(eval - fbi*1000000000 -plom*10000000 -strip*100000))/tim; |
404 |
// |
405 |
if ( mip > 0. && mip < 99999. ) return(mip); |
406 |
// |
407 |
printf(" WARNING: problems decoding value %i at entry %i \n",estrip.At(entry),entry); |
408 |
// |
409 |
view = -1; |
410 |
plane = -1; |
411 |
strip = -1; |
412 |
return(0.); |
413 |
} |
414 |
|
415 |
/* |
416 |
* Returns energy released on plane nplane (where 0<= nplane <= 43, 0 = 1Y, 1 = 1X, 2 = 2Y, 3 = 2X, etc. etc.). |
417 |
*/ |
418 |
Float_t CaloLevel1::qtotpl(Int_t nplane){ |
419 |
// |
420 |
Int_t sview = 1; |
421 |
if ( nplane%2 ) sview = 0; |
422 |
// |
423 |
Int_t splane = nplane-(sview+1)/2; |
424 |
// |
425 |
Float_t totmip = qtotpl(sview,splane); |
426 |
// |
427 |
return(totmip); |
428 |
// |
429 |
}; |
430 |
|
431 |
/* |
432 |
* Returns energy released on view "view" (0 = X, 1 = Y) and plane "plane" ( 0 <= plane <= 21 ). |
433 |
*/ |
434 |
Float_t CaloLevel1::qtotpl(Int_t sview, Int_t splane){ |
435 |
// |
436 |
Int_t view = -1; |
437 |
Int_t plane = -1; |
438 |
Int_t strip = -1; |
439 |
// |
440 |
Float_t mip = 0.; |
441 |
Float_t totmip = 0.; |
442 |
// |
443 |
if ( istrip == 0 ) return(0.); |
444 |
// |
445 |
for (Int_t i = 0; i<istrip; i++ ){ |
446 |
// |
447 |
mip = DecodeEstrip(i,view,plane,strip); |
448 |
// |
449 |
if ( view == sview && splane == plane ) totmip += mip; |
450 |
// |
451 |
// entry are ordered by strip, plane and view number. Go out if you pass the input strip |
452 |
// |
453 |
if ( view == sview && plane > splane ) return(totmip); |
454 |
if ( view > sview ) return(totmip); |
455 |
// |
456 |
}; |
457 |
// |
458 |
return(totmip); |
459 |
// |
460 |
}; |