Updated MUONRawStreamTrackerErrorCount function
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDv2.cxx
index 03d8940..fea5c6b 100644 (file)
@@ -1,4 +1,3 @@
-
 // **************************************************************************
 // * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 // *                                                                        *
@@ -19,7 +18,9 @@
 #include "AliHMPIDParam.h"    //StepManager()
 #include "AliHMPIDHit.h"      //Hits2SDigs(),StepManager()
 #include "AliHMPIDDigit.h"    //Digits2Raw(), Raw2SDigits()
+#include "AliHMPIDRawStream.h"  //Digits2Raw(), Raw2SDigits()
 #include "AliRawReader.h"     //Raw2SDigits()
+#include "AliTrackReference.h"
 #include <TVirtualMC.h>       //StepManager() for gMC
 #include <TPDGCode.h>         //StepHistory() 
 #include <AliStack.h>         //StepManager(),Hits2SDigits()78.6
 #include <AliMC.h>            //StepManager()      
 #include <AliRun.h>           //CreateMaterials()    
 #include <AliMagF.h>          //CreateMaterials()
-#include <TGeoManager.h>      //CreateGeometry()
+#include "AliGeomManager.h"   //AddAlignableVolumes()
+#include <AliCDBEntry.h>      //CreateMaterials()
+#include <AliCDBManager.h>    //CreateMaterials()
 #include <TF1.h>              //DefineOpticalProperties()
 #include <TF2.h>              //DefineOpticalProperties()
-#include <TLorentzVector.h>   //IsLostByFresnel() 
-#include <AliCDBManager.h>    //CreateMaterials()
-#include <AliCDBEntry.h>      //CreateMaterials()
+#include <TGeoGlobalMagField.h>
 #include <TGeoPhysicalNode.h> //AddAlignableVolumes()
+#include <TLorentzVector.h>   //IsLostByFresnel() 
+#include <TTree.h>
+
 ClassImp(AliHMPIDv2)    
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 void AliHMPIDv2::AddAlignableVolumes()const
@@ -43,15 +46,31 @@ void AliHMPIDv2::AddAlignableVolumes()const
 // Associates the symbolic volume name with the corresponding volume path. Interface method from AliModule invoked from AliMC
 // Arguments: none
 //   Returns: none   
-  
+
+  AliGeomManager::ELayerID idHMPID = AliGeomManager::kHMPID;
+  Int_t modUID, modnum = 0;
+
   TGeoHMatrix *pGm = new TGeoHMatrix;
-  Double_t trans[3]={0.5*AliHMPIDDigit::SizeAllX(),0.5*AliHMPIDDigit::SizeAllY(),0};                            //clm: translation from LORS to TGeo RS 
+  Double_t trans[3]={0.5*131.24,0.5*126.16,0};                            //translation from LORS to TGeo RS (half size AllX, half size allY,0)
   pGm->SetTranslation(trans);
  
-  for(Int_t i=AliHMPIDDigit::kMinCh;i<=AliHMPIDDigit::kMaxCh;i++) {
-      TGeoPNEntry *pPn=gGeoManager->SetAlignableEntry(Form("/HMPID/Chamber%i",i),Form("ALIC_1/Hmp_%i",i)); 
-      pPn->SetMatrix(pGm); 
-    }
+  Double_t ph[7]={10.,10., 30.,30.,30. ,50.,50};
+
+  for(Int_t iCh=AliHMPIDParam::kMinCh;iCh<=AliHMPIDParam::kMaxCh;iCh++) {
+    modUID = AliGeomManager::LayerToVolUID(idHMPID,modnum++);
+    if(!gGeoManager->SetAlignableEntry(Form("/HMPID/Chamber%i",iCh),Form("ALIC_1/Hmp_%i",iCh),modUID))
+           AliError("AliHMPIDv3::Unable to set alignable entry!!");  //aligment without AliCluster3D
+    //Get Tracking To Local matricies for alignment with AliCluster3D
+    TGeoPNEntry *eCh = gGeoManager->GetAlignableEntryByUID(modUID);
+    TGeoHMatrix *globMatrix = eCh->GetGlobalOrig();
+
+    //Double_t phi = 20.0 * ((iCh+1) / 3) + 10.0;
+    Double_t phi = ph[iCh];
+    TGeoHMatrix *t2l  = new TGeoHMatrix();
+    t2l->RotateZ(phi);
+    t2l->MultiplyLeft(&(globMatrix->Inverse()));
+    eCh->SetMatrix(t2l);
+  }//iCh loop
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 void AliHMPIDv2::CreateMaterials()
@@ -68,42 +87,155 @@ void AliHMPIDv2::CreateMaterials()
   Float_t aC6F14[2]={ 12.01 , 18.99} , zC6F14[2]={ 6 , 9}   , wC6F14[2]={6 , 14} , dC6F14=1.68    ; Int_t nC6F14=-2;
   Float_t  aSiO2[2]={ 28.09 , 15.99} ,  zSiO2[2]={14 , 8}   ,  wSiO2[2]={1 ,  2} ,  dSiO2=2.64    ; Int_t  nSiO2=-2; 
   Float_t   aCH4[2]={ 12.01 ,  1.01} ,   zCH4[2]={ 6 , 1}   ,   wCH4[2]={1 ,  4} ,   dCH4=7.17e-4 ; Int_t   nCH4=-2; 
-  Float_t   aCsI[2]={132.90 ,126.90} ,   zCsI[2]={55 ,53}   ,   wCsI[2]={1 ,  1} ,   dCsI=0.1     ; Int_t   nCsI=-2; 
+// not necessary...PCB properties instead! Float_t   aCsI[2]={132.90 ,126.90} ,   zCsI[2]={55 ,53}   ,   wCsI[2]={1 ,  1} ,   dCsI=0.1     ; Int_t   nCsI=-2; 
   
-  Float_t     aRoha= 12.01 ,               zRoha=  6 ,                              dRoha=  0.10 ,   radRoha= 18.80 , absRoha=  86.3/dRoha; //special material- quasi quartz
-  Float_t       aCu= 63.55 ,                 zCu= 29 ,                                dCu=  8.96 ,     radCu=  1.43 ,   absCu= 134.9/dCu  ;
-  Float_t        aW=183.84 ,                  zW= 74 ,                                 dW= 19.30 ,      radW=  0.35 ,    absW= 185.0/dW   ;
-  Float_t       aAl= 26.98 ,                 zAl= 13 ,                                dAl=  2.70 ,     radAl=  8.90 ,   absAl= 106.4/dAl  ;
-  Float_t       aAr= 39.94 ,                 zAr= 18 ,                                dAr=  1.396e-3,  radAr=  14.0 ,   absAr= 117.2/dAr  ;   
-           
+  Float_t     aRoha = 12.01 ,   zRoha =  6 ,  dRoha =  0.10    ,   radRoha = 18.80 , absRoha =  86.3/dRoha; //special material- quasi quartz
+  Float_t       aCu = 63.55 ,   zCu   = 29 ,  dCu   =  8.96    ,   radCu   =  1.43 , absCu   = 134.9/dCu  ;
+  Float_t        aW =183.84 ,   zW    = 74 ,  dW    = 19.30    ,   radW    =  0.35 , absW    = 185.0/dW   ;
+  Float_t       aAl = 26.98 ,   zAl   = 13 ,  dAl   =  2.70    ,   radAl   =  8.90 , absAl   = 106.4/dAl  ;
+  Float_t       aAr = 39.94 ,   zAr   = 18 ,  dAr   =  1.396e-3,   radAr   =  14.0 , absAr   = 117.2/dAr  ;   
+
     Int_t   matId=0;                           //tmp material id number
     Int_t   unsens =  0, sens=1;               //sensitive or unsensitive medium
-    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
-    Float_t maxfld = gAlice->Field()->Max();   //max field value
+    Int_t   itgfld = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->Integ(); //type of field intergration 0 no field -1 user in guswim 1 Runge Kutta 2 helix 3 const field along z
+    Float_t maxfld = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->Max();   //max field value
     Float_t tmaxfd = -10.0;                    //max deflection angle due to magnetic field in one step
     Float_t deemax = - 0.2;                    //max fractional energy loss in one step   
-    Float_t stemax = - 0.1;                    //mas step allowed [cm]
+    Float_t stemax = - 0.1;                    //max step allowed [cm]
     Float_t epsil  =   0.001;                  //abs tracking precision [cm]   
     Float_t stmin  = - 0.001;                  //min step size [cm] in continius process transport, negative value: choose it automatically
+
+    // PCB copmposed mainly by G10 (Si,C,H,O) -> CsI is negligible (<500nm thick)
+    // So what is called CsI has the optical properties of CsI, but the composition of G-10 (for delta elec, etc production...)
+    
+    Float_t aG10[4] = {28.09,12.01,1.01,16.00};
+    Float_t zG10[4] = {14.,  6.,  1.,  8.};
+    Float_t wG10[4] = {0.129060,0.515016,0.061873,0.294050};
+    Float_t dG10    = 1.7;
+    Int_t   nG10    = 4;
     
     AliMixture(++matId,"Air"  ,aAir  ,zAir  ,dAir  ,nAir  ,wAir  ); AliMedium(kAir  ,"Air"  ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
     AliMixture(++matId,"C6F14",aC6F14,zC6F14,dC6F14,nC6F14,wC6F14); AliMedium(kC6F14,"C6F14",matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);      
     AliMixture(++matId,"SiO2" ,aSiO2 ,zSiO2 ,dSiO2 ,nSiO2 ,wSiO2 ); AliMedium(kSiO2 ,"SiO2" ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);    
     AliMixture(++matId,"CH4"  ,aCH4  ,zCH4  ,dCH4  ,nCH4  ,wCH4  ); AliMedium(kCH4  ,"CH4"  ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);  
-    AliMixture(++matId,"CsI"  ,aCsI  ,zCsI  ,dCsI  ,nCsI  ,wCsI  ); AliMedium(kCsI  ,"CsI"  ,matId,   sens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);//sensitive
-  
-    AliMixture(++matId ,"Neo" ,aSiO2 ,zSiO2 ,dSiO2 ,nSiO2 ,wSiO2 ); AliMedium(kNeo,"Neo" , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin); //clm neoceram
-    AliMaterial(++matId,"Roha",aRoha,zRoha,dRoha,radRoha,absRoha);  AliMedium(kRoha,"Roha", matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin); //Roha->honeycomb
+//    AliMixture(++matId,"CsI"  ,aCsI  ,zCsI  ,dCsI  ,nCsI  ,wCsI  ); AliMedium(kCsI  ,"CsI"  ,matId,   sens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);//sensitive
+    AliMixture(++matId,"CsI+PCB",aG10  , zG10, dG10,nG10   ,wG10   ); AliMedium(kCsI  ,"CsI"  ,matId,   sens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);//sensitive
+
+    AliMixture(++matId ,"Neo" ,aSiO2 ,zSiO2 ,dSiO2 ,nSiO2 ,wSiO2 ); AliMedium(kNeo  ,"Neo"  ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin); //clm neoceram
+    AliMaterial(++matId,"Roha",aRoha,zRoha,dRoha,radRoha,absRoha);  AliMedium(kRoha ,"Roha" ,matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin); //Roha->honeycomb
+
 
-    
     AliMaterial(++matId,"Cu"  ,aCu  ,zCu  ,dCu  ,radCu  ,absCu  );  AliMedium(kCu  ,"Cu"  , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
     AliMaterial(++matId,"W"   ,aW   ,zW   ,dW   ,radW   ,absW   );  AliMedium(kW   ,"W"   , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
     AliMaterial(++matId,"Al"  ,aAl  ,zAl  ,dAl  ,radAl  ,absAl  );  AliMedium(kAl  ,"Al"  , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
     AliMaterial(++matId,"Ar"  ,aAr  ,zAr  ,dAr  ,radAr  ,absAr  );  AliMedium(kAr  ,"Ar"  , matId, unsens, itgfld, maxfld, tmaxfd, stemax, deemax, epsil, stmin);
-    
-    DefineOpticalProperties();
+
+    //InitProperties();
+        
 }//void AliHMPID::CreateMaterials()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+//void AliHMPIDv2::InitProperties()
+//{
+/*
+* HMPID
+* ====
+*
+*       GAM   ELEC  NHAD   CHAD  MUON  EBREM MUHAB  EDEL  MUDEL MUPA ANNI BREM COMP DCAY DRAY HADR LOSS MULS PAIR PHOT RAYL
+* Quarz Window        (>1000 keV delta-electrons)
+HMPID  3  1.e-4 1.e-4 1.e-4  -1.   1.e-4 -1.   -1.    1.e-3 1.e-3 -1.  -1   -1   -1   -1   1    -1   1    -1   -1   -1   -1 
+* Freon Radiator      (>  500 keV delta-electrons)
+HMPID  4  1.e-4 1.e-4 1.e-4  -1.   1.e-4 -1.   -1.    5.e-4 5.e-4 -1.  -1   -1   -1   -1   1    -1   1    -1   -1   -1   -1 
+* Methane Gap         (>  100 keV delta-electrons)
+HMPID  5  5.e-5 1.e-5 1.e-4 -1.   1.e-4 -1.   -1.     1.e-4 1.e-4 -1.  -1   -1   -1   -1   1    -1   1    -1   -1   -1   -1 
+* Sensitive Volume    (>  50 keV delta-electrons)
+HMPID  9  1.e-5 1.e-5 1.e-4  -1.   1.e-4 -1.   -1.    5.e-5 5.e-5 -1.  -1   -1   -1   -1   1    -1   1    -1   -1   -1   -1 
+* CSI    (>  50 keV delta-electrons)
+HMPID  6  1.e-5 1.e-5 1.e-4  -1.   1.e-4 -1.   -1.    5.e-5 5.e-5 -1.  -1   -1   -1   -1   1    -1   1    -1   -1   -1   -1 
+* PCB backplane   (>  50 keV delta-electrons)
+HMPID 12  1.e-5 1.e-5 1.e-4  -1.   1.e-4 -1.   -1.    5.e-5 5.e-5 -1.  -1   -1   -1   -1   1    -1   1    -1   -1   -1   -1 
+
+    Int_t *idtmed = fIdtmed->GetArray();
+    Int_t imed;
+    
+    imed = kSiO2;   // * Quarz Window        (>1000 keV delta-electrons)
+    gMC->Gstpar(idtmed[imed], "CUTGAM",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTELE",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTNEU",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTMUO",1.e-4);    
+    gMC->Gstpar(idtmed[imed], "DCUTE" ,1.e-3);    
+    gMC->Gstpar(idtmed[imed], "CUTHAD",1.e-3);    
+    
+    gMC->Gstpar(idtmed[imed], "DRAY",1);    
+    gMC->Gstpar(idtmed[imed], "LOSS",1);    
+
+    imed = kC6F14;  // * Freon Radiator      (>  500 keV delta-electrons)
+    gMC->Gstpar(idtmed[imed], "CUTGAM",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTELE",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTNEU",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTMUO",1.e-4);    
+    gMC->Gstpar(idtmed[imed], "DCUTE" ,5.e-4);    
+    gMC->Gstpar(idtmed[imed], "CUTHAD",5.e-4);    
+    
+    gMC->Gstpar(idtmed[imed], "DRAY",1);    
+    gMC->Gstpar(idtmed[imed], "LOSS",1);    
+    
+    imed = kCH4;  // * Methane Gap         (>  100 keV delta-electrons)
+    gMC->Gstpar(idtmed[imed], "CUTGAM",5.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTELE",5.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTNEU",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTMUO",1.e-4);    
+    gMC->Gstpar(idtmed[imed], "DCUTE" ,1.e-4);    
+    gMC->Gstpar(idtmed[imed], "CUTHAD",1.e-4);    
+    
+    gMC->Gstpar(idtmed[imed], "DRAY",1);    
+    gMC->Gstpar(idtmed[imed], "LOSS",1);    
+    
+    imed = kCsI;  // * CSI    (>  50 keV delta-electrons)
+    gMC->Gstpar(idtmed[imed], "CUTGAM",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTELE",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTNEU",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTMUO",1.e-4);    
+    gMC->Gstpar(idtmed[imed], "DCUTE" ,5.e-5);    
+    gMC->Gstpar(idtmed[imed], "CUTHAD",5.e-5);    
+    
+    gMC->Gstpar(idtmed[imed], "DRAY",1);    
+    gMC->Gstpar(idtmed[imed], "LOSS",1);
+    
+    imed = kAl;  // * Alluminium    (>  50 keV delta-electrons)
+    gMC->Gstpar(idtmed[imed], "CUTGAM",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTELE",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTNEU",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTMUO",1.e-4);    
+    gMC->Gstpar(idtmed[imed], "DCUTE" ,5.e-5);    
+    gMC->Gstpar(idtmed[imed], "CUTHAD",5.e-5);    
+    
+    gMC->Gstpar(idtmed[imed], "DRAY",1);    
+    gMC->Gstpar(idtmed[imed], "LOSS",1);    
+    
+    imed = kCu;  // * Copper       (>  50 keV delta-electrons)
+    gMC->Gstpar(idtmed[imed], "CUTGAM",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTELE",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTNEU",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTMUO",1.e-4);    
+    gMC->Gstpar(idtmed[imed], "DCUTE" ,5.e-5);    
+    gMC->Gstpar(idtmed[imed], "CUTHAD",5.e-5);    
+    
+    gMC->Gstpar(idtmed[imed], "DRAY",1);    
+    gMC->Gstpar(idtmed[imed], "LOSS",1);    
+    
+    imed = kW;  // * Tungsten     (>  50 keV delta-electrons)
+    gMC->Gstpar(idtmed[imed], "CUTGAM",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTELE",1.e-5);
+    gMC->Gstpar(idtmed[imed], "CUTNEU",1.e-4);
+    gMC->Gstpar(idtmed[imed], "CUTMUO",1.e-4);    
+    gMC->Gstpar(idtmed[imed], "DCUTE" ,5.e-5);    
+    gMC->Gstpar(idtmed[imed], "CUTHAD",5.e-5);    
+    
+    gMC->Gstpar(idtmed[imed], "DRAY",1);    
+    gMC->Gstpar(idtmed[imed], "LOSS",1);    
+    
+}*/
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 void AliHMPIDv2::CreateGeometry()
 {
 //Creates detailed geometry simulation (currently GEANT volumes tree)         
@@ -126,18 +258,15 @@ void AliHMPIDv2::CreateGeometry()
   TGeoVolume *hmp=gGeoManager->MakeBox ("Hmp",ch4,1681*mm/2, 1466*mm/2,(2*80*mm+2*60*mm)/2);//2033P1  z from p84 TDR  
 
   TString title=GetTitle();
-  if(title.Contains("TestBeam")  )
-      {
-        gGeoManager->GetVolume("ALIC")->AddNode(hmp,0,new TGeoTranslation(5.0*mm/2 ,  5.0*mm/2, 1000.0*mm));
-      }
-  else
-    {
-      for(Int_t iCh=AliHMPIDDigit::kMinCh;iCh<=AliHMPIDDigit::kMaxCh;iCh++){//place 7 chambers
+  if(title.Contains("TestBeam")){
+    gGeoManager->GetVolume("ALIC")->AddNode(hmp,0);
+  }else{
+    for(Int_t iCh=AliHMPIDParam::kMinCh;iCh<=AliHMPIDParam::kMaxCh;iCh++){//place 7 chambers
       TGeoHMatrix *pMatrix=new TGeoHMatrix;
-      AliHMPIDParam::IdealPosition(iCh,pMatrix);
+      IdealPosition(iCh,pMatrix);
       gGeoManager->GetVolume("ALIC")->AddNode(hmp,iCh,pMatrix);
-       }
-     }
+    }
+  }
 
   TGeoRotation *rot=new TGeoRotation("HwireRot"); rot->RotateY(90); //rotate wires around Y to be along X (initially along Z)
   TGeoVolume *sbo=gGeoManager->MakeBox ("Hsbo",ch4  , 1419*mm/2 , 1378.00*mm/2 ,   50.5*mm/2);//2072P1
@@ -155,10 +284,13 @@ void AliHMPIDv2::CreateGeometry()
   TGeoVolume *col=gGeoManager->MakeTube("Hcol",cu   ,    0*mm   ,  100.00*um   , 1323.0*mm/2);
   TGeoVolume *sec=gGeoManager->MakeBox ("Hsec",ch4  ,  648*mm/2 ,  411.00*mm/2 ,   6.2*mm/2);//sec=gap
  
-  TGeoVolume *gap=gGeoManager->MakeBox ("Hgap",ch4  ,  640*mm/2 ,  403.20*mm/2 ,    6.2*mm/2);//gap=pad+ano+cat
-  TGeoVolume *cat=gGeoManager->MakeTube("Hcat",cu   ,    0*mm   ,   50.00*um   ,    8.0*mm/2); 
-  TGeoVolume *ano=gGeoManager->MakeTube("Hano",w    ,    0*mm   ,   20.00*um   ,    8.0*mm/2); 
-  TGeoVolume *pad=gGeoManager->MakeBox ("Hpad",csi  ,    8*mm/2 ,    8.40*mm/2 ,    1.7*mm/2);      
+  Double_t cellx=8.04*mm,celly=8.4*mm; Int_t nPadX=80, nPadY=48; 
+  TGeoVolume *gap=gGeoManager->MakeBox ("Hgap",ch4  ,  cellx*nPadX/2 ,  celly*nPadY/2 ,    6.2*mm/2); //x=8.04*80 y=8.4*48 z=pad+pad-ano+marign 2006p1  
+  TGeoVolume *row=        gap->Divide  ("Hrow",2,nPadY,0,0);//along Y->48 rows
+  TGeoVolume *cel=        row->Divide  ("Hcel",1,nPadX,0,0);//along X->80 cells
+  TGeoVolume *cat=gGeoManager->MakeTube("Hcat",cu   ,    0.00*mm   ,   50.00*um   ,    cellx/2); 
+  TGeoVolume *ano=gGeoManager->MakeTube("Hano",w    ,    0.00*mm   ,   20.00*um   ,    cellx/2); 
+  TGeoVolume *pad=gGeoManager->MakeBox ("Hpad",csi  ,    7.54*mm/2 ,    7.90*mm/2 ,    1.7*mm/2); //2006P1 PCB material...     
   TGeoVolume *fr1=gGeoManager->MakeBox ("Hfr1",al   , 1463*mm/2 , 1422.00*mm/2 ,   58.3*mm/2);//2040P1
   TGeoVolume *fr1up=gGeoManager->MakeBox ("Hfr1up",ch4,(1426.00-37.00)*mm/2 , (1385.00-37.00)*mm/2 ,    20.0*mm/2);//2040P1
   TGeoVolume *fr1perUpBig=gGeoManager->MakeBox ("Hfr1perUpBig",ch4,1389*mm/2,35*mm/2,10*mm/2);    
@@ -171,6 +303,13 @@ void AliHMPIDv2::CreateGeometry()
   TGeoVolume *smo=gGeoManager->MakeBox ("Hsmo",ar   ,  114*mm/2 ,   89.25*mm/2 ,   38.3*mm/2);//2001P2
                
 
+               
+        TGeoVolume *fr3=   gGeoManager->MakeBox("Hfr3",          al,  1463*mm/2,  1422*mm/2,  34*mm/2);//2041P1
+   TGeoVolume *fr3up=    gGeoManager->MakeBox("Hfr3up",     ch4, 1323*mm/2,  1282*mm/2,  20*mm/2);//2041P1
+   TGeoVolume *fr3down=gGeoManager->MakeBox("Hfr3down", ch4, 1437*mm/2,  1370*mm/2,  14*mm/2);//2041P1
+
+
+
 // ^ Y   z=         z=-12mm      z=98.25mm               ALIC->7xHmp (virtual)-->1xHsbo (virtual) --->2xHcov (real) 2072P1
 // |  ____________________________________                                    |                   |-->1xHhon (real) 2072P1
 // | |   ______     ____          ______  |                                   |
@@ -210,8 +349,6 @@ void AliHMPIDv2::CreateGeometry()
   hmp->AddNode(sec,2,new TGeoTranslation(-335*mm,   0*mm,  78.6*mm)); hmp->AddNode(sec,3,new TGeoTranslation(+335*mm,   0*mm,  78.6*mm));
   hmp->AddNode(sec,0,new TGeoTranslation(-335*mm,-433*mm,  78.6*mm)); hmp->AddNode(sec,1,new TGeoTranslation(+335*mm,-433*mm,  78.6*mm));
     sec->AddNode(gap,1,new TGeoTranslation(0,0,0.*mm));
-  TGeoVolume *row=          gap->Divide("Hrow",2,48,0,0);//along Y->48 rows
-  TGeoVolume *cel=          row->Divide("Hcel",1,80,0,0);//along X->80 cells
       cel->AddNode(cat,1,new TGeoCombiTrans (0,  3.15*mm , -2.70*mm , rot)); //4 cathode wires
       cel->AddNode(ano,1,new TGeoCombiTrans (0,  2.00*mm , -0.29*mm , rot)); //2 anod wires
       cel->AddNode(cat,2,new TGeoCombiTrans (0,  1.05*mm , -2.70*mm , rot)); 
@@ -288,14 +425,17 @@ void AliHMPIDv2::CreateGeometry()
   ppf->AddNode(smo,6,new TGeoTranslation(+ 65.0*mm,+ 50.625*mm,  0.*mm));
   ppf->AddNode(smo,7,new TGeoTranslation(+ 65.0*mm,+151.875*mm,  0.*mm)); 
 
-
+hmp->AddNode(fr3,1,new TGeoTranslation(0.,0.,(80.-29.)*mm-34.*mm/2));
+         fr3->AddNode( fr3up,1,    new TGeoTranslation(0.,  0.,  7*mm));
+        fr3->AddNode(fr3down,1,new TGeoTranslation(0.,  0., -10*mm));  
 
   AliDebug(1,"Stop v2. HMPID option");  
 }//CreateGeometry()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 void AliHMPIDv2::Init()
 {
-// This methode defines ID for sensitive volumes, i.e. such geometry volumes for which there are if(gMC->CurrentVolID()==XXX) statements in StepManager()
+// This method defines ID for sensitive volumes, i.e. such geometry volumes for which there are if(gMC->CurrentVolID()==XXX) 
+// statements in StepManager()
 // Arguments: none
 //   Returns: none      
   AliDebug(1,"Start v2 HMPID.");    
@@ -310,11 +450,13 @@ void AliHMPIDv2::DefineOpticalProperties()
   const Int_t kNbins=30;       //number of photon energy points
   Float_t emin=5.5,emax=8.5;         //Photon energy range,[eV]
   Float_t aEckov [kNbins]; 
+  Double_t dEckov [kNbins]; 
   Float_t aAbsRad[kNbins], aAbsWin[kNbins], aAbsGap[kNbins], aAbsMet[kNbins];
   Float_t aIdxRad[kNbins], aIdxWin[kNbins], aIdxGap[kNbins], aIdxMet[kNbins], aIdxPc[kNbins]; 
   Float_t                                                    aQeAll [kNbins], aQePc [kNbins];
+  Double_t dReflMet[kNbins], dQePc[kNbins];
 
-  TF2 *pRaIF=new TF2("HidxRad","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
+  TF2 *pRaIF=new TF2("HidxRad","sqrt(1+0.554*(1239.84/x)^2/((1239.84/x)^2-5769)-0.0005*(y-20))"                                       ,emin,emax,0,50); //DiMauro mail temp 0-50 degrees C
   TF1 *pWiIF=new TF1("HidxWin","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
   TF1 *pGaIF=new TF1("HidxGap","1+0.12489e-6/(2.62e-4 - x*x/1239.84/1239.84)"                                                         ,emin,emax);      //?????? from where  
 
@@ -324,17 +466,22 @@ void AliHMPIDv2::DefineOpticalProperties()
   TF1 *pGaAF=new TF1("HabsGap","(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  
   
   TF1 *pQeF =new TF1("Hqe"    ,"0+(x>6.07267)*0.344811*(1-exp(-1.29730*(x-6.07267)))"                                                 ,emin,emax);  //fit from DiMauro data 28.10.03  
-                            
+                   
+  TString title=GetTitle();
+  Bool_t isFlatIdx=title.Contains("FlatIdx"); 
+  
   for(Int_t i=0;i<kNbins;i++){
     Float_t eV=emin+0.1*i;  //Ckov energy in eV
     aEckov [i] =1e-9*eV;    //Ckov energy in GeV
-    aAbsRad[i]=pRaAF->Eval(eV); aIdxRad[i]=1.292;//pRaIF->Eval(eV,20);      //Simulation for 20 degress C       
-    aAbsWin[i]=pWiAF->Eval(eV); aIdxWin[i]=1.5787;//pWiIF->Eval(eV);
-    aAbsGap[i]=pGaAF->Eval(eV); aIdxGap[i]=1.0005;//pGaIF->Eval(eV);   
+    dEckov [i] = aEckov[i];
+    aAbsRad[i]=pRaAF->Eval(eV); (isFlatIdx)? aIdxRad[i]=1.292: aIdxRad[i]=pRaIF->Eval(eV,20);     
+    aAbsWin[i]=pWiAF->Eval(eV);              aIdxWin[i]=pWiIF->Eval(eV);
+    aAbsGap[i]=pGaAF->Eval(eV);              aIdxGap[i]=pGaIF->Eval(eV);   
     aQeAll[i] =1;                     //QE for all other materials except for PC must be 1.  
     aAbsMet[i] =0.0001;                aIdxMet[i]=0;                                             //metal ref idx must be 0 in order to reflect photon
                                        aIdxPc [i]=1;           aQePc [i]=pQeF->Eval(eV);         //PC ref idx must be 1 in order to apply photon to QE conversion 
-                                       
+    dQePc [i]=pQeF->Eval(eV);
+    dReflMet[i] = 0.;     // no reflection on the surface of the pc (?)                                       
   }
   gMC->SetCerenkov((*fIdtmed)[kC6F14]    , kNbins, aEckov, aAbsRad  , aQeAll , aIdxRad );    
   gMC->SetCerenkov((*fIdtmed)[kSiO2]     , kNbins, aEckov, aAbsWin  , aQeAll , aIdxWin );    
@@ -343,6 +490,13 @@ void AliHMPIDv2::DefineOpticalProperties()
   gMC->SetCerenkov((*fIdtmed)[kW]        , kNbins, aEckov, aAbsMet  , aQeAll , aIdxMet ); //n=0 means reflect photons       
   gMC->SetCerenkov((*fIdtmed)[kCsI]      , kNbins, aEckov, aAbsMet  , aQePc  , aIdxPc  ); //n=1 means convert photons    
   gMC->SetCerenkov((*fIdtmed)[kAl]       , kNbins, aEckov, aAbsMet  , aQeAll , aIdxMet );    
+
+  // Define a skin surface for the photocatode to enable 'detection' in G4
+  gMC->DefineOpSurface("surfPc", kGlisur /*kUnified*/,kDielectric_metal,kPolished, 0.);
+  gMC->SetMaterialProperty("surfPc", "EFFICIENCY", kNbins, dEckov, dQePc);
+  gMC->SetMaterialProperty("surfPc", "REFLECTIVITY", kNbins, dEckov, dReflMet);
+  gMC->SetSkinSurface("skinPc", "Rpc", "surfPc");
+
   delete pRaAF;delete pWiAF;delete pGaAF; delete pRaIF; delete pWiIF; delete pGaIF; delete pQeF;
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -380,7 +534,7 @@ void AliHMPIDv2::GenFee(Float_t qtot)
     gMC->GetRandom()->RndmArray(2,ranf);    //Sample direction
     cthf=ranf[0]*2-1.0;
     if(cthf<0) continue;
-    sthf = TMath::Sqrt((1 - cthf) * (1 + cthf));
+    sthf = TMath::Sqrt((1. - cthf) * (1. + cthf));
     phif = ranf[1] * 2 * TMath::Pi();
     
     if(Double_t randomNumber=gMC->GetRandom()->Rndm()<=0.57)
@@ -487,8 +641,10 @@ void AliHMPIDv2::Digits2Raw()
   }
   treeD->GetEntry(0);
   
-  AliHMPIDDigit::WriteRaw(DigLst());
-    
+  
+  AliHMPIDRawStream *pRS=0x0;
+  pRS->WriteRaw(DigLst());
+   
   GetLoader()->UnloadDigits();
   AliDebug(1,"Stop.");      
 }//Digits2Raw()
@@ -518,7 +674,7 @@ Float_t AliHMPIDv2::Fresnel(Float_t ene,Float_t pdoti, Bool_t pola)
     //FORMULAE FROM HANDBOOK OF OPTICS, 33.23 OR
     //W.R. HUNTER, J.O.S.A. 54 (1964),15 , J.O.S.A. 55(1965),1197
 
-    Float_t sinin=TMath::Sqrt(1-pdoti*pdoti);
+    Float_t sinin=TMath::Sqrt((1.-pdoti)*(1.+pdoti));
     Float_t tanin=sinin/pdoti;
 
     Float_t c1=cn*cn-ck*ck-sinin*sinin;
@@ -562,18 +718,20 @@ Bool_t AliHMPIDv2::Raw2SDigits(AliRawReader *pRR)
 // Interface methode ivoked from AliSimulation to create a list of sdigits from raw digits. Events loop is done in AliSimulation
 // Arguments: pRR- raw reader 
 //   Returns: kTRUE on success (currently ignored in AliSimulation::ConvertRaw2SDigits())      
-  AliHMPIDDigit sdi; //tmp sdigit, raw digit will be converted to it
+  //AliHMPIDDigit sdi; //tmp sdigit, raw digit will be converted to it
   
   if(!GetLoader()->TreeS()) {MakeTree("S");  MakeBranch("S");}
     
   TClonesArray *pSdiLst=SdiLst(); Int_t iSdiCnt=0; //tmp list of sdigits for all chambers
-  pRR->Select("HMPID",0,13);//select all HMPID DDL files
-  UInt_t w32=0;
-  while(pRR->ReadNextInt(w32)){//raw records loop (in selected DDL files)
-    UInt_t ddl=pRR->GetDDLID(); //returns 0,1,2 ... 13
-    sdi.Raw(ddl,w32);  
-    new((*pSdiLst)[iSdiCnt++]) AliHMPIDDigit(sdi); //add this digit to the tmp list
-  }//raw records loop
+  AliHMPIDRawStream stream(pRR);
+  while(stream.Next())
+  {
+    for(Int_t iPad=0;iPad<stream.GetNPads();iPad++) {
+      AliHMPIDDigit sdi(stream.GetPadArray()[iPad],stream.GetChargeArray()[iPad]);
+      new((*pSdiLst)[iSdiCnt++]) AliHMPIDDigit(sdi); //add this digit to the tmp list
+    }
+  }
+  
   GetLoader()->TreeS()->Fill(); GetLoader()->WriteSDigits("OVERWRITE");//write out sdigits
   SdiReset();
   return kTRUE;
@@ -602,15 +760,13 @@ void AliHMPIDv2::StepHistory()
   }
 
   TString flag="fanny combination";
-  if(gMC->IsTrackAlive())
-      if(gMC->IsTrackEntering())      flag="enters to";
-      else if(gMC->IsTrackExiting())  flag="exits from";
-      else if(gMC->IsTrackInside())   flag="inside";
-  else
-      if(gMC->IsTrackStop())          flag="stoped in";        
-  
-
-
+  if(gMC->IsTrackAlive()) {
+    if(gMC->IsTrackEntering())      flag="enters to";
+    else if(gMC->IsTrackExiting())  flag="exits from";
+    else if(gMC->IsTrackInside())   flag="inside";
+  } else {
+    if(gMC->IsTrackStop())          flag="stopped in";
+  }
   
   Int_t vid=0,copy=0;
   TString path=gMC->CurrentVolName(); path.Prepend("-");path.Prepend(gMC->CurrentVolOffName(1));//current volume and his mother are always there
@@ -660,21 +816,23 @@ void AliHMPIDv2::StepManager()
   if((gMC->TrackPid()==50000050||gMC->TrackPid()==50000051)&&gMC->CurrentVolID(copy)==fIdPad){   //photon (Ckov or feedback) hit PC (fIdPad)
     if(gMC->Edep()>0){                                                                           //photon survided QE test i.e. produces electron
       if(IsLostByFresnel()){ gMC->StopTrack(); return;}                                          //photon lost due to fersnel reflection on PC       
-                                                                                        gMC->CurrentVolOffID(5,copy);                                             //current chamber since geomtry tree is Hmp-Hsec-Hgap-Hrow-Hcel-Hpad
+                       gMC->CurrentVolOffID(5,copy);                                             //current chamber since geomtry tree is Hmp-Hsec-Hgap-Hrow-Hcel-Hpad
       Int_t   tid=     gMC->GetStack()->GetCurrentTrackNumber();                                 //take TID
       Int_t   pid=     gMC->TrackPid();                                                          //take PID
       Float_t etot=    gMC->Etot();                                                              //total hpoton energy, [GeV] 
       Double_t x[3];   gMC->TrackPosition(x[0],x[1],x[2]);                                       //take MARS position at entrance to PC
+      Float_t hitTime= (Float_t)gMC->TrackTime();                                                         //hit formation time 
       Float_t xl,yl;   AliHMPIDParam::Instance()->Mars2Lors(copy,x,xl,yl);                       //take LORS position
-       if ( yl < 0  ) Printf("-------------------> SUPER PROBLEM PHOTON>>> Ch: %d, x[]: %f %f %f (MARS)-> xl: %f yl: %f",copy,x[0],x[1],x[2],xl,yl);
-      new((*fHits)[fNhits++])AliHMPIDHit(copy,etot,pid,tid,xl,yl,x);                             //HIT for photon, position at P, etot will be set to Q
-      GenFee(etot);                                                                              //generate feedback photons etot is modified in hit ctor to Q of hit
+      new((*fHits)[fNhits++])AliHMPIDHit(copy,etot,pid,tid,xl,yl,hitTime,x);                             //HIT for photon, position at P, etot will be set to Q
+      if(fDoFeed) GenFee(etot);                                                                  //generate feedback photons etot is modified in hit ctor to Q of hit
     }//photon hit PC and DE >0 
   }//photon hit PC
   
 //Treat charged particles  
   static Float_t eloss;                                                                           //need to store mip parameters between different steps    
-  static Double_t in[3];
+  static Double_t in[3];                                                                          
+  if(gMC->IsTrackEntering() && gMC->TrackCharge() && gMC->CurrentVolID(copy)==fIdPad)             //Trackref stored when entering in the pad volume
+    AddTrackReference(gMC->GetStack()->GetCurrentTrackNumber(), AliTrackReference::kHMPID);       //for acceptance calculations
   if(gMC->TrackCharge() && gMC->CurrentVolID(copy)==fIdCell){                                     //charged particle in amplification gap (fIdCell)
     if(gMC->IsTrackEntering()||gMC->IsNewTrack()) {                                               //entering or newly created
       eloss=0;                                                                                    //reset Eloss collector                         
@@ -685,13 +843,13 @@ void AliHMPIDv2::StepManager()
       Int_t tid=          gMC->GetStack()->GetCurrentTrackNumber();                               //take TID
       Int_t pid=          gMC->TrackPid();                                                        //take PID
       Double_t out[3];    gMC->TrackPosition(out[0],out[1],out[2]);                               //take MARS position at exit
+      Float_t hitTime= (Float_t)gMC->TrackTime();                                                         //hit formation time       
       out[0]=0.5*(out[0]+in[0]);                                                                  //>
       out[1]=0.5*(out[1]+in[1]);                                                                  //take hit position at the anod plane
       out[2]=0.5*(out[2]+in[2]);                                                                  //>
       Float_t xl,yl;AliHMPIDParam::Instance()->Mars2Lors(copy,out,xl,yl);                         //take LORS position
-       if ( yl < 0  ) Printf("-------------------> SUPER PROBLEM CHARGED>>> Ch: %d, x[]: %f %f %f (MARS)-> xl: %f yl: %f",copy,out[0],out[1],out[2],xl,yl);
-      new((*fHits)[fNhits++])AliHMPIDHit(copy,eloss,pid,tid,xl,yl,out);                           //HIT for MIP, position near anod plane, eloss will be set to Q 
-      GenFee(eloss);                                                                              //generate feedback photons 
+      new((*fHits)[fNhits++])AliHMPIDHit(copy,eloss,pid,tid,xl,yl,hitTime,out);                   //HIT for MIP, position near anod plane, eloss will be set to Q 
+      if(fDoFeed) GenFee(eloss);                                                                  //generate feedback photons 
     }else                                                                                         //just going inside
       eloss          += gMC->Edep();                                                              //collect this step eloss 
   }//MIP in GAP
@@ -712,8 +870,32 @@ void AliHMPIDv2::TestGeom()
 //
 // Test method to check geometry
 //
+  //TGeoManager::Import("misaligned_geometry.root");
   TGeoManager::Import("geometry.root");
-  for(Int_t ch=AliHMPIDDigit::kMinCh;ch<=AliHMPIDDigit::kMaxCh;ch++)
+  for(Int_t ch=AliHMPIDParam::kMinCh;ch<=AliHMPIDParam::kMaxCh;ch++)
     TestPoint(ch,0,0);
 }//TestPoint()
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+void  AliHMPIDv2::IdealPosition(Int_t iCh,TGeoHMatrix *pMatrix)       //ideal position of given chamber 
+{
+// Construct ideal position matrix for a given chamber
+// Arguments: iCh- chamber ID; pMatrix- pointer to precreated unity matrix where to store the results
+//   Returns: none
+  const Double_t kAngHor=19.5;        //  horizontal angle between chambers  19.5 grad
+  const Double_t kAngVer=20;          //  vertical angle between chambers    20   grad     
+  const Double_t kAngCom=30;          //  common HMPID rotation with respect to x axis  30   grad     
+  const Double_t kTrans[3]={490,0,0}; //  center of the chamber is on window-gap surface
+  pMatrix->RotateY(90);               //  rotate around y since initial position is in XY plane -> now in YZ plane
+  pMatrix->SetTranslation(kTrans);    //  now plane in YZ is shifted along x 
+  switch(iCh){
+    case 0:                pMatrix->RotateY(kAngHor);  pMatrix->RotateZ(-kAngVer);  break; //right and down 
+    case 1:                                            pMatrix->RotateZ(-kAngVer);  break; //down              
+    case 2:                pMatrix->RotateY(kAngHor);                               break; //right 
+    case 3:                                                                         break; //no rotation
+    case 4:                pMatrix->RotateY(-kAngHor);                              break; //left   
+    case 5:                                            pMatrix->RotateZ(kAngVer);   break; //up
+    case 6:                pMatrix->RotateY(-kAngHor); pMatrix->RotateZ(kAngVer);   break; //left and up 
+  }
+  pMatrix->RotateZ(kAngCom);     //apply common rotation  in XY plane    
+}