/[PAMELA software]/calo/flight/CaloPreSampler/src/CaloPreSampler.cpp
ViewVC logotype

Contents of /calo/flight/CaloPreSampler/src/CaloPreSampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations) (download)
Mon Jul 27 13:58:58 2009 UTC (15 years, 7 months ago) by mocchiut
Branch: MAIN
Changes since 1.12: +10 -0 lines
Fit mode forcing tool added

1 /**
2 * \file CaloPreSampler.cpp
3 * \author Emiliano Mocchiutti (2007/07/18)
4 */
5 //
6 // headers
7 //
8 #include <CaloPreSampler.h>
9 //--------------------------------------
10 /**
11 * Default constructor
12 */
13 CaloPreSampler::CaloPreSampler(){
14 Clear();
15 }
16
17 CaloPreSampler::CaloPreSampler(PamLevel2 *l2p){
18 //
19 L2 = l2p;
20 //
21 if ( !L2->IsORB() ) printf(" WARNING: OrbitalInfo Tree is needed, the plugin could not work properly without it \n");
22 //
23 OBT = 0;
24 PKT = 0;
25 atime = 0;
26 //
27 // Default variables
28 //
29 event = new CaloLevel0();
30 cstrip = new CaloStrip(false);
31 // c1 = new CaloLevel1();
32 pcalo = new CaloLevel2();
33 N = 4;
34 NC = 22-N;
35 debug = false;
36 sel = true;
37 cont = false;
38 emulate18 = true;
39 simulation = false;
40 withtrk = true;
41 rigdefault = 50.;
42 nox = false;
43 noy = false;
44 forcecalo = false;
45 forcefitmode = -1;
46 memset(mask,0,2*22*sizeof(Int_t));
47 //
48 Clear();
49 //
50 // loading magnetic field...
51 //
52 TrkLevel2 *trk = new TrkLevel2();
53 GL_PARAM *q4 = new GL_PARAM();
54 TSQLServer *dbc = 0;
55 TString host = "mysql://localhost/pamelaprod";
56 TString user = "anonymous";
57 TString psw = "";
58 const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
59 const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
60 const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
61 if ( !pamdbhost ) pamdbhost = "";
62 if ( !pamdbuser ) pamdbuser = "";
63 if ( !pamdbpsw ) pamdbpsw = "";
64 if ( strcmp(pamdbhost,"") ) host = pamdbhost;
65 if ( strcmp(pamdbuser,"") ) user = pamdbuser;
66 if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
67 dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
68 //
69 q4->Query_GL_PARAM(1,1,dbc);
70 printf(" Reading magnetic field maps at %s\n",(q4->PATH+q4->NAME).Data());
71 trk->LoadField(q4->PATH+q4->NAME);
72 //
73 }
74
75 void CaloPreSampler::SetNoWpreSampler(Int_t n){
76 if ( NC+n < 23 ){
77 N = n;
78 } else {
79 printf(" ERROR! Calorimeter is made of 22 W planes\n");
80 printf(" you are giving N presampler = %i and N calo = %i \n",n,NC);
81 printf(" WARNING: using default values NWpre = 4, NWcalo = 18\n");
82 NC = 18;
83 N = 4;
84 };
85 }
86
87 void CaloPreSampler::SetNoWcalo(Int_t n){
88 if ( N+n < 23 ){
89 NC = n;
90 } else {
91 printf(" ERROR! Calorimeter is made of 22 W planes\n");
92 printf(" you are giving N W presampler = %i and N W calo = %i \n",N,n);
93 printf(" WARNING: using default values NWpre = 4, NWcalo = 18\n");
94 NC = 18;
95 N = 4;
96 };
97 }
98
99 void CaloPreSampler::SplitInto(Int_t NoWpreSampler, Int_t NoWcalo){
100 this->SetNoWcalo(0);
101 this->SetNoWpreSampler(0);
102 if ( NoWpreSampler < NoWcalo ){
103 this->SetNoWpreSampler(NoWpreSampler);
104 this->SetNoWcalo(NoWcalo);
105 } else {
106 this->SetNoWcalo(NoWcalo);
107 this->SetNoWpreSampler(NoWpreSampler);
108 };
109 }
110
111 void CaloPreSampler::Clear(){
112 //
113 pcalo->Clear();
114 //
115 }
116
117 void CaloPreSampler::Print(){
118 //
119 Process();
120 //
121 printf("========================================================================\n");
122 printf(" OBT: %u PKT: %u ATIME: %u \n",OBT,PKT,atime);
123 printf(" debug [debug flag]:.. %i\n",debug);
124 printf(" simulation [simulation flag]:.. %i\n",simulation);
125 printf(" emulate18 [emulate dead plane 18]:.. %i\n",emulate18);
126 printf(" selection mode :.. %i\n",sel);
127 printf(" contamination mode :.. %i\n",cont);
128 printf(" pre-sampler planes :.. %i\n",N);
129 printf(" pcalo->qtot :.. %f\n",pcalo->qtot);
130 printf(" pcalo->nstrip :.. %i\n",pcalo->nstrip);
131 if ( pcalo->ntrk() > 0 ){
132 printf(" pcalo->track0->qtrack :.. %f\n",pcalo->GetCaloTrkVar(0)->qtrack);
133 printf(" pcalo->track0->dX0l :.. %f\n",pcalo->GetCaloTrkVar(0)->dX0l);
134 };
135 printf("========================================================================\n");
136 //
137 }
138
139 void CaloPreSampler::Delete(){
140 Clear();
141 delete pcalo;
142 //delete this;
143 }
144
145
146 void CaloPreSampler::Process(){
147 //
148 if ( !L2 ){
149 printf(" ERROR: cannot find PamLevel2 object, use the correct constructor or check your program!\n");
150 printf(" ERROR: CaloPreSampler variables _NOT_ filled \n");
151 return;
152 };
153 //
154 // Clear structures used to communicate with fortran
155 //
156 event->ClearStructs();//ELENA
157 if ( forcefitmode > 0 ){
158 if ( forcefitmode != 1000 && forcefitmode != 1001 && forcefitmode != 1002 ){
159 printf(" ERROR! forcefitmode=%i \n Use forcefitmode = 1000 for fit mode 0, 1001 fit mode 1, 1002 fit mode 3\n",forcefitmode);
160 forcefitmode = -1;
161 } else {
162 event->clevel2->fmode[0] = forcefitmode;
163 event->clevel2->fmode[1] = forcefitmode;
164 };
165 };
166 //
167 Bool_t newentry = false;
168 //
169 if ( L2->IsORB() ){
170 if ( L2->GetOrbitalInfo()->pkt_num != PKT || L2->GetOrbitalInfo()->OBT != OBT || L2->GetOrbitalInfo()->absTime != atime || sel != ssel ){
171 newentry = true;
172 OBT = L2->GetOrbitalInfo()->OBT;
173 PKT = L2->GetOrbitalInfo()->pkt_num;
174 atime = L2->GetOrbitalInfo()->absTime;
175 ssel = sel;
176 };
177 } else {
178 newentry = true;
179 };
180 //
181 if ( !newentry ) return;
182 //
183 // Some variables
184 //
185 Int_t S3 = 0;
186 Int_t S2 = 0;
187 Int_t S12 = 0;
188 Int_t S11 = 0;
189 Float_t tmptrigty = -1.;
190 Bool_t trackanyway = true;
191 // Float_t rigdefault = 50.;
192 Bool_t hZn = true;
193 // Bool_t withtrk = true;
194 Bool_t st = true;
195 Int_t ntrkentry = 0;
196 TrkLevel2 *trk = L2->GetTrkLevel2();
197 Bool_t filled = false;
198 //
199 if ( debug ) printf(" Processing event at OBT %u PKT %u time %u \n",OBT,PKT,atime);
200 //
201 this->Clear();
202 //
203 // find out if we have trkseqno = -1, -2 or -3
204 //
205 Bool_t m1 = false;
206 Bool_t m2 = false;
207 Bool_t m3 = false;
208 for (Int_t mm=0; mm < L2->GetCaloLevel2()->ntrk(); mm++ ){
209 if ( L2->GetCaloLevel2()->GetCaloTrkVar(mm)->trkseqno == -1 ) m1 = true;
210 if ( L2->GetCaloLevel2()->GetCaloTrkVar(mm)->trkseqno == -2 ) m2 = true;
211 if ( L2->GetCaloLevel2()->GetCaloTrkVar(mm)->trkseqno == -3 ) m3 = true;
212 };
213 if ( !withtrk ) m3 = true;
214 //
215 if ( debug ) printf(" Fill estrip matrix needed to calculate variables \n");
216 //
217 // Fill the estrip matrix
218 //
219 memset(event->clevel1->estrip, 0, 2*22*96*sizeof(Float_t));
220 Int_t view = 0;
221 Int_t plane = 0;
222 Int_t strip = 0;
223 Float_t mip = 0.;
224 for ( Int_t i=0; i<L2->GetCaloLevel1()->istrip; i++ ){
225 //
226 mip = L2->GetCaloLevel1()->DecodeEstrip(i,view,plane,strip);
227 //
228 // Mask x or y view if nox and/or noy are true (default false)
229 //
230 if ( mask[view][plane] ) mip = 0.;
231 if ( nox && view == 0 ) mip = 0.;
232 if ( noy && view == 1 ) mip = 0.;
233 //
234 if ( emulate18 && view == 0 && plane == 18 ) mip = 0.;
235 //
236 // Selection mode: fill the matrix only for plane < (22 - N) REMEMBER N = number of W planes to be used as presampler, ie if N = 2 then we want to use planes from 0 to 19
237 // included so plane < (22 - 2)
238 //
239 if ( sel ){
240 if ( plane < (22 - N) ){
241 //
242 if ( emulate18 && plane == (18 - N) && view == 0 ) mip = 0.;
243 if ( plane >= NC ) mip = 0.;
244 event->clevel1->estrip[strip][plane][view] = mip;
245 //
246 };
247 };
248 //
249 // Contamination mode: fill the matrix only for planes from N to 22 but shift all planes up to the first one
250 //
251 if ( cont ){
252 if ( plane >= N ){
253 //
254 if ( emulate18 && plane == (18 + N) && view == 0 ) mip = 0.;
255 if ( (plane-N) >= NC ) mip = 0.;
256 event->clevel1->estrip[strip][(plane-N)][view] = mip;
257 //
258 };
259 };
260 //
261 };
262 //
263 // if data comes from the simulation we must use mechanical alignment parameters (default is flight parameters)
264 //
265 if ( simulation ){
266 cstrip->UseMechanicalAlig();
267 };
268 //
269 // Set alignment parameter
270 //
271 event->clevel1->xalig = cstrip->GetXalig();
272 event->clevel1->yalig = cstrip->GetYalig();
273 event->clevel1->zalig = cstrip->GetZalig();
274 //
275 event->clevel1->emin = 0.7;
276 //
277 // in case of the contamination mode we must play with the Z alignment in order to have the correct track in the calo since we have moved the planes up...
278 //
279 if ( cont ){
280 if ( !(N%2) ){
281 event->clevel1->reverse = 0; // if the number of planes is even we have taken away a full module no need to do anything strange...
282 event->clevel1->zalig -= (N/2) * (8.09 + 10.09);
283 } else {
284 event->clevel1->reverse = 1; // if the number of planes is odd we have taken away half a module, we need to reverse silicon planes shifting
285 event->clevel1->zalig -= ((N+1)/2) * 8.09 + ((N-1)/2) * 10.09;
286 };
287 };
288 if ( debug ) printf(" xalig = %f \n",event->clevel1->xalig);
289 if ( debug ) printf(" yalig = %f \n",event->clevel1->yalig);
290 if ( debug ) printf(" zalig = %f \n",event->clevel1->zalig);
291 //
292 if ( debug ) printf(" Calculate variables as done in CaloCore, N = %i \n",N);
293 //
294 // Calculate variables
295 //
296 //
297 // use only N W planes
298 //
299 // event->clevel1->npla = 22-N;
300 event->clevel1->npla = NC;
301 //
302 S3 = 0;
303 S2 = 0;
304 S12 = 0;
305 S11 = 0;
306 S3 = L2->GetTrigLevel2()->patterntrig[2];
307 S2 = L2->GetTrigLevel2()->patterntrig[3];
308 S12 = L2->GetTrigLevel2()->patterntrig[4];
309 S11 = L2->GetTrigLevel2()->patterntrig[5];
310 if ( L2->GetTrigLevel2()->patterntrig[1] & (1<<0) ) tmptrigty = 1.;
311 if ( L2->GetTrigLevel2()->patterntrig[0] ) tmptrigty = 2.;
312 if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
313 if ( !(L2->GetTrigLevel2()->patterntrig[1] & (1<<0)) && !L2->GetTrigLevel2()->patterntrig[0] && !S3 && !S2 && !S12 && !S11 ) tmptrigty = 1.;
314 event->clevel2->trigty = tmptrigty;
315 //
316 // do we have at least one track from the tracker? this check has been disabled
317 //
318 event->clevel1->good2 = 1;
319 //
320 // copy variables calculated during calibration process which is skipped here...
321 //
322 event->clevel2->good = L2->GetCaloLevel2()->good;
323 memcpy(event->clevel2->perr,L2->GetCaloLevel2()->perr,sizeof(L2->GetCaloLevel2()->perr));
324 memcpy(event->clevel2->swerr,L2->GetCaloLevel2()->swerr,sizeof(L2->GetCaloLevel2()->swerr));
325 memcpy(event->clevel2->crc,L2->GetCaloLevel2()->crc,sizeof(L2->GetCaloLevel2()->crc));
326 event->clevel2->selftrigger = L2->GetCaloLevel2()->selftrigger;
327 //
328 // Calculate variables common to all tracks (qtot, nstrip, etc.)
329 //
330 if ( debug ) printf("1 Call GetCommonVar() \n");
331 event->GetCommonVar();
332 //
333 // Fill common variables
334 //
335 if ( debug ) printf("1 Call FillCommonVar() \n");
336 event->FillCommonVar(NULL,pcalo);
337 //
338 // Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker)
339 //
340 ntrkentry = 0;
341 //
342 filled = false;
343 //
344 // Run over tracks (tracker or calorimeter )
345 //
346 if ( withtrk ){
347 //
348 for (Int_t nt=0; nt < trk->ntrk(); nt++){
349 //
350 event->clevel1->good2 = 1;
351 //
352 TrkTrack *ptt = trk->GetStoredTrack(nt);
353 //
354 event->clevel1->trkchi2 = 0;
355 //
356 // Copy the alpha vector in the input structure
357 //
358 for (Int_t e = 0; e < 5 ; e++){
359 event->clevel1->al_p[e][0] = ptt->al[e];
360 };
361 //
362 // Get tracker related variables for this track
363 //
364 if ( debug ) printf("track %i Call GetTrkVar() \n",nt);
365 event->GetTrkVar();
366 if ( debug ) printf(" event->clevel2->dX0l %f \n",event->clevel2->dX0l);
367 //
368 // Save tracker track sequence number
369 //
370 event->trkseqno = nt;
371 //
372 // Copy values in the class ca from the structure clevel2
373 //
374 if ( debug ) printf("track %i Call FillTrkVar() \n",nt);
375 event->FillTrkVar(pcalo,ntrkentry);
376
377
378 ntrkentry++;
379 filled = true;
380 //
381 }; // loop on all the tracks
382 };
383 //
384 // if no tracks found but there is the possibility to have a good track we should try to calculate anyway the track related variables using the calorimeter
385 // fit of the track (to be used for example when TRK is off due to any reason like IPM3/5 off).
386 // here we make an event selection so it must be done very carefully...
387 //
388 // conditions are: 0) no track from the tracker 1) we have a track fit both in x and y 2) no problems with calo for this event 3) no selftrigger event
389 //
390 // if ( trackanyway && !filled && event->clevel2->npcfit[0] >= 2 && event->clevel2->npcfit[1] >= 2 && event->clevel2->good != 0 && event->clevel2->trigty < 2. ){
391 if ( (trackanyway && m3) || forcecalo ){
392 if ( debug ) printf(" Event with a track not fitted by the tracker \n");
393 //
394 // Disable "track mode" in the fortran routine
395 //
396 event->clevel1->good2 = 0;
397 event->clevel1->riginput = rigdefault;
398 if ( debug ) printf(" Using as default rigidity: %f \n",event->clevel1->riginput);
399 //
400 // We have a selftrigger event to analyze.
401 //
402 for (Int_t e = 0; e < 5 ; e++){
403 event->clevel1->al_p[e][0] = 0.;
404 event->clevel1->al_p[e][1] = 0.;
405 };
406 event->clevel1->trkchi2 = 0;
407 //
408 if ( debug ) printf("-3 a Call GetTrkVar() \n");
409 event->GetTrkVar();
410 //
411 // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
412 //
413 if ( event->clevel1->good2 == 0 ) {
414 //
415 // In selftrigger mode the trkentry variable is set to -1
416 //
417 event->trkseqno = -3;
418 //
419 // Copy values in the class ca from the structure clevel2
420 //
421 if ( debug ) printf("-3 a Call FillTrkVar() \n");
422 event->FillTrkVar(pcalo,ntrkentry);
423 ntrkentry++;
424 filled = true;
425 //
426 } else {
427 if ( debug ) printf(" Selftrigger: problems with event \n");
428 };
429 //
430 };
431 //
432 // Call high energy nuclei routine
433 //
434 // if ( hZn && event->clevel2->trigty >= 2. ){
435 if ( hZn && m2 ){
436 if ( debug ) printf(" Calling selftrigger high energy nuclei routine \n");
437 //
438 // Disable "track mode" in the fortran routine
439 //
440 event->clevel1->good2 = 0;
441 //
442 // Set high energy nuclei flag to one
443 //
444 event->clevel1->hzn = 1;
445 event->clevel1->riginput = rigdefault;
446 //
447 // We have a selftrigger event to analyze.
448 //
449 for (Int_t e = 0; e < 5 ; e++){
450 event->clevel1->al_p[e][0] = 0.;
451 event->clevel1->al_p[e][1] = 0.;
452 };
453 event->clevel1->trkchi2 = 0;
454 //
455 if ( debug ) printf("-2 a Call GetTrkVar() \n");
456 event->GetTrkVar();
457 //
458 // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
459 //
460 if ( event->clevel1->good2 == 0 ) {
461 //
462 // In selftrigger mode the trkentry variable is set to -1
463 //
464 event->trkseqno = -2;
465 //
466 // Copy values in the class ca from the structure clevel2
467 //
468 if ( debug ) printf("-2 a Call FillTrkVar() \n");
469 event->FillTrkVar(pcalo,ntrkentry);
470 ntrkentry++;
471 filled = true;
472 //
473 } else {
474 if ( debug ) printf(" Selftrigger: problems with event \n");
475 };
476 //
477 };
478 //
479 // self trigger event
480 //
481 // if ( st && event->clevel2->trigty >= 2. ){
482 if ( st && m1 ){
483 if ( debug ) printf(" Selftrigger event \n");
484 //
485 // Disable "track mode" in the fortran routine
486 //
487 event->clevel1->good2 = 0;
488 //
489 // disable high enery nuclei flag;
490 //
491 event->clevel1->hzn = 0;
492 //
493 // We have a selftrigger event to analyze.
494 //
495 for (Int_t e = 0; e < 5 ; e++){
496 event->clevel1->al_p[e][0] = 0.;
497 event->clevel1->al_p[e][1] = 0.;
498 };
499 event->clevel1->trkchi2 = 0;
500 //
501 if ( debug ) printf("-1 a Call GetTrkVar() \n");
502 event->GetTrkVar();
503 //
504 // if we had no problem (clevel2->good = 0, NOTICE zero, not one in selftrigger mode!), fill and go on
505 //
506 if ( event->clevel1->good2 == 0 ) {
507 //
508 // In selftrigger mode the trkentry variable is set to -1
509 //
510 event->trkseqno = -1;
511 //
512 // Copy values in the class ca from the structure clevel2
513 //
514 if ( debug ) printf("-1 a Call FillTrkVar() \n");
515 event->FillTrkVar(pcalo,ntrkentry);
516 ntrkentry++;
517 filled = true;
518 //
519 } else {
520 if ( debug ) printf(" Selftrigger: problems with event \n");
521 };
522 };
523 // //
524 // // Clear structures used to communicate with fortran
525 // //
526 // event->ClearStructs();
527 // ELENA: moved @ beginning
528 //
529 //
530 //
531 if ( debug ) this->Print();
532 if ( debug ) printf(" exit \n");
533 //
534 }
535
536 //
537 // Method to add a calorimeter track, evaluated around a tracker track defined by a status vector.
538 // (can be used to evaluate the calorimeter track around an arbitrary axis, by setting the status vector with zero deflection )
539 //
540 //
541 CaloTrkVar* CaloPreSampler::AddCaloTrkVar(float *al,int trktag){
542
543 int ntrkentry = pcalo->ntrk();
544 //
545 for (Int_t nt=0; nt < ntrkentry; nt++){
546 if( pcalo->GetCaloTrkVar(nt)->trkseqno == trktag){
547 cout << " CaloTrkVar* CaloPreSampler::AddCaloTrkVar(float *al,int trktag)"<<endl;
548 cout << " --> trktag = "<<trktag<<" already defined "<<endl;
549 return NULL;
550 }
551 }
552 //
553 event->clevel1->good2 = 1; //is a trk track
554 event->clevel1->trkchi2 = 0;
555 event->clevel1->hzn = 0;
556 //
557 // Copy the alpha vector in the input structure
558 //
559 for (Int_t e = 0; e < 5 ; e++){
560 event->clevel1->al_p[e][0] = al[e];
561 };
562 //
563 // Get tracker related variables for this track
564 //
565 if ( debug ) printf("track %i Call GetTrkVar() \n",trktag);
566 event->GetTrkVar();
567 if ( debug ) printf(" event->clevel2->dX0l %f \n",event->clevel2->dX0l);
568 //
569 // Save tracker track sequence number
570 //
571 event->trkseqno = trktag;
572 //
573 // Copy values in the class ca from the structure clevel2
574 //
575 if ( debug ) printf("track %i Call FillTrkVar() \n",trktag);
576 event->FillTrkVar(pcalo,ntrkentry);
577
578 return pcalo->GetCaloTrkVar(ntrkentry);
579
580
581 };//ELENA

  ViewVC Help
Powered by ViewVC 1.1.23