Minor corrections thanks to I.Hrivnacova
[u/mrichter/AliRoot.git] / PHOS / AliPHOS.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17 $Log$
18 Revision 1.8  1999/09/29 09:24:23  fca
19 Introduction of the Copyright and cvs Log
20
21 */
22
23 ////////////////////////////////////////////////
24 //  Manager and hits classes for set:PHOS     //
25 ////////////////////////////////////////////////
26  
27 // --- ROOT system ---
28 #include "TH1.h"
29 #include "TRandom.h"
30 #include "TFile.h"
31 #include "TTree.h"
32 #include "TBRIK.h"
33 #include "TNode.h"
34 #include "TMath.h"
35
36 // --- Standard library ---
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <iostream.h>
41
42 // --- galice header files ---
43 #include "AliPHOS.h"
44 #include "AliRun.h"
45
46 //______________________________________________________________________________
47
48
49 ClassImp(AliPHOS)
50
51 //______________________________________________________________________________
52
53 AliPHOS::~AliPHOS(void)
54 {
55   delete fHits;                 // 28.12.1998
56   delete fTreePHOS;             // 28.12.1998
57   fCradles->Delete();
58   delete fCradles;
59 }
60
61 //______________________________________________________________________________
62
63 AliPHOS::AliPHOS() :
64          fDebugLevel            (0),
65          fTreePHOS              (NULL),
66          fBranchNameOfCradles   ("AliPHOSCradles"),
67          fTreeName              ("PHOS")
68 {
69    fIshunt   = 0;
70
71   if( NULL==(fCradles=new TObjArray) )
72   {
73     Error("AliPHOS","Can not create fCradles");
74     exit(1);
75   }
76   DefPars();
77 }
78  
79 //______________________________________________________________________________
80
81 AliPHOS::AliPHOS(const char *name, const char *title)
82        : AliDetector            (name,title),
83          fDebugLevel            (0),
84          fTreePHOS              (NULL),
85          fBranchNameOfCradles   ("AliPHOSCradles"),
86          fTreeName              ("PHOS")
87 {
88 //Begin_Html
89 /*
90 <img src="picts/aliphos.gif">
91 */
92 //End_Html
93  
94    fHits   = new TClonesArray("AliPHOShit",  405);
95  
96    fIshunt     =  0;
97
98    SetMarkerColor(kGreen);
99    SetMarkerStyle(2);
100    SetMarkerSize(0.4);
101
102   if( NULL==(fCradles=new TObjArray) ) {
103      Error("AliPHOS","Can not create fCradles");
104      exit(1);
105   }
106   DefPars();
107 }
108
109 //______________________________________________________________________________
110
111 void AliPHOS::DefPars()
112
113       PHOSflags[0]=0;
114       PHOSflags[1]=1;
115       PHOSflags[2]=0;
116       PHOSflags[3]=0;
117       PHOSflags[4]=0;
118       PHOSflags[5]=0;
119       PHOSflags[6]=0;
120       PHOSflags[7]=0;
121       PHOSflags[8]=0;
122       PHOScell[0]=2.2;
123       PHOScell[1]=18.;
124       PHOScell[2]=0.01;
125       PHOScell[3]=0.01;
126       PHOScell[4]=1.0;
127       PHOScell[5]=0.1;
128       PHOScell[6]=0.;
129       PHOScell[7]=0.;
130       PHOScell[8]=0.;
131       PHOSradius=460.;
132       PHOSsize[0]=104;
133       PHOSsize[1]=88;
134       PHOSsize[2]=4;
135       PHOScradlesA=0.;
136       PHOSextra[0]=0.001;
137       PHOSextra[1]=6.95;
138       PHOSextra[2]=4.;
139       PHOSextra[3]=5.;
140       PHOSextra[4]=2.;
141       PHOSextra[5]=0.06;
142       PHOSextra[6]=10.;
143       PHOSextra[7]=3.;
144       PHOSextra[8]=1.;
145       PHOSTXW[0]=209.;
146       PHOSTXW[1]=71.;
147       PHOSTXW[2]=250.;
148       PHOSAIR[0]=206.;
149       PHOSAIR[1]=66.;
150       PHOSAIR[2]=244.;
151       PHOSFTI[0]=214.6;
152       PHOSFTI[1]=80.;
153       PHOSFTI[2]=260.;
154       PHOSFTI[3]=467.;
155 }
156 //______________________________________________________________________________
157
158 void AliPHOS::AddHit(Int_t track, Int_t *vol, Float_t *hits)
159 {
160   TClonesArray &lhits = *fHits;
161   new(lhits[fNhits++]) AliPHOShit(fIshunt,track,vol,hits);
162 }
163  
164 //___________________________________________
165 void AliPHOS::BuildGeometry()
166 {
167
168   TNode *Node, *Top;
169
170   const int kColorPHOS = kRed;
171   //
172   Top=gAlice->GetGeometry()->GetNode("alice");
173
174
175   // PHOS
176   Float_t pphi=12.9399462;
177   new TRotMatrix("rot988","rot988",90,-3*pphi,90,90-3*pphi,0,0);
178   new TRotMatrix("rot989","rot989",90,-  pphi,90,90-  pphi,0,0);
179   new TRotMatrix("rot990","rot990",90,   pphi,90,90+  pphi,0,0);
180   new TRotMatrix("rot991","rot991",90, 3*pphi,90,90+3*pphi,0,0);
181   new TBRIK("S_PHOS","PHOS box","void",107.3,40,130);
182   Top->cd();
183   Node = new TNode("PHOS1","PHOS1","S_PHOS",-317.824921,-395.014343,0,"rot988");
184   Node->SetLineColor(kColorPHOS);
185   fNodes->Add(Node);
186   Top->cd();
187   Node = new TNode("PHOS2","PHOS2","S_PHOS",-113.532333,-494.124908,0,"rot989");
188   fNodes->Add(Node);
189   Node->SetLineColor(kColorPHOS);
190   Top->cd();
191   Node = new TNode("PHOS3","PHOS3","S_PHOS", 113.532333,-494.124908,0,"rot990");
192   Node->SetLineColor(kColorPHOS);
193   fNodes->Add(Node);
194   Top->cd();
195   Node = new TNode("PHOS4","PHOS4","S_PHOS", 317.824921,-395.014343,0,"rot991");
196   Node->SetLineColor(kColorPHOS);
197   fNodes->Add(Node);
198 }
199  
200 //___________________________________________
201 void AliPHOS::CreateMaterials()
202 {
203 // *** DEFINITION OF AVAILABLE PHOS MATERIALS *** 
204
205 // CALLED BY : PHOS_MEDIA 
206 // ORIGIN    : NICK VAN EIJNDHOVEN 
207
208
209
210     Int_t   ISXFLD = gAlice->Field()->Integ();
211     Float_t SXMGMX = gAlice->Field()->Max();
212     
213 // --- The PbWO4 crystals --- 
214     Float_t ax[3] = { 207.19,183.85,16. };
215     Float_t zx[3] = { 82.,74.,8. };
216     Float_t wx[3] = { 1.,1.,4. };
217     Float_t dx    = 8.28;
218 // --- Stainless Steel --- 
219     Float_t as[5] = { 55.847,12.011,51.9961,58.69,28.0855 };
220     Float_t zs[5] = { 26.,6.,24.,28.,14. };
221     Float_t ws[5] = { .6392,8e-4,.2,.14,.02 };
222     Float_t ds    = 8.;
223 // --- The polysterene scintillator (CH) --- 
224     Float_t ap[2] = { 12.011,1.00794 };
225     Float_t zp[2] = { 6.,1. };
226     Float_t wp[2] = { 1.,1. };
227     Float_t dp    = 1.032;
228 // --- Tyvek (CnH2n) 
229     Float_t at[2] = { 12.011,1.00794 };
230     Float_t zt[2] = { 6.,1. };
231     Float_t wt[2] = { 1.,2. };
232     Float_t dt    = .331;
233 // --- Polystyrene foam --- 
234     Float_t af[2] = { 12.011,1.00794 };
235     Float_t zf[2] = { 6.,1. };
236     Float_t wf[2] = { 1.,1. };
237     Float_t df    = .12;
238 //--- Foam thermo insulation (actual chemical composition unknown yet!) ---
239     Float_t ati[2] = { 12.011,1.00794 };
240     Float_t zti[2] = { 6.,1. };
241     Float_t wti[2] = { 1.,1. };
242     Float_t dti    = .1;
243 // --- Textolit (actual chemical composition unknown yet!) --- 
244     Float_t atx[2] = { 12.011,1.00794 };
245     Float_t ztx[2] = { 6.,1. };
246     Float_t wtx[2] = { 1.,1. };
247     Float_t dtx    = 1.83;
248
249     Int_t *idtmed = fIdtmed->GetArray()-699;
250
251     AliMixture(  0, "PbWO4$",          ax, zx, dx, -3, wx);
252     AliMixture(  1, "Polystyrene$",    ap, zp, dp, -2, wp);
253     AliMaterial( 2, "Al$",             26.98, 13., 2.7, 8.9, 999);
254 // ---                                Absorption length^ is ignored --- 
255     AliMixture(  3, "Tyvek$",           at, zt, dt, -2, wt);
256     AliMixture(  4, "Foam$",            af, zf, df, -2, wf);
257     AliMixture(  5, "Stainless Steel$", as, zs, ds, 5, ws);
258     AliMaterial( 6, "Si$",              28.09, 14., 2.33, 9.36, 42.3);
259     AliMixture(  7, "Thermo Insul.$",   ati, zti, dti, -2, wti);
260     AliMixture(  8, "Textolit$",        atx, ztx, dtx, -2, wtx);
261     AliMaterial(99, "Air$",             14.61, 7.3, .001205, 30420., 67500);
262
263     AliMedium(0, "PHOS Xtal    $", 0, 1, ISXFLD, SXMGMX, 10., .1, .1, .1, .1);
264     AliMedium(2, "Al parts     $", 2, 0, ISXFLD, SXMGMX, 10., .1, .1, .001, .001);
265     AliMedium(3, "Tyvek wrapper$", 3, 0, ISXFLD, SXMGMX, 10., .1, .1, .001, .001);
266     AliMedium(4, "Polyst. foam $", 4, 0, ISXFLD, SXMGMX, 10., .1, .1, .1, .1);
267     AliMedium(5, "Steel cover  $", 5, 0, ISXFLD, SXMGMX, 10., .1, .1, 1e-4, 1e-4);
268     AliMedium(6, "Si PIN       $", 6, 0, ISXFLD, SXMGMX, 10., .1, .1, .01, .01);
269     AliMedium(7, "Thermo Insul.$", 7, 0, ISXFLD, SXMGMX, 10., .1, .1, .1, .1);
270     AliMedium(8, "Textolit     $", 8, 0, ISXFLD, SXMGMX, 10., .1, .1, .1, .1);
271     AliMedium(99, "Air          $",99, 0, ISXFLD, SXMGMX, 10., 1., .1, .1, 10);
272
273 // --- Generate explicitly delta rays in the steel cover --- 
274     gMC->Gstpar(idtmed[704], "LOSS", 3.);
275     gMC->Gstpar(idtmed[704], "DRAY", 1.);
276 // --- and in aluminium parts --- 
277     gMC->Gstpar(idtmed[701], "LOSS", 3.);
278     gMC->Gstpar(idtmed[701], "DRAY", 1.);
279 }
280  
281 //______________________________________________________________________________
282
283 void AliPHOS::AddPHOSCradles()
284 {
285   Int_t i;
286   for(i=0;i<GetCradlesAmount();i++) {
287     
288     int n = fCradles->GetEntries();
289     fCradles->Add(new AliPHOSCradle( IsVersion(),            // geometry.
290                                      GetCrystalSideSize    (),
291                                      GetCrystalLength      (),
292                                      GetWrapThickness      (),
293                                      GetAirThickness       (),
294                                      GetPIN_SideSize       (),
295                                      GetPIN_Length         (),
296                                      GetRadius             (),
297                                      GetNz                 (),
298                                      GetNphi               (),
299                                      GetCradleAngle        (i)));
300     
301     if( n+1 != fCradles->GetEntries() || NULL == fCradles->At(n) )
302       {
303         cout << "  Can not create or add AliPHOSCradle.\n";
304         exit(1);
305       }
306   }
307 }
308
309 //______________________________________________________________________________
310
311 Int_t AliPHOS::DistancetoPrimitive(Int_t , Int_t )
312 {
313    return 9999;
314 }
315  
316 //___________________________________________
317 void AliPHOS::Init()
318 {
319   Int_t i;
320   //
321   printf("\n");
322   for(i=0;i<35;i++) printf("*");
323   printf(" PHOS_INIT ");
324   for(i=0;i<35;i++) printf("*");
325   printf("\n");
326   //
327   // Here the ABSO initialisation code (if any!)
328   for(i=0;i<80;i++) printf("*");
329   printf("\n");
330 }
331
332 //______________________________________________________________________________
333
334 void AliPHOS::MakeBranch(Option_t *)
335 {
336 // ROOT output initialization to ROOT file.
337 // 
338 // AliDetector::MakeBranch()  is always called.
339 //
340 // There will be also special tree "PHOS" with one branch "AliPHOSCradles"
341 // if it was set next flag in the galice card file:
342 //  * PHOSflags:    YES: X<>0   NO: X=0
343 //  * PHOSflags(1) : -----X.  Create branch for TObjArray of AliPHOSCradle
344 //     Examples:
345 //     PHOSflags      1.
346 //     PHOSflags 636301.
347 // In that case special bit CradlesBranch_Bit will be set for AliPHOS
348
349   AliDetector::MakeBranch();
350   
351   int i;
352   float t = GetPHOS_flag(0)/10;
353   i = (int) t;
354   i = (int) ((t-i)*10);
355   if( !i )
356     return;
357
358   SetBit(CradlesBranch_Bit);
359
360   if( NULL==(fTreePHOS=new TTree(fTreeName.Data(),"PHOS events tree")) )
361   {
362     Error("MakeBranch","Can not create TTree");
363     exit(1);
364   }
365
366   if( NULL==fTreePHOS->GetCurrentFile() )
367   {
368     Error("MakeBranch","There is no opened ROOT file");
369     exit(1);
370   }
371
372   // Create a new branch in the current Root Tree.
373
374   if( NULL==fTreePHOS->Branch(fBranchNameOfCradles.Data(),"TObjArray",&fCradles,4000,0) )
375   {
376     Error("MakeBranch","Can not create branch");
377     exit(1);
378   }
379
380   printf("The branch %s has been created\n",fBranchNameOfCradles.Data());
381 }
382
383 //______________________________________________________________________________
384
385 void AliPHOS::SetTreeAddress(void)
386 {
387 // ROOT input initialization.
388 //
389 // AliDetector::SetTreeAddress()  is always called.
390 //
391 // If CradlesBranch_Bit is set (see AliPHOS::MakeBranch) than fTreePHOS is
392 // initilized.
393
394   AliDetector::SetTreeAddress();
395
396   if( !TestBit(CradlesBranch_Bit) )
397     return;
398
399   if( NULL==(fTreePHOS=(TTree*)gDirectory->Get((char*)(fTreeName.Data()))  ) )
400   {
401     Error("SetTreeAddress","Can not find Tree \"%s\"\n",fTreeName.Data());
402     exit(1);
403   }
404
405   TBranch *branch = fTreePHOS->GetBranch(fBranchNameOfCradles.Data());
406   if( NULL==branch )
407   {
408     Error("SetTreeAddress","Can not find branch %s in TTree:%s",fBranchNameOfCradles.Data(),fTreeName.Data());
409     exit(1);
410   }
411
412   branch->SetAddress(&fCradles);
413 }
414
415 //______________________________________________________________________________
416
417 AliPHOSCradle *AliPHOS::GetCradleOfTheParticle(const TVector3 &p,const TVector3 &v) const
418 {
419 // For a given direction 'p' and source point 'v' returns pointer to AliPHOSCradle
420 // in that direction or NULL if AliPHOSCradle was not found.
421
422   for( int m=0; m<fCradles->GetEntries(); m++ )
423   {
424     AliPHOS *PHOS = (AliPHOS *)this;     // Removing 'const'...
425     AliPHOSCradle *cradle = (AliPHOSCradle *)PHOS->fCradles->operator[](m);
426
427     float x,y,l;
428     const float d = cradle->GetRadius();
429     cradle->GetXY(p,v,d,x,y,l);
430
431     if( l>0 && TMath::Abs(x)<cradle->GetNz  ()*cradle->GetCellSideSize()/2 
432             && TMath::Abs(y)<cradle->GetNphi()*cradle->GetCellSideSize()/2 )
433       return cradle;
434   }
435
436   return NULL;
437 }
438
439 //______________________________________________________________________________
440
441 void AliPHOS::Reconstruction(Float_t signal_step, UInt_t min_signal_reject)
442 {
443 // Call AliPHOSCradle::Reconstruction(Float_t signal_step, UInt_t min_signal_reject)
444 // for all AliPHOSCradles.
445
446   for( int i=0; i<fCradles->GetEntries(); i++ )
447     GetCradle(i).Reconstruction(signal_step,min_signal_reject);
448 }
449
450 //______________________________________________________________________________
451
452 void AliPHOS::ResetDigits(void)
453 {
454   AliDetector::ResetDigits();
455
456   for( int i=0; i<fCradles->GetEntries(); i++ )
457     ((AliPHOSCradle*)(*fCradles)[i]) -> Clear();
458 }
459
460 //______________________________________________________________________________
461
462 void AliPHOS::FinishEvent(void)
463 {
464 // Called at the end of each 'galice' event.
465
466   if( NULL!=fTreePHOS )
467     fTreePHOS->Fill();
468 }
469
470 //______________________________________________________________________________
471
472 void AliPHOS::FinishRun(void)
473 {
474 }
475
476 //______________________________________________________________________________
477
478 void AliPHOS::Print(Option_t *opt)
479 {
480 // Print PHOS information.
481 // For each AliPHOSCradle the function AliPHOSCradle::Print(opt) is called.
482
483   AliPHOS &PHOS = *(AliPHOS *)this;     // Removing 'const'...
484
485   for( int i=0; i<fCradles->GetEntries(); i++ )
486   {
487     printf("PHOS cradle %d from %d\n",i+1, fCradles->GetEntries());
488     PHOS.GetCradle(i).Print(opt);
489     printf( "---------------------------------------------------\n");
490   }
491 }
492
493 //______________________________________________________________________________
494 void AliPHOS::SetFlags(Float_t p1,Float_t p2,Float_t p3,Float_t p4,
495                        Float_t p5,Float_t p6,Float_t p7,Float_t p8,Float_t p9)
496 {
497   PHOSflags[0]=p1;
498   PHOSflags[1]=p2;
499   PHOSflags[2]=p3;
500   PHOSflags[3]=p4;
501   PHOSflags[4]=p5;
502   PHOSflags[5]=p6;
503   PHOSflags[6]=p7;
504   PHOSflags[7]=p8;
505   PHOSflags[8]=p9;
506 }
507
508 //______________________________________________________________________________
509 void AliPHOS::SetCell(Float_t p1,Float_t p2,Float_t p3,Float_t p4,
510                        Float_t p5,Float_t p6,Float_t p7,Float_t p8,Float_t p9)
511 {
512   PHOScell[0]=p1;
513   PHOScell[1]=p2;
514   PHOScell[2]=p3;
515   PHOScell[3]=p4;
516   PHOScell[4]=p5;
517   PHOScell[5]=p6;
518   PHOScell[6]=p7;
519   PHOScell[7]=p8;
520   PHOScell[8]=p9;
521 }
522
523 //______________________________________________________________________________
524 void AliPHOS::SetRadius(Float_t radius)
525 {
526    PHOSradius=radius;
527 }
528
529 //______________________________________________________________________________
530 void AliPHOS::SetCradleSize(Int_t nz, Int_t nphi, Int_t ncradles)
531 {
532    PHOSsize[0]=nz;
533    PHOSsize[1]=nphi;
534    PHOSsize[2]=ncradles;
535 }
536
537 //______________________________________________________________________________
538 void AliPHOS::SetCradleA(Float_t angle)
539 {
540    PHOScradlesA=angle;
541 }
542
543 //______________________________________________________________________________
544 void AliPHOS::SetExtra(Float_t p1,Float_t p2,Float_t p3,Float_t p4,
545                        Float_t p5,Float_t p6,Float_t p7,Float_t p8,Float_t p9)
546 {
547    PHOSextra[0] = p1;
548    PHOSextra[1] = p2;
549    PHOSextra[2] = p3;
550    PHOSextra[3] = p4;
551    PHOSextra[4] = p5;
552    PHOSextra[5] = p6;
553    PHOSextra[6] = p7;
554    PHOSextra[7] = p8;
555    PHOSextra[8] = p9;
556 }
557
558 //______________________________________________________________________________
559 void AliPHOS::SetTextolitWall(Float_t dx, Float_t dy, Float_t dz)
560 {
561    PHOSTXW[0] = dx;
562    PHOSTXW[1] = dy;
563    PHOSTXW[2] = dz;
564 }
565
566 //______________________________________________________________________________
567 void AliPHOS::SetInnerAir(Float_t dx, Float_t dy, Float_t dz)
568 {
569    PHOSAIR[0] = dx;
570    PHOSAIR[1] = dy;
571    PHOSAIR[2] = dz;
572 }
573
574 //______________________________________________________________________________
575 void AliPHOS::SetFoam(Float_t dx, Float_t dy, Float_t dz, Float_t dr)
576 {
577    PHOSFTI[0] = dx;
578    PHOSFTI[1] = dy;
579    PHOSFTI[2] = dz;
580    PHOSFTI[3] = dr;
581 }
582
583 ClassImp(AliPHOSCradle)
584
585 //______________________________________________________________________________
586
587 AliPHOSCradle::AliPHOSCradle(void) {}
588
589 //______________________________________________________________________________
590
591 AliPHOSCradle::AliPHOSCradle( int   Geometry           ,
592                               float CrystalSideSize    ,
593                               float CrystalLength      ,
594                               float WrapThickness      ,
595                               float AirThickness       ,
596                               float PIN_SideSize       ,
597                               float PIN_Length         ,
598                               float Radius             ,
599                               int   Nz                 ,
600                               int   Nphi               ,
601                               float Angle              ) :
602     fGeometry                   (Geometry),
603 //  fCellEnergy                 (),
604 //  fChargedTracksInPIN         (),
605     fCrystalSideSize            (CrystalSideSize),
606     fCrystalLength              (CrystalLength),
607     fWrapThickness              (WrapThickness),
608     fAirThickness               (AirThickness),
609     fPIN_SideSize               (PIN_SideSize),
610     fPIN_Length                 (PIN_Length),
611     fRadius                     (Radius),
612     fNz                         (Nz),
613     fNphi                       (Nphi),
614     fPhi                        (Angle)
615 {
616         fCellEnergy         = TH2F("CellE","Energy deposition in a cells",fNz,0,fNz,fNphi,0,fNphi);
617         fCellEnergy           .SetDirectory(0);
618         fChargedTracksInPIN = TH2S("PINCtracks","Amount of charged tracks in PIN",fNz,0,fNz,fNphi,0,fNphi);
619         fChargedTracksInPIN   .SetDirectory(0);
620 }
621
622 //______________________________________________________________________________
623
624 AliPHOSCradle::~AliPHOSCradle(void)        // 28.12.1998
625 {
626   fGammasReconstructed.Delete();
627   fParticles          .Delete();
628 }
629
630 //______________________________________________________________________________
631
632 void AliPHOSCradle::Clear(Option_t *)
633 {
634 // Clear digit. information.
635
636   fCellEnergy              .Reset();
637   fChargedTracksInPIN      .Reset();
638   GetParticles()           .Delete();
639   GetParticles()           .Compress();
640   GetGammasReconstructed() .Delete();
641   GetGammasReconstructed() .Compress();
642
643 }
644
645 //______________________________________________________________________________
646
647 void AliPHOSCradle::GetXY(const TVector3 &p,const TVector3 &v,float R,float &x,float &y,float &l) const
648 {
649 // This function calculates hit position (x,y) in the CRADLE cells plain from particle in
650 // the direction given by 'p' (not required to be normalized) and start point
651 // given by 3-vector 'v'. So the particle trajectory is   t(l) = v + p*l
652 // were 'l' is a number (distance from 'v' to CRADLE cells plain) and 't' is resulting
653 // three-vector of trajectory point.
654 // 
655 // After the call to this function user should test that l>=0 (the particle HITED the
656 // plain) and (x,y) are in the region of CRADLE:
657 // 
658 // Example:
659 //   AliPHOSCradle cradle(......);
660 //   TVector3 p(....), v(....);
661 //   Float_t x,y,l;
662 //   cradle.GetXY(p,v,x,y,l);
663 //   if( l<0 || TMath::Abs(x)>cradle.GetNz()  *cradle.GetCellSideSize()/2
664 //           || TMath::Abs(y)>cradle.GetNphi()*cradle.GetCellSideSize()/2 )
665 //     cout << "Outside the CRADLE.\n";
666
667   // We have to create three vectors:
668   //    s  - central point on the PHOS surface
669   //    n1 - first vector in CRADLE plain
670   //    n2 - second vector in CRADLE plain
671   // This three vectors are orthonormalized.
672
673   double phi = fPhi/180*TMath::Pi();
674   TVector3        n1(   0.0      ,   0.0      , 1.0 ),   // Z direction (X)
675                   n2(  -sin(phi) ,   cos(phi) , 0 ),   // around beam (Y)
676                   s ( R*cos(phi) , R*sin(phi) , 0 );   // central point
677
678   const double l1_min = 1e-2;
679   double l1,
680          p_n1 = p*n1,        // * - scalar product.
681          p_n2 = p*n2,
682          v_n1 = v*n1,
683          v_n2 = v*n2,
684          s_n1 = s*n1, // 0
685          s_n2 = s*n2; // 0
686   
687   if      ( TMath::Abs(l1=p.X()-n1.X()*p_n1-n2.X()*p_n2)>l1_min )
688     { l = (-v.X()+s.X()+n1.X()*(v_n1-s_n1)+n2.X()*(v_n2-s_n2))/l1; }
689   else if ( TMath::Abs(l1=p.Y()-n1.Y()*p_n1-n2.Y()*p_n2)>l1_min )
690     { l = (-v.Y()+s.Y()+n1.Y()*(v_n1-s_n1)+n2.Y()*(v_n2-s_n2))/l1; }
691   else if ( TMath::Abs(l1=p.Z()-n1.Z()*p_n1-n2.Z()*p_n2)>l1_min )
692     { l = (-v.Z()+s.Z()+n1.Z()*(v_n1-s_n1)+n2.Z()*(v_n2-s_n2))/l1; }
693
694 //         double lx = (-v.X()+s.X()+n1.X()*(v.dot(n1)-s.dot(n1))+n2.X()*(v.dot(n2)-s.dot(n2)))/
695 //                     (p.X()-n1.X()*p.dot(n1)-n2.X()*p.dot(n2)),
696 //                ly = (-v.Y()+s.Y()+n1.Y()*(v.dot(n1)-s.dot(n1))+n2.Y()*(v.dot(n2)-s.dot(n2)))/
697 //                     (p.Y()-n1.Y()*p.dot(n1)-n2.Y()*p.dot(n2)),
698 //                lz = (-v.Z()+s.Z()+n1.Z()*(v.dot(n1)-s.dot(n1))+n2.Z()*(v.dot(n2)-s.dot(n2)))/
699 //                     (p.Z()-n1.Z()*p.dot(n1)-n2.Z()*p.dot(n2));
700 //         cout.form("x: %g %g %g %g\n",lx,-v.X()+s.X()+n1.X()*(v.dot(n1)-s.dot(n1))+n2.X()*(v.dot(n2)-s.dot(n2)),p.X()-n1.X()*p.dot(n1)-n2.X()*p.dot(n2));
701 //         cout.form("y: %g %g %g %g\n",lx,-v.Y()+s.Y()+n1.Y()*(v.dot(n1)-s.dot(n1))+n2.Y()*(v.dot(n2)-s.dot(n2)),p.Y()-n1.Y()*p.dot(n1)-n2.Y()*p.dot(n2));
702 //         cout.form("z: %g %g %g %g\n",lx,-v.Z()+s.Z()+n1.Z()*(v.dot(n1)-s.dot(n1))+n2.Z()*(v.dot(n2)-s.dot(n2)),p.Z()-n1.Z()*p.dot(n1)-n2.Z()*p.dot(n2));
703 //         cout.form("lx,ly,lz =   %g,%g,%g\n",lx,ly,lz);
704
705   x = p_n1*l + v_n1 - s_n1;
706   y = p_n2*l + v_n2 - s_n2;
707 }
708
709 //______________________________________________________________________________
710
711 void AliPHOSCradle::Print(Option_t *opt)
712 {
713 // Print AliPHOSCradle information.
714 // 
715 // options:  'd' - print energy deposition for EVERY cell
716 //           'p' - print particles list that hit the cradle
717 //           'r' - print list of reconstructed particles
718
719   AliPHOSCradle *cr = (AliPHOSCradle *)this;     // Removing 'const'...
720
721   printf("AliPHOSCradle:  Nz=%d  Nphi=%d, fPhi=%f, E=%g\n",fNz,fNphi,fPhi,
722        cr->fCellEnergy.GetSumOfWeights());
723
724   if( NULL!=strchr(opt,'d') )
725   {
726     printf("\n\nCells Energy (in MeV):\n\n   |");
727     for( int x=0; x<fNz; x++ )
728       printf(" %4d|",x+1);
729     printf("\n");
730
731     for( int y=fNphi-1; y>=0; y-- )
732     {
733       printf("%3d|",y+1);
734       for( int x=0; x<fNz; x++ )
735         printf("%6d",(int)(cr->fCellEnergy.GetBinContent(cr->fCellEnergy.GetBin(x,y))*1000));
736       printf("\n");
737     }
738     printf("\n");
739   }
740
741   if( NULL!=strchr(opt,'p') )
742   {
743     printf("This cradle was hit by %d particles\n",
744          ((AliPHOSCradle*)this)->GetParticles().GetEntries());
745     TObjArray &p=((AliPHOSCradle*)this)->GetParticles();
746     for( int i=0; i<p.GetEntries(); i++ )
747       ((AliPHOSgamma*)(p[i]))->Print();
748   }
749
750   if( NULL!=strchr(opt,'p') )
751   {
752     printf("Amount of reconstructed gammas is %d\n",
753          ((AliPHOSCradle*)this)->GetGammasReconstructed().GetEntries());
754
755     TObjArray &p=((AliPHOSCradle*)this)->GetGammasReconstructed();
756     for( int i=0; i<p.GetEntries(); i++ )
757       ((AliPHOSgamma*)(p[i]))->Print();
758   }
759 }
760
761 //______________________________________________________________________________
762
763 void AliPHOSCradle::Distortion(const TH2F *Noise, const TH2F *Stochastic, const TH2F *Calibration)
764 {
765 // This function changes histogram of cell energies fCellEnergy on the base of input
766 // histograms Noise, Stochastic, Calibration. The histograms must have
767 // size Nz x Nphi. 
768
769   //////////////////////////////////
770   // Testing the histograms size. //
771   //////////////////////////////////
772   
773   if( fNz!=fCellEnergy.GetNbinsX() || fNphi!=fCellEnergy.GetNbinsY() )
774   {
775     printf      ("Bad size of CellEnergy!   Must be:   Nz x Nphi = %d x %d\n"
776                  "but size of CellEnergy is:  %d x %d\n",
777                  fNz,fNphi,fCellEnergy.GetNbinsX(),fCellEnergy.GetNbinsY());
778     exit(1);
779   }
780
781   if( fNz!=fChargedTracksInPIN.GetNbinsX() || fNphi!=fChargedTracksInPIN.GetNbinsY() )
782   {
783     printf      ("Bad size of ChargedTracksInPIN!   Must be:   Nz x Nphi = %d x %d\n"
784                  "but size of ChargedTracksInPIN is:  %d x %d\n",
785                  fNz,fNphi,fChargedTracksInPIN.GetNbinsX(),fChargedTracksInPIN.GetNbinsY());
786     exit(1);
787   }
788
789   if( NULL!=Noise && (fNz!=Noise->GetNbinsX() || fNphi!=Noise->GetNbinsX()) )
790   {
791     printf      ("Bad size of Noise!   Must be:   Nz x Nphi = %d x %d\n"
792                  "but size of Noise is:  %d x %d\n",
793                  fNz,fNphi,fChargedTracksInPIN.GetNbinsX(),fChargedTracksInPIN.GetNbinsY());
794     exit(1);
795   }
796
797   if( NULL!=Stochastic && (fNz!=Stochastic->GetNbinsX() || fNphi!=Stochastic->GetNbinsX()) )
798   {
799     printf      ("Bad size of Stochastic!   Must be:   Nz x Nphi = %d x %d\n"
800                  "but size of Stochastic is:  %d x %d\n",
801                  fNz,fNphi,fChargedTracksInPIN.GetNbinsX(),fChargedTracksInPIN.GetNbinsY());
802     exit(1);
803   }
804
805   if( NULL!=Calibration && (fNz!=Calibration->GetNbinsX() || fNphi!=Calibration->GetNbinsX()) )
806   {
807     printf      ("Bad size of Calibration!   Must be:   Nz x Nphi = %d x %d\n"
808                  "but size of Calibration is:  %d x %d\n",
809                  fNz,fNphi,fChargedTracksInPIN.GetNbinsX(),fChargedTracksInPIN.GetNbinsY());
810     exit(1);
811   }
812
813   ////////////////////
814   // Do distortion! //
815   ////////////////////
816
817   for( int y=0; y<fNphi; y++ )
818     for( int x=0; x<fNz; x++ )
819     {
820       const int n = fCellEnergy.GetBin(x,y);   // Bin number
821       static TRandom r;
822     
823       Float_t   E_old=fCellEnergy.GetBinContent(n),   E_new=E_old;
824
825       if( NULL!=Stochastic )
826         E_new   = r.Gaus(E_old,sqrt(E_old)*GetDistortedValue(Stochastic,n));
827
828       if( NULL!=Calibration )
829         E_new  *=  GetDistortedValue(Calibration,n);
830
831       if( NULL!=Noise )
832         E_new  +=  GetDistortedValue(Noise,n);
833
834       fCellEnergy.SetBinContent(n,E_new);
835     }
836 }
837
838 ////////////////////////////////////////////////////////////////////////////////
839
840 TH2F* AliPHOSCradle::CreateHistForDistortion(const char *name, const char *title,
841                                              Int_t Nx, Int_t Ny,
842                                              Float_t MU_mu,    Float_t MU_sigma,
843                                              Float_t SIGMA_mu, Float_t SIGMA_sigma)
844 {
845 // Create (new TH2F(...)) histogram with information (for every bin) that will
846 // be used for VALUE creation.
847 // Two values will be created for each bin:
848 // MU    = TRandom::Gaus(MU_mu,MU_sigma)
849 // and
850 // SIGMA = TRandom::Gaus(SIGMA_mu,SIGMA_sigma)
851 // The VALUE in a particluar bin will be equal
852 // VALUE = TRandom::Gaus(MU,SIGMA)
853 // 
854 // Do not forget to delete the histogram at the end of the work.
855
856   TH2F *h = new TH2F( name,title, Nx,1,Nx, Ny,1,Ny );
857   if( h==NULL )
858   {
859     Error("CreateHistForDistortion","Can not create the histogram");
860     exit(1);
861   }
862   h->SetDirectory(0);
863
864   for( int y=0; y<Ny; y++ )
865     for( int x=0; x<Nx; x++ )
866     {
867       const int n = h->GetBin(x,y);
868       h->SetBinContent(n,r.Gaus(   MU_mu,   MU_sigma));
869       h->SetBinError  (n,r.Gaus(SIGMA_mu,SIGMA_sigma));
870     }
871
872   return h;
873 }
874
875 ////////////////////////////////////////////////////////////////////////////////
876
877 Float_t AliPHOSCradle::GetDistortedValue(const TH2F *h, UInt_t n)
878 {
879   return r.Gaus(((TH2F*)h)->GetBinContent(n),n);
880 }
881
882 ////////////////////////////////////////////////////////////////////////////////
883 //______________________________________________________________________________
884
885 #ifdef WIN32
886   #define common_for_event_storing COMMON_FOR_EVENT_STORING
887 #else
888   #define common_for_event_storing common_for_event_storing_
889 #endif
890
891 extern "C" struct
892 {
893   enum { crystals_matrix_amount_max=4, crystals_in_matrix_amount_max=40000 };
894
895   // Event-independent information
896   UShort_t      crystals_matrix_amount_PHOS,
897                 crystal_matrix_type,
898                 amount_of_crystals_on_Z,
899                 amount_of_crystals_on_PHI;
900   Float_t       radius,
901                 crystal_size,
902                 crystal_length,
903                 matrix_coordinate_Z             [crystals_matrix_amount_max],
904                 matrix_coordinate_PHI           [crystals_matrix_amount_max];
905   UInt_t        event_number;
906   UShort_t      crystals_amount_with_amplitudes [crystals_matrix_amount_max],
907                 crystals_amplitudes_Iad         [crystals_matrix_amount_max]
908                                                 [crystals_in_matrix_amount_max][2];
909 } common_for_event_storing;
910
911 //       integer*4 crystals_amount_max,crystals_in_matrix_amount_max,
912 //      +          crystals_matrix_amount_max
913 //       parameter (crystals_matrix_amount_max=4)
914 //       parameter (crystals_in_matrix_amount_max=40000)
915 //       parameter (crystals_amount_max =crystals_matrix_amount_max*
916 //      +                                crystals_in_matrix_amount_max)
917 // 
918 // * All units are in GeV, cm, radian
919 //       real       crystal_amplitudes_unit, radius_unit,
920 //      +           crystal_size_unit, crystal_length_unit,
921 //      +           matrix_coordinate_Z_unit, matrix_coordinate_PHI_unit
922 //       integer    crystal_amplitudes_in_units_min
923 //       parameter (crystal_amplitudes_in_units_min        = 1)
924 //       parameter (crystal_amplitudes_unit                = 0.001 ) ! 1.0  MeV
925 //       parameter (radius_unit                            = 0.1   ) ! 0.1  cm
926 //       parameter (crystal_size_unit                      = 0.01  ) ! 0.01 cm
927 //       parameter (crystal_length_unit                    = 0.01  ) ! 0.01 cm
928 //       parameter (matrix_coordinate_Z_unit               = 0.1   ) ! 0.1  cm
929 //       parameter (matrix_coordinate_PHI_unit             = 1e-4  ) ! 1e-4 radian
930 // 
931 //       integer*2 crystals_matrix_amount_PHOS, crystal_matrix_type,
932 //      +          amount_of_crystals_on_Z, amount_of_crystals_on_PHI,
933 //      +          crystals_amount_with_amplitudes, crystals_amplitudes_Iad
934 //       integer*4 event_number
935 // 
936 //       real      radius, crystal_size, crystal_length,
937 //      +          matrix_coordinate_Z, matrix_coordinate_PHI
938 // 
939 //       real      crystals_amplitudes, crystals_energy_total
940 //       integer   event_file_unit_number
941 // 
942 //       common /common_for_event_storing/
943 //      + ! Event-independent information
944 //      +        crystals_matrix_amount_PHOS,
945 //      +        crystal_matrix_type,
946 //      +        amount_of_crystals_on_Z,
947 //      +        amount_of_crystals_on_PHI,
948 //      +        radius,
949 //      +        crystal_size,
950 //      +        crystal_length,
951 //      +        matrix_coordinate_Z     (crystals_matrix_amount_max),
952 //      +        matrix_coordinate_PHI   (crystals_matrix_amount_max),
953 //      +
954 //      + ! Event-dependent information
955 //      +        event_number,
956 //      +        crystals_amount_with_amplitudes
957 //      +                                (crystals_matrix_amount_max),
958 //      +        crystals_amplitudes_Iad (2,crystals_in_matrix_amount_max,
959 //      +                                 crystals_matrix_amount_max),
960 //      +        
961 //      + ! These information don't store in data file
962 //      +        crystals_amplitudes     (crystals_amount_max),
963 //      +        crystals_energy_total,
964 //      +        event_file_unit_number
965
966
967 //      parameter (NGp=1000,nsps=10,nvertmax=1000)
968 //         COMMON /GAMMA/KG,MW(ngp),ID(ngp),JD(ngp),E(ngp),E4(ngp),
969 //      ,  XW(ngp),YW(ngp),ES(nsps,ngp),ET(nsps,ngp),ISsd(ngp),
970 //      ,  IGDEV(ngp),ZGDEV(ngp),sigexy(3,ngp),Emimx(2,nsps,ngp),
971 //      ,  kgfix,igfix(ngp),cgfix(3,ngp),sgfix(3,ngp),hiw(ngp),
972 //      ,  wsw(nsps,ngp),h1w(ngp),h0w(ngp),raxay(5,ngp),
973 //      ,  sigmaes0(nsps,ngp),dispeces(nsps,ngp),
974 //      ,  igamvert(ngp)
975
976
977 #ifdef WIN32
978 #define rcgamma RCGAMMA
979 #else
980 #define rcgamma rcgamma_
981 #endif
982
983 extern "C" struct
984 {
985   enum {NGP=1000, nsps=10, nvertmax=1000};
986   int   recons_gammas_amount, mw[NGP],ID[NGP],JD[NGP];
987   float E[NGP], E4[NGP], XW[NGP], YW[NGP], ES[NGP][nsps],ET[NGP][nsps],ISsd[NGP],
988         igdev[NGP],Zgdev[NGP];
989 //      sigexy(3,ngp),Emimx(2,nsps,ngp),
990 //   ,  kgfix,igfix(ngp),cgfix(3,ngp),sgfix(3,ngp),hiw(ngp),
991 //   ,  wsw(nsps,ngp),h1w(ngp),h0w(ngp),raxay(5,ngp),
992 //   ,  sigmaes0(nsps,ngp),dispeces(nsps,ngp),
993 //   ,  igamvert(ngp)
994 } rcgamma;
995
996 #ifdef WIN32
997 #define reconsfirst RECONSFIRST
998 #define type_of_call _stdcall
999 #else
1000 #define reconsfirst reconsfirst_
1001 #define type_of_call
1002 #endif
1003
1004 extern "C" void type_of_call reconsfirst(const float &,const float &);
1005
1006 void AliPHOSCradle::Reconstruction(Float_t signal_step, UInt_t min_signal_reject)
1007 {
1008 // Call of PHOS reconstruction program.
1009 // signal_step=0.001  GeV (1MeV)
1010 // min_signal_reject = 15 or 30 MeV
1011
1012
1013   common_for_event_storing.event_number                       = 0;  // We do not know event number?
1014   common_for_event_storing.crystals_matrix_amount_PHOS        = 1;
1015   common_for_event_storing.crystal_matrix_type                = 1; // 1 - rectangular
1016   common_for_event_storing.amount_of_crystals_on_Z            = fNz;
1017   common_for_event_storing.amount_of_crystals_on_PHI          = fNphi;
1018
1019   common_for_event_storing.radius                             = fRadius;
1020   common_for_event_storing.crystal_size                       = GetCellSideSize();
1021   common_for_event_storing.crystal_length                     = fCrystalLength;
1022
1023   common_for_event_storing.matrix_coordinate_Z            [0] = 0;
1024   common_for_event_storing.matrix_coordinate_PHI          [0] = fPhi;
1025
1026   #define  k    common_for_event_storing.crystals_amount_with_amplitudes[0] 
1027   k=0;
1028
1029   for( int y=0; y<fNphi; y++ )
1030     for( int x=0; x<fNz; x++ )
1031     {
1032       UInt_t    n       = fCellEnergy.GetBin(x,y);
1033       UInt_t    signal  = (int) (fCellEnergy.GetBinContent(n)/signal_step);
1034       if( signal>=min_signal_reject )
1035       {
1036         common_for_event_storing.crystals_amplitudes_Iad[0][k][0] = signal;
1037         common_for_event_storing.crystals_amplitudes_Iad[0][k][1] = x + y*fNz;
1038         k++;
1039       }
1040     }
1041   #undef  k
1042
1043   GetGammasReconstructed().Delete();
1044   GetGammasReconstructed().Compress();
1045
1046   const float   stochastic_term   = 0.03,        // per cents over sqrt(E);  E in GeV
1047                 electronic_noise  = 0.01;        // GeV
1048   reconsfirst(stochastic_term,electronic_noise); // Call of reconstruction program.
1049
1050   for( int i=0; i<rcgamma.recons_gammas_amount; i++ )
1051   {
1052 //     new (GetGammasReconstructed().UncheckedAt(i) ) AliPHOSgamma;
1053 //     AliPHOSgamma &g = *(AliPHOSgamma*)(GetGammasReconstructed().UncheckedAt(i));
1054
1055     AliPHOSgamma *gggg = new AliPHOSgamma;
1056     if( NULL==gggg )
1057     {
1058       Error("Reconstruction","Can not create AliPHOSgamma");
1059       exit(1);
1060     }
1061
1062     GetGammasReconstructed().Add(gggg);
1063     AliPHOSgamma &g=*gggg;
1064     
1065     Float_t thetta, alpha, betta, R=fRadius+rcgamma.Zgdev[i]/10;
1066
1067     g.fX      = rcgamma.YW[i]/10;
1068     g.fY      = rcgamma.XW[i]/10;
1069     g.fE      = rcgamma.E [i];
1070
1071     thetta      = atan(g.fX/R);
1072
1073     alpha = atan(g.fY/R);
1074     betta = fPhi/180*TMath::Pi() + alpha;
1075
1076     g.fPx = g.fE * cos(thetta) * cos(betta);
1077     g.fPy = g.fE * cos(thetta) * sin(betta);
1078     g.fPz = g.fE * sin(thetta);
1079   }
1080 }
1081
1082 //______________________________________________________________________________
1083 //______________________________________________________________________________
1084 //______________________________________________________________________________
1085 //______________________________________________________________________________
1086 //______________________________________________________________________________
1087
1088 ClassImp(AliPHOSgamma)
1089
1090 //______________________________________________________________________________
1091
1092 void AliPHOSgamma::Print(Option_t *)
1093 {
1094   float mass = fE*fE - fPx*fPx - fPy*fPy - fPz*fPz;
1095
1096   if( mass>=0 )
1097     mass =  sqrt( mass);
1098   else
1099     mass = -sqrt(-mass);
1100
1101   printf("XY=(%+7.2f,%+7.2f)  (%+7.2f,%+7.2f,%+7.2f;%7.2f)  mass=%8.4f  Ipart=%2d\n",
1102           fX,fY,fPx,fPy,fPz,fE,mass,fIpart);
1103 }
1104
1105 //______________________________________________________________________________
1106
1107 AliPHOSgamma &AliPHOSgamma::operator=(const AliPHOSgamma &g)
1108 {
1109   fX           = g.fX;
1110   fY           = g.fY;
1111   fE           = g.fE;
1112   fPx          = g.fPx;
1113   fPy          = g.fPy;
1114   fPz          = g.fPz;
1115   fIpart       = g.fIpart;
1116
1117   return *this;
1118 }
1119
1120 //______________________________________________________________________________
1121 //______________________________________________________________________________
1122 //______________________________________________________________________________
1123 //______________________________________________________________________________
1124 //______________________________________________________________________________
1125
1126 ClassImp(AliPHOShit)
1127
1128 //______________________________________________________________________________
1129
1130 AliPHOShit::AliPHOShit(Int_t shunt, Int_t track, Int_t *vol, Float_t *hits):
1131 AliHit(shunt, track)
1132 {
1133    Int_t i;
1134    for (i=0;i<5;i++) fVolume[i] = vol[i];
1135    fX       = hits[0];
1136    fY       = hits[1];
1137    fZ       = hits[2];
1138    fELOS    = hits[3];
1139 }
1140  
1141 //______________________________________________________________________________