]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PHOS/AliPHOSv0.cxx
minor bugs
[u/mrichter/AliRoot.git] / PHOS / AliPHOSv0.cxx
index b6666b4e6b37115e301dda00edc5b384f2967957..dbb1662b41a17d999251a1a7e678c617ff14e9f8 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+/* $Id$ */
+
 //_________________________________________________________________________
-// Manager class for PHOS version SUBATECH
-//*-- Author : Y. Schutz SUBATECH 
-//////////////////////////////////////////////////////////////////////////////
+// Implementation version v0 of PHOS Manager class 
+// Layout EMC + PPSD has name GPS2  
+//                  
+//*-- Author: Yves Schutz (SUBATECH)
+
 
 // --- ROOT system ---
 
 #include "TNode.h"
 #include "TRandom.h"
 
+
 // --- Standard library ---
 
-#include <cstdio>
-#include <cstring>
-#include <cstdlib>
-#include <strstream>
-#include <cassert>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <strstream.h>
 
 // --- AliRoot header files ---
 
@@ -46,6 +50,7 @@ ClassImp(AliPHOSv0)
 //____________________________________________________________________________
 AliPHOSv0::AliPHOSv0()
 {
+  // ctor
   fNTmpHits = 0 ; 
   fTmpHits  = 0 ; 
 }
@@ -54,7 +59,8 @@ AliPHOSv0::AliPHOSv0()
 AliPHOSv0::AliPHOSv0(const char *name, const char *title):
   AliPHOS(name,title)
 {
-  
+  // ctor : title is used to identify the layout
+  //        GPS2 = 5 modules (EMC + PPSD)   
   // We use 2 arrays of hits :
   //
   //   - fHits (the "normal" one), which retains the hits associated with
@@ -64,16 +70,22 @@ AliPHOSv0::AliPHOSv0(const char *name, const char *title):
   //   - fTmpHits, which retains all the hits of the current event. It 
   //     is used for the digitization part.
 
-  fPINElectronicNoise = 0.010 ;
+  fPinElectronicNoise = 0.010 ;
+  fDigitThreshold      = 1. ;   // 1 GeV 
 
-  fHits   = new TClonesArray("AliPHOSHit",100) ;
-  gAlice->AddHitList(fHits) ; 
+  // We do not want to save in TreeH the raw hits
+  // fHits   = new TClonesArray("AliPHOSHit",100) ;
+  // gAlice->AddHitList(fHits) ; 
 
-  fTmpHits= new TClonesArray("AliPHOSHit",100) ;
+  // But save the cumulated hits instead (need to create the branch myself)
+  // It is put in the Digit Tree because the TreeH is filled after each primary
+ // and the TreeD at the end of the event (branch is set in FinishEvent() ).
+  
+  fTmpHits= new TClonesArray("AliPHOSHit",1000) ;
 
   fNTmpHits = fNhits = 0 ;
 
-  fDigits = new TClonesArray("AliPHOSDigit",100) ;
+  fDigits = new TClonesArray("AliPHOSDigit",1000) ;
 
 
   fIshunt     =  1 ; // All hits are associated with primary particles
@@ -87,11 +99,13 @@ AliPHOSv0::AliPHOSv0(const char *name, const char *title):
   else
    cout << "AliPHOSv0 : PHOS geometry initialization failed !" << endl ;   
 }
+
 //____________________________________________________________________________
 AliPHOSv0::AliPHOSv0(AliPHOSReconstructioner * Reconstructioner, const char *name, const char *title):
   AliPHOS(name,title)
 {
-  
+  // ctor : title is used to identify the layout
+  //        GPS2 = 5 modules (EMC + PPSD)   
   // We use 2 arrays of hits :
   //
   //   - fHits (the "normal" one), which retains the hits associated with
@@ -100,10 +114,14 @@ AliPHOSv0::AliPHOSv0(AliPHOSReconstructioner * Reconstructioner, const char *nam
   //
   //   - fTmpHits, which retains all the hits of the current event. It 
   //     is used for the digitization part.
-  fPINElectronicNoise = 0.010 ;
-  fHits   = new TClonesArray("AliPHOSHit",100) ;
-  fDigits = new TClonesArray("AliPHOSDigit",100) ;
-  fTmpHits= new TClonesArray("AliPHOSHit",100) ;
+
+  fPinElectronicNoise = 0.010 ;
+
+  // We do not want to save in TreeH the raw hits
+  //fHits   = new TClonesArray("AliPHOSHit",100) ;
+
+  fDigits = new TClonesArray("AliPHOSDigit",1000) ;
+  fTmpHits= new TClonesArray("AliPHOSHit",1000) ;
 
   fNTmpHits = fNhits = 0 ;
 
@@ -125,60 +143,76 @@ AliPHOSv0::AliPHOSv0(AliPHOSReconstructioner * Reconstructioner, const char *nam
 //____________________________________________________________________________
 AliPHOSv0::~AliPHOSv0()
 {
-  fTmpHits->Delete() ; 
-  delete fTmpHits ;
-  fTmpHits = 0 ; 
+  // dtor
+
+  if ( fTmpHits) {
+    fTmpHits->Delete() ; 
+    delete fTmpHits ;
+    fTmpHits = 0 ; 
+  }
 
-  fEmcClusters->Delete() ; 
-  delete fEmcClusters ; 
-  fEmcClusters = 0 ; 
+  if ( fEmcRecPoints ) {
+    fEmcRecPoints->Delete() ; 
+    delete fEmcRecPoints ; 
+    fEmcRecPoints = 0 ; 
+  }
 
-  fPpsdClusters->Delete() ;
-  delete fPpsdClusters ;
-  fPpsdClusters = 0 ; 
+  if ( fPpsdRecPoints ) { 
+    fPpsdRecPoints->Delete() ;
+    delete fPpsdRecPoints ;
+    fPpsdRecPoints = 0 ; 
+  }
+  
+  if ( fTrackSegments ) {
+    fTrackSegments->Delete() ; 
+    delete fTrackSegments ;
+    fTrackSegments = 0 ; 
+  }
 
-  fTrackSegments->Delete() ; 
-  delete fTrackSegments ;
-  fTrackSegments = 0 ; 
 }
 
 //____________________________________________________________________________
 void AliPHOSv0::AddHit(Int_t primary, Int_t Id, Float_t * hits)
 {
+  // Add a hit to the hit list.
+  // A PHOS hit is the sum of all hits in a single crystal
+  //   or in a single PPSD gas cell
+
   Int_t hitCounter ;
   TClonesArray &ltmphits = *fTmpHits ;
   AliPHOSHit *newHit ;
   AliPHOSHit *curHit ;
+  //  AliPHOSHit *curHit2 ;
   Bool_t deja = kFALSE ;
 
   // In any case, fills the fTmpHit TClonesArray (with "accumulated hits")
 
   newHit = new AliPHOSHit(primary, Id, hits) ;
 
+  // We do not want to save in TreeH the raw hits 
+  //  TClonesArray &lhits = *fHits;
+
   for ( hitCounter = 0 ; hitCounter < fNTmpHits && !deja ; hitCounter++ ) {
     curHit = (AliPHOSHit*) ltmphits[hitCounter] ;
-    if( *curHit == *newHit ) {
-     *curHit = *curHit + *newHit ;
-      deja = kTRUE ;
+  if( *curHit == *newHit ) {
+    *curHit = *curHit + *newHit ;
+    deja = kTRUE ;
     }
   }
-       
+         
   if ( !deja ) {
     new(ltmphits[fNTmpHits]) AliPHOSHit(*newHit) ;
     fNTmpHits++ ;
   }
 
+  // We do not want to save in TreeH the raw hits 
+  //   new(lhits[fNhits]) AliPHOSHit(*newHit) ;    
+  //   fNhits++ ;
+
   // Please note that the fTmpHits array must survive up to the
   // end of the events, so it does not appear e.g. in ResetHits() (
   // which is called at the end of each primary).  
 
-  //  if (IsTreeSelected('H')) {
-    // And, if we really want raw hits tree, have the fHits array filled also
-  //    TClonesArray &lhits = *fHits;
-  //    new(lhits[fNhits]) AliPHOSHit(*newHit) ;
-  //    fNhits++ ;
-  //  }
-
   delete newHit;
 
 }
@@ -187,6 +221,36 @@ void AliPHOSv0::AddHit(Int_t primary, Int_t Id, Float_t * hits)
 //____________________________________________________________________________
 void AliPHOSv0::BuildGeometry()
 {
+  // Build the PHOS geometry for the ROOT display
+  //BEGIN_HTML
+  /*
+    <H2>
+     PHOS in ALICE displayed by root
+    </H2>
+    <UL>
+    <LI> All Views
+    <P>
+    <CENTER>
+    <IMG Align=BOTTOM ALT="All Views" SRC="../images/AliPHOSv0AllViews.gif"> 
+    </CENTER></P></LI>
+    <LI> Front View
+    <P>
+    <CENTER>
+    <IMG Align=BOTTOM ALT="Front View" SRC="../images/AliPHOSv0FrontView.gif"> 
+    </CENTER></P></LI>
+     <LI> 3D View 1
+    <P>
+    <CENTER>
+    <IMG Align=BOTTOM ALT="3D View 1" SRC="../images/AliPHOSv03DView1.gif"> 
+    </CENTER></P></LI>
+    <LI> 3D View 2
+    <P>
+    <CENTER>
+    <IMG Align=BOTTOM ALT="3D View 2" SRC="../images/AliPHOSv03DView2.gif"> 
+    </CENTER></P></LI>
+    </UL>
+  */
+  //END_HTML  
 
   this->BuildGeometryforPHOS() ; 
   if ( ( strcmp(fGeom->GetName(), "GPS2" ) == 0 ) ) 
@@ -199,7 +263,7 @@ void AliPHOSv0::BuildGeometry()
 //____________________________________________________________________________
 void AliPHOSv0:: BuildGeometryforPHOS(void)
 {
- // Build the PHOS geometry for the ROOT display
+ // Build the PHOS-EMC geometry for the ROOT display
 
   const Int_t kColorPHOS = kRed ;
   const Int_t kColorXTAL = kBlue ;
@@ -298,8 +362,26 @@ void AliPHOSv0:: BuildGeometryforPHOS(void)
 //____________________________________________________________________________
 void AliPHOSv0:: BuildGeometryforPPSD(void)
 {
- //  Build the PPSD geometry for the ROOT display
-
+ //  Build the PHOS-PPSD geometry for the ROOT display
+ //BEGIN_HTML
+  /*
+    <H2>
+     PPSD displayed by root
+    </H2>
+    <UL>
+    <LI> Zoom on PPSD: Front View
+    <P>
+    <CENTER>
+    <IMG Align=BOTTOM ALT="PPSD Front View" SRC="../images/AliPHOSv0PPSDFrontView.gif"> 
+    </CENTER></P></LI>
+    <LI> Zoom on PPSD: Perspective View
+    <P>
+    <CENTER>
+    <IMG Align=BOTTOM ALT="PPSD Prespective View" SRC="../images/AliPHOSv0PPSDPerspectiveView.gif"> 
+    </CENTER></P></LI>
+    </UL>
+  */
+  //END_HTML  
   Double_t const kRADDEG = 180.0 / kPI ;
 
   const Int_t kColorPHOS = kRed ;
@@ -393,65 +475,67 @@ void AliPHOSv0:: BuildGeometryforPPSD(void)
     // inside the PPSD box: 
     //   1.   fNumberOfModulesPhi x fNumberOfModulesZ top micromegas
     x = ( fGeom->GetPPSDBoxSize(0) - fGeom->GetPPSDModuleSize(0) ) / 2. ;  
-    for ( Int_t iphi = 1; iphi <= fGeom->GetNumberOfModulesPhi(); iphi++ ) { // the number of micromegas modules in phi per PHOS module
-      Float_t z = ( fGeom->GetPPSDBoxSize(2) - fGeom->GetPPSDModuleSize(2) ) / 2. ;
-      TNode * micro1node ; 
-      for ( Int_t iz = 1; iz <= fGeom->GetNumberOfModulesZ(); iz++ ) { // the number of micromegas modules in z per PHOS module
-       y = ( fGeom->GetPPSDBoxSize(1) - fGeom->GetMicromegas1Thickness() ) / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "Mic1", i, iphi, iz) ;
-       micro1node  = new TNode(nodename, nodename, "PPSDModule", x, y, z) ;
-       micro1node->SetLineColor(kColorPPSD) ;  
-       fNodes->Add(micro1node) ; 
-       // inside top micromegas
-       micro1node->cd() ; 
-       //      a. top lid
-       y = ( fGeom->GetMicromegas1Thickness() - fGeom->GetLidThickness() ) / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "Lid", i, iphi, iz) ;
-       TNode * toplidnode = new TNode(nodename, nodename, "TopLid", 0, y, 0) ;
-       toplidnode->SetLineColor(kColorPPSD) ;  
-       fNodes->Add(toplidnode) ; 
-       //      b. composite panel
-       y = y - fGeom->GetLidThickness() / 2. - fGeom->GetCompositeThickness() / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "CompU", i, iphi, iz) ;
-       TNode * compupnode = new TNode(nodename, nodename, "TopPanel", 0, y, 0) ;
-       compupnode->SetLineColor(kColorPPSD) ;  
-       fNodes->Add(compupnode) ; 
-       //      c. anode
-       y = y - fGeom->GetCompositeThickness() / 2. - fGeom->GetAnodeThickness()  / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "Ano", i, iphi, iz) ;
-       TNode * anodenode = new TNode(nodename, nodename, "Anode", 0, y, 0) ;
-       anodenode->SetLineColor(kColorPHOS) ;  
-       fNodes->Add(anodenode) ; 
-       //      d.  gas 
-       y = y - fGeom->GetAnodeThickness() / 2. - ( fGeom->GetConversionGap() +  fGeom->GetAvalancheGap() ) / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "GGap", i, iphi, iz) ;
-       TNode * ggapnode = new TNode(nodename, nodename, "GasGap", 0, y, 0) ;
-       ggapnode->SetLineColor(kColorGas) ;  
-       fNodes->Add(ggapnode) ;          
+    {
+      for ( Int_t iphi = 1; iphi <= fGeom->GetNumberOfModulesPhi(); iphi++ ) { // the number of micromegas modules in phi per PHOS module
+       Float_t z = ( fGeom->GetPPSDBoxSize(2) - fGeom->GetPPSDModuleSize(2) ) / 2. ;
+       TNode * micro1node ; 
+       for ( Int_t iz = 1; iz <= fGeom->GetNumberOfModulesZ(); iz++ ) { // the number of micromegas modules in z per PHOS module
+         y = ( fGeom->GetPPSDBoxSize(1) - fGeom->GetMicromegas1Thickness() ) / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "Mic1", i, iphi, iz) ;
+         micro1node  = new TNode(nodename, nodename, "PPSDModule", x, y, z) ;
+         micro1node->SetLineColor(kColorPPSD) ;  
+         fNodes->Add(micro1node) ; 
+         // inside top micromegas
+         micro1node->cd() ; 
+         //      a. top lid
+         y = ( fGeom->GetMicromegas1Thickness() - fGeom->GetLidThickness() ) / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "Lid", i, iphi, iz) ;
+         TNode * toplidnode = new TNode(nodename, nodename, "TopLid", 0, y, 0) ;
+         toplidnode->SetLineColor(kColorPPSD) ;  
+         fNodes->Add(toplidnode) ; 
+         //      b. composite panel
+         y = y - fGeom->GetLidThickness() / 2. - fGeom->GetCompositeThickness() / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "CompU", i, iphi, iz) ;
+         TNode * compupnode = new TNode(nodename, nodename, "TopPanel", 0, y, 0) ;
+         compupnode->SetLineColor(kColorPPSD) ;  
+         fNodes->Add(compupnode) ; 
+         //      c. anode
+         y = y - fGeom->GetCompositeThickness() / 2. - fGeom->GetAnodeThickness()  / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "Ano", i, iphi, iz) ;
+         TNode * anodenode = new TNode(nodename, nodename, "Anode", 0, y, 0) ;
+         anodenode->SetLineColor(kColorPHOS) ;  
+         fNodes->Add(anodenode) ; 
+         //      d.  gas 
+         y = y - fGeom->GetAnodeThickness() / 2. - ( fGeom->GetConversionGap() +  fGeom->GetAvalancheGap() ) / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "GGap", i, iphi, iz) ;
+         TNode * ggapnode = new TNode(nodename, nodename, "GasGap", 0, y, 0) ;
+         ggapnode->SetLineColor(kColorGas) ;  
+         fNodes->Add(ggapnode) ;          
          //      f. cathode
-       y = y - ( fGeom->GetConversionGap() +  fGeom->GetAvalancheGap() ) / 2. - fGeom->GetCathodeThickness()  / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "Cathode", i, iphi, iz) ;
-       TNode * cathodenode = new TNode(nodename, nodename, "Cathode", 0, y, 0) ;
-       cathodenode->SetLineColor(kColorPHOS) ;  
-       fNodes->Add(cathodenode) ;        
-       //      g. printed circuit
-       y = y - fGeom->GetCathodeThickness() / 2. - fGeom->GetPCThickness()  / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "PC", i, iphi, iz) ;
-       TNode * pcnode = new TNode(nodename, nodename, "PCBoard", 0, y, 0) ;
-       pcnode->SetLineColor(kColorPPSD) ;  
-       fNodes->Add(pcnode) ;        
-       //      h. composite panel
-       y = y - fGeom->GetPCThickness() / 2. - fGeom->GetCompositeThickness()  / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "CompDown", i, iphi, iz) ;
-       TNode * compdownnode = new TNode(nodename, nodename, "BottomPanel", 0, y, 0) ;
-       compdownnode->SetLineColor(kColorPPSD) ;  
-       fNodes->Add(compdownnode) ;   
-       z = z - fGeom->GetPPSDModuleSize(2) ;
+         y = y - ( fGeom->GetConversionGap() +  fGeom->GetAvalancheGap() ) / 2. - fGeom->GetCathodeThickness()  / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "Cathode", i, iphi, iz) ;
+         TNode * cathodenode = new TNode(nodename, nodename, "Cathode", 0, y, 0) ;
+         cathodenode->SetLineColor(kColorPHOS) ;  
+         fNodes->Add(cathodenode) ;        
+         //      g. printed circuit
+         y = y - fGeom->GetCathodeThickness() / 2. - fGeom->GetPCThickness()  / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "PC", i, iphi, iz) ;
+         TNode * pcnode = new TNode(nodename, nodename, "PCBoard", 0, y, 0) ;
+         pcnode->SetLineColor(kColorPPSD) ;  
+         fNodes->Add(pcnode) ;        
+         //      h. composite panel
+         y = y - fGeom->GetPCThickness() / 2. - fGeom->GetCompositeThickness()  / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "CompDown", i, iphi, iz) ;
+         TNode * compdownnode = new TNode(nodename, nodename, "BottomPanel", 0, y, 0) ;
+         compdownnode->SetLineColor(kColorPPSD) ;  
+         fNodes->Add(compdownnode) ;   
+         z = z - fGeom->GetPPSDModuleSize(2) ;
+         ppsdboxnode->cd() ;
+       } // end of Z module loop     
+       x = x -  fGeom->GetPPSDModuleSize(0) ; 
        ppsdboxnode->cd() ;
-      } // end of Z module loop     
-      x = x -  fGeom->GetPPSDModuleSize(0) ; 
-      ppsdboxnode->cd() ;
-    } // end of phi module loop
+      } // end of phi module loop
+    }
     //   2. air gap      
     ppsdboxnode->cd() ;
     y = ( fGeom->GetPPSDBoxSize(1) - 2 * fGeom->GetMicromegas1Thickness() - fGeom->GetMicro1ToLeadGap() ) / 2. ; 
@@ -473,17 +557,18 @@ void AliPHOSv0:: BuildGeometryforPPSD(void)
     fNodes->Add(gapdownnode) ;        
     //    5.  fNumberOfModulesPhi x fNumberOfModulesZ bottom micromegas
     x = ( fGeom->GetPPSDBoxSize(0) - fGeom->GetPPSDModuleSize(0) ) / 2. - fGeom->GetPhiDisplacement() ;  
-    for ( Int_t iphi = 1; iphi <= fGeom->GetNumberOfModulesPhi(); iphi++ ) { 
-      Float_t z = ( fGeom->GetPPSDBoxSize(2) - fGeom->GetPPSDModuleSize(2) ) / 2.  - fGeom->GetZDisplacement() ;;
-      TNode * micro2node ; 
-      for ( Int_t iz = 1; iz <= fGeom->GetNumberOfModulesZ(); iz++ ) { 
-       y = - ( fGeom->GetPPSDBoxSize(1) - fGeom->GetMicromegas2Thickness() ) / 2. ; 
-       sprintf(nodename, "%s%d%d%d", "Mic2", i, iphi, iz) ;
-       micro2node  = new TNode(nodename, nodename, "PPSDModule", x, y, z) ;
-       micro2node->SetLineColor(kColorPPSD) ;  
-       fNodes->Add(micro2node) ; 
-       // inside bottom micromegas
-       micro2node->cd() ; 
+    {
+      for ( Int_t iphi = 1; iphi <= fGeom->GetNumberOfModulesPhi(); iphi++ ) { 
+       Float_t z = ( fGeom->GetPPSDBoxSize(2) - fGeom->GetPPSDModuleSize(2) ) / 2.  - fGeom->GetZDisplacement() ;;
+       TNode * micro2node ; 
+       for ( Int_t iz = 1; iz <= fGeom->GetNumberOfModulesZ(); iz++ ) { 
+         y = - ( fGeom->GetPPSDBoxSize(1) - fGeom->GetMicromegas2Thickness() ) / 2. ; 
+         sprintf(nodename, "%s%d%d%d", "Mic2", i, iphi, iz) ;
+         micro2node  = new TNode(nodename, nodename, "PPSDModule", x, y, z) ;
+         micro2node->SetLineColor(kColorPPSD) ;  
+         fNodes->Add(micro2node) ; 
+         // inside bottom micromegas
+         micro2node->cd() ; 
          //      a. top lid
          y = ( fGeom->GetMicromegas2Thickness() - fGeom->GetLidThickness() ) / 2. ; 
          sprintf(nodename, "%s%d", "Lidb", i) ;
@@ -531,15 +616,19 @@ void AliPHOSv0:: BuildGeometryforPPSD(void)
        } // end of Z module loop     
        x = x -  fGeom->GetPPSDModuleSize(0) ; 
        ppsdboxnode->cd() ;
-       } // end of phi module loop
-     } // PHOS modules
- delete rotname ; 
- delete nodename ; 
+      } // end of phi module loop
+    }
+  } // PHOS modules
+  delete[] rotname ;  
+  delete[] nodename ; 
+
 }
 
 //____________________________________________________________________________
 void AliPHOSv0::CreateGeometry()
 {
+  // Create the PHOS geometry for Geant
 
   AliPHOSv0 *phostmp = (AliPHOSv0*)gAlice->GetModule("PHOS") ;
 
@@ -549,7 +638,6 @@ void AliPHOSv0::CreateGeometry()
     return;
     
   }
-
   // Get pointer to the array containing media indeces
   Int_t *idtmed = fIdtmed->GetArray() - 699 ;
 
@@ -590,7 +678,19 @@ void AliPHOSv0::CreateGeometry()
 //____________________________________________________________________________
 void AliPHOSv0::CreateGeometryforPHOS()
 {
-  // Get pointer to the array containing media indeces
+  // Create the PHOS-EMC geometry for GEANT
+    //BEGIN_HTML
+  /*
+    <H2>
+    Geant3 geometry tree of PHOS-EMC in ALICE
+    </H2>
+    <P><CENTER>
+    <IMG Align=BOTTOM ALT="EMC geant tree" SRC="../images/EMCinAlice.gif"> 
+    </CENTER><P>
+  */
+  //END_HTML  
+  
+  // Get pointer to the array containing media indexes
   Int_t *idtmed = fIdtmed->GetArray() - 699 ;
 
   // ---
@@ -846,7 +946,20 @@ void AliPHOSv0::CreateGeometryforPHOS()
 //____________________________________________________________________________
 void AliPHOSv0::CreateGeometryforPPSD()
 {
-  // Get pointer to the array containing media indeces
+  // Create the PHOS-PPSD geometry for GEANT
+
+  //BEGIN_HTML
+  /*
+    <H2>
+    Geant3 geometry tree of PHOS-PPSD in ALICE
+    </H2>
+    <P><CENTER>
+    <IMG Align=BOTTOM ALT="PPSD geant tree" SRC="../images/PPSDinAlice.gif"> 
+    </CENTER><P>
+  */
+  //END_HTML  
+
+  // Get pointer to the array containing media indexes
   Int_t *idtmed = fIdtmed->GetArray() - 699 ;
   
   // The box containing all ppsd's for one PHOS module filled with air 
@@ -1033,17 +1146,28 @@ void AliPHOSv0::CreateGeometryforPPSD()
 }
 
 //___________________________________________________________________________
-Int_t AliPHOSv0::Digitize(Float_t Energy){
+Int_t AliPHOSv0::Digitize(Float_t Energy)
+{
+  // Applies the energy calibration
+  
   Float_t fB = 100000000. ;
   Float_t fA = 0. ;
   Int_t chan = Int_t(fA + Energy*fB ) ;
   return chan ;
 }
+
 //___________________________________________________________________________
 void AliPHOSv0::FinishEvent()
 {
-//    cout << "//_____________________________________________________" << endl ;
-//    cout << "<I> AliPHOSv0::FinishEvent() -- Starting digitalization" << endl ;
+  // Makes the digits from the sum of summed hit in a single crystal or PPSD gas cell
+  // Adds to the energy the electronic noise
+  // Keeps digits with energy above fDigitThreshold
+
+  // Save the cumulated hits instead of raw hits (need to create the branch myself)
+  // It is put in the Digit Tree because the TreeH is filled after each primary
+  // and the TreeD at the end of the event.
+  
+  
   Int_t i ;
   Int_t relid[4];
   Int_t j ; 
@@ -1052,10 +1176,11 @@ void AliPHOSv0::FinishEvent()
   AliPHOSDigit * newdigit ;
   AliPHOSDigit * curdigit ;
   Bool_t deja = kFALSE ; 
-
+  
   for ( i = 0 ; i < fNTmpHits ; i++ ) {
     hit = (AliPHOSHit*)fTmpHits->At(i) ;
     newdigit = new AliPHOSDigit( hit->GetPrimary(), hit->GetId(), Digitize( hit->GetEnergy() ) ) ;
+    deja =kFALSE ;
     for ( j = 0 ; j < fNdigits ;  j++) { 
       curdigit = (AliPHOSDigit*) lDigits[j] ;
       if ( *curdigit == *newdigit) {
@@ -1077,22 +1202,33 @@ void AliPHOSv0::FinishEvent()
   for ( i = 0 ; i < fNdigits ; i++ ) {
     newdigit =  (AliPHOSDigit * ) fDigits->At(i) ;
     fGeom->AbsToRelNumbering(newdigit->GetId(), relid) ;
+
     if (relid[1]==0){   // Digits belong to EMC (PbW0_4 crystals)
-      energyandnoise = newdigit->GetAmp() + Digitize(gRandom->Gaus(0., fPINElectronicNoise)) ;
+      energyandnoise = newdigit->GetAmp() + Digitize(gRandom->Gaus(0., fPinElectronicNoise)) ;
+
       if (energyandnoise < 0 ) 
        energyandnoise = 0 ;
-      newdigit->SetAmp(energyandnoise) ;
+
+      if ( newdigit->GetAmp() < fDigitThreshold ) // if threshold not surpassed, remove digit from list
+       fDigits->RemoveAt(i) ; 
     }
   }
+  
+  fDigits->Compress() ;  
 
-  fNTmpHits = 0 ;
-  fTmpHits->Delete();
+  fNdigits =  fDigits->GetEntries() ; 
+  for (i = 0 ; i < fNdigits ; i++) { 
+    newdigit = (AliPHOSDigit *) fDigits->At(i) ; 
+    newdigit->SetIndexInList(i) ; 
+  }
+  
 }
 
 //____________________________________________________________________________
 void AliPHOSv0::Init(void)
 {
+  // Just prints an information message
+  
   Int_t i;
 
   printf("\n");
@@ -1111,64 +1247,189 @@ void AliPHOSv0::Init(void)
 //___________________________________________________________________________
 void AliPHOSv0::MakeBranch(Option_t* opt)
 {  
-  //
-  // Create a new branch in the current Root Tree
-  // The branch of fHits is automatically split
-  //
+  // Create new branche in the current Root Tree in the digit Tree
+
   AliDetector::MakeBranch(opt) ;
   
   char branchname[10];
   sprintf(branchname,"%s",GetName());
-  char *cd = strstr(opt,"D");
-  
-  if (fDigits && gAlice->TreeD() && cd) {
-    gAlice->TreeD()->Branch(branchname,&fDigits, fBufferSize);
-    //    printf("* AliPHOS::MakeBranch * Making Branch %s for digits\n",branchname);
+  char *cdD = strstr(opt,"D");
+  if (fDigits && gAlice->TreeD() && cdD) {
+    gAlice->TreeD()->Branch(branchname, &fDigits, fBufferSize);
+  }
+
+  // Create new branche PHOSCH in the current Root Tree in the digit Tree for accumulated Hits
+  if ( ! (gAlice->IsLegoRun()) ) { // only when not in lego plot mode 
+    if ( fTmpHits && gAlice->TreeD()  && cdD) {
+      char branchname[10] ;
+      sprintf(branchname, "%sCH", GetName()) ;
+      gAlice->TreeD()->Branch(branchname, &fTmpHits, fBufferSize) ;
+    }   
   }
+
+}
+
+//____________________________________________________________________________
+AliPHOSRecPoint::RecPointsList * AliPHOSv0::PpsdRecPoints(Int_t evt) 
+{
+  // returns the pointer to the PPSD RecPoints list
+  // if the list is empty, get it from TreeR on the disk file
+
+  AliPHOSRecPoint::RecPointsList * rv = 0 ; 
+
+  if ( fPpsdRecPoints ) 
+    rv = fPpsdRecPoints ; 
+
+  else {
+    fPpsdRecPoints = new TClonesArray("AliPHOSPpsdRecPoint", 100) ; 
+    gAlice->GetEvent(evt) ; 
+    TTree * fReconstruct = gAlice->TreeR() ; 
+    fReconstruct->SetBranchAddress( "PHOSPpsdRP", &fPpsdRecPoints) ;
+    fReconstruct->GetEvent(0) ;
+    rv =  fPpsdRecPoints ;
+  }
+  
+  fPpsdRecPoints->Expand( fPpsdRecPoints->GetEntries() ) ; 
+    
+  return rv ; 
+  
 }
 
 //_____________________________________________________________________________
 void AliPHOSv0::Reconstruction(AliPHOSReconstructioner * Reconstructioner)
 { 
-  // reinitializes the existing RecPoint Lists and steers the reconstruction processes
-
+  // 1. Reinitializes the existing RecPoint, TrackSegment, and RecParticles Lists and 
+  // 2. Creates TreeR with a branch for each list
+  // 3. Steers the reconstruction processes
+  // 4. Saves the 3 lists in TreeR
+  // 5. Write the Tree to File
+  
   fReconstructioner = Reconstructioner ;
+  
+  char branchname[10] ;
+
+  
 
-  if (fEmcClusters) { 
-    fEmcClusters->Delete() ; 
-    delete fEmcClusters ;
-    fEmcClusters = 0 ; 
+
+  // 1.
+
+  //  gAlice->MakeTree("R") ; 
+  Int_t splitlevel = 0 ; 
+  
+  if (fEmcRecPoints) { 
+    fEmcRecPoints->Delete() ; 
+    delete fEmcRecPoints ;
+    fEmcRecPoints = 0 ; 
   }
-  fEmcClusters= new RecPointsList("AliPHOSEmcRecPoint", 100) ;
-  if (fPpsdClusters) { 
-    fPpsdClusters->Delete() ; 
-    delete fPpsdClusters ; 
-    fPpsdClusters = 0 ; 
+
+  //  fEmcRecPoints= new AliPHOSRecPoint::RecPointsList("AliPHOSEmcRecPoint", 1000) ; if TClonesArray
+  fEmcRecPoints= new AliPHOSRecPoint::RecPointsList(100) ; 
+
+  if ( fEmcRecPoints && gAlice->TreeR() ) {
+    sprintf(branchname,"%sEmcRP",GetName()) ;
+    
+    // gAlice->TreeR()->Branch(branchname, &fEmcRecPoints, fBufferSize); if TClonesArray
+    gAlice->TreeR()->Branch(branchname, "TObjArray", &fEmcRecPoints, fBufferSize, splitlevel) ; 
   }
-  fPpsdClusters = new RecPointsList("AliPHOSPpsdRecPoint", 100) ;
 
-  if (fTrackSegments) {  
+  if (fPpsdRecPoints) { 
+    fPpsdRecPoints->Delete() ; 
+    delete fPpsdRecPoints ; 
+    fPpsdRecPoints = 0 ; 
+  }
+
+  //  fPpsdRecPoints = new AliPHOSRecPoint::RecPointsList("AliPHOSPpsdRecPoint", 1000) ; if TClonesArray
+  fPpsdRecPoints = new AliPHOSRecPoint::RecPointsList(100) ;
+
+  if ( fPpsdRecPoints && gAlice->TreeR() ) {
+    sprintf(branchname,"%sPpsdRP",GetName()) ;
+     
+     // gAlice->TreeR()->Branch(branchname, &fPpsdRecPoints, fBufferSize); if TClonesArray
+    gAlice->TreeR()->Branch(branchname, "TObjArray", &fPpsdRecPoints, fBufferSize, splitlevel) ;
+  }
+
+  if (fTrackSegments) { 
    fTrackSegments->Delete() ; 
     delete fTrackSegments ; 
     fTrackSegments = 0 ; 
   }
-  fTrackSegments = new TrackSegmentsList(100) ;
- if (fRecParticles) {  
-   fRecParticles->Delete() ; 
+
+  fTrackSegments = new AliPHOSTrackSegment::TrackSegmentsList("AliPHOSTrackSegment", 1000) ;
+  if ( fTrackSegments && gAlice->TreeR() ) { 
+    sprintf(branchname,"%sTS",GetName()) ;
+    gAlice->TreeR()->Branch(branchname, &fTrackSegments, fBufferSize) ;
+  }
+
+  if (fRecParticles) {  
+    fRecParticles->Delete() ; 
     delete fRecParticles ; 
     fRecParticles = 0 ; 
   }
-  fRecParticles = new RecParticlesList("AliPHOSRecParticle", 100) ;
+  fRecParticles = new AliPHOSRecParticle::RecParticlesList("AliPHOSRecParticle", 1000) ;
+  if ( fRecParticles && gAlice->TreeR() ) { 
+     sprintf(branchname,"%sRP",GetName()) ;
+     gAlice->TreeR()->Branch(branchname, &fRecParticles, fBufferSize) ;
+  }
+  
+  // 3.
+
+  fReconstructioner->Make(fDigits, fEmcRecPoints, fPpsdRecPoints, fTrackSegments, fRecParticles);
+
+  // 4. Expand or Shrink the arrays to the proper size
+  
+  Int_t size ;
+  
+  size = fEmcRecPoints->GetEntries() ;
+  fEmcRecPoints->Expand(size) ;
+  size = fPpsdRecPoints->GetEntries() ;
+  fPpsdRecPoints->Expand(size) ;
 
-  fReconstructioner->Make(fDigits, fEmcClusters, fPpsdClusters, fTrackSegments, fRecParticles);
+  size = fTrackSegments->GetEntries() ;
+  fTrackSegments->Expand(size) ;
 
+  size = fRecParticles->GetEntries() ;
+  fRecParticles->Expand(size) ;
+
+  gAlice->TreeR()->Fill() ;
+  cout << "filled" << endl ;
+  // 5.
+
+  gAlice->TreeR()->Write() ;
+  cout << "writen" << endl ;
+  // Deleting reconstructed objects
+  ResetReconstruction();
+
+  
 }
 
+//____________________________________________________________________________
+void AliPHOSv0::ResetDigits() 
+{ 
+  // May sound strange, but cumulative hits are store in digits Tree
+  AliDetector::ResetDigits();
+  if(  fTmpHits ) {
+    fTmpHits->Delete();
+    fNTmpHits = 0 ;
+  }
+}  
+//____________________________________________________________________________
+void AliPHOSv0::ResetReconstruction() 
+{ 
+  // Deleting reconstructed objects
+
+  if ( fEmcRecPoints )   fEmcRecPoints->Delete();
+  if ( fPpsdRecPoints )  fPpsdRecPoints->Delete();
+  if ( fTrackSegments )  fTrackSegments->Delete();
+  if ( fRecParticles )   fRecParticles->Delete();
+  
+}
 //____________________________________________________________________________
 void AliPHOSv0::StepManager(void)
 {
+  // Accumulates hits as long as the track stays in a single crystal or PPSD gas Cell
+
   Int_t          relid[4] ;      // (box, layer, row, column) indices
   Float_t        xyze[4] ;       // position wrt MRS and energy deposited
   TLorentzVector pos ;
@@ -1176,9 +1437,8 @@ void AliPHOSv0::StepManager(void)
 
   Int_t primary =  gAlice->GetPrimary( gAlice->CurrentTrack() ); 
   TString name = fGeom->GetName() ; 
-
   if ( name == "GPS2" ) { // the CPV is a PPSD
-    if( gMC->CurrentVolID(copy) == gMC->VolId("GCEL") )
+    if( gMC->CurrentVolID(copy) == gMC->VolId("GCEL") ) // We are inside a gas cell 
     {
       gMC->TrackPosition(pos) ;
       xyze[0] = pos[0] ;