]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TPC/AliTPCv2.cxx
QA ref defaut storage setter in sim and rec
[u/mrichter/AliRoot.git] / TPC / AliTPCv2.cxx
index 5e7bcdce4a2e04f96f85dcf96b36c4c4a11e2173..4d85e1c3b1e0bcd486003d295fae4f9c6e5261da 100644 (file)
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
-#include <stdlib.h>
+//#include <stdlib.h>
 
 #include <TLorentzVector.h>
-#include <TMath.h>
 #include <TPDGCode.h>
-#include <TVirtualMC.h>
 #include <TString.h>
-#include <TSystem.h>
-
-#include "AliConst.h"
 #include "AliLog.h"
-#include "AliMC.h"
-#include "AliRun.h"
-#include "AliTPCDigitsArray.h"
+#include "AliMathBase.h"
+#include "AliTrackReference.h"
 #include "AliTPCParam.h"
-#include "AliTPCParamSR.h"
 #include "AliTPCTrackHitsV2.h"
 #include "AliTPCv2.h"
-#include "TGeoManager.h"
+#include "AliGeomManager.h"
 #include "TGeoVolume.h"
 #include "TGeoPcon.h"
 #include "TGeoTube.h"
 #include "TGeoTrd1.h"
 #include "TGeoCompositeShape.h"
 #include "TGeoPara.h"
+#include "TGeoPhysicalNode.h"
+
 ClassImp(AliTPCv2)
  
 //_____________________________________________________________________________
 AliTPCv2::AliTPCv2(const char *name, const char *title) :
-  AliTPC(name, title) 
+  AliTPC(name, title),
+  fIdSens(0),
+  fIDrift(0),
+  fSecOld(0) 
 {
   //
   // Standard constructor for Time Projection Chamber version 2
   //
 
-  SetSens(-1); // no strips selected
-
 
   SetBufferSize(128000);
 
 
-  if (fTPCParam)
-     fTPCParam->Write(fTPCParam->GetTitle());
+//   if (fTPCParam)
+//      fTPCParam->Write(fTPCParam->GetTitle());
 }
  
 //_____________________________________________________________________________
@@ -105,33 +101,37 @@ void AliTPCv2::CreateGeometry()
   // here I define a volume TPC
   // retrive the medium name with "TPC_" as a leading string
   //
-  TGeoPcon *tpc = new TGeoPcon(0.,360.,18); //18 sections
-  tpc->DefineSection(0,-290.,77.,278.);
-  tpc->DefineSection(1,-259.6,70.,278.);
+  TGeoPcon *tpc = new TGeoPcon(0.,360.,20); //20 sections
+  //
+  tpc->DefineSection(0,-291.,77.,278.);
+  tpc->DefineSection(1,-270,77.,278.);
   //
-  tpc->DefineSection(2,-259.6,68.1,278.);
-  tpc->DefineSection(3,-253.6,68.1,278.);
+  tpc->DefineSection(2,-270.,77.,278.);
+  tpc->DefineSection(3,-259.6,70.,278.);
   //
-  tpc->DefineSection(4,-253.6,68.,278.);
-  tpc->DefineSection(5,-74.0,60.8,278.);
+  tpc->DefineSection(4,-259.6,68.1,278.);
+  tpc->DefineSection(5,-253.6,68.1,278.);
   //
-  tpc->DefineSection(6,-74.0,60.1,278.);
-  tpc->DefineSection(7,-73.3,60.1,278.);
+  tpc->DefineSection(6,-253.6,68.,278.);
+  tpc->DefineSection(7,-74.0,60.8,278.);
   //
-  tpc->DefineSection(8,-73.3,56.9,278.); 
-  tpc->DefineSection(9,73.3,56.9,278.);
+  tpc->DefineSection(8,-74.0,60.1,278.);
+  tpc->DefineSection(9,-73.3,60.1,278.);
   //
-  tpc->DefineSection(10,73.3,60.1,278.);
-  tpc->DefineSection(11,74.0,60.1,278.);
+  tpc->DefineSection(10,-73.3,56.9,278.); 
+  tpc->DefineSection(11,73.3,56.9,278.);
   //
-  tpc->DefineSection(12,74.0,60.8,278.);
-  tpc->DefineSection(13,253.6,65.5,278.);
+  tpc->DefineSection(12,73.3,60.1,278.);
+  tpc->DefineSection(13,74.0,60.1,278.);
   //
-  tpc->DefineSection(14,253.6,65.6,278.);
-  tpc->DefineSection(15,259.6,65.6,278.);
+  tpc->DefineSection(14,74.0,60.8,278.);
+  tpc->DefineSection(15,253.6,65.5,278.);
   //
-  tpc->DefineSection(16,259.6,70.0,278.);
-  tpc->DefineSection(17,290.,77.,278.);
+  tpc->DefineSection(16,253.6,65.6,278.);
+  tpc->DefineSection(17,259.6,65.6,278.);
+  //
+  tpc->DefineSection(18,259.6,70.0,278.);
+  tpc->DefineSection(19,291.,77.,278.);
   //
   TGeoMedium *m1 = gGeoManager->GetMedium("TPC_Air");
   TGeoVolume *v1 = new TGeoVolume("TPC_M",tpc,m1);
@@ -393,8 +393,8 @@ void AliTPCv2::CreateGeometry()
   TGeoTube *cs = new TGeoTube(56.9,61.2,0.1);
   TGeoMedium *sm7 = gGeoManager->GetMedium("TPC_Mylar");
   TGeoVolume *csv = new TGeoVolume("TPC_CDRS",cs,sm7);
-  v1->AddNode(csv,1,new TGeoTranslation(0.,0.,-71.));
-  v1->AddNode(csv,2,new TGeoTranslation(0.,0.,71.));
+  v1->AddNode(csv,1,new TGeoTranslation(0.,0.,-71.2));
+  v1->AddNode(csv,2,new TGeoTranslation(0.,0.,71.2));
   //
   // seal collars 
   TGeoPcon *se = new TGeoPcon(0.,360.,6);
@@ -414,12 +414,12 @@ void AliTPCv2::CreateGeometry()
   //
   // define reflection matrix 
   //
-  TGeoRotation *ref = new TGeoRotation("ref",90.,0.,90.,270.,180.,0.);
+  TGeoRotation *ref = new TGeoRotation("ref",90.,0.,90.,90.,180.,0.);
   //
   cd1v->AddNode(cd2v,1); cd2v->AddNode(cd3v,1); cflv->AddNode(cd1v,1);
   //
-  v1->AddNode(siv,1,new TGeoTranslation(0.,0.,-72.1));
-  v1->AddNode(siv,2,new TGeoTranslation(0.,0.,72.1));
+  v1->AddNode(siv,1,new TGeoTranslation(0.,0.,-69.9));
+  v1->AddNode(siv,2,new TGeoTranslation(0.,0.,69.9));
   v1->AddNode(sev,1); v1->AddNode(sev,2,ref); v1->AddNode(cflv,1);
   //
   // central membrane - 2 rings and a mylar membrane - assembly
@@ -440,25 +440,25 @@ void AliTPCv2::CreateGeometry()
   // end caps - they are make as an assembly of single segments
   // containing both readout chambers
   //
-  Double_t OpeningAngle = 10.*TMath::DegToRad();
+  Double_t openingAngle = 10.*TMath::DegToRad();
   Double_t thick=1.5; // rib
-  Double_t shift = thick/TMath::Sin(OpeningAngle);
+  Double_t shift = thick/TMath::Sin(openingAngle);
   //
-  Double_t LowEdge = 86.3; // hole in the wheel
-  Double_t UpEdge = 240.4; // hole in the wheel
+  Double_t lowEdge = 86.3; // hole in the wheel
+  Double_t upEdge = 240.4; // hole in the wheel
   //
   new TGeoTubeSeg("sec",74.5,264.4,3.,0.,20.);
   //
   TGeoPgon *hole = new TGeoPgon("hole",0.,20.,1,4);
   //
-  hole->DefineSection(0,-3.5,LowEdge-shift,UpEdge-shift);
-  hole->DefineSection(1,-1.5,LowEdge-shift,UpEdge-shift);
+  hole->DefineSection(0,-3.5,lowEdge-shift,upEdge-shift);
+  hole->DefineSection(1,-1.5,lowEdge-shift,upEdge-shift);
   //
-  hole->DefineSection(2,-1.5,LowEdge-shift,UpEdge+3.-shift);
-  hole->DefineSection(3,3.5,LowEdge-shift,UpEdge+3.-shift);
+  hole->DefineSection(2,-1.5,lowEdge-shift,upEdge+3.-shift);
+  hole->DefineSection(3,3.5,lowEdge-shift,upEdge+3.-shift);
   //
-  Double_t ys = shift*TMath::Sin(OpeningAngle); 
-  Double_t xs = shift*TMath::Cos(OpeningAngle);
+  Double_t ys = shift*TMath::Sin(openingAngle); 
+  Double_t xs = shift*TMath::Cos(openingAngle);
   TGeoTranslation *tr = new TGeoTranslation("tr",xs,ys,0.);  
   tr->RegisterYourself();
   TGeoCompositeShape *chamber = new TGeoCompositeShape("sec-hole:tr");
@@ -676,7 +676,8 @@ void AliTPCv2::CreateGeometry()
    // 
    // now iroc and oroc are placed into a sector...
    //
-   TGeoVolumeAssembly *sect = new TGeoVolumeAssembly("TPC_SECT");
+   TGeoVolumeAssembly *secta = new TGeoVolumeAssembly("TPC_SECT"); // a-side
+   TGeoVolumeAssembly *sectc = new TGeoVolumeAssembly("TPC_SECT"); // c-side
    TGeoRotation rot1("rot1",90.,90.,0.);
    TGeoRotation rot2("rot2");
    rot2.RotateY(10.);
@@ -684,34 +685,46 @@ void AliTPCv2::CreateGeometry()
    *rot=rot1*rot2;
    //
    Double_t x0,y0;
-   x0=110.2*TMath::Cos(OpeningAngle);
-   y0=110.2*TMath::Sin(OpeningAngle);
-   TGeoCombiTrans *combi1 = new TGeoCombiTrans("combi1",x0,y0,1.09,rot);
-   x0=188.45*TMath::Cos(OpeningAngle);
-   y0=188.45*TMath::Sin(OpeningAngle);
-   TGeoCombiTrans *combi2 = new TGeoCombiTrans("combi2",x0,y0,0.99,rot);
+   x0=110.2*TMath::Cos(openingAngle);
+   y0=110.2*TMath::Sin(openingAngle);
+   TGeoCombiTrans *combi1a = new TGeoCombiTrans("combi1",x0,y0,1.09+0.195,rot); //a-side 
+   TGeoCombiTrans *combi1c = new TGeoCombiTrans("combi1",x0,y0,1.09+0.222,rot); //c-side
+   x0=188.45*TMath::Cos(openingAngle);
+   y0=188.45*TMath::Sin(openingAngle);
+   TGeoCombiTrans *combi2a = new TGeoCombiTrans("combi2",x0,y0,0.99+0.195,rot); //a-side
+   TGeoCombiTrans *combi2c = new TGeoCombiTrans("combi2",x0,y0,0.99+0.222,rot); //c-side
+   //
+   //
+   // A-side
    //
-   sect->AddNode(ch,1);
-   sect->AddNode(iroc,1,combi1);
-   sect->AddNode(oroc,1,combi2);
+   secta->AddNode(ch,1);
+   secta->AddNode(iroc,1,combi1a);
+   secta->AddNode(oroc,1,combi2a);
    //
-   // segment is ready...
-   // now I try to make a wheel...
+   // C-side
    //
-   TGeoVolumeAssembly *wheel = new TGeoVolumeAssembly("TPC_ENDCAP");
+   sectc->AddNode(ch,1);
+   sectc->AddNode(iroc,1,combi1c);
+   sectc->AddNode(oroc,1,combi2c);
+   //
+   // now I try to make  wheels...
+   //
+   TGeoVolumeAssembly *wheela = new TGeoVolumeAssembly("TPC_ENDCAP");
+   TGeoVolumeAssembly *wheelc = new TGeoVolumeAssembly("TPC_ENDCAP");
    //
    for(Int_t i =0;i<18;i++){
      Double_t phi = (20.*i);
      TGeoRotation *r = new TGeoRotation();
      r->RotateZ(phi);
-     wheel->AddNode(sect,i+1,r);
+     wheela->AddNode(secta,i+1,r);
+     wheelc->AddNode(sectc,i+1,r); 
     
    }
-   // wheels in the drift volume!
-   v9->AddNode(wheel,1,new TGeoTranslation(0.,0.,-256.6));
+   // wheels in the drift volume!   
+
    TGeoCombiTrans *combi3 = new TGeoCombiTrans("combi3",0.,0.,256.6,ref);
-   v9->AddNode(wheel,2,combi3);
-   
+   v9->AddNode(wheela,1,combi3);
+   v9->AddNode(wheelc,2,new TGeoTranslation(0.,0.,-256.6));
    //_____________________________________________________________
    // service support wheel
    //_____________________________________________________________
@@ -721,14 +734,23 @@ void AliTPCv2::CreateGeometry()
   TGeoVolume *swv = new TGeoVolume("TPC_SWSEG",sw,m3); //Al
   //
   thick=1.;
-  shift = thick/TMath::Sin(OpeningAngle);
+  shift = thick/TMath::Sin(openingAngle);
   TGeoPgon *sh = new TGeoPgon(0.,20.,1,2);
   sh->DefineSection(0,-4.,81.5-shift,250.75-shift);
   sh->DefineSection(1,4.,81.5-shift,250.75-shift);
   TGeoVolume *shv = new TGeoVolume("TPC_SWS1",sh,m1); //Air
   //
-  ys = shift*TMath::Sin(OpeningAngle);
-  xs = shift*TMath::Cos(OpeningAngle);
+  TGeoMedium *m9 =  gGeoManager->GetMedium("TPC_Si"); 
+  TGeoPgon *el = new TGeoPgon(0.,20.,1,2);
+  el->DefineSection(0,-1.872,81.5-shift,250.75-shift);
+  el->DefineSection(1,1.872,81.5-shift,250.75-shift);
+  TGeoVolume *elv = new TGeoVolume("TPC_ELEC",el,m9); //Si 
+  //
+  shv->AddNode(elv,1);
+  //
+  //
+  ys = shift*TMath::Sin(openingAngle);
+  xs = shift*TMath::Cos(openingAngle);
   swv->AddNode(shv,1,new TGeoTranslation(xs,ys,0.));
   // cover
   TGeoPgon *co = new TGeoPgon(0.,20.,1,2);
@@ -737,14 +759,14 @@ void AliTPCv2::CreateGeometry()
   TGeoVolume *cov = new TGeoVolume("TPC_SWC1",co,m3);//Al
   // hole in a cover
   TGeoPgon *coh = new TGeoPgon(0.,20.,1,2);
-  shift=4./TMath::Sin(OpeningAngle);
+  shift=4./TMath::Sin(openingAngle);
   coh->DefineSection(0,-0.5,85.-shift,247.25-shift);
   coh->DefineSection(1,0.5,85.-shift,247.25-shift);  
   //
   TGeoVolume *cohv = new TGeoVolume("TPC_SWC2",coh,m1);
   //
-  ys = shift*TMath::Sin(OpeningAngle);
-  xs = shift*TMath::Cos(OpeningAngle);  
+  ys = shift*TMath::Sin(openingAngle);
+  xs = shift*TMath::Cos(openingAngle);  
   cov->AddNode(cohv,1,new TGeoTranslation(xs,ys,0.));
   //
   // Sector as an Assembly
@@ -826,6 +848,9 @@ void AliTPCv2::CreateGeometry()
   //----------------------------------------------------------
   TGeoMedium *m6=gGeoManager->GetMedium("TPC_Makrolon");
   TGeoMedium *m7=gGeoManager->GetMedium("TPC_Cu");
+  TGeoMedium *m10 =  gGeoManager->GetMedium("TPC_Alumina");
+  TGeoMedium *m11 =  gGeoManager->GetMedium("TPC_Peek");
+  TGeoMedium *m12 =  gGeoManager->GetMedium("TPC_Water");
   // upper and lower rods differ in length!
   delete [] upar;
   upar=NULL;
@@ -836,14 +861,62 @@ void AliTPCv2::CreateGeometry()
   
   //
   //HV rods - makrolon + 0.58cm (diameter) Cu
-  TGeoTube *hvr = new TGeoTube(0.,4.4,126.64);
+  TGeoTube *hvr = new TGeoTube(0.,2.2,126.64);
   TGeoTube *hvc = new TGeoTube(0.,0.29,126.64);
   //
   TGeoVolume *hvrv = new TGeoVolume("TPC_HV_Rod",hvr,m6);
   TGeoVolume *hvcv = new TGeoVolume("TPC_HV_Cable",hvc,m7);
   hvrv->AddNode(hvcv,1);
-  
-  for(Int_t i=0;i<17;i++){
+  //
+  // resistor rods
+  //
+  TGeoTube *cri = new TGeoTube(0.,0.45,126.64); //inner 
+  TGeoTube *cro = new TGeoTube(0.,0.45,126.54); //outer 
+  TGeoTube *cwi = new TGeoTube(0.,0.15,126.64); // water inner
+  TGeoTube *cwo = new TGeoTube(0.,0.15,126.54); // water outer
+  //
+  TGeoVolume *criv = new TGeoVolume("TPC_CR_I",cri,m10);
+  TGeoVolume *crov = new TGeoVolume("TPC_CR_O",cro,m10);    
+  TGeoVolume *cwiv = new TGeoVolume("TPC_W_I",cwi,m11);
+  TGeoVolume *cwov = new TGeoVolume("TPC_W_O",cwo,m11);   
+  //
+  // ceramic rod with water
+  //
+  criv->AddNode(cwiv,1); 
+  crov->AddNode(cwov,1);
+  //
+  TGeoTube *pri =new TGeoTube(0.2,0.35,126.64); //inner 
+  TGeoTube *pro = new TGeoTube(0.2,0.35,126.54); //outer    
+  //
+  // peek rod
+  //
+  TGeoVolume *priv = new TGeoVolume("TPC_PR_I",pri,m12);
+  TGeoVolume *prov = new TGeoVolume("TPC_PR_O",pro,m12); 
+  //
+  // resistor rods assembly
+  //
+   TGeoRotation* rotr = new TGeoRotation("rotr");
+   rotr->RotateZ(-21.);
+  //
+  TGeoTube *rri = new TGeoTube(1.8,2.2,126.64);//inner
+  TGeoTube *rro = new TGeoTube(1.8,2.2,126.54);//inner
+  //
+  TGeoVolume *rriv = new TGeoVolume("TPC_RR_I",rri,m6);
+  TGeoVolume *rrov = new TGeoVolume("TPC_RR_O",rro,m6);  
+  //
+  TGeoVolumeAssembly *rrin = new TGeoVolumeAssembly("TPC_RROD_I");
+  TGeoVolumeAssembly *rrou = new TGeoVolumeAssembly("TPC_RROD_O");
+  rrin->AddNode(rriv,1);
+  rrin->AddNode(criv,1,new TGeoTranslation(0.5,0.866, 0.));
+  rrin->AddNode(criv,2,new TGeoTranslation(0.5,-0.866, 0.)); 
+  rrin->AddNode(priv,1); 
+  //
+  rrou->AddNode(rrov,1);
+  rrou->AddNode(crov,1,new TGeoTranslation(0.5,0.866, 0.));
+  rrou->AddNode(crov,2,new TGeoTranslation(0.5,-0.866, 0.)); 
+  rrou->AddNode(prov,1); 
+  //
+  for(Int_t i=0;i<18;i++){
     Double_t angle,x,y;
     Double_t z,r; 
     angle=TMath::DegToRad()*20.*(Double_t)i;
@@ -852,31 +925,155 @@ void AliTPCv2::CreateGeometry()
     y=r * TMath::Sin(angle);
     upar[2]=126.64; //lower
     z= 126.96;
-    if(i==15){
-      v9->AddNode(hvrv,1,new TGeoTranslation(x,y,z));
-      v9->AddNode(hvrv,2,new TGeoTranslation(x,y,-z));
-    }
-    else{
-     gGeoManager->Node("TPC_Rod",i+1,"TPC_Drift",x,y,z,0,kTRUE,upar,3);//shaft
-     gGeoManager->Node("TPC_Rod",i+18,"TPC_Drift",x,y,-z,0,kTRUE,upar,3);//muon
+    //
+    if(i==3){
+       v9->AddNode(rrin,1,new TGeoCombiTrans(x,y, z, rotr)); //A
+       v9->AddNode(rrin,2,new TGeoCombiTrans(x,y,-z, rotr)); //C      
+    } 
+    else { 
+      gGeoManager->Node("TPC_Rod",i+1,"TPC_Drift",x,y,z,0,kTRUE,upar,3);//shaft
+      gGeoManager->Node("TPC_Rod",i+19,"TPC_Drift",x,y,-z,0,kTRUE,upar,3);//muon
     }
+   
+
+    //
     r=254.25;
     x=r * TMath::Cos(angle);
     y=r * TMath::Sin(angle);
     upar[2]=126.54; //upper
     z=127.06;
-    gGeoManager->Node("TPC_Rod",i+36,"TPC_Drift",x,y,z,0,kTRUE,upar,3);
-    gGeoManager->Node("TPC_Rod",i+54,"TPC_Drift",x,y,-z,0,kTRUE,upar,3);
+    //
+    if(i==15){
+      //v9->AddNode(hvrv,1,new TGeoTranslation(x,y,z));//A-side only
+        v9->AddNode(hvrv,1,new TGeoTranslation(x,y,127.14));//A-side only
+       gGeoManager->Node("TPC_Rod",i+55,"TPC_Drift",x,y,-z,0,kTRUE,upar,3);
+    }
+    else if(i==11){
+       v9->AddNode(rrou,1,new TGeoCombiTrans(x,y, z, rotr));  //A
+       v9->AddNode(rrou,2,new TGeoCombiTrans(x,y,-z, rotr));  //C
+    }
+    else{
+    //
+      gGeoManager->Node("TPC_Rod",i+37,"TPC_Drift",x,y,z,0,kTRUE,upar,3);
+      gGeoManager->Node("TPC_Rod",i+55,"TPC_Drift",x,y,-z,0,kTRUE,upar,3);
+    }
   }
 
+  delete [] upar;
+
   TGeoVolume *alice = gGeoManager->GetVolume("ALIC");
   alice->AddNode(v1,1);  
   
-
 } // end of function
  
 //_____________________________________________________________________________
-void AliTPCv2::DrawDetector()
+void AliTPCv2::AddAlignableVolumes() const
+{
+  //
+  // Create entries for alignable volumes associating the symbolic volume
+  // name with the corresponding volume path. Needs to be syncronized with
+  // eventual changes in the geometry.
+  // 
+  SetInnerChambersAlignable();
+  SetOuterChambersAlignable();
+}
+//_____________________________________________________________________________
+void AliTPCv2::SetInnerChambersAlignable() const
+{
+  //
+  AliGeomManager::ELayerID idTPC1 = AliGeomManager::kTPC1;
+  Int_t modUID, modnum = 0;
+  TString vpstr1 = "ALIC_1/TPC_M_1/TPC_Drift_1/TPC_ENDCAP_1/TPC_SECT_";
+  TString vpstr2 = "ALIC_1/TPC_M_1/TPC_Drift_1/TPC_ENDCAP_2/TPC_SECT_";
+  TString vpappend = "/TPC_IROC_1";
+  TString snstr1="TPC/EndcapA/Sector";
+  TString snstr2="TPC/EndcapC/Sector";
+  TString snappend="/InnerChamber";
+  TString volpath, symname;
+  
+  for(Int_t cnt=1; cnt<=18; cnt++){
+    modUID = AliGeomManager::LayerToVolUID(idTPC1,modnum++);
+    volpath = vpstr1;
+    volpath += cnt;
+    volpath += vpappend;
+    symname = snstr1;
+    symname += cnt;
+    symname += snappend;
+    if(!gGeoManager->SetAlignableEntry(symname.Data(),volpath.Data(),modUID))
+      AliFatal(Form("Alignable entry %s not created. Volume path %s not valid", symname.Data(),volpath.Data()));
+    TGeoPNEntry *alignableEntry = gGeoManager->GetAlignableEntryByUID(modUID);
+    TGeoHMatrix* globMatrix = alignableEntry->GetGlobalOrig();
+    TGeoHMatrix* matTtoL = fTPCParam->Tracking2LocalMatrix(globMatrix,cnt-1);
+    alignableEntry->SetMatrix(matTtoL);
+  }
+
+  for(Int_t cnt=1; cnt<=18; cnt++){
+    modUID = AliGeomManager::LayerToVolUID(idTPC1,modnum++);
+    volpath = vpstr2;
+    volpath += cnt;
+    volpath += vpappend;
+    symname = snstr2;
+    symname += cnt;
+    symname += snappend;
+    if(!gGeoManager->SetAlignableEntry(symname.Data(),volpath.Data(),modUID))
+      AliFatal(Form("Alignable entry %s not created. Volume path %s not valid", symname.Data(),volpath.Data()));
+    TGeoPNEntry *alignableEntry = gGeoManager->GetAlignableEntryByUID(modUID);
+    TGeoHMatrix* globMatrix = alignableEntry->GetGlobalOrig();
+    TGeoHMatrix* matTtoL = fTPCParam->Tracking2LocalMatrix(globMatrix,18+cnt-1);
+    alignableEntry->SetMatrix(matTtoL);
+  }
+}
+
+//_____________________________________________________________________________
+void AliTPCv2::SetOuterChambersAlignable() const
+{
+  //
+  AliGeomManager::ELayerID idTPC2 = AliGeomManager::kTPC2;
+  Int_t modUID, modnum = 0;
+  TString vpstr1 = "ALIC_1/TPC_M_1/TPC_Drift_1/TPC_ENDCAP_1/TPC_SECT_";
+  TString vpstr2 = "ALIC_1/TPC_M_1/TPC_Drift_1/TPC_ENDCAP_2/TPC_SECT_";
+  TString vpappend = "/TPC_OROC_1";
+  TString snstr1="TPC/EndcapA/Sector";
+  TString snstr2="TPC/EndcapC/Sector";
+  TString snappend="/OuterChamber";
+  TString volpath, symname;
+  
+  for(Int_t cnt=1; cnt<=18; cnt++){
+    modUID = AliGeomManager::LayerToVolUID(idTPC2,modnum++);
+    volpath = vpstr1;
+    volpath += cnt;
+    volpath += vpappend;
+    symname = snstr1;
+    symname += cnt;
+    symname += snappend;
+    if(!gGeoManager->SetAlignableEntry(symname.Data(),volpath.Data(),modUID))
+      AliFatal(Form("Alignable entry %s not created. Volume path %s not valid", symname.Data(),volpath.Data()));
+    TGeoPNEntry *alignableEntry = gGeoManager->GetAlignableEntryByUID(modUID);
+    TGeoHMatrix* globMatrix = alignableEntry->GetGlobalOrig();
+    TGeoHMatrix* matTtoL = fTPCParam->Tracking2LocalMatrix(globMatrix,36+cnt-1);
+    alignableEntry->SetMatrix(matTtoL);
+  }
+
+  for(Int_t cnt=1; cnt<=18; cnt++){
+    modUID = AliGeomManager::LayerToVolUID(idTPC2,modnum++);
+    volpath = vpstr2;
+    volpath += cnt;
+    volpath += vpappend;
+    symname = snstr2;
+    symname += cnt;
+    symname += snappend;
+    if(!gGeoManager->SetAlignableEntry(symname.Data(),volpath.Data(),modUID))
+      AliFatal(Form("Alignable entry %s not created. Volume path %s not valid", symname.Data(),volpath.Data()));
+    TGeoPNEntry *alignableEntry = gGeoManager->GetAlignableEntryByUID(modUID);
+    TGeoHMatrix* globMatrix = alignableEntry->GetGlobalOrig();
+    TGeoHMatrix* matTtoL = fTPCParam->Tracking2LocalMatrix(globMatrix,36+18+cnt-1);
+    alignableEntry->SetMatrix(matTtoL);
+  }
+}
+
+//_____________________________________________________________________________
+void AliTPCv2::DrawDetector() const
 {
   //
   // Draw a shaded view of the Time Projection Chamber version 1
@@ -1010,9 +1207,18 @@ void AliTPCv2::Init()
   fIDrift=gMC->VolId("TPC_Drift");
   fSecOld=-100; // fake number 
 
-  gMC->SetMaxNStep(30000); // max. number of steps increased
+  gMC->SetMaxNStep(-30000); // max. number of steps increased
+
+  if (fPrimaryIonisation) {
+      gMC->Gstpar(idtmed[2],"PRIMIO_E", 20.77); // 1st ionisation potential
+      gMC->Gstpar(idtmed[2],"PRIMIO_N", 14.35);
+      gMC->Gstpar(idtmed[2],"LOSS", 14); // specific energy loss
+      gMC->Gstpar(idtmed[2],"STRA",4);
+  } else {
+      gMC->Gstpar(idtmed[2],"LOSS", 5); // specific energy loss
+  }
 
-  gMC->Gstpar(idtmed[2],"LOSS",5); // specific energy loss
 
   AliInfo("*** TPC version 2 initialized ***");
   AliInfo(Form("Maximum number of steps = %d",gMC->GetMaxNStep()));
@@ -1034,7 +1240,7 @@ void AliTPCv2::StepManager()
   const Float_t kprim = 14.35; // number of primary collisions per 1 cm
   const Float_t kpoti = 20.77e-9; // first ionization potential for Ne/CO2
   const Float_t kwIon = 35.97e-9; // energy for the ion-electron pair creation 
+  const Int_t   kMaxDistRef =15;     // maximal difference between 2 stored references 
  
   const Float_t kbig = 1.e10;
 
@@ -1045,7 +1251,7 @@ void AliTPCv2::StepManager()
   
   vol[1]=0; // preset row number to 0
   //
-  gMC->SetMaxStep(kbig);
+  if (fPrimaryIonisation) gMC->SetMaxStep(kbig);
   
   if(!gMC->IsTrackAlive()) return; // particle has disappeared
   
@@ -1075,8 +1281,8 @@ void AliTPCv2::StepManager()
   fTPCParam->AdjustCosSin(sector,cos,sin);
   Float_t x1=p[0]*cos + p[1]*sin;
   // check if within sector's limits
-  if(x1>=fTPCParam->GetInnerRadiusLow()&&x1<=fTPCParam->GetInnerRadiusUp()
-     ||x1>=fTPCParam->GetOuterRadiusLow()&&x1<=fTPCParam->GetOuterRadiusUp()){
+  if((x1>=fTPCParam->GetInnerRadiusLow()&&x1<=fTPCParam->GetInnerRadiusUp())
+     ||(x1>=fTPCParam->GetOuterRadiusLow()&&x1<=fTPCParam->GetOuterRadiusUp())){
   // calculate real sector number...
   if (x1>fTPCParam->GetOuterRadiusLow()){
     sector = TMath::Nint((angle-fTPCParam->GetOuterAngleShift())/
@@ -1090,11 +1296,18 @@ void AliTPCv2::StepManager()
   //
 
   vol[0]=sector;
+
+  static Double_t lastReferenceR=0;
+  if (TMath::Abs(lastReferenceR-r)>kMaxDistRef){
+    AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTPC);
+    lastReferenceR = r;
+  }
+
   // check if change of sector
   if(sector != fSecOld){
     fSecOld=sector;
     // add track reference
-    AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
+    AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTPC);
   }  
   // track is in the sensitive strip
   if(id == fIdSens){
@@ -1140,8 +1353,14 @@ void AliTPCv2::StepManager()
   //  charged particle is in the sensitive drift volume
   //-----------------------------------------------------------------
   if(gMC->TrackStep() > 0) {
-
-    Int_t nel = (Int_t)(((gMC->Edep())-kpoti)/kwIon) + 1;
+    Int_t nel=0;
+    if (!fPrimaryIonisation) {
+      nel = (Int_t)(((gMC->Edep())-kpoti)/kwIon) + 1;
+    }
+    else {
+         Float_t edep = gMC->Edep();
+         if (edep > 0.) nel = (Int_t)((gMC->Edep()*1.5)/kwIon) + 1;      
+    }
     nel=TMath::Min(nel,300); // 300 electrons corresponds to 10 keV
     //
     gMC->TrackPosition(p);
@@ -1170,53 +1389,30 @@ void AliTPCv2::StepManager()
   
   Float_t pp;
   TLorentzVector mom;
-  gMC->TrackMomentum(mom);
-  Float_t ptot=mom.Rho();
-  Float_t betaGamma = ptot/gMC->TrackMass();
-  
-  Int_t pid=gMC->TrackPid();
-  if((pid==kElectron || pid==kPositron) && ptot > 0.002)
-    { 
-      pp = kprim*1.58; // electrons above 20 MeV/c are on the plateau!
-    }
-  else
-    {
+  // below is valid only for Geant3 (fPromaryIonisation not set)
+  if(!fPrimaryIonisation){
+    gMC->TrackMomentum(mom);
+    Float_t ptot=mom.Rho();
+    Float_t betaGamma = ptot/gMC->TrackMass();
 
-      betaGamma = TMath::Max(betaGamma,(Float_t)7.e-3); // protection against too small bg
-      pp=kprim*BetheBloch(betaGamma); 
+    Int_t pid=gMC->TrackPid();
+    if((pid==kElectron || pid==kPositron) && ptot > 0.002)
+      { 
+        pp = kprim*1.58; // electrons above 20 MeV/c are on the plateau!
+      }
+    else
+      {
+
+        betaGamma = TMath::Max(betaGamma,(Float_t)7.e-3); // protection against too small bg
+        pp=kprim*AliMathBase::BetheBlochAleph(betaGamma); 
    
-      if(TMath::Abs(charge) > 1.) pp *= (charge*charge);
     }
   
-  Double_t rnd = gMC->GetRandom()->Rndm();
-  
-  gMC->SetMaxStep(-TMath::Log(rnd)/pp);
+    Double_t rnd = gMC->GetRandom()->Rndm();
   
+    gMC->SetMaxStep(-TMath::Log(rnd)/pp);
+  }
   
 }
 
-//_____________________________________________________________________________
-Float_t AliTPCv2::BetheBloch(Float_t bg)
-{
-  //
-  // Bethe-Bloch energy loss formula
-  //
-  const Double_t kp1=0.76176e-1;
-  const Double_t kp2=10.632;
-  const Double_t kp3=0.13279e-4;
-  const Double_t kp4=1.8631;
-  const Double_t kp5=1.9479;
-
-  Double_t dbg = (Double_t) bg;
-
-  Double_t beta = dbg/TMath::Sqrt(1.+dbg*dbg);
-
-  Double_t aa = TMath::Power(beta,kp4);
-  Double_t bb = TMath::Power(1./dbg,kp5);
-
-  bb=TMath::Log(kp3+bb);
-  
-  return ((Float_t)((kp2-aa-bb)*kp1/aa));
-}
-