Correct initialization of optical properties needed by Geant4 (Andrei)
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDv1.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 #include "AliHMPIDv1.h"     //class header
18 #include "AliHMPIDParam.h"  //CreateMaterials()
19 #include "AliHMPIDHit.h"    //Hits2SDigs(),StepManager()
20 #include "AliHMPIDDigit.h"  //CreateMaterials()
21 #include "AliRawReader.h"  //Raw2SDigits()
22 #include <TParticle.h>     //Hits2SDigits()
23 #include <TRandom.h> 
24 #include <TVirtualMC.h>    //StepManager() for gMC
25 #include <TPDGCode.h>      //StepHistory() 
26 #include <AliStack.h>      //StepManager(),Hits2SDigits()
27 #include <AliLoader.h>        //Hits2SDigits()
28 #include <AliRunLoader.h>     //Hits2SDigits()
29 #include <AliConst.h>
30 #include <AliPDG.h>
31 #include <AliMC.h>            //StepManager()      
32 #include <AliRawDataHeader.h> //Digits2Raw()
33 #include <AliDAQ.h>           //Digits2Raw()
34 #include <AliRun.h>           //CreateMaterials()    
35 #include <AliMagF.h>          //CreateMaterials()
36 #include <TGeoManager.h>      //CreateGeometry()
37 #include <TMultiGraph.h>      //Optics() 
38 #include <TGraph.h>           //Optics() 
39 #include <TLegend.h>          //Optics() 
40 #include <TCanvas.h>          //Optics() 
41 #include <TF2.h>              //CreateMaterials()
42 #include <AliCDBManager.h>    //CreateMaterials()
43 #include <AliCDBEntry.h>      //CreateMaterials()
44  
45 ClassImp(AliHMPIDv1)    
46 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
47 void AliHMPIDv1::AddAlignableVolumes()const
48 {
49 // Associates the symbolic volume name with the corresponding volume path. Interface methode from AliModule ivoked from AliMC
50 // Arguments: none
51 //   Returns: none   
52   for(Int_t i=0;i<7;i++)
53     gGeoManager->SetAlignableEntry(Form("/HMPID/Chamber%i",i),Form("ALIC_1/HMPID_%i",i));
54 }
55 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56 void AliHMPIDv1::CreateMaterials()
57 {
58 // Definition of available HMPID materials  
59 // Arguments: none
60 //   Returns: none    
61   AliDebug(1,"Start v1 HMPID.");
62     
63 //data from PDG booklet 2002     density [gr/cm^3] rad len [cm] abs len [cm]    
64   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 0.9999999
65   Float_t aC6F14[2]={ 12.01 , 18.99} , zC6F14[2]={ 6 , 9}   , wC6F14[2]={6 , 14} , dC6F14=1.68    ; Int_t nC6F14=-2;
66   Float_t  aSiO2[2]={ 28.09 , 15.99} ,  zSiO2[2]={14 , 8}   ,  wSiO2[2]={1 ,  2} ,  dSiO2=2.64    ; Int_t  nSiO2=-2; 
67   Float_t   aCH4[2]={ 12.01 ,  1.01} ,   zCH4[2]={ 6 , 1}   ,   wCH4[2]={1 ,  4} ,   dCH4=7.17e-4 ; Int_t   nCH4=-2; 
68   Float_t   aCsI[2]={132.90 ,126.90} ,   zCsI[2]={55 ,53}   ,   wCsI[2]={1 ,  1} ,   dCsI=0.1     ; Int_t   nCsI=-2; 
69   Float_t     aRoha= 12.01 ,               zRoha=  6 ,                              dRoha=  0.10 , radRoha= 18.80 , absRoha=  86.3/dRoha; //special material- quazi carbon
70   Float_t       aCu= 63.55 ,                 zCu= 29 ,                                dCu=  8.96 ,   radCu=  1.43 ,   absCu= 134.9/dCu  ;
71   Float_t        aW=183.84 ,                  zW= 74 ,                                 dW= 19.30 ,    radW=  0.35 ,    absW= 185.0/dW   ;
72   Float_t       aAl= 26.98 ,                 zAl= 13 ,                                dAl=  2.70 ,   radAl=  8.90 ,   absAl= 106.4/dAl  ;
73   
74     Int_t   matId=0;                           //tmp material id number
75     Int_t   unsens =  0, sens=1;               //sensitive or unsensitive medium
76     Int_t   itgfld = gAlice->Field()->Integ(); //type of field intergration 0 no field -1 user in guswim 1 Runge Kutta 2 helix 3 const field along z
77     Float_t maxfld = gAlice->Field()->Max();   //max field value
78     Float_t tmaxfd = -10.0;                    //max deflection angle due to magnetic field in one step
79     Float_t deemax = - 0.2;                    //max fractional energy loss in one step   
80     Float_t stemax = - 0.1;                    //mas step allowed [cm]
81     Float_t epsil  =   0.001;                  //abs tracking precision [cm]   
82     Float_t stmin  = - 0.001;                  //min step size [cm] in continius process transport, negative value: choose it automatically
83     AliMixture(++matId,"Air"  ,aAir  ,zAir  ,dAir  ,nAir  ,wAir  ); AliMedium(kAir  ,"Air"  ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
84     AliMixture(++matId,"C6F14",aC6F14,zC6F14,dC6F14,nC6F14,wC6F14); AliMedium(kC6F14,"C6F14",matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);      
85     AliMixture(++matId,"SiO2" ,aSiO2 ,zSiO2 ,dSiO2 ,nSiO2 ,wSiO2 ); AliMedium(kSiO2 ,"SiO2" ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);    
86     AliMixture(++matId,"CH4"  ,aCH4  ,zCH4  ,dCH4  ,nCH4  ,wCH4  ); AliMedium(kCH4  ,"CH4"  ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);  
87     AliMixture(++matId,"CsI"  ,aCsI  ,zCsI  ,dCsI  ,nCsI  ,wCsI  ); AliMedium(kCsI  ,"CsI"  ,matId,   sens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);//sensitive
88   
89     AliMaterial(++matId,"Roha",aRoha,zRoha,dRoha,radRoha,absRoha);  AliMedium(kRoha,"Roha", matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
90     AliMaterial(++matId,"Cu"  ,aCu  ,zCu  ,dCu  ,radCu  ,absCu  );  AliMedium(kCu  ,"Cu"  , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
91     AliMaterial(++matId,"W"   ,aW   ,zW   ,dW   ,radW   ,absW   );  AliMedium(kW   ,"W"   , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
92     AliMaterial(++matId,"Al"  ,aAl  ,zAl  ,dAl  ,radAl  ,absAl  );  AliMedium(kAl  ,"Al"  , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
93     
94     AliDebug(1,"Stop v1 HMPID.");
95
96   TString ttl=GetTitle(); if(!ttl.Contains("ShowOptics")) return;              //user didn't aks to plot optical curves
97   
98   const Double_t kWidth=0.25,kHeight=0.2;  
99   const Int_t kRadM=24 , kRadC=kRed;  
100   const Int_t kWinM=26 , kWinC=kBlue;  
101   const Int_t kGapM=25 , kGapC=kGreen;  
102   const Int_t kPcM = 2 , kPcC =kMagenta;  
103   const Int_t kNbins=30;       //number of photon energy points
104
105   Float_t aEckov [kNbins]; 
106   Float_t aAbsRad[kNbins], aAbsWin[kNbins], aAbsGap[kNbins];
107   Float_t aIdxRad[kNbins], aIdxWin[kNbins], aIdxGap[kNbins]; 
108   Float_t aQePc [kNbins];  
109   Float_t aTraRad[kNbins],aTraWin[kNbins],aTraGap[kNbins],aTraTot[kNbins];
110   for(Int_t i=0;i<kNbins;i++){//calculate probability for photon to survive during transversing a volume of material with absorption length  
111     aTraRad[i]=TMath::Exp(-AliHMPIDDigit::SizeRad()/ (aAbsRad[i]+0.0001)); //radiator
112     aTraWin[i]=TMath::Exp(-AliHMPIDDigit::SizeWin()/ (aAbsWin[i] +0.0001)); //window
113     aTraGap[i]=TMath::Exp(-AliHMPIDDigit::SizeGap()/ (aAbsGap[i]  +0.0001)); //from window to PC   
114     aTraTot[i]=aTraRad[i]*aTraWin[i]*aTraGap[i]*aQePc[i];
115   }
116   
117   TGraph *pRaAG=new TGraph(kNbins,aEckov,aAbsRad);pRaAG->SetMarkerStyle(kRadM);pRaAG->SetMarkerColor(kRadC);
118   TGraph *pRaIG=new TGraph(kNbins,aEckov,aIdxRad);pRaIG->SetMarkerStyle(kRadM);pRaIG->SetMarkerColor(kRadC);
119   TGraph *pRaTG=new TGraph(kNbins,aEckov,aTraRad);pRaTG->SetMarkerStyle(kRadM);pRaTG->SetMarkerColor(kRadC);  
120   
121   TGraph *pWiAG=new TGraph(kNbins,aEckov,aAbsWin);pWiAG->SetMarkerStyle(kWinM);pWiAG->SetMarkerColor(kWinC);
122   TGraph *pWiIG=new TGraph(kNbins,aEckov,aIdxWin);pWiIG->SetMarkerStyle(kWinM);pWiIG->SetMarkerColor(kWinC);  
123   TGraph *pWiTG=new TGraph(kNbins,aEckov,aTraWin);pWiTG->SetMarkerStyle(kWinM);pWiTG->SetMarkerColor(kWinC);  
124   
125   TGraph *pGaAG=new TGraph(kNbins,aEckov,aAbsGap);pGaAG->SetMarkerStyle(kGapM);pGaAG->SetMarkerColor(kGapC);
126   TGraph *pGaIG=new TGraph(kNbins,aEckov,aIdxGap);pGaIG->SetMarkerStyle(kGapM);pGaIG->SetMarkerColor(kGapC);
127   TGraph *pGaTG=new TGraph(kNbins,aEckov,aTraGap);pGaTG->SetMarkerStyle(kGapM);pGaTG->SetMarkerColor(kGapC);   
128   
129   TGraph *pQeG =new TGraph(kNbins,aEckov,aQePc);  pQeG  ->SetMarkerStyle(kPcM );pQeG->SetMarkerColor(kPcC);
130   TGraph *pToG =new TGraph(kNbins,aEckov,aTraTot);pToG  ->SetMarkerStyle(30)   ;pToG->SetMarkerColor(kYellow);  
131   
132   TMultiGraph *pIdxMG=new TMultiGraph("idx","Ref index;E_{#check{C}} [GeV]");       
133   TMultiGraph *pAbsMG=new TMultiGraph("abs","Absorption [cm];E_{#check{C}} [GeV]"); 
134   TMultiGraph *pTraMG=new TMultiGraph("tra","Transmission;E_{#check{C}} [GeV]");    TLegend *pTraLe=new TLegend(0.2,0.4,0.2+kWidth,0.4+kHeight);
135   pAbsMG->Add(pRaAG);  pIdxMG->Add(pRaIG);     pTraMG->Add(pRaTG);     pTraLe->AddEntry(pRaTG, "Rad", "p");           
136   pAbsMG->Add(pWiAG);  pIdxMG->Add(pWiIG);     pTraMG->Add(pWiTG);     pTraLe->AddEntry(pWiTG, "Win", "p");               
137   pAbsMG->Add(pGaAG);  pIdxMG->Add(pGaIG);     pTraMG->Add(pGaTG);     pTraLe->AddEntry(pGaTG, "Gap", "p");               
138                                                pTraMG->Add(pToG);      pTraLe->AddEntry(pToG,  "Tot", "p");          
139                                                pTraMG->Add(pQeG);      pTraLe->AddEntry(pQeG,   "QE" , "p");  
140   TCanvas *pC=new TCanvas("c1","HMPID optics to check",1100,900);  pC->Divide(2,2);           
141   pC->cd(1);                    pIdxMG->Draw("AP"); 
142   pC->cd(2);  gPad->SetLogy();  pAbsMG->Draw("AP"); 
143   pC->cd(3);                    pTraLe->Draw();    
144   pC->cd(4);                    pTraMG->Draw("AP");
145 }//void AliHMPID::CreateMaterials()
146 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
147 void AliHMPIDv1::CreateGeometry()
148 {
149 //Creates detailed geometry simulation (currently GEANT volumes tree)         
150   AliDebug(1,"Start main.");
151   if(!gMC->IsRootGeometrySupported()) return;     
152   
153   Double_t cm=1,mm=0.1*cm,mkm=0.001*mm,dx,dy,dz;//default is cm
154   
155   TGeoVolume *pRich=gGeoManager->MakeBox("HMPID",gGeoManager->GetMedium("HMPID_CH4"),dx=(6*mm+1681*mm+6*mm)/2,  //main HMPID volume
156                                                                                    dy=(6*mm+1466*mm+6*mm)/2,
157                                                                                    dz=(80*mm+40*mm)*2/2);     //x,y taken from 2033P1  z from p84 TDR  
158   const Double_t kAngHor=19.5; //  horizontal angle between chambers  19.5 grad
159   const Double_t kAngVer=20;   //  vertical angle between chambers    20   grad     
160   const Double_t kAngCom=30;   //  common HMPID rotation with respect to x axis  30   grad     
161   const Double_t trans[3]={490,0,0}; //center of the chamber is on window-gap surface
162   for(Int_t iCh=0;iCh<7;iCh++){//place 7 chambers
163     TGeoHMatrix *pMatrix=new TGeoHMatrix;
164     pMatrix->RotateY(90);           //rotate around y since initial position is in XY plane -> now in YZ plane
165     pMatrix->SetTranslation(trans); //now plane in YZ is shifted along x 
166     switch(iCh){
167       case 0:                pMatrix->RotateY(kAngHor);  pMatrix->RotateZ(-kAngVer);  break; //right and down 
168       case 1:                                            pMatrix->RotateZ(-kAngVer);  break; //down              
169       case 2:                pMatrix->RotateY(kAngHor);                               break; //right 
170       case 3:                                                                         break; //no rotation
171       case 4:                pMatrix->RotateY(-kAngHor);                              break; //left   
172       case 5:                                            pMatrix->RotateZ(kAngVer);   break; //up
173       case 6:                pMatrix->RotateY(-kAngHor); pMatrix->RotateZ(kAngVer);   break; //left and up 
174     }
175     pMatrix->RotateZ(kAngCom);     //apply common rotation  in XY plane    
176     gGeoManager->GetVolume("ALIC")->AddNode(pRich,iCh,pMatrix);
177   }
178
179   Float_t par[3];
180   Int_t matrixIdReturn=0; //matrix id returned by AliMatrix
181 //Pad Panel frame  6 sectors
182   par[0]=648*mm/2;par[1]=  411*mm/2;par[2]=40  *mm/2;gMC->Gsvolu("Rppf"     ,"BOX ",(*fIdtmed)[kAl]  ,par,3);//PPF 2001P2 inner size of the slab by 1mm more
183   par[0]=181*mm/2;par[1]=89.25*mm/2;par[2]=38.3*mm/2;gMC->Gsvolu("RppfLarge","BOX ",(*fIdtmed)[kAir] ,par,3);//large whole
184   par[0]=114*mm/2;par[1]=89.25*mm/2;par[2]=38.3*mm/2;gMC->Gsvolu("RppfSmall","BOX ",(*fIdtmed)[kAir] ,par,3);//small whole
185   par[0]=644*mm/2;par[1]=  407*mm/2;par[2]= 1.7*mm/2;gMC->Gsvolu("Rpc"      ,"BOX ",(*fIdtmed)[kCsI] ,par,3);//by 0.2 mm more then actual size (PCB 2006P1)
186   
187   gMC->Gspos("Rppf",0,"HMPID",    -335*mm,      -433*mm,  8*cm+20*mm,  0,"ONLY");//F1 2040P1 z p.84 TDR
188   gMC->Gspos("Rppf",1,"HMPID",    +335*mm,      -433*mm,  8*cm+20*mm,  0,"ONLY");
189   gMC->Gspos("Rppf",2,"HMPID",    -335*mm,         0*mm,  8*cm+20*mm,  0,"ONLY");
190   gMC->Gspos("Rppf",3,"HMPID",    +335*mm,         0*mm,  8*cm+20*mm,  0,"ONLY");
191   gMC->Gspos("Rppf",4,"HMPID",    -335*mm,      +433*mm,  8*cm+20*mm,  0,"ONLY");
192   gMC->Gspos("Rppf",5,"HMPID",    +335*mm,      +433*mm,  8*cm+20*mm,  0,"ONLY");  
193     gMC->Gspos("Rpc"      ,1,"Rppf",       0*mm,         0*mm,   -19.15*mm,  0,"ONLY");//PPF 2001P2 
194     gMC->Gspos("RppfLarge",1,"Rppf",  -224.5*mm,  -151.875*mm,     0.85*mm,  0,"ONLY");
195     gMC->Gspos("RppfLarge",2,"Rppf",  -224.5*mm,  - 50.625*mm,     0.85*mm,  0,"ONLY");
196     gMC->Gspos("RppfLarge",3,"Rppf",  -224.5*mm,  + 50.625*mm,     0.85*mm,  0,"ONLY");
197     gMC->Gspos("RppfLarge",4,"Rppf",  -224.5*mm,  +151.875*mm,     0.85*mm,  0,"ONLY");
198     gMC->Gspos("RppfSmall",1,"Rppf",  - 65.0*mm,  -151.875*mm,     0.85*mm,  0,"ONLY");
199     gMC->Gspos("RppfSmall",2,"Rppf",  - 65.0*mm,  - 50.625*mm,     0.85*mm,  0,"ONLY");
200     gMC->Gspos("RppfSmall",3,"Rppf",  - 65.0*mm,  + 50.625*mm,     0.85*mm,  0,"ONLY");
201     gMC->Gspos("RppfSmall",4,"Rppf",  - 65.0*mm,  +151.875*mm,     0.85*mm,  0,"ONLY");
202     gMC->Gspos("RppfSmall",5,"Rppf",  + 65.0*mm,  -151.875*mm,     0.85*mm,  0,"ONLY");
203     gMC->Gspos("RppfSmall",6,"Rppf",  + 65.0*mm,  - 50.625*mm,     0.85*mm,  0,"ONLY");
204     gMC->Gspos("RppfSmall",7,"Rppf",  + 65.0*mm,  + 50.625*mm,     0.85*mm,  0,"ONLY");
205     gMC->Gspos("RppfSmall",8,"Rppf",  + 65.0*mm,  +151.875*mm,     0.85*mm,  0,"ONLY"); 
206     gMC->Gspos("RppfLarge",5,"Rppf",  +224.5*mm,  -151.875*mm,     0.85*mm,  0,"ONLY");
207     gMC->Gspos("RppfLarge",6,"Rppf",  +224.5*mm,  - 50.625*mm,     0.85*mm,  0,"ONLY");
208     gMC->Gspos("RppfLarge",7,"Rppf",  +224.5*mm,  + 50.625*mm,     0.85*mm,  0,"ONLY");
209     gMC->Gspos("RppfLarge",8,"Rppf",  +224.5*mm,  +151.875*mm,     0.85*mm,  0,"ONLY");
210 //Gap - anod wires 6 copies to HMPID
211   par[0]=648*mm/2;par[1]=  411*mm/2 ;par[2]=4.45*mm/2;gMC->Gsvolu("Rgap","BOX ",(*fIdtmed)[kCH4] ,par,3);//xy as PPF 2001P2 z WP 2099P1
212   par[0]=  0*mm  ;par[1]=  20*mkm/2 ;par[2]= 648*mm/2;gMC->Gsvolu("Rano","TUBE",(*fIdtmed)[kW]   ,par,3);//WP 2099P1 z = gap x PPF 2001P2
213   AliMatrix(matrixIdReturn,180,0, 90,90, 90,0); //wires along x
214   
215   gMC->Gspos("Rgap",0,"HMPID",    -335*mm,      -433*mm,8*cm-2.225*mm, 0,"ONLY"); //F1 2040P1 z WP 2099P1
216   gMC->Gspos("Rgap",1,"HMPID",    +335*mm,      -433*mm,8*cm-2.225*mm, 0,"ONLY"); 
217   gMC->Gspos("Rgap",2,"HMPID",    -335*mm,         0*mm,8*cm-2.225*mm, 0,"ONLY"); 
218   gMC->Gspos("Rgap",3,"HMPID",    +335*mm,         0*mm,8*cm-2.225*mm, 0,"ONLY"); 
219   gMC->Gspos("Rgap",4,"HMPID",    -335*mm,      +433*mm,8*cm-2.225*mm, 0,"ONLY"); 
220   gMC->Gspos("Rgap",5,"HMPID",    +335*mm,      +433*mm,8*cm-2.225*mm, 0,"ONLY"); 
221   for(int i=1;i<=96;i++)
222     gMC->Gspos("Rano",i,"Rgap",     0*mm, -411/2*mm+i*4*mm, 0.185*mm, matrixIdReturn,"ONLY"); //WP 2099P1  
223 //Defines radiators geometry  
224   par[0]=1330*mm/2 ;par[1]= 413*mm/2  ;par[2]=  24*mm/2;  gMC->Gsvolu("Rrad"      ,"BOX ",(*fIdtmed)[kC6F14]     ,par,3); // Rad 2011P1
225   par[0]=1330*mm/2 ;par[1]= 413*mm/2  ;par[2]=   4*mm/2;  gMC->Gsvolu("RradFront" ,"BOX ",(*fIdtmed)[kRoha]      ,par,3); //front 
226   par[0]=1330*mm/2 ;par[1]= 413*mm/2  ;par[2]=   5*mm/2;  gMC->Gsvolu("RradWin"   ,"BOX ",(*fIdtmed)[kSiO2]      ,par,3); //window
227   par[0]=1330*mm/2 ;par[1]=   5*mm/2  ;par[2]=  15*mm/2;  gMC->Gsvolu("RradLong"  ,"BOX ",(*fIdtmed)[kRoha]      ,par,3); //long side  
228   par[0]=  10*mm/2 ;par[1]= 403*mm/2  ;par[2]=  15*mm/2;  gMC->Gsvolu("RradShort" ,"BOX ",(*fIdtmed)[kRoha]      ,par,3); //short side 
229   par[0]=   0      ;par[1]=  10*mm/2  ;par[2]=  15*mm/2;  gMC->Gsvolu("RradSpacer","TUBE",(*fIdtmed)[kSiO2]      ,par,3); //spacer        
230     
231   gMC->Gspos("Rrad",1,"HMPID",   0*mm,-434*mm,   -12*mm,  0,"ONLY"); //3 radiators to HMPID
232   gMC->Gspos("Rrad",2,"HMPID",   0*mm,   0*mm,   -12*mm,  0,"ONLY"); 
233   gMC->Gspos("Rrad",3,"HMPID",   0*mm,+434*mm,   -12*mm,  0,"ONLY"); 
234     gMC->Gspos("RradFront",1,"Rrad",   0*mm,   0*mm, -10.0*mm,  0,"ONLY"); //front cover 
235     gMC->Gspos("RradWin"  ,1,"Rrad",   0*mm,   0*mm,   9.5*mm,  0,"ONLY"); //quartz window (back cover)
236     gMC->Gspos("RradLong" ,1,"Rrad",   0*mm,-204*mm,  -0.5*mm,  0,"ONLY"); //long side
237     gMC->Gspos("RradLong" ,2,"Rrad",   0*mm,+204*mm,  -0.5*mm,  0,"ONLY"); //long side
238     gMC->Gspos("RradShort",1,"Rrad",-660*mm,   0*mm,  -0.5*mm,  0,"ONLY"); //short side
239     gMC->Gspos("RradShort",2,"Rrad",+660*mm,   0*mm,  -0.5*mm,  0,"ONLY"); //short side 
240     for(int i=0;i<3;i++)
241       for(int j=0;j<10;j++)
242         gMC->Gspos("RradSpacer",10*i+j,"Rrad",-1330*mm/2+116*mm+j*122*mm,(i-1)*105*mm,-0.5*mm,0,"ONLY");//spacers
243 //Defines SandBox geometry
244   par[0]=1419*mm/2 ;par[1]=1378*mm/2;par[2]=50.5*mm/2; gMC->Gsvolu("Rsb"     ,"BOX ",(*fIdtmed)[kAir]  ,par,3);  //2072P1   
245   par[0]=1419*mm/2 ;par[1]=1378*mm/2;par[2]= 0.5*mm/2; gMC->Gsvolu("RsbCover","BOX ",(*fIdtmed)[kAl]   ,par,3);  //cover
246   par[0]=1359*mm/2 ;par[1]=1318*mm/2;par[2]=49.5*mm/2; gMC->Gsvolu("RsbComb" ,"BOX ",(*fIdtmed)[kRoha] ,par,3);  //honeycomb structure 
247   
248   gMC->Gspos("Rsb",1,"HMPID",   0*mm, 0*mm, -73.75*mm, 0,"ONLY"); //p.84 TDR sandbox to rich
249     gMC->Gspos("RsbComb" ,1,"Rsb", 0*mm, 0*mm,      0*mm, 0,"ONLY"); //2072P1 honeycomv to sandbox
250     gMC->Gspos("RsbCover",1,"Rsb", 0*mm, 0*mm,    +25*mm, 0,"ONLY"); //cover to sandbox
251     gMC->Gspos("RsbCover",2,"Rsb", 0*mm, 0*mm,    -25*mm, 0,"ONLY"); //cover to sandbox
252   AliDebug(1,"Stop v1. HMPID option");  
253 }//CreateGeometry()
254 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
255 void AliHMPIDv1::Init()
256 {
257 // This methode defines ID for sensitive volumes, i.e. such geometry volumes for which there are if(gMC->CurrentVolID()==XXX) statements in StepManager()
258 // Arguments: none
259 //   Returns: none      
260   AliDebug(1,"Start v1 HMPID.");    
261   fIdRad     = gMC->VolId("Rrad");
262   fIdWin     = gMC->VolId("RradWin");
263   fIdPc      = gMC->VolId("Rpc");
264   fIdAmpGap  = gMC->VolId("Rgap");
265   fIdProxGap = gMC->VolId("Rgap");
266
267   AliDebug(1,"Stop v1 HMPID.");    
268 }
269 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
270 void AliHMPIDv1::DefineOpticalProperties()
271 {
272 // Optical properties definition.
273   const Int_t kNbins=30;       //number of photon energy points
274   Float_t emin=5.5,emax=8.5;         //Photon energy range,[eV]
275   Float_t aEckov [kNbins]; 
276   Float_t aAbsRad[kNbins], aAbsWin[kNbins], aAbsGap[kNbins], aAbsMet[kNbins];
277   Float_t aIdxRad[kNbins], aIdxWin[kNbins], aIdxGap[kNbins], aIdxMet[kNbins], aIdxPc[kNbins]; 
278   Float_t                                                    aQeAll [kNbins], aQePc [kNbins];
279
280   TF2 *pRaIF=new TF2("RidxRad","sqrt(1+0.554*(1239.84/x)^2/((1239.84/x)^2-5796)-0.0005*(y-20))"                                       ,emin,emax,0,50); //DiMauro mail temp 0-50 degrees C
281   TF1 *pWiIF=new TF1("RidxWin","sqrt(1+46.411/(10.666*10.666-x*x)+228.71/(18.125*18.125-x*x))"                                        ,emin,emax);      //SiO2 idx TDR p.35
282   TF1 *pGaIF=new TF1("RidxGap","1+0.12489e-6/(2.62e-4 - x*x/1239.84/1239.84)"                                                         ,emin,emax);      //?????? from where  
283
284   TF1 *pRaAF=new TF1("RabsRad","(x<7.8)*(gaus+gaus(3))+(x>=7.8)*0.0001"                                                               ,emin,emax);  //fit from DiMauro data 28.10.03 
285   pRaAF->SetParameters(3.20491e16,-0.00917890,0.742402,3035.37,4.81171,0.626309);
286   TF1 *pWiAF=new TF1("RabsWin","(x<8.2)*(818.8638-301.0436*x+36.89642*x*x-1.507555*x*x*x)+(x>=8.2)*0.0001"                            ,emin,emax);  //fit from DiMauro data 28.10.03 
287   TF1 *pGaAF=new TF1("RabsGap","(x<7.75)*6512.399+(x>=7.75)*3.90743e-2/(-1.655279e-1+6.307392e-2*x-8.011441e-3*x*x+3.392126e-4*x*x*x)",emin,emax);  //????? from where  
288   
289   TF1 *pQeF =new TF1("Qe"    ,"0+(x>6.07267)*0.344811*(1-exp(-1.29730*(x-6.07267)))"                                                  ,emin,emax);  //fit from DiMauro data 28.10.03  
290                             
291   for(Int_t i=0;i<kNbins;i++){
292     Float_t eV=emin+0.1*i;  //Ckov energy in eV
293     aEckov [i] =1e-9*eV;    //Ckov energy in GeV
294     aAbsRad[i]=pRaAF->Eval(eV); aIdxRad[i]=1.292;//pRaIF->Eval(eV,20);      //Simulation for 20 degress C       
295     aAbsWin[i]=pWiAF->Eval(eV); aIdxWin[i]=1.5787;//pWiIF->Eval(eV);
296     aAbsGap[i]=pGaAF->Eval(eV); aIdxGap[i]=1.0005;//pGaIF->Eval(eV);    aQeAll[i] =1;                     //QE for all other materials except for PC must be 1.  
297     aAbsMet[i] =0.0001;                aIdxMet[i]=0;                                             //metal ref idx must be 0 in order to reflect photon
298                                        aIdxPc [i]=1;           aQePc [i]=pQeF->Eval(eV);         //PC ref idx must be 1 in order to apply photon to QE conversion 
299                                        
300   }
301   gMC->SetCerenkov((*fIdtmed)[kC6F14]    , kNbins, aEckov, aAbsRad  , aQeAll , aIdxRad );    
302   gMC->SetCerenkov((*fIdtmed)[kSiO2]     , kNbins, aEckov, aAbsWin  , aQeAll , aIdxWin );    
303   gMC->SetCerenkov((*fIdtmed)[kCH4]      , kNbins, aEckov, aAbsGap  , aQeAll , aIdxGap );    
304   gMC->SetCerenkov((*fIdtmed)[kCu]       , kNbins, aEckov, aAbsMet  , aQeAll , aIdxMet );    
305   gMC->SetCerenkov((*fIdtmed)[kW]        , kNbins, aEckov, aAbsMet  , aQeAll , aIdxMet ); //n=0 means reflect photons       
306   gMC->SetCerenkov((*fIdtmed)[kCsI]      , kNbins, aEckov, aAbsMet  , aQePc  , aIdxPc  ); //n=1 means convert photons    
307   gMC->SetCerenkov((*fIdtmed)[kAl]       , kNbins, aEckov, aAbsMet  , aQeAll , aIdxMet );    
308   delete pRaAF;delete pWiAF;delete pGaAF; delete pRaIF; delete pWiIF; delete pGaIF; delete pQeF;
309 }
310 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
311 Bool_t AliHMPIDv1::IsLostByFresnel()
312 {
313 // Calculate probability for the photon to be lost by Fresnel reflection.
314   TLorentzVector p4;
315   Double_t mom[3],localMom[3];
316   gMC->TrackMomentum(p4);   mom[0]=p4(1);   mom[1]=p4(2);   mom[2]=p4(3);
317   localMom[0]=0; localMom[1]=0; localMom[2]=0;
318   gMC->Gmtod(mom,localMom,2);
319   Double_t localTc    = localMom[0]*localMom[0]+localMom[2]*localMom[2];
320   Double_t localTheta = TMath::ATan2(TMath::Sqrt(localTc),localMom[1]);
321   Double_t cotheta = TMath::Abs(TMath::Cos(localTheta));
322   if(gMC->GetRandom()->Rndm() < Fresnel(p4.E()*1e9,cotheta,1)){
323     AliDebug(1,"Photon lost");
324     return kTRUE;
325   }else
326     return kFALSE;
327 }//IsLostByFresnel()
328 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
329 void AliHMPIDv1::GenFee(Int_t iCh,Float_t eloss)
330 {
331 // Generate FeedBack photons for the current particle. To be invoked from StepManager().
332 // eloss=0 means photon so only pulse height distribution is to be analysed. This one is done in AliHMPIDParam::TotQdc()
333   TLorentzVector x4;
334   gMC->TrackPosition(x4); 
335   Int_t iNphotons=gMC->GetRandom()->Poisson(0.02*200); eloss++; iCh++;   //??????????????????????
336   AliDebug(1,Form("N photons=%i",iNphotons));
337   Int_t j;
338   Float_t cthf, phif, enfp = 0, sthf, e1[3], e2[3], e3[3], vmod, uswop,dir[3], phi,pol[3], mom[4];
339 //Generate photons
340   for(Int_t i=0;i<iNphotons;i++){//feedbacks loop
341     Double_t ranf[2];
342     gMC->GetRandom()->RndmArray(2,ranf);    //Sample direction
343     cthf=ranf[0]*2-1.0;
344     if(cthf<0) continue;
345     sthf = TMath::Sqrt((1 - cthf) * (1 + cthf));
346     phif = ranf[1] * 2 * TMath::Pi();
347     
348     if(Double_t randomNumber=gMC->GetRandom()->Rndm()<=0.57)
349       enfp = 7.5e-9;
350     else if(randomNumber<=0.7)
351       enfp = 6.4e-9;
352     else
353       enfp = 7.9e-9;
354     
355
356     dir[0] = sthf * TMath::Sin(phif);    dir[1] = cthf;    dir[2] = sthf * TMath::Cos(phif);
357     gMC->Gdtom(dir, mom, 2);
358     mom[0]*=enfp;    mom[1]*=enfp;    mom[2]*=enfp;
359     mom[3] = TMath::Sqrt(mom[0]*mom[0]+mom[1]*mom[1]+mom[2]*mom[2]);
360     
361     // Polarisation
362     e1[0]=      0;    e1[1]=-dir[2];    e1[2]= dir[1];
363     e2[0]=-dir[1];    e2[1]= dir[0];    e2[2]=      0;
364     e3[0]= dir[1];    e3[1]=      0;    e3[2]=-dir[0];
365     
366     vmod=0;
367     for(j=0;j<3;j++) vmod+=e1[j]*e1[j];
368     if (!vmod) for(j=0;j<3;j++) {
369       uswop=e1[j];
370       e1[j]=e3[j];
371       e3[j]=uswop;
372     }
373     vmod=0;
374     for(j=0;j<3;j++) vmod+=e2[j]*e2[j];
375     if (!vmod) for(j=0;j<3;j++) {
376       uswop=e2[j];
377       e2[j]=e3[j];
378       e3[j]=uswop;
379     }
380     
381     vmod=0;  for(j=0;j<3;j++) vmod+=e1[j]*e1[j];  vmod=TMath::Sqrt(1/vmod);  for(j=0;j<3;j++) e1[j]*=vmod;    
382     vmod=0;  for(j=0;j<3;j++) vmod+=e2[j]*e2[j];  vmod=TMath::Sqrt(1/vmod);  for(j=0;j<3;j++) e2[j]*=vmod;
383     
384     phi = gMC->GetRandom()->Rndm()* 2 * TMath::Pi();
385     for(j=0;j<3;j++) pol[j]=e1[j]*TMath::Sin(phi)+e2[j]*TMath::Cos(phi);
386     gMC->Gdtom(pol, pol, 2);
387     Int_t outputNtracksStored;    
388     gAlice->GetMCApp()->PushTrack(1,                             //transport
389                      gAlice->GetMCApp()->GetCurrentTrackNumber(),//parent track 
390                      kFeedback,                                  //PID
391                      mom[0],mom[1],mom[2],mom[3],                //track momentum  
392                      x4.X(),x4.Y(),x4.Z(),x4.T(),                //track origin 
393                      pol[0],pol[1],pol[2],                       //polarization
394                      kPFeedBackPhoton,                           //process ID   
395                      outputNtracksStored,                        //on return how many new photons stored on stack
396                      1.0);                                       //weight
397   }//feedbacks loop
398   AliDebug(1,"Stop.");
399 }//GenerateFeedbacks()
400 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
401 void AliHMPIDv1::Hits2SDigits()
402 {
403 // Interface methode ivoked from AliSimulation to create a list of sdigits corresponding to list of hits. Every hit generates one or more sdigits.
404 // Arguments: none
405 //   Returns: none   
406   AliDebug(1,"Start.");
407   for(Int_t iEvt=0;iEvt < GetLoader()->GetRunLoader()->GetNumberOfEvents();iEvt++){                //events loop
408     GetLoader()->GetRunLoader()->GetEvent(iEvt);                          //get next event
409   
410     if(!GetLoader()->TreeH()) {GetLoader()->LoadHits();                    }
411     if(!GetLoader()->TreeS()) {GetLoader()->MakeTree("S"); MakeBranch("S");}//to
412           
413     for(Int_t iPrimN=0;iPrimN<GetLoader()->TreeH()->GetEntries();iPrimN++){//prims loop
414       GetLoader()->TreeH()->GetEntry(iPrimN);
415       Hit2Sdi(Hits(),SdiLst());
416     }//prims loop
417     GetLoader()->TreeS()->Fill();
418     GetLoader()->WriteSDigits("OVERWRITE");
419     SdiReset();
420   }//events loop  
421   GetLoader()->UnloadHits();
422   GetLoader()->UnloadSDigits();  
423   AliDebug(1,"Stop.");
424 }//Hits2SDigits()
425 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
426 void AliHMPIDv1::Hit2Sdi(TClonesArray *pHitLst,TClonesArray *pSdiLst)
427 {
428 // Converts list of hits to list of sdigits.  For each hit in a loop the following steps are done:
429 // - calcultion of the total charge induced by the hit
430 // - determination of the pad contaning the hit and shifting hit y position to the nearest anod wire y
431 // - defining a set of pads affected (up to 9 including the hitted pad)    
432 // - calculating charge induced to all those pads using integrated Mathieson distribution and creating sdigit  
433 // Arguments: pHitLst  - list of hits provided not empty
434 //            pSDigLst - list of sdigits where to store the results
435 //   Returns: none         
436   for(Int_t iHit=0;iHit<pHitLst->GetEntries();iHit++){         //hits loop
437     AliHMPIDHit *pHit=(AliHMPIDHit*)pHitLst->At(iHit);           //get pointer to current hit   
438     AliHMPIDDigit::Hit2Sdi(pHit,pSdiLst);                       //convert this hit to list of sdigits     
439   }//hits loop loop
440 }//Hits2SDigs() for TVector2
441 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
442 void AliHMPIDv1::Digits2Raw()
443 {
444 // Creates raw data files in DDL format. Invoked by AliSimulation where loop over events is done
445 // Arguments: none
446 //   Returns: none    
447   AliDebug(1,"Start.");
448   GetLoader()->LoadDigits();
449   TTree * treeD = GetLoader()->TreeD();
450   if(!treeD) {
451     AliError("No digits tree!");
452     return;
453   }
454   treeD->GetEntry(0);
455   
456   ofstream file[AliHMPIDDigit::kNddls];   //output streams
457   Int_t    cnt[AliHMPIDDigit::kNddls];        //data words counters for DDLs
458   AliRawDataHeader header; //empty DDL header
459   UInt_t w32=0;            //32 bits data word 
460   
461   for(Int_t i=0;i<AliHMPIDDigit::kNddls;i++){        
462     file[i].open(AliDAQ::DdlFileName(GetName(),i)); //open all 14 DDL in parallel
463     file[i].write((char*)&header,sizeof(header));   //write dummy header as place holder, actual will be written later when total size of DDL is known
464     cnt[i]=0;                                       //reset counters
465   }
466   
467   for(Int_t iCh=0;iCh<7;iCh++)
468     for(Int_t iDig=0;iDig<DigLst(iCh)->GetEntriesFast();iDig++){//digits loop for a given chamber
469       AliHMPIDDigit *pDig=(AliHMPIDDigit*)DigLst(iCh)->At(iDig);
470       Int_t ddl=pDig->Raw(w32);                             //ddl is 0..13 
471       file[ddl].write((char*)&w32,sizeof(w32));  cnt[ddl]++;//write formated digit to the propriate file (as decided in Dig2Raw) and increment corresponding counter
472     }//digits 
473     
474     
475   for(Int_t i=0;i<AliHMPIDDigit::kNddls;i++){
476     header.fSize=sizeof(header)+cnt[i]*sizeof(w32);                //now calculate total number of bytes for each DDL file  
477     header.SetAttribute(0); 
478     file[i].seekp(0); file[i].write((char*)&header,sizeof(header));//rewrite DDL header with fSize field properly set
479     file[i].close();                                               //close DDL file
480   }
481   GetLoader()->UnloadDigits();
482   AliDebug(1,"Stop.");      
483 }//Digits2Raw()
484 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
485 Float_t AliHMPIDv1::Fresnel(Float_t ene,Float_t pdoti, Bool_t pola)
486 {
487 // Correction for Fresnel   ???????????
488 // Arguments:   ene - photon energy [GeV],
489 //              PDOTI=COS(INC.ANG.), PDOTR=COS(POL.PLANE ROT.ANG.)
490 //   Returns:  
491     Float_t en[36] = {5.0,5.1,5.2,5.3,5.4,5.5,5.6,5.7,5.8,5.9,6.0,6.1,6.2,
492                       6.3,6.4,6.5,6.6,6.7,6.8,6.9,7.0,7.1,7.2,7.3,7.4,7.5,7.6,7.7,
493                       7.8,7.9,8.0,8.1,8.2,8.3,8.4,8.5};
494     Float_t csin[36] = {2.14,2.21,2.33,2.48,2.76,2.97,2.99,2.59,2.81,3.05,
495                         2.86,2.53,2.55,2.66,2.79,2.96,3.18,3.05,2.84,2.81,2.38,2.11,
496                         2.01,2.13,2.39,2.73,3.08,3.15,2.95,2.73,2.56,2.41,2.12,1.95,
497                         1.72,1.53};
498     Float_t csik[36] = {0.,0.,0.,0.,0.,0.196,0.408,0.208,0.118,0.49,0.784,0.543,
499                         0.424,0.404,0.371,0.514,0.922,1.102,1.139,1.376,1.461,1.253,0.878,
500                         0.69,0.612,0.649,0.824,1.347,1.571,1.678,1.763,1.857,1.824,1.824,
501                         1.714,1.498};
502     Float_t xe=ene;
503     Int_t  j=Int_t(xe*10)-49;
504     Float_t cn=csin[j]+((csin[j+1]-csin[j])/0.1)*(xe-en[j]);
505     Float_t ck=csik[j]+((csik[j+1]-csik[j])/0.1)*(xe-en[j]);
506
507     //FORMULAE FROM HANDBOOK OF OPTICS, 33.23 OR
508     //W.R. HUNTER, J.O.S.A. 54 (1964),15 , J.O.S.A. 55(1965),1197
509
510     Float_t sinin=TMath::Sqrt(1-pdoti*pdoti);
511     Float_t tanin=sinin/pdoti;
512
513     Float_t c1=cn*cn-ck*ck-sinin*sinin;
514     Float_t c2=4*cn*cn*ck*ck;
515     Float_t aO=TMath::Sqrt(0.5*(TMath::Sqrt(c1*c1+c2)+c1));
516     Float_t b2=0.5*(TMath::Sqrt(c1*c1+c2)-c1);
517     
518     Float_t rs=((aO-pdoti)*(aO-pdoti)+b2)/((aO+pdoti)*(aO+pdoti)+b2);
519     Float_t rp=rs*((aO-sinin*tanin)*(aO-sinin*tanin)+b2)/((aO+sinin*tanin)*(aO+sinin*tanin)+b2);
520     
521
522     //CORRECTION FACTOR FOR SURFACE ROUGHNESS
523     //B.J. STAGG  APPLIED OPTICS, 30(1991),4113
524
525     Float_t sigraf=18.;
526     Float_t lamb=1240/ene;
527     Float_t fresn;
528  
529     Float_t  rO=TMath::Exp(-(4*TMath::Pi()*pdoti*sigraf/lamb)*(4*TMath::Pi()*pdoti*sigraf/lamb));
530
531     if(pola)
532     {
533         Float_t pdotr=0.8;                                 //DEGREE OF POLARIZATION : 1->P , -1->S
534         fresn=0.5*(rp*(1+pdotr)+rs*(1-pdotr));
535     }
536     else
537         fresn=0.5*(rp+rs);
538       
539     fresn = fresn*rO;
540     return fresn;
541 }//Fresnel()
542 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
543 void AliHMPIDv1::Print(Option_t *option)const
544 {
545 // Debug printout
546   TObject::Print(option);
547 }//void AliHMPID::Print(Option_t *option)const
548 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
549 Bool_t AliHMPIDv1::Raw2SDigits(AliRawReader *pRR)
550 {
551 // Interface methode ivoked from AliSimulation to create a list of sdigits from raw digits. Events loop is done in AliSimulation
552 // Arguments: pRR- raw reader 
553 //   Returns: kTRUE on success (currently ignored in AliSimulation::ConvertRaw2SDigits())      
554   AliHMPIDDigit sdi; //tmp sdigit, raw digit will be converted to it
555   
556   if(!GetLoader()->TreeS()) {MakeTree("S");  MakeBranch("S");}
557     
558   TClonesArray *pSdiLst=SdiLst(); Int_t iSdiCnt=0; //tmp list of sdigits for all chambers
559   pRR->Select("HMPID",0,13);//select all HMPID DDL files
560   UInt_t w32=0;
561   while(pRR->ReadNextInt(w32)){//raw records loop (in selected DDL files)
562     UInt_t ddl=pRR->GetDDLID(); //returns 0,1,2 ... 13
563     sdi.Raw(ddl,w32);  
564     new((*pSdiLst)[iSdiCnt++]) AliHMPIDDigit(sdi); //add this digit to the tmp list
565   }//raw records loop
566   GetLoader()->TreeS()->Fill(); GetLoader()->WriteSDigits("OVERWRITE");//write out sdigits
567   SdiReset();
568   return kTRUE;
569 }//Raw2SDigits
570 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
571 void AliHMPIDv1::StepCount()
572 {
573 // Count number of ckovs created  
574 }
575 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
576 void AliHMPIDv1::StepHistory()
577 {
578 // This methode is invoked from StepManager() in order to print out 
579   static Int_t iStepN;
580   const char *sParticle;
581   switch(gMC->TrackPid()){
582     case kProton:      sParticle="PROTON"    ;break;
583     case kNeutron:     sParticle="neutron"   ;break;
584     case kGamma:       sParticle="gamma"     ;break;
585     case kCerenkov:    sParticle="CKOV"    ;break;
586     case kPi0:         sParticle="Pi0"       ;break;  
587     case kPiPlus:      sParticle="Pi+"       ;break;  
588     case kPiMinus:     sParticle="Pi-"       ;break;  
589     case kElectron:    sParticle="electron"  ;break;  
590     default:           sParticle="not known" ;break;
591   }
592
593   TString flag="fanny combination";
594   if(gMC->IsTrackAlive())
595       if(gMC->IsTrackEntering())      flag="enters to";
596       else if(gMC->IsTrackExiting())  flag="exits from";
597       else if(gMC->IsTrackInside())   flag="inside";
598   else
599       if(gMC->IsTrackStop())          flag="stoped in";        
600   
601   Int_t vid=0,copy=0;
602   TString path=gMC->CurrentVolName(); path.Prepend("-");path.Prepend(gMC->CurrentVolOffName(1));//current volume and his mother are always there
603   vid=gMC->CurrentVolOffID(2,copy);  if(vid) {path.Prepend("-");path.Prepend(gMC->VolName(vid));}
604   vid=gMC->CurrentVolOffID(3,copy);  if(vid) {path.Prepend("-");path.Prepend(gMC->VolName(vid));}
605   
606   Printf("Step %i: %s (%i) %s %s m=%.6f GeV q=%.1f dEdX=%.4f",iStepN,sParticle,gMC->TrackPid(),flag.Data(),path.Data(),gMC->TrackMass(),gMC->TrackCharge(),gMC->Edep()*1e9);
607   
608   Printf("Step %i: tid=%i flags alive=%i disap=%i enter=%i exit=%i inside=%i out=%i stop=%i new=%i",
609                             iStepN, gAlice->GetMCApp()->GetCurrentTrackNumber(),
610                             gMC->IsTrackAlive(), gMC->IsTrackDisappeared(),gMC->IsTrackEntering(), gMC->IsTrackExiting(),
611                             gMC->IsTrackInside(),gMC->IsTrackOut(),        gMC->IsTrackStop(),     gMC->IsNewTrack());
612   
613   Float_t a,z,den,rad,abs; a=z=den=rad=abs=-1;
614   Int_t mid=gMC->CurrentMaterial(a,z,den,rad,abs);
615   Printf("Step %i: id=%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);
616   iStepN++;
617 }//StepHistory()
618 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
619 void AliHMPIDv1::StepManager()
620 {
621 // Full Step Manager.
622 // Arguments: none
623 //   Returns: none           
624 //  StepHistory(); return; //uncomment to print tracks history
625 //  StepCount(); return;     //uncomment to count photons
626   
627   Int_t   copy; //volume copy aka node
628   
629 //Treat photons    
630   if((gMC->TrackPid()==kCerenkov||gMC->TrackPid()==kFeedback)&&gMC->CurrentVolID(copy)==fIdPc){  //photon (Ckov or feedback) hit PC (fIdPc)
631     if(gMC->Edep()>0){                                                                           //photon survided QE test i.e. produces electron
632       if(IsLostByFresnel()){ gMC->StopTrack(); return;}                                          //photon lost due to fersnel reflection on PC       
633                        gMC->CurrentVolOffID(2,copy);                                             //current chamber since geomtry tree is HMPID-Rppf-Rpc
634       Int_t   tid=     gMC->GetStack()->GetCurrentTrackNumber();                                 //take TID
635       Int_t   pid=     gMC->TrackPid();                                                          //take PID
636       Float_t etot=    gMC->Etot();                                                              //total hpoton energy, [GeV] 
637       Double_t x[3];   gMC->TrackPosition(x[0],x[1],x[2]);                                       //take MARS position at entrance to PC
638       Float_t xl,yl;   AliHMPIDParam::Instance()->Mars2Lors(copy,x,xl,yl);                        //take LORS position
639       new((*fHits)[fNhits++])AliHMPIDHit(copy,etot,pid,tid,xl,yl,x);                              //HIT for photon, position at PC
640       GenFee(copy);                                                                              //generate feedback photons
641     }//photon hit PC and DE >0 
642   }//photon hit PC
643   
644 //Treat charged particles  
645   static Double_t dEdX;                                                                           //need to store mip parameters between different steps    
646   static Double_t in[3];
647   if(gMC->TrackCharge() && gMC->CurrentVolID(copy)==fIdAmpGap){                                   //charged particle in amplification gap (fIdAmpGap)
648     if(gMC->IsTrackEntering()||gMC->IsNewTrack()) {                                               //entering or newly created
649       dEdX=0;                                                                                     //reset dEdX collector                         
650       gMC->TrackPosition(in[0],in[1],in[2]);                                                      //take position at the entrance
651     }else if(gMC->IsTrackExiting()||gMC->IsTrackStop()||gMC->IsTrackDisappeared()){               //exiting or disappeared
652       dEdX              +=gMC->Edep();                                                            //take into account last step dEdX
653                           gMC->CurrentVolOffID(1,copy);                                           //take current chamber since geometry tree is HMPID-Rgap
654       Int_t tid=          gMC->GetStack()->GetCurrentTrackNumber();                               //take TID
655       Int_t pid=          gMC->TrackPid();                                                        //take PID
656       Double_t out[3];    gMC->TrackPosition(out[0],out[1],out[2]);                               //take MARS position at exit
657       out[0]=0.5*(out[0]+in[0]); out[1]=0.5*(out[1]+in[1]); out[1]=0.5*(out[1]+in[1]);            //take hit position at the anod plane
658       Float_t xl,yl;AliHMPIDParam::Instance()->Mars2Lors(copy,out,xl,yl);                          //take LORS position
659       new((*fHits)[fNhits++])AliHMPIDHit(copy,dEdX,pid,tid,xl,yl,out);                             //HIT for MIP, position near anod plane 
660       GenFee(copy,dEdX);                                                                          //generate feedback photons
661     }else                                                                                         //just going inside
662       dEdX          += gMC->Edep();                                                               //collect this step dEdX 
663   }//MIP in GAP
664 }//StepManager()
665 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++