]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/UPGRADE/AliITSupgrade.cxx
fix for memory leak and mods according to ITStrackerV2
[u/mrichter/AliRoot.git] / ITS / UPGRADE / AliITSupgrade.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 /* $Id$ */
17
18 #include <TArrayD.h>            //new constructor
19 #include <TFile.h>  
20 #include <TGeoManager.h>        //CreateGeometry()
21 #include <TGeoVolume.h>         //CreateGeometry()
22 #include <TVirtualMC.h>         //->gMC in StepManager
23 #include <TPDGCode.h>           //StepHistory
24 #include <TClonesArray.h>
25 #include "AliRun.h"             //CreateMaterials()    
26 #include "AliMC.h"             //CreateMaterials()    
27 #include "AliMagF.h"            //CreateMaterials()
28 #include "AliCDBManager.h"      //CreateMaterials()
29 #include "AliCDBEntry.h"        //CreateMaterials()
30 #include "AliITSupgrade.h"      //class header
31 #include "AliITShit.h"           // StepManager()
32 #include "AliITSDigitUpgrade.h"
33 #include "AliTrackReference.h"   // StepManager()
34 #include "AliITSDetTypeSim.h"
35 ClassImp(AliITSupgrade) 
36 //__________________________________________________________________________________________________
37   AliITSupgrade::AliITSupgrade():
38     AliITS(),
39     fWidths(0),
40     fRadii(0),
41     fRadiiCu(0),
42     fWidthsCu(0),
43     fCopper(0),
44     fBeampipe(0), 
45     fRadiusBP(0),
46     fWidthBP(0),
47     fHalfLengthBP(0),
48     fNlayers(0),
49     fHalfLength(0),
50     fSdigits(0),
51     fDigits(0),
52     //fClusters(0),
53     fSegmentation(0x0)
54 {
55   //
56   // default ctor
57 }
58 //__________________________________________________________________________________________________
59 AliITSupgrade::AliITSupgrade(const char *name,const char *title, TArrayD widths, TArrayD radii,TArrayD halfLengths, TArrayD radiiCu, TArrayD widthsCu, TArrayS copper,Bool_t bp,Double_t radiusBP, Double_t widthBP, Double_t halfLengthBP):
60   AliITS(name,title),
61   fWidths(0),
62   fRadii(0),
63   fRadiiCu(radiiCu),
64   fWidthsCu(widthsCu),
65   fCopper(copper),
66   fBeampipe(bp),
67   fRadiusBP(radiusBP),
68   fWidthBP(widthBP),
69   fHalfLengthBP(halfLengthBP),
70   fNlayers(widths.GetSize()),
71   fHalfLength(halfLengths),
72   fSdigits(0),
73   fDigits(0),
74   //fClusters(0),
75   fSegmentation(0x0)
76 {
77
78   //
79   // constructor
80   for(Int_t i=0;i<fNlayers;i++){
81     fWidths.Set(i+1);
82     fWidths.AddAt(widths.At(i),i);
83     fRadii.Set(i+1);
84     fRadii.AddAt(radii.At(i),i);
85     AliDebug(1,"Creating Volume");
86   }
87   Init();
88
89 }
90
91 AliITSupgrade::~AliITSupgrade(){
92
93   if(fSdigits) {fSdigits->Delete(); delete fSdigits;}
94   if(fDigits)  {fDigits->Delete(); delete fDigits;}
95 }
96
97
98  
99 //_________________________________________________________________________________________________
100 void AliITSupgrade::AddAlignableVolumes()const
101 {
102   //not needed
103   return;
104 }
105
106 //__________________________________________________________________________________________________
107
108 void AliITSupgrade::CreateMaterials()
109 {
110   //
111   // Definition of ITS materials  
112   //
113   
114   AliInfo("Start ITS materials");
115   //data from PDG booklet 2002     density [gr/cm^3] rad len [cm] abs len [cm]    
116   Float_t   aAir[4]={12,14,16,36} ,   zAir[4]={6,7,8,18} ,   wAir[4]={0.000124,0.755267,0.231781,0.012827} , dAir=0.00120479; Int_t nAir=4;//mixture
117   Float_t       aBe = 9.012 ,          zBe   = 4 ,            dBe   =  1.848    ,   radBe   =  65.19/dBe , absBe   = 77.8/dBe  ; // UPGRADE -> beryllium beampipe
118   Float_t       aSi = 28.085 ,         zSi   = 14 ,           dSi   =  2.329    ,   radSi   =  21.82/dSi , absSi   = 108.4/dSi  ; // UPGRADE -> Si tube
119   Float_t aCu = 63.54, zCu = 29 , dCu= 8.96 , radCu = 12.86/dCu, absCu= 137.3/dCu;//Upgrade -> Copper Tube
120
121   Int_t   matId=0;                           //tmp material id number
122   Int_t   unsens = 0, sens=1;                //sensitive or unsensitive medium
123   Int_t   itgfld = 3;                        //type of field intergration 0 no field -1 user in guswim 1 Runge Kutta 2 helix 3 const field along z
124   Float_t maxfld = 5.;                       //max field value
125   Float_t tmaxfd = -10.0;                    //max deflection angle due to magnetic field in one step
126   Float_t deemax = - 0.2;                    //max fractional energy loss in one step   
127   Float_t stemax = - 0.1;                    //max step allowed [cm]
128   Float_t epsil  =   0.001;                  //abs tracking precision [cm]   
129   Float_t stmin  = - 0.001;                  //min step size [cm] in continius process transport, negative value: choose it automatically
130   Float_t tmaxfdSi = 0.1; // .10000E+01; // Degree
131   Float_t stemaxSi = 0.0075; //  .10000E+01; // cm
132   Float_t deemaxSi = 0.1; // 0.30000E-02; // Fraction of particle's energy 0<deemax<=1
133   Float_t epsilSi  = 1.0E-4;// .10000E+01;
134   Float_t stminSi  = 0.0; // cm "Default value used"
135
136   Float_t epsilBe  = .001;    // Tracking precision,
137   Float_t stemaxBe = -0.01;   // Maximum displacement for multiple scat
138   Float_t tmaxfdBe = -20.;    // Maximum angle due to field deflection
139   Float_t deemaxBe = -.3;     // Maximum fractional energy loss, DLS
140   Float_t stminBe  = -.8;
141
142       
143   AliMixture(++matId,"UpgradeAir"  ,aAir  ,zAir  ,dAir  ,nAir  ,wAir  ); 
144   AliMedium(kAir  ,"UpgradeAir"  ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
145     
146   AliMaterial(++matId,"UpgradeSi"  ,aSi  ,zSi  ,dSi  ,radSi  ,absSi  );  
147   AliMedium(kSi  ,"UpgradeSi"  , matId, sens, itgfld, maxfld, tmaxfdSi, stemaxSi, deemaxSi, epsilSi, stminSi);
148     
149   AliMaterial(++matId,"UpgradeBe"  ,aBe  ,zBe  ,dBe  ,radBe  ,absBe  );  
150   AliMedium(kBe  ,"UpgradeBe"  , matId, unsens, itgfld, maxfld, tmaxfdBe, stemaxBe, deemaxBe, epsilBe, stminBe);
151
152   AliMaterial(++matId, "UpgradeCu", aCu, zCu, dCu, radCu, absCu);
153   AliMedium(kCu, "UpgradeCu", matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
154    
155   AliInfo("End ITS materials");
156           
157 }//void AliITS::CreateMaterials()
158 //__________________________________________________________________________________________________
159 void AliITSupgrade::CreateGeometry()
160 {
161   //
162   //Creates detailed geometry simulation (currently GEANT volumes tree)        
163   //
164   AliInfo("Start ITS upgrade preliminary version building");
165   if(!gMC->IsRootGeometrySupported()) return;                
166   TGeoVolumeAssembly *vol= CreateVol();
167   gGeoManager->GetVolume("ALIC")->AddNode(vol,0);
168   AliInfo("Stop ITS upgrade preliminary version building");
169
170 //__________________________________________________________________________________________________
171 void AliITSupgrade::Init()
172 {
173   // This method defines ID for sensitive volumes, i.e. such geometry volumes for which there are if(gMC->CurrentVolID()==XXX) 
174   // statements in StepManager()
175   // Arguments: none
176   //   Returns: none      
177   AliDebug(1,"Init ITS upgrade preliminary version.");    
178 }
179 //__________________________________________________________________________________________________
180 void AliITSupgrade::StepManager()
181 {
182   // Full Step Manager.
183   // Arguments: none
184   //   Returns: none           
185   //  StepHistory(); return; //uncomment to print tracks history
186   //  StepCount(); return;   //uncomment to count photons
187   if(!(this->IsActive())) return;
188   if(!(gMC->TrackCharge())) return;
189   TString volumeName=gMC->CurrentVolName();
190   if(gMC->IsTrackExiting() && !volumeName.Contains("Cu") && !volumeName.Contains("Be")) {
191     AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kITS);
192   } // if Outer ITS mother Volume
193
194   static TLorentzVector position, momentum; // Saves on calls to construtors
195   static AliITShit hit;// Saves on calls to constructors
196
197   
198   Int_t  status = 0;
199   
200   //
201   // Track status
202   if(gMC->IsTrackInside())      status +=  1;
203   if(gMC->IsTrackEntering())    status +=  2;
204   if(gMC->IsTrackExiting())     status +=  4;
205   if(gMC->IsTrackOut())         status +=  8;
206   if(gMC->IsTrackDisappeared()) status += 16;
207   if(gMC->IsTrackStop())        status += 32;
208   if(gMC->IsTrackAlive())       status += 64;
209
210   //
211   // Fill hit structure.
212   //
213   TString volname = gMC->CurrentVolName();
214   if(volname.Contains("Cu"))return;
215   if(volname.Contains("Be"))return; 
216   volname.Remove(0,12);          // remove letters to get the layer number
217   hit.SetModule(volname.Atoi()); // this will be the layer, not the module
218   hit.SetTrack(gAlice->GetMCApp()->GetCurrentTrackNumber());
219     
220   gMC->TrackPosition(position);
221   gMC->TrackMomentum(momentum);
222   hit.SetPosition(position);
223     
224   hit.SetTime(gMC->TrackTime());
225   hit.SetMomentum(momentum);
226   hit.SetStatus(status);
227   hit.SetEdep(gMC->Edep());
228   hit.SetShunt(GetIshunt());
229   if(gMC->IsTrackEntering()){
230     hit.SetStartPosition(position);
231     hit.SetStartTime(gMC->TrackTime());
232     hit.SetStartStatus(status);
233     return; // don't save entering hit.
234   } 
235     // Fill hit structure with this new hit.
236     
237   new((*fHits)[fNhits++]) AliITShit(hit); // Use Copy Construtor.
238   // Save old position... for next hit.
239   hit.SetStartPosition(position);
240   hit.SetStartTime(gMC->TrackTime());
241   hit.SetStartStatus(status);
242   return;
243
244 }
245 //__________________________________________________________________________________________________
246 TGeoVolumeAssembly * AliITSupgrade::CreateVol()
247 {
248   TGeoVolumeAssembly *vol = new TGeoVolumeAssembly("ITSupgrade");
249   TGeoMedium *si   =gGeoManager->GetMedium("ITS_UpgradeSi");
250   TGeoMedium *cu   =gGeoManager->GetMedium("ITS_UpgradeCu");
251   TGeoMedium *be   =gGeoManager->GetMedium("ITS_UpgradeBe");
252   for(Int_t ivol=0;ivol<fNlayers;ivol++){
253     TGeoVolume *layer=gGeoManager->MakeTube(Form("LayerSilicon%i",ivol),si   ,    fRadii.At(ivol)   ,   fRadii.At(ivol)+fWidths.At(ivol)  ,   fHalfLength.At(ivol)); //upgraded situation 
254     
255     TGeoVolume *layerCu=gGeoManager->MakeTube(Form("LayerCu%i",ivol),cu   ,    fRadiiCu.At(ivol)   ,   fRadiiCu.At(ivol)+fWidthsCu.At(ivol) ,  fHalfLength.At(ivol)  ); //upgraded situation 
256     
257
258     vol ->AddNode(layer,ivol);
259     if(fCopper.At(ivol)){
260       vol->AddNode(layerCu,ivol);
261     }
262   }
263   TGeoVolume *beampipe=gGeoManager->MakeTube("BeamPipe", be   ,    fRadiusBP   ,  fRadiusBP+ fWidthBP ,  fHalfLengthBP  ); //upgraded situation
264
265   if(fBeampipe) vol->AddNode(beampipe,0);
266   return vol; 
267   
268 }
269 //_________________________________________________________________________________________________
270 void AliITSupgrade::SetFullSegmentation(TArrayD xsize,TArrayD zsize){
271   TFile *file= TFile::Open("Segmentation.root","recreate");
272   file->WriteObjectAny(&xsize,"TArrayD","CellSizeX");
273   file->WriteObjectAny(&zsize,"TArrayD","CellSizeZ");    
274   file->Close();
275 }
276 //_________________________________________________________________________________________________
277 void AliITSupgrade::StepHistory()
278 {
279   // This methode is invoked from StepManager() in order to print out
280   static Int_t iStepN;
281   const char *sParticle;
282   switch(gMC->TrackPid()){
283   case kProton:      sParticle="PROTON"    ;break;
284   case kNeutron:     sParticle="neutron"   ;break;
285   case kGamma:       sParticle="gamma"     ;break;
286   case kPi0:         sParticle="Pi0"       ;break;
287   case kPiPlus:      sParticle="Pi+"       ;break;
288   case kPiMinus:     sParticle="Pi-"       ;break;
289   case kElectron:    sParticle="electron"  ;break;
290   default:           sParticle="not known" ;break;
291   }
292
293   TString flag="fanny combination";
294   if(gMC->IsTrackAlive())
295     if(gMC->IsTrackEntering())      flag="enters to";
296     else if(gMC->IsTrackExiting())  flag="exits from";
297     else if(gMC->IsTrackInside())   flag="inside";
298     else if(gMC->IsTrackStop())     flag="stopped in";
299   
300   Int_t vid=0,copy=0;
301   TString path=gMC->CurrentVolName(); path.Prepend("-");path.Prepend(gMC->CurrentVolOffName(1));//current volume and his mother are always there
302   vid=gMC->CurrentVolOffID(2,copy);  if(vid) {path.Prepend("-");path.Prepend(gMC->VolName(vid));}
303   vid=gMC->CurrentVolOffID(3,copy);  if(vid) {path.Prepend("-");path.Prepend(gMC->VolName(vid));}
304
305   AliDebug(10, Form("Step %i: %s (%i) %s %s m=%.6f GeV q=%.1f dEdX=%.4f Etot=%.4f",iStepN,sParticle,gMC->TrackPid(),flag.Data(),path.Data(),gMC->TrackMass(),gMC->TrackCharge(),gMC->Edep()*1e9,gMC->Etot()));
306
307   Double_t gMcTrackPos[3]; gMC->TrackPosition(gMcTrackPos[0],gMcTrackPos[1],gMcTrackPos[2]);
308   Double_t  gMcTrackPosLoc[3]; gMC->Gmtod(gMcTrackPos,gMcTrackPosLoc,1);
309   AliDebug(10,Form("gMC Track Position (MARS) x: %5.3lf, y: %5.3lf, z: %5.3lf (r: %5.3lf) ---> (LOC) x: %5.3f, y: %5.3f, z: %5.3f",gMcTrackPos[0],gMcTrackPos[1],gMcTrackPos[2],TMath::Sqrt(gMcTrackPos[0]*gMcTrackPos[0]+gMcTrackPos[1]*gMcTrackPos[1]+gMcTrackPos[2]*gMcTrackPos[2]),gMcTrackPosLoc[0],gMcTrackPosLoc[1],gMcTrackPosLoc[2]));
310
311
312   AliDebug(10,Form("Step %i: tid=%i flags alive=%i disap=%i enter=%i exit=%i inside=%i out=%i stop=%i new=%i",
313                    iStepN, gAlice->GetMCApp()->GetCurrentTrackNumber(),
314                    gMC->IsTrackAlive(), gMC->IsTrackDisappeared(),gMC->IsTrackEntering(), gMC->IsTrackExiting(),
315                    gMC->IsTrackInside(),gMC->IsTrackOut(),        gMC->IsTrackStop(),     gMC->IsNewTrack()));
316
317   Float_t a,z,den,rad,abs; a=z=den=rad=abs=-1;
318   Int_t mid=gMC->CurrentMaterial(a,z,den,rad,abs);
319   AliDebug(10, Form("Step %i: mid=%i a=%7.2f z=%7.2f den=%9.4f rad=%9.2f abs=%9.2f\n\n",iStepN,mid,a,z,den,rad,abs));
320
321   TArrayI proc;  gMC->StepProcesses(proc);
322   AliDebug(1,"Processes in this step:");
323   for ( int i = 0 ; i < proc.GetSize(); i++)
324     {
325       AliDebug(10, Form("%s",TMCProcessName[proc.At(i)]));
326     }
327   AliDebug(1,"End process list");
328   iStepN++;
329 }//StepHistory()
330 //______________________________________________________
331 void AliITSupgrade::Hits2SDigits(){
332   
333   // Interface method ivoked from AliSimulation to create a list of sdigits corresponding to list of hits. Every hit generates one or more sdigits.
334   // Arguments: none
335   //   Returns: none
336   AliDebug(1,"Start Hits2SDigits.");
337   
338   if(!fSegmentation)fSegmentation=new AliITSsegmentationUpgrade();
339  
340   if(!GetLoader()->TreeH()) {GetLoader()->LoadHits();}
341   if(!GetLoader()->TreeS())
342
343     for(Int_t iEvt=0;iEvt < GetLoader()->GetRunLoader()->GetNumberOfEvents();iEvt++){//events loop
344       GetLoader()->GetRunLoader()->GetEvent(iEvt);                          //get next event
345       {
346         GetLoader()->MakeTree("S");
347         MakeBranch("S");
348       }
349       SetTreeAddress();
350       Int_t nSdigit[10]; for(Int_t i=0;i<10;i++)  nSdigit[i] =0; 
351       for(Int_t iEnt=0;iEnt<GetLoader()->TreeH()->GetEntries();iEnt++){//prims loop
352         GetLoader()->TreeH()->GetEntry(iEnt);     
353         Hit2SumDig(Hits(),SDigitsList(),nSdigit);//convert this hit to list of sdigits  
354       }//prims loop
355       GetLoader()->TreeS()->Fill();
356       GetLoader()->WriteSDigits("OVERWRITE");
357       SDigitsReset();
358     }//events loop
359   GetLoader()->UnloadHits();
360   GetLoader()->UnloadSDigits();
361   AliDebug(1,"Stop Hits2SDigits.");
362   
363 }
364 //____________________________
365 void AliITSupgrade::Hit2SumDig(TClonesArray *hits,TObjArray *pSDig, Int_t *nSdigit)
366 {
367   // Adds  sdigits of this hit to the list
368   //   Returns: none
369   
370   AliDebug(1,"Start Hit2SumDig");
371   
372   
373   if(!fSegmentation){    
374     AliDebug(1,"Segmentation Not inizialized!!");
375     return ;
376   }
377   
378   TClonesArray *pSdigList[10]; 
379   
380   for(Int_t i=0;i<fNlayers;i++){ 
381     pSdigList[i]=(TClonesArray*)(*pSDig)[i];
382     if(pSdigList[i]->GetEntries()!=0) AliErrorClass("Some of sdigits lists is not empty");         //in principle those lists should be empty 
383   }
384   
385   for(Int_t iHit=0;iHit<hits->GetEntries();iHit++){         //hits loop
386     AliITShit *hit = (AliITShit*)hits->At(iHit);
387     Double_t xz[2];
388     if(!fSegmentation->GlobalToDet(hit->GetModule(),hit->GetXG(),hit->GetYG(),hit->GetZG(),xz[0],xz[1])) continue;
389     AliITSDigitUpgrade digit;
390     digit.SetSignal(hit->GetIonization());
391     digit.SetNelectrons(hit->GetIonization()/(3.62*1e-09));
392     digit.SetLayer(hit->GetModule()); 
393     digit.SetTrackID(hit->GetTrack()); 
394     
395     Int_t xpix = 999;
396     if(fSegmentation->GetCellSizeX(hit->GetModule())!=0) xpix =(Int_t) (xz[0]/ fSegmentation->GetCellSizeX(hit->GetModule()));
397     
398     Int_t zpix = 999; // shift at the next line to have zpix always positive. Such numbers are used to build the Pixel Id in the layer (> 0!) 
399     if(fSegmentation->GetCellSizeZ(hit->GetModule())!=0){
400       zpix =(Int_t)((xz[1]+fHalfLength.At(hit->GetModule())) / fSegmentation->GetCellSizeZ(hit->GetModule())); 
401     }
402     digit.SetPixId(xpix,zpix);
403
404     
405     new((*pSdigList[hit->GetModule()])[nSdigit[hit->GetModule()]++]) AliITSDigitUpgrade(digit);
406   }
407   
408   AliDebug(1,"Stop Hit2SumDig.");
409 }
410 //_______________________________________________________________________________________________
411 void AliITSupgrade::MakeBranch(Option_t *option){
412   //Create Tree branches 
413   AliDebug(1,Form("Start with option= %s.",option));
414   
415   const Int_t kBufSize = 4000;
416   
417   const char *cH = strstr(option,"H");
418   const char *cD = strstr(option,"D");
419   //const char *cR = strstr(option,"R");
420   const char *cS = strstr(option,"S");
421
422   if(cH&&fLoader->TreeH()){
423
424     HitCreate();
425     MakeBranchInTree(fLoader->TreeH(),"ITSupgrade",&fHits,kBufSize,0);   
426   }
427
428     
429   if(cS&&fLoader->TreeS()){
430     SDigitsCreate();
431     for(Int_t i=0;i<fNlayers;i++) MakeBranchInTree(fLoader->TreeS(),Form("Layer%d",i),&((*fSdigits)[i]),kBufSize,0);
432   }
433
434
435   if(cD&&fLoader->TreeD()){
436     DigitsCreate();
437     for(Int_t i=0;i<fNlayers;i++) MakeBranchInTree(fLoader->TreeD(),Form("Layer%d",i),&((*fDigits)[i]),kBufSize,0);
438   }
439   AliDebug(1,"Stop.");
440 }
441 //____________________________________________________________________________________________________ 
442 void AliITSupgrade::SetTreeAddress()
443 {
444   //Set branch address for the Hits and Digits Tree.
445   AliDebug(1,"Start.");
446   if(fLoader->TreeH() && fLoader->TreeH()->GetBranch("ITSupgrade" )){
447     HitCreate();
448     fLoader->TreeH()->SetBranchAddress("ITSupgrade",&fHits);
449
450   }
451     
452   if(fLoader->TreeS() && fLoader->TreeS()->GetBranch("Layer0" )){
453     SDigitsCreate();
454     for(int i=0;i<fNlayers;i++){
455       fLoader->TreeS()->SetBranchAddress(Form("Layer%d",i),&((*fSdigits)[i]));
456     }
457   }
458     
459   if(fLoader->TreeD() && fLoader->TreeD()->GetBranch("Layer0")){
460     DigitsCreate(); 
461     for(int i=0;i<fNlayers;i++){
462       fLoader->TreeD()->SetBranchAddress(Form("Layer%d",i),&((*fDigits)[i]));
463     }
464   }
465   AliDebug(1,"Stop.");
466 }