]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RICH/AliRICH.cxx
Protecting if the events are not there
[u/mrichter/AliRoot.git] / RICH / AliRICH.cxx
index ba6800bf14cb1b4c6e2cd10203cc6481c0f286f6..bfcb7e8d2fc74a8ee4185ce0d521fe640350bb71 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-  $Log$
-  Revision 1.54  2001/09/07 08:38:10  hristov
-  Pointers initialised to 0 in the default constructors
-
-  Revision 1.53  2001/08/30 09:51:23  hristov
-  The operator[] is replaced by At() or AddAt() in case of TObjArray.
-
-  Revision 1.52  2001/05/16 14:57:20  alibrary
-  New files for folders and Stack
-
-  Revision 1.51  2001/05/14 10:18:55  hristov
-  Default arguments declared once
-
-  Revision 1.50  2001/05/10 14:44:16  jbarbosa
-  Corrected some overlaps (thanks I. Hrivnacovna).
-
-  Revision 1.49  2001/05/10 12:23:49  jbarbosa
-  Repositioned the RICH modules.
-  Eliminated magic numbers.
-  Incorporated diagnostics (from macros).
-
-  Revision 1.48  2001/03/15 10:35:00  jbarbosa
-  Corrected bug in MakeBranch (was using a different version of STEER)
-
-  Revision 1.47  2001/03/14 18:13:56  jbarbosa
-  Several changes to adapt to new IO.
-  Removed digitising function, using AliRICHMerger::Digitise from now on.
-
-  Revision 1.46  2001/03/12 17:46:33  hristov
-  Changes needed on Sun with CC 5.0
-
-  Revision 1.45  2001/02/27 22:11:46  jbarbosa
-  Testing TreeS, removing of output.
-
-  Revision 1.44  2001/02/27 15:19:12  jbarbosa
-  Transition to SDigits.
-
-  Revision 1.43  2001/02/23 17:19:06  jbarbosa
-  Corrected photocathode definition in BuildGeometry().
-
-  Revision 1.42  2001/02/13 20:07:23  jbarbosa
-  Parametrised definition of photcathode dimensions. New spacers. New data members in AliRICHHit to store particle momentum
-  when entering the freon. Corrected calls to particle stack.
-
-  Revision 1.41  2001/01/26 20:00:20  hristov
-  Major upgrade of AliRoot code
-
-  Revision 1.40  2001/01/24 20:58:03  jbarbosa
-  Enhanced BuildGeometry. Now the photocathodes are drawn.
-
-  Revision 1.39  2001/01/22 21:40:24  jbarbosa
-  Removing magic numbers
-
-  Revision 1.37  2000/12/20 14:07:25  jbarbosa
-  Removed dependencies on TGeant3 (thanks to F. Carminati and I. Hrivnacova)
-
-  Revision 1.36  2000/12/18 17:45:54  jbarbosa
-  Cleaned up PadHits object.
-
-  Revision 1.35  2000/12/15 16:49:40  jbarbosa
-  Geometry and materials updates (wire supports, pcbs, backplane supports, frame).
-
-  Revision 1.34  2000/11/10 18:12:12  jbarbosa
-  Bug fix for AliRICHCerenkov (thanks to P. Hristov)
-
-  Revision 1.33  2000/11/02 10:09:01  jbarbosa
-  Minor bug correction (some pointers were not initialised in the default constructor)
-
-  Revision 1.32  2000/11/01 15:32:55  jbarbosa
-  Updated to handle both reconstruction algorithms.
-
-  Revision 1.31  2000/10/26 20:18:33  jbarbosa
-  Supports for methane and freon vessels
-
-  Revision 1.30  2000/10/24 13:19:12  jbarbosa
-  Geometry updates.
-
-  Revision 1.29  2000/10/19 19:39:25  jbarbosa
-  Some more changes to geometry. Further correction of digitisation "per part. type"
-
-  Revision 1.28  2000/10/17 20:50:57  jbarbosa
-  Inversed digtise by particle type (now, only the selected particle type is not digitsed).
-  Corrected several geometry minor bugs.
-  Added new parameter (opaque quartz thickness).
-
-  Revision 1.27  2000/10/11 10:33:55  jbarbosa
-  Corrected bug introduced by earlier revisions  (CerenkovData array cannot be reset to zero on wach call of StepManager)
-
-  Revision 1.26  2000/10/03 21:44:08  morsch
-  Use AliSegmentation and AliHit abstract base classes.
-
-  Revision 1.25  2000/10/02 21:28:12  fca
-  Removal of useless dependecies via forward declarations
-
-  Revision 1.24  2000/10/02 15:43:17  jbarbosa
-  Fixed forward declarations.
-  Fixed honeycomb density.
-  Fixed cerenkov storing.
-  New electronics.
-
-  Revision 1.23  2000/09/13 10:42:14  hristov
-  Minor corrections for HP, DEC and Sun; strings.h included
-
-  Revision 1.22  2000/09/12 18:11:13  fca
-  zero hits area before using
-
-  Revision 1.21  2000/07/21 10:21:07  morsch
-  fNrawch   = 0; and  fNrechits = 0; in the default constructor.
-
-  Revision 1.20  2000/07/10 15:28:39  fca
-  Correction of the inheritance scheme
-
-  Revision 1.19  2000/06/30 16:29:51  dibari
-  Added kDebugLevel variable to control output size on demand
-
-  Revision 1.18  2000/06/12 15:15:46  jbarbosa
-  Cleaned up version.
-
-  Revision 1.17  2000/06/09 14:58:37  jbarbosa
-  New digitisation per particle type
-
-  Revision 1.16  2000/04/19 12:55:43  morsch
-  Newly structured and updated version (JB, AM)
-
-*/
-
+/* $Id$ */
 
 ////////////////////////////////////////////////
 //  Manager and hits classes for set:RICH     //
 ////////////////////////////////////////////////
 
-#include <TBRIK.h>
-#include <TTUBE.h>
-#include <TNode.h> 
-#include <TRandom.h> 
-#include <TObject.h>
-#include <TVector.h>
-#include <TObjArray.h>
+#include <Riostream.h>
+#include <strings.h>
+
 #include <TArrayF.h>
+#include <TBRIK.h>
+#include <TCanvas.h>
+#include <TF1.h>
 #include <TFile.h>
-#include <TParticle.h>
 #include <TGeometry.h>
-#include <TTree.h>
 #include <TH1.h>
 #include <TH2.h>
-#include <TCanvas.h>
-//#include <TPad.h>
-#include <TF1.h>
-
-#include <iostream.h>
-#include <strings.h>
-
+#include <TNode.h> 
+#include <TObjArray.h>
+#include <TObject.h>
+#include <TParticle.h>
+#include <TPDGCode.h>
+#include <TRandom.h> 
+#include <TStyle.h>
+#include <TTUBE.h>
+#include <TTree.h>
+#include <TVector.h>
+#include "AliConst.h"
+#include "AliMagF.h"
+#include "AliPoints.h"
 #include "AliRICH.h"
-#include "AliSegmentation.h"
-#include "AliRICHSegmentationV0.h"
-#include "AliRICHHit.h"
 #include "AliRICHCerenkov.h"
-#include "AliRICHSDigit.h"
+#include "AliRICHClusterFinder.h"
 #include "AliRICHDigit.h"
-#include "AliRICHTransientDigit.h"
+#include "AliRICHDigitizer.h"
+#include "AliRICHHit.h"
+#include "AliRICHHitMapA1.h"
+#include "AliRICHMerger.h"
 #include "AliRICHRawCluster.h"
 #include "AliRICHRecHit1D.h"
 #include "AliRICHRecHit3D.h"
-#include "AliRICHHitMapA1.h"
-#include "AliRICHClusterFinder.h"
-#include "AliRICHMerger.h"
+#include "AliRICHSDigit.h"
+#include "AliRICHSegmentationV0.h"
+#include "AliRICHTransientDigit.h"
 #include "AliRun.h"
-#include "AliMC.h"
-#include "AliMagF.h"
-#include "AliConst.h"
-#include "AliPDG.h"
-#include "AliPoints.h"
-#include "AliCallf77.h" 
+#include "AliRunDigitizer.h"
+#include "AliSegmentation.h"
 
 
-// Static variables for the pad-hit iterator routines
-static Int_t sMaxIterPad=0;
+static Int_t sMaxIterPad=0;    // Static variables for the pad-hit iterator routines
 static Int_t sCurIterPad=0;
  
 ClassImp(AliRICH)
     
 //___________________________________________
-AliRICH::AliRICH()
-{
-// Default constructor for RICH manager class
-
-    fIshunt     = 0;
-    fHits       = 0;
-    fSDigits    = 0;
-    fNSDigits   = 0;
-    fNcerenkovs = 0;
-    fDchambers  = 0;
-    fRecHits1D = 0;
-    fRecHits3D = 0;
-    fRawClusters = 0;
-    fChambers = 0;
-    fCerenkovs  = 0;
-    for (Int_t i=0; i<7; i++)
-      {
-       fNdch[i]       = 0;
-       fNrawch[i]   = 0;
-       fNrechits1D[i] = 0;
-       fNrechits3D[i] = 0;
-      }
-
-    fFileName = 0;
-    fMerger = 0;
-}
-
-//___________________________________________
-AliRICH::AliRICH(const char *name, const char *title)
-    : AliDetector(name,title)
-{
+// RICH manager class   
 //Begin_Html
 /*
   <img src="gif/alirich.gif">
 */
 //End_Html
-    
-    fHits       = new TClonesArray("AliRICHHit",1000  );
-    gAlice->AddHitList(fHits);
-    fSDigits    = new TClonesArray("AliRICHSDigit",100000);
-    fCerenkovs  = new TClonesArray("AliRICHCerenkov",1000);
-    gAlice->AddHitList(fCerenkovs);
-    //gAlice->AddHitList(fHits);
-    fNSDigits   = 0;
-    fNcerenkovs = 0;
-    fIshunt     = 0;
-    
-    //fNdch      = new Int_t[kNCH];
-    
-    fDchambers = new TObjArray(kNCH);
-
-    fRecHits1D = new TObjArray(kNCH);
-    fRecHits3D = new TObjArray(kNCH);
-    
-    Int_t i;
-   
-    for (i=0; i<kNCH ;i++) {
-      //PH     (*fDchambers)[i] = new TClonesArray("AliRICHDigit",10000); 
-       fDchambers->AddAt(new TClonesArray("AliRICHDigit",10000), i); 
-       fNdch[i]=0;
-    }
-
-    //fNrawch      = new Int_t[kNCH];
-    
-    fRawClusters = new TObjArray(kNCH);
-    //printf("Created fRwClusters with adress:%p",fRawClusters);
-
-    for (i=0; i<kNCH ;i++) {
-      //PH      (*fRawClusters)[i] = new TClonesArray("AliRICHRawCluster",10000); 
-      fRawClusters->AddAt(new TClonesArray("AliRICHRawCluster",10000), i); 
-      fNrawch[i]=0;
-    }
 
-    //fNrechits      = new Int_t[kNCH];
-    
-    for (i=0; i<kNCH ;i++) {
-      //PH      (*fRecHits1D)[i] = new TClonesArray("AliRICHRecHit1D",1000);
-      fRecHits1D->AddAt(new TClonesArray("AliRICHRecHit1D",1000), i);
-    }
-    for (i=0; i<kNCH ;i++) {
-      //PH      (*fRecHits3D)[i] = new TClonesArray("AliRICHRecHit3D",1000);
-      fRecHits3D->AddAt(new TClonesArray("AliRICHRecHit3D",1000), i);
-    }
-    //printf("Created fRecHits with adress:%p",fRecHits);
-
-        
-    SetMarkerColor(kRed);
-    
-    /*fChambers = new TObjArray(kNCH);
-    for (i=0; i<kNCH; i++) 
-      (*fChambers)[i] = new AliRICHChamber();*/  
-    
-    fFileName = 0;
-}
-
-AliRICH::AliRICH(const AliRICH& RICH)
-{
-// Copy Constructor
-}
-
-
-//___________________________________________
+AliRICH::AliRICH()
+{//Default ctor should not contain any new operators
+  fIshunt     = 0;
+  fHits       = 0;
+  fSDigits    = 0;
+  fNSDigits   = 0;
+  fNcerenkovs = 0;
+  fDchambers  = 0;
+  fRecHits1D = 0;
+  fRecHits3D = 0;
+  fRawClusters = 0;
+  fChambers = 0;
+  fCerenkovs  = 0;
+  for (Int_t i=0; i<kNCH; i++){
+      fNdch[i]       = 0;
+      fNrawch[i]     = 0;
+      fNrechits1D[i] = 0;
+      fNrechits3D[i] = 0;
+  }
+  fFileName = 0;
+  fMerger = 0;
+}//AliRICH::AliRICH()
+//______________________________________________________________________________
+AliRICH::AliRICH(const char *name, const char *title)
+        :AliDetector(name,title)
+{//Named ctor
+  if(GetDebug())Info("named ctor","Start.");
+  fHits       = new TClonesArray("AliRICHHit",1000  );
+  fCerenkovs  = new TClonesArray("AliRICHCerenkov",1000);
+  fSDigits    = new TClonesArray("AliRICHSDigit",100000);
+  gAlice->AddHitList(fHits);
+  gAlice->AddHitList(fCerenkovs);
+  fNSDigits   = 0;
+  fNcerenkovs = 0;
+  fIshunt     = 0;
+  fDchambers  =new TObjArray(kNCH);
+  fRawClusters=new TObjArray(kNCH);
+  fRecHits1D  =new TObjArray(kNCH);
+  fRecHits3D  =new TObjArray(kNCH);
+  for (int i=0; i<kNCH ;i++) {
+    fDchambers->AddAt(new TClonesArray("AliRICHDigit",10000), i); 
+    fRawClusters->AddAt(new TClonesArray("AliRICHRawCluster",10000), i); 
+    fRecHits1D->AddAt(new TClonesArray("AliRICHRecHit1D",1000), i);
+    fRecHits3D->AddAt(new TClonesArray("AliRICHRecHit3D",1000), i);
+    fNdch[i]=0;
+    fNrawch[i]=0;
+  }
+  SetMarkerColor(kRed);
+  
+  /*fChambers = new TObjArray(kNCH);
+  for (i=0; i<kNCH; i++) 
+    (*fChambers)[i] = new AliRICHChamber();*/  
+  
+  fFileName = 0;
+  fMerger = 0;
+  if(GetDebug())Info("named ctor","Stop.");
+}//AliRICH::AliRICH(const char *name, const char *title)
+//______________________________________________________________________________
 AliRICH::~AliRICH()
-{
-
-// Destructor of RICH manager class
+{//dtor
+  if(GetDebug()) Info("dtor","Start.");
 
     fIshunt  = 0;
     delete fHits;
@@ -329,27 +163,24 @@ AliRICH::~AliRICH()
       fRecHits3D->Delete();
       delete fRecHits3D;
     }                     
-    
-}
-
-
+  if(GetDebug()) Info("dtor","Stop.");    
+}//AliRICH::~AliRICH()
 //_____________________________________________________________________________
 Int_t AliRICH::Hits2SDigits(Float_t xhit,Float_t yhit,Float_t eloss, Int_t idvol, ResponseType res)
 {
-//
 //  Calls the charge disintegration method of the current chamber and adds
-//  the simulated cluster to the root treee 
-//
-    Int_t clhits[5];
-    Float_t newclust[4][500];
-    Int_t nnew;
+//  the simulated cluster to the root tree 
+  if(GetDebug()) Info("Hits2SDigits","Start.");
+   
+   Int_t clhits[5];
+   Float_t newclust[4][500];
+   Int_t nnew;
     
 //
 //  Integrated pulse height on chamber
     
     clhits[0]=fNhits+1;
 
-    //PH    ((AliRICHChamber*) (*fChambers)[idvol])->DisIntegration(eloss, xhit, yhit, nnew, newclust, res);
     ((AliRICHChamber*)fChambers->At(idvol))->DisIntegration(eloss, xhit, yhit, nnew, newclust, res);
     Int_t ic=0;
     
@@ -373,166 +204,122 @@ Int_t AliRICH::Hits2SDigits(Float_t xhit,Float_t yhit,Float_t eloss, Int_t idvol
        }
     }
     
-    if (gAlice->TreeS())
-      {
+   if(gAlice->TreeS()){
        gAlice->TreeS()->Fill();
        gAlice->TreeS()->Write(0,TObject::kOverwrite);
        //printf("Filled SDigits...\n");
-      }
-    
-return nnew;
-}
-//___________________________________________
-void AliRICH::Hits2SDigits()
-{
+   }
+  if(GetDebug()) Info("Hits2SDigits","Stop.");    
+   return nnew;
+}//Int_t AliRICH::Hits2SDigits(Float_t xhit,Float_t yhit,Float_t eloss, Int_t idvol, ResponseType res)
 
-// Dummy: sdigits are created during transport.
-// Called from alirun.
+void AliRICH::Hits2SDigits()
+{//Dummy: sdigits are created during transport. Called from alirun.   
+  if(GetDebug()) Info("Hit2SDigits","Start.");
 
   int nparticles = gAlice->GetNtrack();
   cout << "Particles (RICH):" <<nparticles<<endl;
   if (nparticles > 0) printf("SDigits were already generated.\n");
 
 }
-
-//___________________________________________
-void AliRICH::SDigits2Digits(Int_t nev, Int_t flag)
-{
-
-//
-// Generate digits.
-// Called from macro. Multiple events, more functionality.
-
-  AliRICHChamber*       iChamber;
+//______________________________________________________________________________
+void AliRICH::SDigits2Digits()
+{//Generate digits from sdigits.
+  if(GetDebug()) Info("SDigits2Digits","Start.");
+   //AliRICHChamber*       iChamber;
   
-  printf("Generating tresholds...\n");
+   //printf("Generating tresholds...\n");
   
-  for(Int_t i=0;i<7;i++) {
-    iChamber = &(Chamber(i));
-    iChamber->GenerateTresholds();
-  }
+   //for(Int_t i=0;i<7;i++) {
+   //iChamber = &(Chamber(i));
+   //iChamber->GenerateTresholds();
+   //}
   
-  int nparticles = gAlice->GetNtrack();
-  cout << "Particles (RICH):" <<nparticles<<endl;
-  if (nparticles <= 0) return;
-  if (!fMerger) {
-    fMerger = new AliRICHMerger();
-  }
-  fMerger->Init();
-  fMerger->Digitise(nev,flag);
-}
-//___________________________________________
-void AliRICH::SDigits2Digits()
-{
-  SDigits2Digits(0,0);
-}
-//___________________________________________
+   //int nparticles = gAlice->GetNtrack();
+   //cout << "Particles (RICH):" <<nparticles<<endl;
+   //if (nparticles <= 0) return;
+   //if (!fMerger) {
+   //fMerger = new AliRICHMerger();
+   //}
+
+
+   //fMerger->Init();
+   //fMerger->Digitise(nev,flag);
+
+   AliRunDigitizer * manager = new AliRunDigitizer(1,1);
+   manager->SetInputStream(0,"galice.root");
+   //AliRICHDigitizer *dRICH  = new AliRICHDigitizer(manager);
+   manager->Exec("deb");
+  if(GetDebug()) Info("SDigits2Digits","Stop.");
+}//void AliRICH::SDigits2Digits()
+//______________________________________________________________________________
 void AliRICH::Digits2Reco()
 {
-
 // Generate clusters
-// Called from alirun, single event only.  
+// Called from alirun, single event only.     
+  if(GetDebug()) Info("Digits2Reco","Start.");
 
   int nparticles = gAlice->GetNtrack();
   cout << "Particles (RICH):" <<nparticles<<endl;
   if (nparticles > 0) FindClusters(0,0);
 
-}  
-
-//___________________________________________
+}//void AliRICH::Digits2Reco()  
+//______________________________________________________________________________
 void AliRICH::AddHit(Int_t track, Int_t *vol, Float_t *hits)
-{
-
-//  
-// Adds a hit to the Hits list
+{// Adds the current hit to the RICH hits list
 
     TClonesArray &lhits = *fHits;
     new(lhits[fNhits++]) AliRICHHit(fIshunt,track,vol,hits);
 }
-//_____________________________________________________________________________
+
 void AliRICH::AddCerenkov(Int_t track, Int_t *vol, Float_t *cerenkovs)
 {
-
-//
-// Adds a RICH cerenkov hit to the Cerenkov Hits list
-//
+// Adds a RICH cerenkov hit to the Cerenkov Hits list   
 
     TClonesArray &lcerenkovs = *fCerenkovs;
     new(lcerenkovs[fNcerenkovs++]) AliRICHCerenkov(fIshunt,track,vol,cerenkovs);
-    //printf ("Done for Cerenkov %d\n\n\n\n",fNcerenkovs);
 }
-//___________________________________________
-void AliRICH::AddSDigit(Int_t *clhits)
-{
 
-//
-// Add a RICH pad hit to the list
-//
+void AliRICH::AddSDigit(Int_t *aSDigit)
+{// Adds the current S digit to the RICH list of S digits   
 
-  //printf("fsdigits:%p, data: %d\n",fSDigits,clhits[2]);
   TClonesArray &lSDigits = *fSDigits;
-  new(lSDigits[fNSDigits++]) AliRICHSDigit(clhits);
+  new(lSDigits[fNSDigits++]) AliRICHSDigit(aSDigit);
 } 
-//_____________________________________________________________________________
-void AliRICH::AddDigits(Int_t id, Int_t *tracks, Int_t *charges, Int_t *digits)
-{
 
-  //
-  // Add a RICH digit to the list
-  //
 
-  //printf("fdigits:%p, data: %d\n",((TClonesArray*)(*fDchambers)[id]),digits[0]);
-  //PH  TClonesArray &ldigits = *((TClonesArray*)(*fDchambers)[id]);
-  TClonesArray &ldigits = *((TClonesArray*)fDchambers->At(id));
-  new(ldigits[fNdch[id]++]) AliRICHDigit(tracks,charges,digits);
+void AliRICH::AddDigits(Int_t id, Int_t *tracks, Int_t *charges, Int_t *digits)
+{// Add a RICH digit to the list   
+
+   TClonesArray &ldigits = *((TClonesArray*)fDchambers->At(id));
+   new(ldigits[fNdch[id]++]) AliRICHDigit(tracks,charges,digits);
 }
 
-//_____________________________________________________________________________
 void AliRICH::AddRawCluster(Int_t id, const AliRICHRawCluster& c)
-{
-    //
-    // Add a RICH digit to the list
-    //
-
-  //PH    TClonesArray &lrawcl = *((TClonesArray*)(*fRawClusters)[id]);
+{// Add a RICH digit to the list
+   
     TClonesArray &lrawcl = *((TClonesArray*)fRawClusters->At(id));
     new(lrawcl[fNrawch[id]++]) AliRICHRawCluster(c);
 }
-
 //_____________________________________________________________________________
 void AliRICH::AddRecHit1D(Int_t id, Float_t *rechit, Float_t *photons, Int_t *padsx, Int_t* padsy)
-{
-  
-  //
-  // Add a RICH reconstructed hit to the list
-  //
+{// Add a RICH reconstructed hit to the list
 
-  //PH    TClonesArray &lrec1D = *((TClonesArray*)(*fRecHits1D)[id]);
     TClonesArray &lrec1D = *((TClonesArray*)fRecHits1D->At(id));
     new(lrec1D[fNrechits1D[id]++]) AliRICHRecHit1D(id,rechit,photons,padsx,padsy);
 }
-
 //_____________________________________________________________________________
-void AliRICH::AddRecHit3D(Int_t id, Float_t *rechit)
-{
-  
-  //
-  // Add a RICH reconstructed hit to the list
-  //
+void AliRICH::AddRecHit3D(Int_t id, Float_t *rechit, Float_t omega, Float_t theta, Float_t phi)
+{// Add a RICH reconstructed hit to the list
 
-  //PH    TClonesArray &lrec3D = *((TClonesArray*)(*fRecHits3D)[id]);
     TClonesArray &lrec3D = *((TClonesArray*)fRecHits3D->At(id));
-    new(lrec3D[fNrechits3D[id]++]) AliRICHRecHit3D(id,rechit);
+    new(lrec3D[fNrechits3D[id]++]) AliRICHRecHit3D(id,rechit,omega,theta,phi);
 }
-
-//___________________________________________
-void AliRICH::BuildGeometry()
-    
-{
+//______________________________________________________________________________
+void AliRICH::BuildGeometry() 
+{// Builds a TNode geometry for event display
+  if(GetDebug())Info("BuildGeometry","Start.");
   
-  //
-  // Builds a TNode geometry for event display
-  //
     TNode *node, *subnode, *top;
     
     const int kColorRICH = kRed;
@@ -545,7 +332,7 @@ void AliRICH::BuildGeometry()
     AliRICHGeometry*  geometry;
  
     iChamber = &(pRICH->Chamber(0));
-    segmentation=(AliRICHSegmentationV0*) iChamber->GetSegmentationModel(0);
+    segmentation=(AliRICHSegmentationV0*) iChamber->GetSegmentationModel();
     geometry=iChamber->GetGeometryModel();
     
     new TBRIK("S_RICH","S_RICH","void",71.09999,11.5,73.15);
@@ -779,13 +566,11 @@ void AliRICH::BuildGeometry()
     subnode->SetLineColor(kGreen);
     fNodes->Add(subnode);
     fNodes->Add(node); 
-    
-}
-
-//___________________________________________
+  if(GetDebug())Info("BuildGeometry","Stop.");    
+}//void AliRICH::BuildGeometry()
+//______________________________________________________________________________
 void AliRICH::CreateGeometry()
 {
-    //
     // Create the geometry for RICH version 1
     //
     // Modified by:  N. Colonna (INFN - BARI, Nicola.Colonna@ba.infn.it) 
@@ -809,7 +594,7 @@ void AliRICH::CreateGeometry()
   AliRICHChamber*       iChamber;
 
   iChamber = &(pRICH->Chamber(0));
-  segmentation=(AliRICHSegmentationV0*) iChamber->GetSegmentationModel(0);
+  segmentation=(AliRICHSegmentationV0*) iChamber->GetSegmentationModel();
   geometry=iChamber->GetGeometryModel();
 
   Float_t distance;
@@ -1276,9 +1061,7 @@ void AliRICH::CreateGeometry()
     gMC->Gspos("RICH", 7, "ALIC", -(offset)*sinphi  , offset*costheta*cosphi, -offset*sinphi,idrotm[1006], "ONLY");
     
 }
-
-
-//___________________________________________
+//______________________________________________________________________________
 void AliRICH::CreateMaterials()
 {
     //
@@ -1601,9 +1384,7 @@ void AliRICH::CreateMaterials()
     gMC->SetCerenkov(idtmed[1009], 26, ppckov, abscoGrid, efficGrid, rIndexGrid);
     gMC->SetCerenkov(idtmed[1010], 26, ppckov, abscoOpaqueQuarz, efficAll, rIndexOpaqueQuarz);
 }
-
-//___________________________________________
-
+//______________________________________________________________________________
 Float_t AliRICH::Fresnel(Float_t ene,Float_t pdoti, Bool_t pola)
 {
 
@@ -1811,813 +1592,49 @@ Float_t AliRICH::AbsoCH4(Float_t x)
 
 
 //___________________________________________
-Int_t AliRICH::DistancetoPrimitive(Int_t , Int_t )
-{
-
-// Default value
-
-    return 9999;
+void AliRICH::ResetHits()
+{// Reset number of clusters and the cluster array for this detector
+  AliDetector::ResetHits();
+  fNSDigits   = 0;
+  fNcerenkovs = 0;
+  if (fSDigits)  fSDigits->Clear();
+  if (fCerenkovs) fCerenkovs->Clear();
+}
+//____________________________________________
+void AliRICH::ResetDigits()
+{//Reset number of digits and the digits array for this detector
+  for ( int i=0;i<kNCH;i++ ) {
+    if (fDchambers && fDchambers->At(i))   fDchambers->At(i)->Clear();
+    if (fNdch)  fNdch[i]=0;
+  }
+}
+//____________________________________________
+void AliRICH::ResetRawClusters()
+{//Reset number of raw clusters and the raw clust array for this detector
+  for ( int i=0;i<kNCH;i++ ) {
+    if (fRawClusters->At(i))    ((TClonesArray*)fRawClusters->At(i))->Clear();
+    if (fNrawch)  fNrawch[i]=0;
+  }
+}
+//____________________________________________
+void AliRICH::ResetRecHits1D()
+{//Reset number of raw clusters and the raw clust array for this detector
+  for ( int i=0;i<kNCH;i++ ) {
+    if (fRecHits1D->At(i))    ((TClonesArray*)fRecHits1D->At(i))->Clear();
+    if (fNrechits1D)  fNrechits1D[i]=0;
+  }
 }
 
-//___________________________________________
-void AliRICH::MakeBranch(Option_t* option, const char *file)
-{
-  // Create Tree branches for the RICH.
-    
-    const Int_t kBufferSize = 4000;
-    char branchname[20];
-      
-    AliDetector::MakeBranch(option,file);
-   
-    const char *cH = strstr(option,"H");
-    const char *cD = strstr(option,"D");
-    const char *cR = strstr(option,"R");
-    const char *cS = strstr(option,"S");
-
-
-    if (cH) {
-      sprintf(branchname,"%sCerenkov",GetName());
-      if (fCerenkovs   && gAlice->TreeH()) {
-       //TBranch* branch = MakeBranchInTree(gAlice->TreeH(),branchname, &fCerenkovs, kBufferSize, file) ;
-       MakeBranchInTree(gAlice->TreeH(),branchname, &fCerenkovs, kBufferSize, file) ;
-       //branch->SetAutoDelete(kFALSE);
-      } 
-      sprintf(branchname,"%sSDigits",GetName());
-      if (fSDigits   && gAlice->TreeH()) {
-       //TBranch* branch = MakeBranchInTree(gAlice->TreeH(),branchname, &fSDigits, kBufferSize, file) ;
-       MakeBranchInTree(gAlice->TreeH(),branchname, &fSDigits, kBufferSize, file) ;
-       //branch->SetAutoDelete(kFALSE);
-       //printf("Making branch %sSDigits in TreeH\n",GetName());
-      }
-    }   
-      
-    if (cS) {  
-      sprintf(branchname,"%sSDigits",GetName());
-      if (fSDigits   && gAlice->TreeS()) {
-       //TBranch* branch = MakeBranchInTree(gAlice->TreeS(),branchname, &fSDigits, kBufferSize, file) ;
-       MakeBranchInTree(gAlice->TreeS(),branchname, &fSDigits, kBufferSize, file) ;
-       //branch->SetAutoDelete(kFALSE);
-       //printf("Making branch %sSDigits in TreeS\n",GetName());
-      }
-    }
-    
-    if (cD) {
-    //
-    // one branch for digits per chamber
-    //
-      Int_t i;
-    
-      for (i=0; i<kNCH ;i++) {
-       sprintf(branchname,"%sDigits%d",GetName(),i+1); 
-       if (fDchambers   && gAlice->TreeD()) {
-          //TBranch* branch = MakeBranchInTree(gAlice->TreeD(),branchname, &((*fDchambers)[i]), kBufferSize, file) ;
-         MakeBranchInTree(gAlice->TreeD(),branchname, &((*fDchambers)[i]), kBufferSize, file) ;
-          //branch->SetAutoDelete(kFALSE);
-         //printf("Making Branch %sDigits%d\n",GetName(),i+1);
-       }       
-      }
-    }
-
-    if (cR) {    
-    //
-    // one branch for raw clusters per chamber
-    //
-
-      //printf("Called MakeBranch for TreeR\n");
-
-      Int_t i;
-
-      for (i=0; i<kNCH ;i++) {
-        sprintf(branchname,"%sRawClusters%d",GetName(),i+1);      
-        if (fRawClusters && gAlice->TreeR()) {
-           //TBranch* branch = MakeBranchInTree(gAlice->TreeR(),branchname, &((*fRawClusters)[i]), kBufferSize, file) ;
-         MakeBranchInTree(gAlice->TreeR(),branchname, &((*fRawClusters)[i]), kBufferSize, file) ;
-          //branch->SetAutoDelete(kFALSE);     
-        }        
-      }
-     //
-     // one branch for rec hits per chamber
-     // 
-     for (i=0; i<kNCH ;i++) {
-       sprintf(branchname,"%sRecHits1D%d",GetName(),i+1);    
-       if (fRecHits1D   && gAlice->TreeR()) {
-        //TBranch* branch = MakeBranchInTree(gAlice->TreeR(),branchname, &((*fRecHits1D)[i]), kBufferSize, file) ;
-        MakeBranchInTree(gAlice->TreeR(),branchname, &((*fRecHits1D)[i]), kBufferSize, file) ;
-        //branch->SetAutoDelete(kFALSE);
-       }       
-     }
-     for (i=0; i<kNCH ;i++) {
-       sprintf(branchname,"%sRecHits3D%d",GetName(),i+1);  
-       if (fRecHits3D   && gAlice->TreeR()) {
-        MakeBranchInTree(gAlice->TreeR(),branchname, &((*fRecHits3D)[i]), kBufferSize, file) ;
-        //branch->SetAutoDelete(kFALSE);
-      }        
-    }
-  }  
-}
-
-//___________________________________________
-void AliRICH::SetTreeAddress()
-{
-  // Set branch address for the Hits and Digits Tree.
-  char branchname[20];
-  Int_t i;
-
-    AliDetector::SetTreeAddress();
-    
-    TBranch *branch;
-    TTree *treeH = gAlice->TreeH();
-    TTree *treeD = gAlice->TreeD();
-    TTree *treeR = gAlice->TreeR();
-    TTree *treeS = gAlice->TreeS();
-    
-    if (treeH) {
-      if (fCerenkovs) {
-           branch = treeH->GetBranch("RICHCerenkov");
-           if (branch) branch->SetAddress(&fCerenkovs);
-       }
-    if (fSDigits) {
-       branch = treeH->GetBranch("RICHSDigits");
-       if (branch) 
-         {
-           branch->SetAddress(&fSDigits);
-           //printf("Setting sdigits branch address at %p in TreeH\n",&fSDigits);
-         }
-      }
-    }
-    
-    if (treeS) {
-      if (fSDigits) {
-       branch = treeS->GetBranch("RICHSDigits");
-       if (branch) 
-         {
-           branch->SetAddress(&fSDigits);
-           //printf("Setting sdigits branch address at %p in TreeS\n",&fSDigits);
-         }
-      }
-    }
-    
-    
-    if (treeD) {
-       for (int i=0; i<kNCH; i++) {
-           sprintf(branchname,"%sDigits%d",GetName(),i+1);
-           if (fDchambers) {
-             branch = treeD->GetBranch(branchname);
-             if (branch) branch->SetAddress(&((*fDchambers)[i]));
-           }
-       }
-    }
-  if (treeR) {
-      for (i=0; i<kNCH; i++) {
-         sprintf(branchname,"%sRawClusters%d",GetName(),i+1);
-         if (fRawClusters) {
-             branch = treeR->GetBranch(branchname);
-             if (branch) branch->SetAddress(&((*fRawClusters)[i]));
-         }
-      }
-      
-      for (i=0; i<kNCH; i++) {
-       sprintf(branchname,"%sRecHits1D%d",GetName(),i+1);
-       if (fRecHits1D) {
-         branch = treeR->GetBranch(branchname);
-         if (branch) branch->SetAddress(&((*fRecHits1D)[i]));
-         }
-      }
-      
-     for (i=0; i<kNCH; i++) {
-       sprintf(branchname,"%sRecHits3D%d",GetName(),i+1);
-       if (fRecHits3D) {
-         branch = treeR->GetBranch(branchname);
-         if (branch) branch->SetAddress(&((*fRecHits3D)[i]));
-         }
-      } 
-      
-  }
-}
-//___________________________________________
-void AliRICH::ResetHits()
-{
-  // Reset number of clusters and the cluster array for this detector
-    AliDetector::ResetHits();
-    fNSDigits   = 0;
-    fNcerenkovs = 0;
-    if (fSDigits)  fSDigits->Clear();
-    if (fCerenkovs) fCerenkovs->Clear();
-}
-
-
-//____________________________________________
-void AliRICH::ResetDigits()
-{
-  //
-  // Reset number of digits and the digits array for this detector
-  //
-    for ( int i=0;i<kNCH;i++ ) {
-      //PH     if (fDchambers && (*fDchambers)[i])   (*fDchambers)[i]->Clear();
-       if (fDchambers && fDchambers->At(i))   fDchambers->At(i)->Clear();
-       if (fNdch)  fNdch[i]=0;
-    }
-}
-
-//____________________________________________
-void AliRICH::ResetRawClusters()
-{
-  //
-  // Reset number of raw clusters and the raw clust array for this detector
-  //
-    for ( int i=0;i<kNCH;i++ ) {
-      //PH     if ((*fRawClusters)[i])    ((TClonesArray*)(*fRawClusters)[i])->Clear();
-       if (fRawClusters->At(i))    ((TClonesArray*)fRawClusters->At(i))->Clear();
-       if (fNrawch)  fNrawch[i]=0;
-    }
-}
-
-//____________________________________________
-void AliRICH::ResetRecHits1D()
-{
-  //
-  // Reset number of raw clusters and the raw clust array for this detector
-  //
-  
-  for ( int i=0;i<kNCH;i++ ) {
-    //PH       if ((*fRecHits1D)[i])    ((TClonesArray*)(*fRecHits1D)[i])->Clear();
-       if (fRecHits1D->At(i))    ((TClonesArray*)fRecHits1D->At(i))->Clear();
-       if (fNrechits1D)  fNrechits1D[i]=0;
-    }
-}
-
-//____________________________________________
-void AliRICH::ResetRecHits3D()
-{
-  //
-  // Reset number of raw clusters and the raw clust array for this detector
-  //
-  
-  for ( int i=0;i<kNCH;i++ ) {
-    //PH       if ((*fRecHits3D)[i])    ((TClonesArray*)(*fRecHits3D)[i])->Clear();
-       if (fRecHits3D->At(i))    ((TClonesArray*)fRecHits3D->At(i))->Clear();
-       if (fNrechits3D)  fNrechits3D[i]=0;
-    }
-}
-
-//___________________________________________
-void   AliRICH::SetGeometryModel(Int_t id, AliRICHGeometry *geometry)
-{
-
-//
-// Setter for the RICH geometry model
-//
-
-
-  //PH    ((AliRICHChamber*) (*fChambers)[id])->GeometryModel(geometry);
-    ((AliRICHChamber*)fChambers->At(id))->GeometryModel(geometry);
-}
-
-//___________________________________________
-void   AliRICH::SetSegmentationModel(Int_t id, AliSegmentation *segmentation)
-{
-
-//
-// Setter for the RICH segmentation model
-//
-
-  //PH    ((AliRICHChamber*) (*fChambers)[id])->SetSegmentationModel(segmentation);
-    ((AliRICHChamber*)fChambers->At(id))->SetSegmentationModel(segmentation);
-}
-
-//___________________________________________
-void   AliRICH::SetResponseModel(Int_t id, AliRICHResponse *response)
-{
-
-//
-// Setter for the RICH response model
-//
-
-  //PH    ((AliRICHChamber*) (*fChambers)[id])->ResponseModel(response);
-    ((AliRICHChamber*)fChambers->At(id))->ResponseModel(response);
-}
-
-void   AliRICH::SetReconstructionModel(Int_t id, AliRICHClusterFinder *reconst)
-{
-
-//
-// Setter for the RICH reconstruction model (clusters)
-//
-
-  //PH    ((AliRICHChamber*) (*fChambers)[id])->SetReconstructionModel(reconst);
-    ((AliRICHChamber*)fChambers->At(id))->SetReconstructionModel(reconst);
-}
-
-//___________________________________________
-void AliRICH::StepManager()
-{
-
-// Full Step Manager
-
-    Int_t          copy, id;
-    static Int_t   idvol;
-    static Int_t   vol[2];
-    Int_t          ipart;
-    static Float_t hits[22];
-    static Float_t ckovData[19];
-    TLorentzVector position;
-    TLorentzVector momentum;
-    Float_t        pos[3];
-    Float_t        mom[4];
-    Float_t        localPos[3];
-    Float_t        localMom[4];
-    Float_t        localTheta,localPhi;
-    Float_t        theta,phi;
-    Float_t        destep, step;
-    Float_t        ranf[2];
-    Int_t          nPads;
-    Float_t        coscerenkov;
-    static Float_t eloss, xhit, yhit, tlength;
-    const  Float_t kBig=1.e10;
-       
-    TClonesArray &lhits = *fHits;
-    TParticle *current = (TParticle*)(*gAlice->Particles())[gAlice->CurrentTrack()];
-
- //if (current->Energy()>1)
-   //{
-        
-    // Only gas gap inside chamber
-    // Tag chambers and record hits when track enters 
-    
-    idvol=-1;
-    id=gMC->CurrentVolID(copy);
-    Float_t cherenkovLoss=0;
-    //gAlice->KeepTrack(gAlice->CurrentTrack());
-    
-    gMC->TrackPosition(position);
-    pos[0]=position(0);
-    pos[1]=position(1);
-    pos[2]=position(2);
-    //bzero((char *)ckovData,sizeof(ckovData)*19);
-    ckovData[1] = pos[0];                 // X-position for hit
-    ckovData[2] = pos[1];                 // Y-position for hit
-    ckovData[3] = pos[2];                 // Z-position for hit
-    ckovData[6] = 0;                      // dummy track length
-    //ckovData[11] = gAlice->CurrentTrack();
-    
-    //printf("\n+++++++++++\nTrack: %d\n++++++++++++\n",gAlice->CurrentTrack());
-
-    //AliRICH *RICH = (AliRICH *) gAlice->GetDetector("RICH"); 
-    
-    /********************Store production parameters for Cerenkov photons************************/ 
-//is it a Cerenkov photon? 
-    if (gMC->TrackPid() == 50000050) { 
-
-      //if (gMC->VolId("GAP ")==gMC->CurrentVolID(copy))
-        //{                    
-         Float_t ckovEnergy = current->Energy();
-         //energy interval for tracking
-         if  (ckovEnergy > 5.6e-09 && ckovEnergy < 7.8e-09 )       
-           //if (ckovEnergy > 0)
-           {
-             if (gMC->IsTrackEntering()){        //is track entering?
-               //printf("Track entered (1)\n");
-               if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy))
-                 {                                                          //is it in freo?
-                   if (gMC->IsNewTrack()){                          //is it the first step?
-                     //printf("I'm in!\n");
-                     Int_t mother = current->GetFirstMother(); 
-                     
-                     //printf("Second Mother:%d\n",current->GetSecondMother());
-                     
-                     ckovData[10] = mother;
-                     ckovData[11] = gAlice->CurrentTrack();
-                     ckovData[12] = 1;             //Media where photon was produced 1->Freon, 2->Quarz
-                     //printf("Produced in FREO\n");
-                     fCkovNumber++;
-                     fFreonProd=1;
-                     //printf("Index: %d\n",fCkovNumber);
-                   }    //first step question
-                 }        //freo question
-               
-               if (gMC->IsNewTrack()){                                  //is it first step?
-                 if (gMC->VolId("QUAR")==gMC->CurrentVolID(copy))             //is it in quarz?
-                   {
-                     ckovData[12] = 2;
-                     //printf("Produced in QUAR\n");
-                   }    //quarz question
-               }        //first step question
-               
-               //printf("Before %d\n",fFreonProd);
-             }   //track entering question
-             
-             if (ckovData[12] == 1)                                        //was it produced in Freon?
-               //if (fFreonProd == 1)
-               {
-                 if (gMC->IsTrackEntering()){                                     //is track entering?
-                   //printf("Track entered (2)\n");
-                   //printf("Current volume (should be META): %s\n",gMC->CurrentVolName());
-                   //printf("VolId: %d, CurrentVolID: %d\n",gMC->VolId("META"),gMC->CurrentVolID(copy));
-                   if (gMC->VolId("META")==gMC->CurrentVolID(copy))                //is it in gap?      
-                     {
-                       //printf("Got in META\n");
-                       gMC->TrackMomentum(momentum);
-                       mom[0]=momentum(0);
-                       mom[1]=momentum(1);
-                       mom[2]=momentum(2);
-                       mom[3]=momentum(3);
-                       // Z-position for hit
-                       
-                       
-                       /**************** Photons lost in second grid have to be calculated by hand************/ 
-                       
-                       Float_t cophi = TMath::Cos(TMath::ATan2(mom[0], mom[1]));
-                       Float_t t = (1. - .025 / cophi) * (1. - .05 /  cophi);
-                       gMC->Rndm(ranf, 1);
-                       //printf("grid calculation:%f\n",t);
-                       if (ranf[0] > t) {
-                         gMC->StopTrack();
-                         ckovData[13] = 5;
-                         AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
-                         //printf("Added One (1)!\n");
-                         //printf("Lost one in grid\n");
-                       }
-                       /**********************************************************************************/
-                     }    //gap
-                   
-                   //printf("Current volume (should be CSI) (1): %s\n",gMC->CurrentVolName());
-                   //printf("VolId: %d, CurrentVolID: %d\n",gMC->VolId("CSI "),gMC->CurrentVolID(copy));
-                   if (gMC->VolId("CSI ")==gMC->CurrentVolID(copy))             //is it in csi?      
-                     {
-                       //printf("Got in CSI\n");
-                       gMC->TrackMomentum(momentum);
-                       mom[0]=momentum(0);
-                       mom[1]=momentum(1);
-                       mom[2]=momentum(2);
-                       mom[3]=momentum(3);
-                       
-                       /********* Photons lost by Fresnel reflection have to be calculated by hand********/ 
-                       /***********************Cerenkov phtons (always polarised)*************************/
-                       
-                       Float_t cophi = TMath::Cos(TMath::ATan2(mom[0], mom[1]));
-                       Float_t t = Fresnel(ckovEnergy*1e9,cophi,1);
-                       gMC->Rndm(ranf, 1);
-                       if (ranf[0] < t) {
-                         gMC->StopTrack();
-                         ckovData[13] = 6;
-                         AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
-                         //printf("Added One (2)!\n");
-                         //printf("Lost by Fresnel\n");
-                       }
-                       /**********************************************************************************/
-                     }
-                 } //track entering?
-                 
-                 
-                 /********************Evaluation of losses************************/
-                 /******************still in the old fashion**********************/
-                 
-                 TArrayI procs;
-                 Int_t i1 = gMC->StepProcesses(procs);            //number of physics mechanisms acting on the particle
-                 for (Int_t i = 0; i < i1; ++i) {
-                   //        Reflection loss 
-                   if (procs[i] == kPLightReflection) {        //was it reflected
-                     ckovData[13]=10;
-                     if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy)) 
-                       ckovData[13]=1;
-                     if (gMC->CurrentVolID(copy) == gMC->VolId("QUAR")) 
-                       ckovData[13]=2;
-                     //gMC->StopTrack();
-                     //AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
-                   } //reflection question
-                    
-                   //        Absorption loss 
-                   else if (procs[i] == kPLightAbsorption) {              //was it absorbed?
-                     //printf("Got in absorption\n");
-                     ckovData[13]=20;
-                     if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy)) 
-                       ckovData[13]=11;
-                     if (gMC->CurrentVolID(copy) == gMC->VolId("QUAR")) 
-                       ckovData[13]=12;
-                     if (gMC->CurrentVolID(copy) == gMC->VolId("META")) 
-                       ckovData[13]=13;
-                     if (gMC->CurrentVolID(copy) == gMC->VolId("GAP ")) 
-                       ckovData[13]=13;
-                     
-                     if (gMC->CurrentVolID(copy) == gMC->VolId("SRIC")) 
-                       ckovData[13]=15;
-                     
-                     //        CsI inefficiency 
-                     if (gMC->CurrentVolID(copy) == gMC->VolId("CSI ")) {
-                       ckovData[13]=16;
-                     }
-                     gMC->StopTrack();
-                     AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
-                     //printf("Added One (3)!\n");
-                     //printf("Added cerenkov %d\n",fCkovNumber);
-                   } //absorption question 
-                   
-                   
-                   //        Photon goes out of tracking scope 
-                   else if (procs[i] == kPStop) {                 //is it below energy treshold?
-                     ckovData[13]=21;
-                     gMC->StopTrack();
-                     AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
-                     //printf("Added One (4)!\n");
-                   }   // energy treshold question         
-                 }  //number of mechanisms cycle
-                 /**********************End of evaluation************************/
-               } //freon production question
-           } //energy interval question
-       //}//inside the proximity gap question
-    } //cerenkov photon question
-      
-    /**************************************End of Production Parameters Storing*********************/ 
-    
-    
-    /*******************************Treat photons that hit the CsI (Ckovs and Feedbacks)************/ 
-    
-    if (gMC->TrackPid() == 50000050 || gMC->TrackPid() == 50000051) {
-      //printf("Cerenkov\n");
-      
-      //if (gMC->TrackPid() == 50000051)
-       //printf("Tracking a feedback\n");
-      
-      if (gMC->VolId("CSI ")==gMC->CurrentVolID(copy))
-       {
-         //printf("Current volume (should be CSI) (2): %s\n",gMC->CurrentVolName());
-         //printf("VolId: %d, CurrentVolID: %d\n",gMC->VolId("CSI "),gMC->CurrentVolID(copy));
-         //printf("Got in CSI\n");
-         //printf("Tracking a %d\n",gMC->TrackPid());
-         if (gMC->Edep() > 0.){
-               gMC->TrackPosition(position);
-               gMC->TrackMomentum(momentum);
-               pos[0]=position(0);
-               pos[1]=position(1);
-               pos[2]=position(2);
-               mom[0]=momentum(0);
-               mom[1]=momentum(1);
-               mom[2]=momentum(2);
-               mom[3]=momentum(3);
-               Double_t tc = mom[0]*mom[0]+mom[1]*mom[1];
-               Double_t rt = TMath::Sqrt(tc);
-               theta   = Float_t(TMath::ATan2(rt,Double_t(mom[2])))*kRaddeg;
-               phi     = Float_t(TMath::ATan2(Double_t(mom[1]),Double_t(mom[0])))*kRaddeg;
-               gMC->Gmtod(pos,localPos,1);                                                                    
-               gMC->Gmtod(mom,localMom,2);
-               
-               gMC->CurrentVolOffID(2,copy);
-               vol[0]=copy;
-               idvol=vol[0]-1;
-
-               //Int_t sector=((AliRICHChamber*) (*fChambers)[idvol])
-                       //->Sector(localPos[0], localPos[2]);
-               //printf("Sector:%d\n",sector);
-
-               /*if (gMC->TrackPid() == 50000051){
-                 fFeedbacks++;
-                 printf("Feedbacks:%d\n",fFeedbacks);
-               }*/     
-               
-        //PH           ((AliRICHChamber*) (*fChambers)[idvol])
-               ((AliRICHChamber*)fChambers->At(idvol))
-                   ->SigGenInit(localPos[0], localPos[2], localPos[1]);
-               if(idvol<kNCH) {        
-                   ckovData[0] = gMC->TrackPid();        // particle type
-                   ckovData[1] = pos[0];                 // X-position for hit
-                   ckovData[2] = pos[1];                 // Y-position for hit
-                   ckovData[3] = pos[2];                 // Z-position for hit
-                   ckovData[4] = theta;                      // theta angle of incidence
-                   ckovData[5] = phi;                      // phi angle of incidence 
-                   ckovData[8] = (Float_t) fNSDigits;      // first sdigit
-                   ckovData[9] = -1;                       // last pad hit
-                   ckovData[13] = 4;                       // photon was detected
-                   ckovData[14] = mom[0];
-                   ckovData[15] = mom[1];
-                   ckovData[16] = mom[2];
-                   
-                   destep = gMC->Edep();
-                   gMC->SetMaxStep(kBig);
-                   cherenkovLoss  += destep;
-                   ckovData[7]=cherenkovLoss;
-                   
-                   nPads = Hits2SDigits(localPos[0],localPos[2],cherenkovLoss,idvol,kCerenkov);
-                                   
-                   if (fNSDigits > (Int_t)ckovData[8]) {
-                       ckovData[8]= ckovData[8]+1;
-                       ckovData[9]= (Float_t) fNSDigits;
-                   }
-
-                   //printf("Cerenkov loss: %f\n", cherenkovLoss);
-
-                   ckovData[17] = nPads;
-                   //printf("nPads:%d",nPads);
-                   
-                   //TClonesArray *Hits = RICH->Hits();
-                   AliRICHHit *mipHit =  (AliRICHHit*) (fHits->UncheckedAt(0));
-                   if (mipHit)
-                     {
-                       mom[0] = current->Px();
-                       mom[1] = current->Py();
-                       mom[2] = current->Pz();
-                       Float_t mipPx = mipHit->MomX();
-                       Float_t mipPy = mipHit->MomY();
-                       Float_t mipPz = mipHit->MomZ();
-                       
-                       Float_t r = mom[0]*mom[0] + mom[1]*mom[1] + mom[2]*mom[2];
-                       Float_t rt = TMath::Sqrt(r);
-                       Float_t mipR = mipPx*mipPx + mipPy*mipPy + mipPz*mipPz; 
-                       Float_t mipRt = TMath::Sqrt(mipR);
-                       if ((rt*mipRt) > 0)
-                         {
-                           coscerenkov = (mom[0]*mipPx + mom[1]*mipPy + mom[2]*mipPz)/(rt*mipRt);
-                         }
-                       else
-                         {
-                           coscerenkov = 0;
-                         }
-                       Float_t cherenkov = TMath::ACos(coscerenkov);
-                       ckovData[18]=cherenkov;
-                     }
-                   //if (sector != -1)
-                   //{
-                   AddHit(gAlice->CurrentTrack(),vol,ckovData);
-                   AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
-                   //printf("Added One (5)!\n");
-                   //}
-               }
-           }
-       }
-    }
-    
-    /***********************************************End of photon hits*********************************************/
-    
-
-    /**********************************************Charged particles treatment*************************************/
-
-    else if (gMC->TrackCharge())
-    //else if (1 == 1)
-      {
-//If MIP
-       /*if (gMC->IsTrackEntering())
-         {                
-           hits[13]=20;//is track entering?
-         }*/
-       if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy))
-         {
-           gMC->TrackMomentum(momentum);
-           mom[0]=momentum(0);
-           mom[1]=momentum(1);
-           mom[2]=momentum(2);
-           mom[3]=momentum(3);
-           hits [19] = mom[0];
-           hits [20] = mom[1];
-           hits [21] = mom[2];
-           fFreonProd=1;
-         }
-
-       if (gMC->VolId("GAP ")== gMC->CurrentVolID(copy)) {
-// Get current particle id (ipart), track position (pos)  and momentum (mom)
-           
-           gMC->CurrentVolOffID(3,copy);
-           vol[0]=copy;
-           idvol=vol[0]-1;
-
-           //Int_t sector=((AliRICHChamber*) (*fChambers)[idvol])
-                       //->Sector(localPos[0], localPos[2]);
-           //printf("Sector:%d\n",sector);
-           
-           gMC->TrackPosition(position);
-           gMC->TrackMomentum(momentum);
-           pos[0]=position(0);
-           pos[1]=position(1);
-           pos[2]=position(2);
-           mom[0]=momentum(0);
-           mom[1]=momentum(1);
-           mom[2]=momentum(2);
-           mom[3]=momentum(3);
-           gMC->Gmtod(pos,localPos,1);                                                                    
-           gMC->Gmtod(mom,localMom,2);
-           
-           ipart  = gMC->TrackPid();
-           //
-           // momentum loss and steplength in last step
-           destep = gMC->Edep();
-           step   = gMC->TrackStep();
-  
-           //
-           // record hits when track enters ...
-           if( gMC->IsTrackEntering()) {
-//             gMC->SetMaxStep(fMaxStepGas);
-               Double_t tc = mom[0]*mom[0]+mom[1]*mom[1];
-               Double_t rt = TMath::Sqrt(tc);
-               theta   = Float_t(TMath::ATan2(rt,Double_t(mom[2])))*kRaddeg;
-               phi     = Float_t(TMath::ATan2(Double_t(mom[1]),Double_t(mom[0])))*kRaddeg;
-               
-
-               Double_t localTc = localMom[0]*localMom[0]+localMom[2]*localMom[2];
-               Double_t localRt = TMath::Sqrt(localTc);
-               localTheta   = Float_t(TMath::ATan2(localRt,Double_t(localMom[1])))*kRaddeg;                       
-               localPhi     = Float_t(TMath::ATan2(Double_t(localMom[2]),Double_t(localMom[0])))*kRaddeg;    
-               
-               hits[0] = Float_t(ipart);         // particle type
-               hits[1] = localPos[0];                 // X-position for hit
-               hits[2] = localPos[1];                 // Y-position for hit
-               hits[3] = localPos[2];                 // Z-position for hit
-               hits[4] = localTheta;                  // theta angle of incidence
-               hits[5] = localPhi;                    // phi angle of incidence 
-               hits[8] = (Float_t) fNSDigits;    // first sdigit
-               hits[9] = -1;                     // last pad hit
-               hits[13] = fFreonProd;           // did id hit the freon?
-               hits[14] = mom[0];
-               hits[15] = mom[1];
-               hits[16] = mom[2];
-               hits[18] = 0;               // dummy cerenkov angle
-
-               tlength = 0;
-               eloss   = 0;
-               fFreonProd = 0;
-       
-               Chamber(idvol).LocaltoGlobal(localPos,hits+1);
-          
-               
-               //To make chamber coordinates x-y had to pass localPos[0], localPos[2]
-               xhit    = localPos[0];
-               yhit    = localPos[2];
-               // Only if not trigger chamber
-               if(idvol<kNCH) {
-                   //
-                   //  Initialize hit position (cursor) in the segmentation model 
-          //PH             ((AliRICHChamber*) (*fChambers)[idvol])
-                   ((AliRICHChamber*)fChambers->At(idvol))
-                       ->SigGenInit(localPos[0], localPos[2], localPos[1]);
-               }
-           }
-           
-           // 
-           // Calculate the charge induced on a pad (disintegration) in case 
-           //
-           // Mip left chamber ...
-           if( gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()){
-               gMC->SetMaxStep(kBig);
-               eloss   += destep;
-               tlength += step;
-               
-                               
-               // Only if not trigger chamber
-               if(idvol<kNCH) {
-                 if (eloss > 0) 
-                   {
-                     if(gMC->TrackPid() == kNeutron)
-                       printf("\n\n\n\n\n Neutron Making Pad Hit!!! \n\n\n\n");
-                     nPads = Hits2SDigits(xhit,yhit,eloss,idvol,kMip);
-                     hits[17] = nPads;
-                     //printf("nPads:%d",nPads);
-                   }
-               }
-               
-               hits[6]=tlength;
-               hits[7]=eloss;
-               if (fNSDigits > (Int_t)hits[8]) {
-                   hits[8]= hits[8]+1;
-                   hits[9]= (Float_t) fNSDigits;
-               }
-               
-               //if(sector !=-1)
-               new(lhits[fNhits++]) AliRICHHit(fIshunt,gAlice->CurrentTrack(),vol,hits);
-               eloss = 0; 
-               //
-               // Check additional signal generation conditions 
-               // defined by the segmentation
-               // model (boundary crossing conditions) 
-           } else if 
-          //PH         (((AliRICHChamber*) (*fChambers)[idvol])
-               (((AliRICHChamber*)fChambers->At(idvol))
-                ->SigGenCond(localPos[0], localPos[2], localPos[1]))
-           {
-          //PH         ((AliRICHChamber*) (*fChambers)[idvol])
-               ((AliRICHChamber*)fChambers->At(idvol))
-                   ->SigGenInit(localPos[0], localPos[2], localPos[1]);
-               if (eloss > 0) 
-                 {
-                   if(gMC->TrackPid() == kNeutron)
-                     printf("\n\n\n\n\n Neutron Making Pad Hit!!! \n\n\n\n");
-                   nPads = Hits2SDigits(xhit,yhit,eloss,idvol,kMip);
-                   hits[17] = nPads;
-                   //printf("Npads:%d",NPads);
-                 }
-               xhit     = localPos[0];
-               yhit     = localPos[2]; 
-               eloss    = destep;
-               tlength += step ;
-               //
-               // nothing special  happened, add up energy loss
-           } else {        
-               eloss   += destep;
-               tlength += step ;
-           }
-       }
-      }
-    /*************************************************End of MIP treatment**************************************/
-   //}
-}
-
-void AliRICH::FindClusters(Int_t nev,Int_t lastEntry)
+//____________________________________________
+void AliRICH::ResetRecHits3D()
+{// Reset number of raw clusters and the raw clust array for this detector
+  for ( int i=0;i<kNCH;i++ ) {
+    if (fRecHits3D->At(i))    ((TClonesArray*)fRecHits3D->At(i))->Clear();
+    if (fNrechits3D)  fNrechits3D[i]=0;
+  }
+}
+//______________________________________________________________________________
+void AliRICH::FindClusters(Int_t nev,Int_t lastEntry)
 {
 
 //
@@ -2666,16 +1683,12 @@ void AliRICH::FindClusters(Int_t nev,Int_t lastEntry)
     char hname[30];
     sprintf(hname,"TreeR%d",nev);
     gAlice->TreeR()->Write(hname,kOverwrite,0);
-    gAlice->TreeR()->Reset();
-    
+    gAlice->TreeR()->Reset();    
     //gObjectTable->Print();
-}
-
+}//void AliRICH::FindClusters(Int_t nev,Int_t lastEntry)
+//______________________________________________________________________________
 AliRICHSDigit* AliRICH::FirstPad(AliRICHHit*  hit,TClonesArray *clusters ) 
-{
-//
-    // Initialise the pad iterator
-    // Return the address of the first sdigit for hit
+{// Initialise the pad iterator Return the address of the first sdigit for hit
     TClonesArray *theClusters = clusters;
     Int_t nclust = theClusters->GetEntriesFast();
     if (nclust && hit->PHlast() > 0) {
@@ -2687,11 +1700,9 @@ AliRICHSDigit* AliRICH::FirstPad(AliRICHHit*  hit,TClonesArray *clusters )
     }
     
 }
-
+//______________________________________________________________________________
 AliRICHSDigit* AliRICH::NextPad(TClonesArray *clusters) 
-{
-
-  // Iterates over pads
+{// Iterates over pads
   
     sCurIterPad++;
     if (sCurIterPad <= sMaxIterPad) {
@@ -2701,12 +1712,6 @@ AliRICHSDigit* AliRICH::NextPad(TClonesArray *clusters)
     }
 }
 
-AliRICH& AliRICH::operator=(const AliRICH& rhs)
-{
-// Assignment operator
-    return *this;
-    
-}
 
 void AliRICH::DiagnosticsFE(Int_t evNumber1,Int_t evNumber2)
 {
@@ -2779,7 +1784,7 @@ void AliRICH::DiagnosticsFE(Int_t evNumber1,Int_t evNumber2)
        
        AliRICH *pRICH = (AliRICH *) gAlice->GetDetector("RICH");
      
-       TTree *treeH = gAlice->TreeH();
+       TTree *treeH = TreeH();
        Int_t ntracks =(Int_t) treeH->GetEntries();
             
 // Start loop on tracks in the hits containers
@@ -3020,6 +2025,10 @@ void AliRICH::DiagnosticsFE(Int_t evNumber1,Int_t evNumber2)
        
    }
    //   }
+
+   TStyle *mystyle=new TStyle("Plain","mystyle");
+   mystyle->SetPalette(1,0);
+   mystyle->cd();
    
    //Create canvases, set the view range, show histograms
 
@@ -3221,11 +2230,8 @@ void AliRICH::DiagnosticsFE(Int_t evNumber1,Int_t evNumber2)
  
   printf("\nEnd of analysis\n");
    
-}
-
-//_________________________________________________________________________________________________
-
-
+}//void AliRICH::DiagnosticsFE(Int_t evNumber1,Int_t evNumber2)
+//______________________________________________________________________________
 void AliRICH::DiagnosticsSE(Int_t diaglevel,Int_t evNumber1,Int_t evNumber2)
 {
 
@@ -3234,7 +2240,7 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    AliRICHChamber*       chamber;
    
    chamber = &(pRICH->Chamber(0));
-   segmentation=(AliRICHSegmentationV0*) chamber->GetSegmentationModel(0);
+   segmentation=(AliRICHSegmentationV0*) chamber->GetSegmentationModel();
 
    Int_t NpadX = segmentation->Npx();                 // number of pads on X
    Int_t NpadY = segmentation->Npy();                 // number of pads on Y
@@ -3251,6 +2257,11 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    Int_t xmax=  NpadX/2;
    Int_t ymin= -NpadY/2;
    Int_t ymax=  NpadY/2;
+
+   Float_t PTfinal = 0;
+   Int_t pionCount = 0;
+   Int_t kaonCount = 0;
+   Int_t protonCount = 0;
    
    TH2F *feedback = 0;
    TH2F *mip = 0;
@@ -3259,17 +2270,17 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    TH1F *hitsX = 0;
    TH1F *hitsY = 0;
 
-   TH2F *hc0 = new TH2F("hc0","Zoom on center of central chamber",150,-30,30,150,-50,10);
+   TH2F *hc0 = new TH2F("hc0","Zoom on center of central chamber",150,-25,25,150,-45,5);
 
    if (diaglevel == 1)
      {
        printf("Single Ring Hits\n");
-       feedback = new TH2F("feedback","Feedback hit distribution",150,-30,30,150,-50,10);
-       mip = new TH2F("mip","Mip hit distribution",150,-30,30,150,-50,10);
-       cerenkov = new TH2F("cerenkov","Cerenkov hit distribution",150,-30,30,150,-50,10);
-       h = new TH2F("h","Detector hit distribution",150,-30,30,150,-50,10);
-       hitsX = new TH1F("hitsX","Distribution of hits along x-axis",150,-30,30);
-       hitsY = new TH1F("hitsY","Distribution of hits along z-axis",150,-50,10);
+       feedback = new TH2F("feedback","Feedback hit distribution",150,-20,20,150,-35,5);
+       mip = new TH2F("mip","Mip hit distribution",150,-20,20,150,-35,5);
+       cerenkov = new TH2F("cerenkov","Cerenkov hit distribution",150,-20,20,150,-35,5);
+       h = new TH2F("h","Detector hit distribution",150,-20,20,150,-35,5);
+       hitsX = new TH1F("hitsX","Distribution of hits along x-axis",150,-50,50);
+       hitsY = new TH1F("hitsY","Distribution of hits along z-axis",150,-50,50);
      }       
    else
      {
@@ -3294,7 +2305,7 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    TH2F *hc7 = new TH2F("hc7","Chamber 7 signal distribution",NpadX,xmin,xmax,NpadY,ymin,ymax);
       
    TH1F *Clcharge = new TH1F("Clcharge","Cluster Charge Distribution",500,0.,500.);
-   TH1F *ckovangle = new TH1F("ckovangle","Cerenkov angle per photon",200,.3,1);
+   TH1F *ckovangle = new TH1F("ckovangle","Cerenkov angle per photon",100,.35,.8);
    TH1F *hckphi = new TH1F("hckphi","Cerenkov phi angle per photon",620,-3.1,3.1);
    TH1F *mother = new TH1F("mother","Cerenkovs per Mip",75,0.,75.);
    TH1F *radius = new TH1F("radius","Mean distance to Mip",100,0.,20.);
@@ -3314,15 +2325,23 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    TH1F *protonspectra = new TH1F("protonspectra","Proton Spectra",200,.5,10.);
    TH1F *kaonspectra = new TH1F("kaonspectra","Kaon Spectra",100,.5,10.);
    TH1F *chargedspectra = new TH1F("chargedspectra","Charged particles above 1 GeV Spectra",100,.5,10.);
-   TH1F *hitsPhi = new TH1F("hitsPhi","Distribution of phi angle of incidence",100,0,360);
-   TH1F *hitsTheta = new TH1F("hitsTheta","Distribution of Theta angle of incidence",100,0,15);
-   TH1F *Omega1D = new TH1F("omega","Reconstructed Cerenkov angle per track",200,.5,1);
-   TH1F *Theta = new TH1F("theta","Reconstructed theta incidence angle per track",200,0,15);
-   TH1F *Phi = new TH1F("phi","Reconstructed phi incidence per track",200,0,360);
-   TH1F *Omega3D = new TH1F("omega","Reconstructed Cerenkov angle per track",200,.3,1);
-   TH1F *PhotonCer = new TH1F("photoncer","Reconstructed Cerenkov angle per photon",200,.3,1);
+   TH1F *hitsPhi = new TH1F("hitsPhi","Distribution of phi angle of incidence",50,0,360);
+   TH1F *hitsTheta = new TH1F("hitsTheta","Distribution of theta angle of incidence",50,0,15);
+   TH1F *Omega1D = new TH1F("omega","Reconstructed Cerenkov angle per track",50,.5,1);
+   TH1F *Theta = new TH1F("theta","Reconstructed theta incidence angle per track",100,0,15);
+   TH1F *Phi = new TH1F("phi","Reconstructed phi incidence per track",100,0,360);
+   TH1F *Omega3D = new TH1F("omega","Reconstructed Cerenkov angle per track",100,.35,.8);
+   TH1F *PhotonCer = new TH1F("photoncer","Reconstructed Cerenkov angle per photon",100,.35,.8);
    TH2F *PadsUsed = new TH2F("padsused","Pads Used for Reconstruction",100,-30,30,100,-30,30);
    TH1F *MeanRadius = new TH1F("radius","Mean Radius for reconstructed track",100,0.,20.);
+   TH2F *identification = new TH2F("identification","Particle Identification",100,1,5,100,0,.8);
+   TH1F *OriginalOmega = new TH1F("Original Omega","Cerenkov angle per track",100,.35,.8);
+   TH1F *OriginalPhi = new TH1F("Original Phi","Distribution of phi angle of incidence per track",100,0,360);
+   TH1F *OriginalTheta = new TH1F("Original Theta","Distribution of theta angle per track",100,0,15);
+   TH1F *OmegaError = new TH1F("Omega Error","Difference between original an reconstructed cerenkov angle",100,0,.2);
+   TH1F *PhiError = new TH1F("Phi Error","Difference between original an reconstructed phi angle",100,0,360);
+   TH1F *ThetaError = new TH1F("Theta Error","Difference between original an reconstructed phi angle",100,0,15);
+
 
 //   Start loop over events 
 
@@ -3337,6 +2356,17 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    Int_t feed=0;
    Int_t padmip=0;
    Float_t x=0,y=0;
+
+   Float_t chiSquareOmega = 0;
+   Float_t chiSquareTheta = 0;
+   Float_t chiSquarePhi = 0;
+
+   Float_t recEffEvent = 0;
+   Float_t recEffTotal = 0;
+
+   Float_t trackglob[3];
+   Float_t trackloc[3];
+
    
    for (Int_t i=0;i<100;i++) mothers[i]=0;
 
@@ -3354,7 +2384,7 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
 // Get pointers to RICH detector and Hits containers
        
 
-       TTree *TH = gAlice->TreeH(); 
+       TTree *TH = TreeH(); 
        Stat_t ntracks = TH->GetEntries();
 
        // Start loop on tracks in the hits containers
@@ -3371,14 +2401,37 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
             mHit;
             mHit=(AliRICHHit*)pRICH->NextHit()) 
           {
-            //Int_t nch  = mHit->fChamber;              // chamber number
-            x  = mHit->X();                           // x-pos of hit
-            y  = mHit->Z();                           // y-pos
+            Int_t nch  = mHit->Chamber();              // chamber number
+            trackglob[0] = mHit->X();                 // x-pos of hit
+            trackglob[1] = mHit->Y();
+            trackglob[2] = mHit->Z();                 // y-pos of hit
+            //x  = mHit->X();                           // x-pos of hit
+            //y  = mHit->Z();                           // y-pos
             Float_t phi = mHit->Phi();                 //Phi angle of incidence
             Float_t theta = mHit->Theta();             //Theta angle of incidence
             Int_t index = mHit->Track();
             Int_t particle = (Int_t)(mHit->Particle());        
             //Int_t freon = (Int_t)(mHit->fLoss);    
+            Float_t px = mHit->MomX();
+            Float_t py = mHit->MomY();
+            
+            if (TMath::Abs(particle) < 10000000)
+              {
+                PTfinal=TMath::Sqrt(px*px + py*py);
+                //printf("PTfinal 0: %f\n",PTfinal);
+              }
+       
+            chamber = &(pRICH->Chamber(nch-1));
+            
+            //printf("Nch:%d\n",nch);
+            
+            chamber->GlobaltoLocal(trackglob,trackloc);
+            
+            chamber->LocaltoGlobal(trackloc,trackglob);
+            
+       
+            x=trackloc[0];
+            y=trackloc[2];
             
             hitsX->Fill(x,(float) 1);
             hitsY->Fill(y,(float) 1);
@@ -3432,7 +2485,7 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
              h->Fill(x,y,(float) 1);
                  //}
               //}
-          }
+          }
           
           Int_t ncerenkovs = pRICH->Cerenkovs()->GetEntriesFast();
           //if (current->GetPdgCode() < 50000051 && current->GetPdgCode() > 50000040)
@@ -3443,16 +2496,33 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
             totalphotonsevent->Fill(ncerenkovs,(float) 1);
             for (Int_t hit=0;hit<ncerenkovs;hit++) {
               AliRICHCerenkov* cHit = (AliRICHCerenkov*) pRICH->Cerenkovs()->UncheckedAt(hit);
-              //Int_t nchamber = cHit->fChamber;     // chamber number
+              Int_t nchamber = cHit->fChamber;     // chamber number
               Int_t index =    cHit->Track();
               //Int_t pindex =   (Int_t)(cHit->fIndex);
-              Float_t cx  =      cHit->X();                // x-position
-              Float_t cy  =      cHit->Z();                // y-position
+              trackglob[0] = cHit->X();                 // x-pos of hit
+              trackglob[1] = cHit->Y();
+              trackglob[2] = cHit->Z();                 // y-pos of hit
+              //Float_t cx  =      cHit->X();                // x-position
+              //Float_t cy  =      cHit->Z();                // y-position
               Int_t cmother =  cHit->fCMother;      // Index of mother particle
               Int_t closs =    (Int_t)(cHit->fLoss);           // How did the particle get lost? 
               Float_t cherenkov = cHit->fCerenkovAngle;   //production cerenkov angle
-              //printf ("Cerenkov hit number %d/%d, X:%d, Y:%d\n",hit,ncerenkovs,cx,cy); 
               
+              chamber = &(pRICH->Chamber(nchamber-1));
+            
+              //printf("Nch:%d\n",nch);
+              
+              chamber->GlobaltoLocal(trackglob,trackloc);
+            
+              chamber->LocaltoGlobal(trackloc,trackglob);
+            
+       
+              Float_t cx=trackloc[0];
+              Float_t cy=trackloc[2];
+              
+              //printf ("Cerenkov hit number %d/%d, X:%f, Y:%f\n",hit,ncerenkovs,cx,cy); 
+
+
               //printf("Particle:%9d\n",index);
                                 
               TParticle *current = (TParticle*)gAlice->Particle(index);
@@ -3515,8 +2585,8 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
                       Float_t mx = mipHit->X();
                       Float_t my = mipHit->Z();
                       //printf("FX %e, FY %e, VX %e, VY %e\n",cx,cy,mx,my);
-                      Float_t dx = cx - mx;
-                      Float_t dy = cy - my;
+                      Float_t dx = trackglob[0] - mx;
+                      Float_t dy = trackglob[2] - my;
                       //printf("Dx:%f, Dy:%f\n",dx,dy);
                       Float_t final_radius = TMath::Sqrt(dx*dx+dy*dy);
                       //printf("Final radius:%f\n",final_radius);
@@ -3607,28 +2677,115 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
               
               if(nrechits3D)
                 {
-                  for (Int_t hit=0;hit<nrechits3D;hit++) {
-                    AliRICHRecHit3D* recHit3D = (AliRICHRecHit3D*) pRICH->RecHitsAddress3D(2)->UncheckedAt(hit);
-                    Float_t r_omega    = recHit3D->fOmega;                  // Cerenkov angle
-                    Float_t r_theta    = recHit3D->fTheta;                  // Theta angle of incidence
-                    Float_t r_phi      = recHit3D->fPhi;                    // Phi angle if incidence
-                    Float_t meanradius = recHit3D->fMeanRadius;              // Mean radius for reconstructed point
-                   
-                    //printf("rechit %f %f %f %f %f\n",recHit3D->fOmega,recHit3D->fTheta,recHit3D->fPhi, recHit3D->fX,recHit3D->fY);  
-                    
-                    Omega3D->Fill(r_omega,(float) 1);
-                    Theta->Fill(r_theta*180/TMath::Pi(),(float) 1);
-                    Phi->Fill(r_phi*180/TMath::Pi()-180,(float) 1);
-                    MeanRadius->Fill(meanradius,(float) 1);
-                  }
+                  recEffEvent = 0;
+                  
+                  //for (Int_t hit=0;hit<nrechits3D;hit++) {
+                  AliRICHRecHit3D* recHit3D = (AliRICHRecHit3D*) pRICH->RecHitsAddress3D(2)->UncheckedAt(track);
+                  Float_t r_omega    = recHit3D->fOmega;                  // Cerenkov angle
+                  Float_t r_theta    = recHit3D->fTheta;                  // Theta angle of incidence
+                  Float_t r_phi      = recHit3D->fPhi;                    // Phi angle if incidence
+                  Float_t meanradius = recHit3D->fMeanRadius;              // Mean radius for reconstructed point
+                  Float_t originalOmega = recHit3D->fOriginalOmega;       // Real Cerenkov angle
+                  Float_t originalTheta = recHit3D->fOriginalTheta;       // Real incidence angle
+                  Float_t originalPhi = recHit3D->fOriginalPhi;           // Real azimuthal angle
+                  
+                  
+                  //correction to track cerenkov angle
+                  originalOmega = (Float_t) ckovangle->GetMean();
+                  
+                  if(diaglevel == 4)
+                    {
+                      printf("\nMean cerenkov angle: %f\n", originalOmega);
+                      printf("Reconstructed cerenkov angle: %f\n",r_omega);
+                    }
+                  
+                  Float_t omegaError = TMath::Abs(originalOmega - r_omega);
+                  Float_t thetaError = TMath::Abs(originalTheta - r_theta);
+                  Float_t phiError   = TMath::Abs(originalPhi - r_phi);
+                  
+                  //chiSquareOmega += (omegaError/originalOmega)*(omegaError/originalOmega); 
+                  //chiSquareTheta += (thetaError/originalTheta)*(thetaError/originalTheta); 
+                  //chiSquarePhi += (phiError/originalPhi)*(phiError/originalPhi); 
+                  
+                  if(TMath::Abs(omegaError) < 0.015)
+                    recEffEvent += 1;
+                  
+                  
+                  
+                  //printf("rechit %f %f %f %f %f\n",recHit3D->fOmega,recHit3D->fTheta,recHit3D->fPhi, recHit3D->fX,recHit3D->fY);  
+                  
+                  Omega3D->Fill(r_omega,(float) 1);
+                  Theta->Fill(r_theta*180/TMath::Pi(),(float) 1);
+                  Phi->Fill(r_phi*180/TMath::Pi()-180,(float) 1);
+                  MeanRadius->Fill(meanradius,(float) 1);
+                  identification->Fill(PTfinal, r_omega,1);
+                  OriginalOmega->Fill(originalOmega, (float) 1);
+                  OriginalTheta->Fill(originalTheta, (float) 1);
+                  OriginalPhi->Fill(TMath::Abs(originalPhi), (float) 1);
+                  OmegaError->Fill(omegaError, (float) 1);
+                  ThetaError->Fill(thetaError, (float) 1);
+                  PhiError->Fill(phiError, (float) 1);
+                  
+                  recEffEvent = recEffEvent;
+                  recEffTotal += recEffEvent;
+                  
+                  Float_t pioncer = acos(sqrt((.139*.139+PTfinal*PTfinal)/(PTfinal*PTfinal*1.285*1.285)));
+                  Float_t kaoncer = acos(sqrt((.439*.439+PTfinal*PTfinal)/(PTfinal*PTfinal*1.285*1.285)));
+                  Float_t protoncer = acos(sqrt((.938*.938+PTfinal*PTfinal)/(PTfinal*PTfinal*1.285*1.285)));
+
+                  Float_t piondist = TMath::Abs(r_omega - pioncer);
+                  Float_t kaondist = TMath::Abs(r_omega - kaoncer);
+                  Float_t protondist = TMath::Abs(r_omega - protoncer);
+
+                  if(diaglevel == 4)
+                    {
+                      if(pioncer<r_omega)
+                        {
+                          printf("Identified as a PION!\n");
+                          pionCount += 1;
+                        }
+                      if(kaoncer<r_omega && pioncer>r_omega)
+                        {
+                          if(kaondist>piondist)
+                            {
+                              printf("Identified as a PION!\n");
+                              pionCount += 1;
+                            }
+                          else
+                            {
+                              printf("Identified as a KAON!\n");
+                              kaonCount += 1;
+                            }
+                        }                       }
+                      if(protoncer<r_omega && kaoncer>r_omega)
+                        {
+                          if(kaondist>protondist)
+                            {
+                              printf("Identified as a PROTON!\n");
+                              protonCount += 1;
+                            }
+                          else
+                            {
+                              printf("Identified as a KAON!\n");
+                              pionCount += 1;
+                            }
+                        }
+                      if(protoncer>r_omega)
+                        {
+                          printf("Identified as a PROTON!\n");
+                          protonCount += 1;
+                        }
+
+                      printf("\nReconstruction efficiency: %5.2f%%\n", recEffEvent*100);
                 }
             }
        }
+   
        
        for (Int_t nmothers=0;nmothers<ntracks;nmothers++){
-          totalphotonstrack->Fill(mothers[nmothers],(float) 1);
-          mother->Fill(mothers2[nmothers],(float) 1);
-          //printf ("Entries in %d : %d\n",nmothers, mothers[nmothers]);
+        totalphotonstrack->Fill(mothers[nmothers],(float) 1);
+        mother->Fill(mothers2[nmothers],(float) 1);
+        //printf ("Entries in %d : %d\n",nmothers, mothers[nmothers]);
        }
        
        clusev->Fill(nraw,(float) 1);
@@ -3642,17 +2799,17 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
        pads = 0;
        nraw=0;
        padmip=0;
-
-
-
+       
+       
+       
        gAlice->ResetDigits();
        //Int_t nent=(Int_t)gAlice->TreeD()->GetEntries();
        gAlice->TreeD()->GetEvent(0);
-
+       
        if (diaglevel < 4)
         {
-
-
+          
+          
           TClonesArray *Digits  = pRICH->DigitsAddress(2);
           Int_t ndigits = Digits->GetEntriesFast();
           printf("Digits          : %d\n",ndigits);
@@ -3666,7 +2823,7 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
             if( ipx<=100 && ipy <=100) hc0->Fill(ipx,ipy,(float) qtot);
           }
         }
-        
+       
        if (diaglevel == 5)
         {
           for (Int_t ich=0;ich<7;ich++)
@@ -3699,7 +2856,61 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
             }
         }
    }
+   
+   if(diaglevel == 4)
+     {
+
+       Stat_t omegaE;
+       Stat_t thetaE;
+       Stat_t phiE;
        
+       Stat_t omegaO;
+       Stat_t thetaO;
+       Stat_t phiO;
+       
+       for(Int_t i=0;i<99;i++)
+        {
+          omegaE = OriginalOmega->GetBinContent(i);
+          if(omegaE != 0)
+            {
+              omegaO = Omega3D->GetBinContent(i);
+              chiSquareOmega += (TMath::Power(omegaE,2) - TMath::Power(omegaO,2))/omegaO;
+            }
+
+          thetaE = OriginalTheta->GetBinContent(i);
+          if(thetaE != 0)
+            {
+              thetaO = Theta->GetBinContent(i);
+              chiSquareTheta += (TMath::Power(thetaE,2) - TMath::Power(thetaO,2))/thetaO;
+            }
+
+          phiE = OriginalPhi->GetBinContent(i);
+          if(phiE != 0)
+            {
+              phiO = Phi->GetBinContent(i);
+              chiSquarePhi += (TMath::Power(phiE,2) - TMath::Power(phiO,2))/phiO;
+            }
+          
+          //printf(" o: %f  t: %f  p: %f\n", OriginalOmega->GetBinContent(i), OriginalTheta->GetBinContent(i),OriginalPhi->GetBinContent(i));
+
+        }
+
+       
+
+       printf("\nChi square test values:   Omega - %f\n", chiSquareOmega);
+       printf("                          Theta - %f\n", chiSquareTheta);
+       printf("                          Phi   - %f\n", chiSquarePhi);
+       
+       printf("\nKolmogorov test values:   Omega - %5.4f\n", Omega3D->KolmogorovTest(OriginalOmega));
+       printf("                          Theta - %5.4f\n", Theta->KolmogorovTest(OriginalTheta));
+       printf("                          Phi   - %5.4f\n", Phi->KolmogorovTest(OriginalPhi));
+
+       recEffTotal = recEffTotal/evNumber2;
+       printf("\nTotal reconstruction efficiency: %5.2f%%\n", recEffTotal*100);
+       printf("\n Pions: %d\n Kaons: %d\n Protons:%d\n",pionCount, kaonCount, protonCount);
+
+     }
+   
    
    //Create canvases, set the view range, show histograms
 
@@ -3715,9 +2926,25 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    TCanvas *c10 = 0;
    TCanvas *c11 = 0;
    TCanvas *c12 = 0;
+   TCanvas *c13 = 0;
+
    //TF1* expo = 0;
    //TF1* gaus = 0;
    
+   TStyle *mystyle=new TStyle("Plain","mystyle");
+   mystyle->SetPalette(1,0);
+   //mystyle->SetTitleYSize(0.2);
+   //mystyle->SetStatW(0.19);
+   //mystyle->SetStatH(0.1);
+   //mystyle->SetStatFontSize(0.01);
+   //mystyle->SetTitleYSize(0.3);
+   mystyle->SetFuncColor(2);
+   //mystyle->SetOptStat(0111);
+   mystyle->SetDrawBorder(0);
+   mystyle->SetTitleBorderSize(0);
+   mystyle->SetOptFit(1111);
+   mystyle->cd();
+
    
    TClonesArray *RecHits3D = pRICH->RecHitsAddress3D(2);
    Int_t nrechits3D = RecHits3D->GetEntriesFast();
@@ -3730,7 +2957,7 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
        
        c1 = new TCanvas("c1","Alice RICH digits",50,50,300,350);
        hc0->SetXTitle("ix (npads)");
-       hc0->Draw("box");
+       hc0->Draw("colz");
        
 //
        c2 = new TCanvas("c2","Hits per type",100,100,600,700);
@@ -3740,25 +2967,25 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
        c2->cd(1);
        feedback->SetXTitle("x (cm)");
        feedback->SetYTitle("y (cm)");
-       feedback->Draw();
+       feedback->Draw("colz");
        
        c2->cd(2);
        //mip->SetFillColor(5);
        mip->SetXTitle("x (cm)");
        mip->SetYTitle("y (cm)");
-       mip->Draw();
+       mip->Draw("colz");
        
        c2->cd(3);
        //cerenkov->SetFillColor(5);
        cerenkov->SetXTitle("x (cm)");
        cerenkov->SetYTitle("y (cm)"); 
-       cerenkov->Draw();
+       cerenkov->Draw("colz");
        
        c2->cd(4);
        //h->SetFillColor(5);
        h->SetXTitle("x (cm)");
        h->SetYTitle("y (cm)");
-       h->Draw();
+       h->Draw("colz");
 
        c3 = new TCanvas("c3","Hits distribution",150,150,600,350);
        c3->Divide(2,1);
@@ -3925,38 +3152,149 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
 
        if(nrechits3D)
         {
-          c8 = new TCanvas("c8","3D reconstruction",50,50,1100,700);
-          c8->Divide(4,2);
+          c8 = new TCanvas("c8","3D reconstruction of Phi angle",50,50,300,1050);
+          c8->Divide(1,3);
           //c2->SetFillColor(42);
           
+          
+          // data per hit
           c8->cd(1);
           hitsPhi->SetFillColor(5);
+          if (evNumber2>10)
+            hitsPhi->Fit("gaus");
           hitsPhi->Draw();
+          
+           //data per track
           c8->cd(2);
-          hitsTheta->SetFillColor(5);
-          hitsTheta->Draw();
+          OriginalPhi->SetFillColor(5);
+          if (evNumber2>10)
+            OriginalPhi->Fit("gaus");
+          OriginalPhi->Draw();
+
+          //recontructed data
           c8->cd(3);
-          ckovangle->SetFillColor(5);
-          ckovangle->SetXTitle("angle (radians)");
-          ckovangle->Draw();
-          c8->cd(4);
-          radius->SetFillColor(5);
-          radius->SetXTitle("radius (cm)");
-          radius->Draw();
-          c8->cd(5);
           Phi->SetFillColor(5);
+          if (evNumber2>10)
+            Phi->Fit("gaus");
           Phi->Draw();
-          c8->cd(6);
+
+          c9 = new TCanvas("c9","3D reconstruction of theta angle",75,75,300,1050);
+          c9->Divide(1,3);
+
+          // data per hit
+          c9->cd(1);
+          hitsTheta->SetFillColor(5);
+          if (evNumber2>10)
+            hitsTheta->Fit("gaus");
+          hitsTheta->Draw();
+          
+          //data per track
+          c9->cd(2);
+          OriginalTheta->SetFillColor(5);
+          if (evNumber2>10)
+            OriginalTheta->Fit("gaus");
+          OriginalTheta->Draw();
+
+          //recontructed data
+          c9->cd(3);
           Theta->SetFillColor(5);
+          if (evNumber2>10)
+            Theta->Fit("gaus");
           Theta->Draw();
-          c8->cd(7);
+
+          c10 = new TCanvas("c10","3D reconstruction of cherenkov angle",100,100,300,1050);
+          c10->Divide(1,3);
+
+          // data per hit
+          c10->cd(1);
+          ckovangle->SetFillColor(5);
+          ckovangle->SetXTitle("angle (radians)");
+          if (evNumber2>10)
+            ckovangle->Fit("gaus");
+          ckovangle->Draw();
+          
+          //data per track
+          c10->cd(2);
+          OriginalOmega->SetFillColor(5);
+          OriginalOmega->SetXTitle("angle (radians)");
+          if (evNumber2>10)
+            OriginalOmega->Fit("gaus");
+          OriginalOmega->Draw();
+
+          //recontructed data
+          c10->cd(3);
           Omega3D->SetFillColor(5);
           Omega3D->SetXTitle("angle (radians)");
+          if (evNumber2>10)
+            Omega3D->Fit("gaus");
           Omega3D->Draw(); 
-          c8->cd(8);
+
+
+          c11 = new TCanvas("c11","3D reconstruction of mean radius",125,125,300,700);
+          c11->Divide(1,2);
+
+          // data per hit
+          c11->cd(1);
+          radius->SetFillColor(5);
+          radius->SetXTitle("radius (cm)");
+          radius->Draw();
+
+          //recontructed data
+          c11->cd(2);
           MeanRadius->SetFillColor(5);
           MeanRadius->SetXTitle("radius (cm)");
           MeanRadius->Draw();
+
+          
+          c12 = new TCanvas("c12","Cerenkov angle vs. Momentum",150,150,550,350);
+
+          c12->cd(1);
+          identification->SetFillColor(5);
+          identification->SetXTitle("Momentum (GeV/c)");
+          identification->SetYTitle("Cherenkov angle (radians)");
+          
+          //Float_t pionmass=.139;
+          //Float_t kaonmass=.493;
+          //Float_t protonmass=.938;
+          //Float_t n=1.295;
+          
+          TF1 *pionplot = new TF1("pion","acos(sqrt((.139*.139+x*x)/(x*x*1.285*1.285)))",1,5);
+          TF1 *kaonplot = new TF1("kaon","acos(sqrt((.439*.439+x*x)/(x*x*1.285*1.285)))",1,5);
+          TF1 *protonplot = new TF1("proton","acos(sqrt((.938*.938+x*x)/(x*x*1.285*1.285)))",1,5);
+          
+          identification->Draw();
+
+          pionplot->SetLineColor(5);
+          pionplot->Draw("same");
+
+          kaonplot->SetLineColor(4);
+          kaonplot->Draw("same");
+
+          protonplot->SetLineColor(3);
+          protonplot->Draw("same");
+          //identification->Draw("same");
+
+
+
+          c13 = new TCanvas("c13","Reconstruction Errors",200,200,900,350);
+          c13->Divide(3,1);
+
+          c13->cd(1);
+          PhiError->SetFillColor(5);
+          if (evNumber2>10)
+            PhiError->Fit("gaus");
+          PhiError->Draw();
+          c13->cd(2);
+          ThetaError->SetFillColor(5);
+          if (evNumber2>10)
+            ThetaError->Fit("gaus");
+          ThetaError->Draw();
+          c13->cd(3);
+          OmegaError->SetFillColor(5);
+          OmegaError->SetXTitle("angle (radians)");
+          if (evNumber2>10)
+            OmegaError->Fit("gaus");
+          OmegaError->Draw();
           
         }
        
@@ -4093,4 +3431,238 @@ AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
    //printf("The total number of pads which give a signal: %d %d\n",Nh,Nh1);
    printf("\nEnd of analysis\n");
    printf("**********************************\n");
+}//void AliRICH::DiagnosticsSE(Int_t diaglevel,Int_t evNumber1,Int_t evNumber2)
+//______________________________________________________________________________
+void AliRICH::MakeBranchInTreeD(TTree *treeD, const char *file)
+{// Create TreeD branches for the RICH.
+  if(GetDebug())Info("MakeBranchInTreeD","Start.");
+
+  const Int_t kBufferSize = 4000;
+  char branchname[30];
+    
+  //
+  // one branch for digits per chamber
+  // 
+  for (Int_t i=0; i<kNCH ;i++) {
+    sprintf(branchname,"%sDigits%d",GetName(),i+1);    
+    if (fDchambers && treeD) {
+      MakeBranchInTree(treeD,branchname, &((*fDchambers)[i]), kBufferSize, file);
+//      printf("Making Branch %s for digits in chamber %d\n",branchname,i+1);
+    }
+  }
+}
+//______________________________________________________________________________
+void AliRICH::MakeBranch(Option_t* option)
+{//Create Tree branches for the RICH.
+  if(GetDebug())Info("MakeBranch","Start with option= %s.",option);
+    
+  const Int_t kBufferSize = 4000;
+  char branchname[20];
+      
+   
+  const char *cH = strstr(option,"H");
+  const char *cD = strstr(option,"D");
+  const char *cR = strstr(option,"R");
+  const char *cS = strstr(option,"S");
+
+
+  if(cH&&TreeH()){
+    if(!fHits) fHits=new TClonesArray("AliRICHHit",1000  );
+    if(!fCerenkovs) fCerenkovs  = new TClonesArray("AliRICHCerenkov",1000);
+    MakeBranchInTree(TreeH(),"RICHCerenkov", &fCerenkovs, kBufferSize, 0) ;
+
+    //kir if(!fSDigits) fSDigits    = new TClonesArray("AliRICHSDigit",100000);
+    //kir MakeBranchInTree(TreeH(),"RICHSDigits", &fSDigits, kBufferSize, 0) ;
+  }     
+  AliDetector::MakeBranch(option);//this is after cH because we need to guarantee that fHits array is created
+      
+  if(cS&&fLoader->TreeS()){  
+    if(!fSDigits) fSDigits=new TClonesArray("AliRICHSDigit",100000);
+    MakeBranchInTree(gAlice->TreeS(),"RICHSDigits",&fSDigits,kBufferSize,0) ;
+  }
+   
+  int i;
+  if (cD&&fLoader->TreeD()){
+    if(!fDchambers){
+      fDchambers=new TObjArray(kNCH);    // one branch for digits per chamber
+      for(i=0;i<kNCH;i++){ 
+        fDchambers->AddAt(new TClonesArray("AliRICHDigit",10000), i); 
+      }       
+    }
+    for (i=0; i<kNCH ;i++) 
+      {
+        sprintf(branchname,"%sDigits%d",GetName(),i+1);        
+        MakeBranchInTree(fLoader->TreeD(),branchname, &((*fDchambers)[i]), kBufferSize, 0);
+      }
+   }
+
+  if (cR&&gAlice->TreeR()){//one branch for raw clusters per chamber
+    Int_t i;
+    if (fRawClusters == 0x0 ) 
+     {
+       fRawClusters = new TObjArray(kNCH);
+       for (i=0; i<kNCH ;i++) 
+         {
+           fRawClusters->AddAt(new TClonesArray("AliRICHRawCluster",10000), i); 
+         }
+     }
+     
+    if (fRecHits1D == 0x0) 
+     {
+        fRecHits1D = new TObjArray(kNCH);
+        for (i=0; i<kNCH ;i++) 
+         {
+          fRecHits1D->AddAt(new TClonesArray("AliRICHRecHit1D",1000), i);
+         }
+     }
+
+    if (fRecHits3D == 0x0) 
+     {
+        fRecHits3D = new TObjArray(kNCH);
+        for (i=0; i<kNCH ;i++) 
+         {
+          fRecHits3D->AddAt(new TClonesArray("AliRICHRecHit3D",1000), i);
+         }
+     }
+       
+    for (i=0; i<kNCH ;i++){
+       sprintf(branchname,"%sRawClusters%d",GetName(),i+1);      
+       MakeBranchInTree(gAlice->TreeR(),branchname, &((*fRawClusters)[i]), kBufferSize, 0);
+       sprintf(branchname,"%sRecHits1D%d",GetName(),i+1);
+       MakeBranchInTree(fLoader->TreeR(),branchname, &((*fRecHits1D)[i]), kBufferSize, 0);
+       sprintf(branchname,"%sRecHits3D%d",GetName(),i+1);  
+       MakeBranchInTree(fLoader->TreeR(),branchname, &((*fRecHits3D)[i]), kBufferSize, 0);
+     }
+   }//if (cR && gAlice->TreeR())
+  if(GetDebug())Info("MakeBranch","Stop.");   
 }
+//______________________________________________________________________________
+void AliRICH::SetTreeAddress()
+{//Set branch address for the Hits and Digits Tree.
+  if(GetDebug())Info("SetTreeAddress","Start.");
+  
+  char branchname[20];
+  Int_t i;
+
+    
+  TBranch *branch;
+  TTree *treeH = fLoader->TreeH();
+  TTree *treeD = fLoader->TreeD();
+  TTree *treeR = fLoader->TreeR();
+  TTree *treeS = fLoader->TreeS();
+    
+  if(treeH){
+    branch = treeH->GetBranch("RICHCerenkov");
+    if(branch){
+      if (fCerenkovs == 0x0) fCerenkovs  = new TClonesArray("AliRICHCerenkov",1000); 
+        branch->SetAddress(&fCerenkovs);
+    }
+       
+     branch = treeH->GetBranch("RICHSDigits");
+     if (branch) 
+      {
+        if (fSDigits == 0x0) fSDigits    = new TClonesArray("AliRICHSDigit",100000);
+        branch->SetAddress(&fSDigits);
+         //printf("Setting sdigits branch address at %p in TreeH\n",&fSDigits);
+      }
+     if (fHits == 0x0) fHits       = new TClonesArray("AliRICHHit",1000  ); 
+    }
+   //this is after TreeH because we need to guarantee that fHits array is created
+   AliDetector::SetTreeAddress();
+    
+   if (treeS) {
+      branch = treeS->GetBranch("RICHSDigits");
+      if (branch) 
+        {
+          if (fSDigits == 0x0) fSDigits = new TClonesArray("AliRICHSDigit",100000);
+          branch->SetAddress(&fSDigits);
+          //printf("Setting sdigits branch address at %p in TreeS\n",&fSDigits);
+        }
+    }
+    
+    
+   if (treeD) 
+    {
+      if (fDchambers == 0x0) 
+        {
+           fDchambers = new TObjArray(kNCH);
+           for (i=0; i<kNCH ;i++) 
+             {
+               fDchambers->AddAt(new TClonesArray("AliRICHDigit",10000), i); 
+             }
+        }
+      
+      for (i=0; i<kNCH; i++) {
+        sprintf(branchname,"%sDigits%d",GetName(),i+1);
+        if (fDchambers) {
+           branch = treeD->GetBranch(branchname);
+           if (branch) branch->SetAddress(&((*fDchambers)[i]));
+        }
+      }
+    }
+    
+  if (treeR) {
+    
+    if (fRawClusters == 0x0 ) 
+     {
+       fRawClusters = new TObjArray(kNCH);
+       for (i=0; i<kNCH ;i++) 
+         {
+           fRawClusters->AddAt(new TClonesArray("AliRICHRawCluster",10000), i); 
+         }
+     }
+     
+    if (fRecHits1D == 0x0) 
+     {
+        fRecHits1D = new TObjArray(kNCH);
+        for (i=0; i<kNCH ;i++) 
+         {
+          fRecHits1D->AddAt(new TClonesArray("AliRICHRecHit1D",1000), i);
+         }
+     }
+
+    if (fRecHits3D == 0x0) 
+     {
+        fRecHits3D = new TObjArray(kNCH);
+        for (i=0; i<kNCH ;i++) 
+         {
+          fRecHits3D->AddAt(new TClonesArray("AliRICHRecHit3D",1000), i);
+         }
+     }
+    
+    for (i=0; i<kNCH; i++) {
+         sprintf(branchname,"%sRawClusters%d",GetName(),i+1);
+         if (fRawClusters) {
+             branch = treeR->GetBranch(branchname);
+             if (branch) branch->SetAddress(&((*fRawClusters)[i]));
+         }
+    }
+      
+    for (i=0; i<kNCH; i++) {
+       sprintf(branchname,"%sRecHits1D%d",GetName(),i+1);
+       if (fRecHits1D) {
+         branch = treeR->GetBranch(branchname);
+         if (branch) branch->SetAddress(&((*fRecHits1D)[i]));
+         }
+     }
+      
+     for (i=0; i<kNCH; i++) {
+       sprintf(branchname,"%sRecHits3D%d",GetName(),i+1);
+       if (fRecHits3D) {
+         branch = treeR->GetBranch(branchname);
+         if (branch) branch->SetAddress(&((*fRecHits3D)[i]));
+         }
+      } 
+      
+  }
+  if(GetDebug())Info("SetTreeAddress","Stop.");
+}//void AliRICH::SetTreeAddress()
+//______________________________________________________________________________
+void AliRICH::Print(Option_t *option)const
+{
+  TObject::Print(option);
+  GetGeometryModel(0)->Print(option);
+  GetSegmentationModel(0)->Print(option);
+  GetResponseModel(0)->Print(option);
+}//void AliRICH::Print(Option_t *option)const