]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOF.cxx
Several pointers were set to zero in the default constructors to avoid memory managem...
[u/mrichter/AliRoot.git] / TOF / AliTOF.cxx
index 5f8b4287a9f769f1f4ea8a5ed90c294e4834c873..c76333dae3c6d719597fdfaf1fb8c04096ec07fa 100644 (file)
 
 /*
 $Log$
+Revision 1.29  2001/10/17 14:19:24  hristov
+delete replaced by delete []
+
+Revision 1.28  2001/10/05 12:02:01  vicinanz
+Minor improvements on Merger and SDigitizer
+
+Revision 1.27  2001/10/02 13:03:13  vicinanz
+Minor improvements on the code
+
+Revision 1.26  2001/09/27 10:39:20  vicinanz
+SDigitizer and Merger added
+
+Revision 1.25  2001/09/07 08:37:40  hristov
+Pointers initialised to 0 in the default constructor
+
+Revision 1.24  2001/09/05 16:31:00  hristov
+The deletion of TOF folders temporarily commented out
+
+Revision 1.23  2001/08/29 12:59:01  vicinanz
+Minor changes to the Digitizer procedure
+
+Revision 1.22  2001/08/28 08:45:58  vicinanz
+TTask and TFolder structures implemented
+
+Revision 1.21  2001/05/16 14:57:24  alibrary
+New files for folders and Stack
+
+Revision 1.20  2001/05/04 10:09:47  vicinanz
+Major upgrades to the strip structure
+
+Revision 1.19  2001/03/12 17:47:25  hristov
+Changes needed on Sun with CC 5.0
+
 Revision 1.18  2001/01/26 19:57:42  hristov
 Major upgrade of AliRoot code
 
@@ -49,9 +82,10 @@ Revision 1.9  1999/09/29 09:24:33  fca
 Introduction of the Copyright and cvs Log
 
 */
+
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
-//  Time Of Flight                               FCA                         //
+//  Time Of Flight                                                           //
 //  This class contains the basic functions for the Time Of Flight           //
 //  detector. Functions specific to one particular geometry are              //
 //  contained in the derived classes                                         //
@@ -76,15 +110,26 @@ Introduction of the Copyright and cvs Log
 ///////////////////////////////////////////////////////////////////////////////
 
 #include <iostream.h>
+#include <strstream.h>
+#include <fstream.h>
+#include <stdlib.h>
 
 #include "AliTOF.h"
-#include "AliTOFD.h"
+#include "AliTOFhit.h"
+#include "AliTOFdigit.h"
+#include "AliTOFRawSector.h"
+#include "AliTOFRoc.h"
+#include "AliTOFRawDigit.h"
+
+#include "TROOT.h"
 #include "TBRIK.h"
 #include "TNode.h"
 #include "TObject.h"
-#include "TRandom.h"
+#include "TSystem.h"
 #include "TTree.h"
 #include "TFile.h"
+#include "TFolder.h"
+#include "TTask.h"
 
 #include "AliRun.h"
 #include "AliMC.h"
@@ -100,7 +145,18 @@ AliTOF::AliTOF()
   //
   // Default constructor
   //
+  fFGeom = 0;
+  fDTask = 0;
+  fReTask = 0;
   fIshunt   = 0;
+  fSDigits       = 0 ;
+  fDigits        = 0 ;
+  fReconParticles = 0;
+  fName="TOF";
+  fMerger = 0;
+/* fp
+  CreateTOFFolders();
+*/
 }
  
 //_____________________________________________________________________________
@@ -113,12 +169,13 @@ AliTOF::AliTOF(const char *name, const char *title)
   // Here are fixed some important parameters
   //
 
-  // Initialization of hits and digits array
+  // Initialization of hits, sdigits and digits array
   //
-  fHits   = new TClonesArray("AliTOFhit",  405);
+  fHits   = new TClonesArray("AliTOFhit",  1000);
   gAlice->AddHitList(fHits);
   fIshunt  = 0;
-  fDigits = new TClonesArray("AliTOFdigit",405);
+  fSDigits       = new TClonesArray("AliTOFdigit",  1000);
+  fDigits        = new TClonesArray("AliTOFdigit",  1000);
   //
   // Digitization parameters
   //
@@ -129,13 +186,13 @@ AliTOF::AliTOF(const char *name, const char *title)
   SetMarkerSize(0.4);
 
 // General Geometrical Parameters
-  fNTof    =  18;
-  fRmax    = 399.0;//cm
+  fNTof    =  18;  // number of sectors
+  fRmax    = 399.0;//cm 
   fRmin    = 370.0;//cm
-  fZlenC   = 177.5;//cm
-  fZlenB   = 141.0;//cm
-  fZlenA   = 106.0;//cm
-  fZtof    = 370.5;//cm
+  fZlenC   = 177.5;//cm length of module C
+  fZlenB   = 141.0;//cm length of module B
+  fZlenA   = 106.0;//cm length of module A
+  fZtof    = 371.5;//cm total semi-length of TOF detector
 
 // Strip Parameters
   fStripLn = 122.0;//cm  Strip Length
@@ -150,20 +207,111 @@ AliTOF::AliTOF(const char *name, const char *title)
   fNpadX   =  48;  // Number of pads in a strip along the X direction
   fNpadZ   =   2;  // Number of pads in a strip along the Z direction
   fPadXStr = fNpadX*fNpadZ; //Number of pads per strip
-  fNStripA = 0;
-  fNStripB = 0;
-  fNStripC = 0;
+  fNStripA = 15; // number of strips in A type module 
+  fNStripB = 19; // number of strips in B type module
+  fNStripC = 20; // number of strips in C type module
  
 // Physical performances
   fTimeRes = 100.;//ps
   fChrgRes = 100.;//pC
 
 // DAQ characteristics
-  fPadXSector = 1932;
-  fNRoc       = 14;
-  fNFec       = 32;
-  fNTdc       = 32;
-  fNPadXRoc   = (Int_t)fPadXSector/fNRoc;
+// cfr. TOF-TDR pag. 105 for Glossary
+// TARODA : TOF-ALICE Read Out and Data Acquisition system
+  fPadXSector = 8928; // number of pad per sector -with no holes-
+                      // ((15+2*19+2*20)*(48*2))
+  fNRoc       = 14;   // number of Roc (Read Out Controller) (TARODA)
+  fNFec       = 32;   // number of Fec (Front-End electronic Card)
+                      // (TARODA)
+  fNTdc       = 32;   // number of Tdc (Time to Digital Converter)
+  fNPadXRoc   = (Int_t)fPadXSector/fNRoc; // number of pads for each ROC
+  /* fp 25 Sept 2001
+  // Create TOF Folder Structure
+  CreateTOFFolders();
+  */ 
+}
+
+//_____________________________________________________________________________
+void AliTOF::CreateTOFFolders()
+{
+  // create the ALICE TFolder
+  // create the ALICE TTasks
+  // create the ALICE main TFolder
+  // to be done by AliRun
+
+  TFolder * alice = new TFolder();
+  alice->SetNameTitle("FPAlice", "Alice Folder") ;
+  gROOT->GetListOfBrowsables()->Add(alice) ;
+
+  TFolder * aliceF  = alice->AddFolder("folders", "Alice memory Folder") ;
+  //  make it the owner of the objects that it contains
+  aliceF->SetOwner() ;
+  // geometry folder
+  TFolder * geomF = aliceF->AddFolder("Geometry", "Geometry objects") ;
+  TFolder * aliceT  = alice->AddFolder("tasks", "Alice tasks Folder") ;   
+  //  make it the owner of the objects that it contains
+  aliceT->SetOwner() ;
+
+  TTask * aliceDi = new TTask("(S)Digitizer", "Alice SDigitizer & Digitizer") ;
+  aliceT->Add(aliceDi);
+
+  TTask * aliceRe = new TTask("Reconstructioner", "Alice Reconstructioner") ;
+  aliceT->Add(aliceRe);
+
+  char * tempo = new char[80] ;
+
+  // creates the TOF Digitizer and adds it to alice main (S)Digitizer task
+  sprintf(tempo, "%sDigitizers container",GetName() ) ;
+  fDTask = new TTask(GetName(), tempo);
+  aliceDi->Add(fDTask) ;
+
+  // creates the TOF reconstructioner and adds it to alice main Reconstructioner task
+  sprintf(tempo, "%sReconstructioner container",GetName() ) ;
+  fReTask = new TTask(GetName(), tempo);
+  aliceRe->Add(fReTask) ;
+
+  delete [] tempo ;
+  // creates the TOF geometry  folder
+  geomF->AddFolder("TOF", "Geometry for TOF") ;
+}
+
+//_____________________________________________________________________________
+AliTOF::~AliTOF()
+{
+  // dtor:
+  // it remove also the alice folder 
+  // and task that TOF creates instead of AliRun
+  /* PH Temporarily commented because of problems
+  TFolder * alice = (TFolder*)gROOT->GetListOfBrowsables()->FindObject("FPAlice") ;
+  delete alice;
+  alice = 0;
+  */
+  if (fHits)
+    {
+      fHits->Delete ();
+      delete fHits;
+      fHits = 0;
+    }
+  if (fDigits)
+    {
+      fDigits->Delete ();
+      delete fDigits;
+      fDigits = 0;
+    }
+  if (fSDigits)
+    {
+      fSDigits->Delete ();
+      delete fSDigits;
+      fSDigits = 0;
+    }
+  if (fReconParticles)
+    {
+      fReconParticles->Delete ();
+      delete fReconParticles;
+      fReconParticles = 0;
+    }
+
 }
 
 //_____________________________________________________________________________
@@ -171,6 +319,7 @@ void AliTOF::AddHit(Int_t track, Int_t *vol, Float_t *hits)
 {
   //
   // Add a TOF hit
+  // new with placement used
   //
   TClonesArray &lhits = *fHits;
   new(lhits[fNhits++]) AliTOFhit(fIshunt, track, vol, hits);
@@ -181,11 +330,61 @@ void AliTOF::AddDigit(Int_t *tracks, Int_t *vol, Float_t *digits)
 {
   //
   // Add a TOF digit
+  // new with placement used
   //
   TClonesArray &ldigits = *fDigits;
   new (ldigits[fNdigits++]) AliTOFdigit(tracks, vol, digits);
 }
 
+//___________________________________________
+void AliTOF::AddSDigit(Int_t *tracks, Int_t *vol, Float_t *digits)
+{
+     
+//
+// Add a TOF sdigit
+//
+        
+  TClonesArray &lSDigits = *fSDigits;   
+  new(lSDigits[fNSDigits++]) AliTOFdigit(tracks, vol, digits);
+}
+
+//_____________________________________________________________________________
+void AliTOF::SetTreeAddress ()
+{
+  // Set branch address for the Hits and Digits Tree.
+  char branchname[30];
+  AliDetector::SetTreeAddress ();
+
+  TBranch *branch;
+  TTree *treeD = gAlice->TreeD ();
+
+
+  if (treeD)
+    {
+      if (fDigits)
+       {
+         branch = treeD->GetBranch (branchname);
+         if (branch)
+           branch->SetAddress (&fDigits);
+       }
+
+    }
+  if (fSDigits)
+    //  fSDigits->Clear ();
+
+  if (gAlice->TreeS () && fSDigits)
+    {
+      branch = gAlice->TreeS ()->GetBranch ("TOF");
+      if (branch)
+       branch->SetAddress (&fSDigits);
+    }
+
+  if (gAlice->TreeR() && fReconParticles) 
+    {
+      branch = gAlice->TreeR()->GetBranch("TOF"); 
+      if (branch) branch->SetAddress(&fReconParticles) ;
+    }   
+}
 
 //_____________________________________________________________________________
 void AliTOF::CreateGeometry()
@@ -202,13 +401,13 @@ void AliTOF::CreateGeometry()
   const Double_t kPi=TMath::Pi();
   const Double_t kDegrad=kPi/180.;
   //
-  Float_t xTof, yTof, Wall;
+  Float_t xTof, yTof, wall;
 
   // frame inbetween TOF modules
-  Wall = 4.;//cm
+  wall = 4.;//cm
 
   // Sizes of TOF module with its support etc..
-  xTof = 2.*(fRmin*TMath::Tan(10*kDegrad)-Wall/2-.5);
+  xTof = 2.*(fRmin*TMath::Tan(10*kDegrad)-wall/2-.5);
   yTof = fRmax-fRmin;
 
 //  TOF module internal definitions 
@@ -216,7 +415,7 @@ void AliTOF::CreateGeometry()
 }
 
 //_____________________________________________________________________________
-void AliTOF::DrawModule()
+void AliTOF::DrawModule() const
 {
   //
   // Draw a shaded view of the common part of the TOF geometry
@@ -257,9 +456,11 @@ void AliTOF::CreateMaterials()
   // Defines TOF materials for all versions
   // Authors :   Maxim Martemianov, Boris Zagreev (ITEP) 
   //            18/09/98 
+  // Revision: F. Pierella 5-3-2001
+  // Bologna University
   //
-  Int_t   ISXFLD = gAlice->Field()->Integ();
-  Float_t SXMGMX = gAlice->Field()->Max();
+  Int_t   isxfld = gAlice->Field()->Integ();
+  Float_t sxmgmx = gAlice->Field()->Max();
   //
   //--- Quartz (SiO2) 
   Float_t   aq[2] = { 28.0855,15.9994 };
@@ -267,12 +468,28 @@ void AliTOF::CreateMaterials()
   Float_t   wq[2] = { 1.,2. };
   Float_t   dq = 2.20;
   Int_t nq = -2;
-  // --- Freon
-  Float_t afre[2]  = {12.011,18.9984032 };
-  Float_t zfre[2]  = { 6., 9.};
-  Float_t wfre[2]  = { 5.,12.};
-  Float_t densfre  = 1.5;
-  Int_t nfre = -2;
+  // --- Freon C2F4H2 (TOF-TDR pagg.)
+  // Geant Manual CONS110-1, pag. 43 (Geant, Detector Description and Simulation Tool)
+  Float_t afre[3]  = {12.011,18.998,1.007};
+  Float_t zfre[3]  = { 6., 9., 1.}; 
+  Float_t wfre[3]  = { 2., 4., 2.};
+  Float_t densfre  = 0.00375;   
+// http://www.fi.infn.it/sezione/prevprot/gas/freon.html
+  Int_t nfre = -3; 
+/*
+  //-- Isobutane quencher C4H10 (5% in the sensitive mixture)
+  Float_t aiso[2]  = {12.011,1.007};
+  Float_t ziso[2]  = { 6.,  1.};
+  Float_t wiso[2]  = { 4., 10.};
+  Float_t densiso  = .......;  // (g/cm3) density
+  Int_t nfre = -2; // < 0 i.e. proportion by number of atoms of each kind
+  //-- SF6 (5% in the sensitive mixture)
+  Float_t asf[3]  = {32.066,18.998};
+  Float_t zsf[3]  = { 16., 9.};
+  Float_t wsf[3]  = {  1., 6.}; 
+  Float_t denssf  = .....;   // (g/cm3) density
+  Int_t nfre = -2; // < 0 i.e. proportion by number of atoms of each kind
+*/
   // --- CO2 
   Float_t ac[2]   = {12.,16.};
   Float_t zc[2]   = { 6., 8.};
@@ -285,7 +502,7 @@ void AliTOF::CreateMaterials()
   Float_t wmy[3] = {  5., 4.,  2. };
   Float_t dmy    = 1.39;
   Int_t nmy = -3;
- // For polyethilene (CH2) for honeycomb!!!!
+ // For polyethilene (CH2) - honeycomb -
   Float_t ape[2] = { 12., 1. };
   Float_t zpe[2] = {  6., 1. };
   Float_t wpe[2] = {  1., 2. };
@@ -346,33 +563,48 @@ void AliTOF::CreateMaterials()
   deemax = -.3;   // Maximum fractional energy loss, DLS
   stmin  = -.8;
 
-  AliMedium( 1, "Air$"  ,  1, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium( 2, "Cu $"  ,  2, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium( 3, "C  $"  ,  3, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium( 4, "Pol$"  ,  4, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium( 5, "G10$"  ,  5, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium( 6, "DME$"  ,  6, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium( 7, "CO2$"  ,  7, 0, ISXFLD, SXMGMX, 10., -.01, -.1, .01, -.01);
-  AliMedium( 8,"ALUMINA$", 8, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium( 9,"Al Frame$",9, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium(10, "DME-S$",  6, 1, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium(11, "C-TRD$", 10, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium(12, "Myl$"  , 11, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium(13, "Fre$"  , 12, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium(14, "Fre-S$", 12, 1, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium(15, "Glass$", 13, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
-  AliMedium(16, "Water$", 14, 0, ISXFLD, SXMGMX, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 1, "Air$"  ,  1, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 2, "Cu $"  ,  2, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 3, "C  $"  ,  3, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 4, "Pol$"  ,  4, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 5, "G10$"  ,  5, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 6, "DME$"  ,  6, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 7, "CO2$"  ,  7, 0, isxfld, sxmgmx, 10., -.01, -.1, .01, -.01);
+  AliMedium( 8,"ALUMINA$", 8, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium( 9,"Al Frame$",9, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium(10, "DME-S$",  6, 1, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium(11, "C-TRD$", 10, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium(12, "Myl$"  , 11, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium(13, "Fre$"  , 12, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium(14, "Fre-S$", 12, 1, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium(15, "Glass$", 13, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
+  AliMedium(16, "Water$", 14, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
 }
 
 //_____________________________________________________________________________
-Int_t AliTOF::DistancetoPrimitive(Int_t , Int_t )
+Int_t AliTOF::DistancetoPrimitive(Int_t , Int_t ) const
 {
   //
   // Returns distance from mouse pointer to detector, default version
   //
   return 9999;
 }
+
+//___________________________________________
+void AliTOF::ResetHits ()
+{
+  // Reset number of clusters and the cluster array for this detector
+  AliDetector::ResetHits ();
+}
+
+//____________________________________________
+void AliTOF::ResetDigits ()
+{
+  //
+  // Reset number of digits and the digits array for this detector
+  AliDetector::ResetDigits ();
+  //
+} 
 //_____________________________________________________________________________
 void AliTOF::Init()
 {
@@ -385,231 +617,393 @@ void AliTOF::Init()
 }
 
 //____________________________________________________________________________
-void AliTOF::MakeBranch(Option_t* option, char *file)
-//
-// Initializes the Branches of the TOF inside the 
-// trees written for each event. 
-//  AliDetector::MakeBranch initializes just the 
-// Branch inside TreeH. Here we add the branch in 
-// TreeD.
-//
+void AliTOF::MakeBranch(Option_t* option, const char *file)
 {
-
+ //
+ // Initializes the Branches of the TOF inside the 
+ // trees written for each event. 
+ // AliDetector::MakeBranch initializes just the 
+ // Branch inside TreeH. Here we add the branches in 
+ // TreeD, TreeS and TreeR.
+ //
   AliDetector::MakeBranch(option,file);
 
   Int_t buffersize = 4000;
   Char_t branchname[10];
   sprintf(branchname,"%s",GetName());
   
-  const char *D = strstr(option,"D");
+  const char *oD = strstr(option,"D");
+  const char *oS = strstr(option,"S");
+  const char *oR = strstr(option,"R");
 
-  if (fDigits && gAlice->TreeD() && D){
-     gAlice->MakeBranchInTree(gAlice->TreeD(), 
+  if (oD)
+  //
+  // one branch for TOF digits
+  // 
+
+
+  if (fDigits && gAlice->TreeD() && oD){
+             MakeBranchInTree(gAlice->TreeD(), 
                               branchname, &fDigits,buffersize, file) ;
   }
+
+  if (oS)
+  //
+  // one branch for TOF sdigits
+  //
+
+
+  if (fSDigits && gAlice->TreeS() && oS){
+             MakeBranchInTree(gAlice->TreeS(),
+                              branchname, &fSDigits,buffersize, file) ;
+  }
+
+  if (oR)
+  //
+  // one branch for TOF reconstructed particles
+  //
+
+
+  if (fReconParticles && gAlice->TreeR() && oR){
+             MakeBranchInTree(gAlice->TreeR(),
+                              branchname, &fReconParticles,buffersize, file) ;
+  }
+
+}
+
+//____________________________________________________________________________
+void AliTOF::Makehits(Bool_t hits) 
+{
+// default argument used, see AliTOF.h
+// Enable/Disable the writing of the TOF-hits branch 
+// on TreeH
+// by default : enabled for TOFv1, v2, v3, v4
+//              disabled for TOFv0
+// 
+   if (hits &&  (IsVersion()!=0))
+      fIdSens = gMC->VolId("FPAD");
+   else
+      cout << "Option for writing the TOF-hits branch on TreeH: disabled" << endl;
 }
 
 //____________________________________________________________________________
 void AliTOF::FinishEvent()
 {
-//  Hits2Digits();
+// do nothing
 }
 
 //___________________________________________
 void AliTOF::SDigits2Digits()
 {
 //
-// Genneratedigits
+// Generate digits performing merging
 //
+  /*
     int nparticles = gAlice->GetNtrack();
     cout << "Particles       :" <<nparticles<<endl;
     if (nparticles > 0 ) {
-      Hits2Digits(0);
+      
+      AliTOF::Hits2Digits();
+      
     }
+  */
+  cout<<"AliTOF::SDigits2Digits"<<endl; 
+    if (fMerger) {
+      fMerger->Init();
+      cout<<"AliTOF::SDigits2Digits Init"<<endl; 
+      fMerger->Digitise();
+      cout<<"AliTOF::SDigits2Digits Digitise() "<<endl; 
+     }
+}
+
+//---------------------------------------------------------------------
+void   AliTOF::SetMerger(AliTOFMerger* merger)
+{
+// Set pointer to merger
+    fMerger = merger;
+}
+
+//---------------------------------------------------------------------
+AliTOFMerger*  AliTOF::Merger()
+{
+// Return pointer to merger
+    return fMerger;
 }
 
 //____________________________________________________________________________
-void AliTOF::Hits2Digits(Int_t evNumber)
+void AliTOF::TOFHits2SDigits()
+{
 //
-//  Starting from the Hits Tree (TreeH), this
-// function writes the Digits Tree (TreeD) storing 
+// Starting from the Hits Tree (TreeH), this
+// function writes the TOF SDigits Branch in the TreeS storing 
 // the digits informations.
-// Has to be called just at the end of an event or 
+// It has to be called just at the end of an event or 
 // at the end of a whole run.
-//  It could  also be called by AliTOF::Finish Event()
-// but it can be too heavy.
+// It could  also be called by AliTOF::Finish Event()
 // Just for MC events. 
 //
-// Called by the macro H2D.C
+// Called by the ROOT script Hits2SDigits.C
 //
-{
-  AliTOFhit* currentHit;
-  TTree    *TD, *TH;
-  Int_t    tracks[3];
-  Int_t    vol[5];
-  Float_t  digit[2];
-  TClonesArray* TOFhits=this->Hits();
-
-  Int_t nparticles =  gAlice->GetNtrack();
-  if (nparticles <= 0) return;
-
-  TD = gAlice->TreeD();
-  TH = gAlice->TreeH();
-  Int_t    ntracks =(Int_t) TH->GetEntries();
-  Int_t    nbytes, nhits;
-  TRandom *rnd = new TRandom();
-
-  for (Int_t ntk=0; ntk<ntracks; ntk++){
-
-     nbytes = TH->GetEvent(ntk);
-     nhits  = TOFhits->GetEntriesFast();
-
-     for (Int_t hit=0; hit<nhits; hit++){
-
-        currentHit = (AliTOFhit*)(TOFhits->At(hit));
+// Simulation of detector response.
+
+   Int_t ver = this->IsVersion();
+   if(ver==0) return; // no sdigits for AliTOFv0
+
+  AliTOF *TOF = (AliTOF *) gAlice->GetDetector ("TOF");
+
+  if (fNevents == 0)
+    fNevents = (Int_t) gAlice->TreeE ()->GetEntries ();
+
+       cout << "nevents found on file " << fNevents << endl;
+      // Start Event ------------------------- LOOP
+
+  for (Int_t ievent = 0; ievent < fNevents; ievent++)
+    {
+      gAlice->GetEvent (ievent);
+      if (gAlice->TreeH () == 0)
+       return; // no hits stored 
+      if (gAlice->TreeS () == 0)
+       gAlice->MakeTree ("S");
+
+
+      Int_t nSdigits = 0;
+      
+            //Make branches
+      char branchname[20];
+       sprintf (branchname, "%s", TOF->GetName ());
+      //Make branch for TOF sdigits
+      TOF->MakeBranch ("S");
+
+      Int_t    tracks[3];    // track info
+      Int_t    vol[5];       // location for a digit
+      Float_t  digit[2];     // TOF digit variables
+      Int_t hit, nbytes;
+      TParticle *particle;
+      AliTOFhit *tofHit;
+      TClonesArray *TOFhits = TOF->Hits();
+
+
+      if (TOF)
+       {
+         TOFhits = TOF->Hits();             // pointer to the TClonesArray of TOF hits
+         TTree *TH = gAlice->TreeH();       // pointer to the current TreeH
+         Stat_t ntracks = TH->GetEntries(); // number of tracks for the current event
+         for (Int_t track = 0; track < ntracks; track++)
+           {
+             gAlice->ResetHits ();
+             nbytes += TH->GetEvent(track);
+             particle = gAlice->Particle(track);
+             Int_t nhits = TOFhits->GetEntriesFast(); // number of hits for the current track
+
+             for (hit = 0; hit < nhits; hit++)
+               {
+               tofHit = (AliTOFhit*) TOFhits->UncheckedAt(hit);
+               vol[0] = tofHit->GetSector();
+               vol[1] = tofHit->GetPlate();
+               vol[2] = tofHit->GetStrip();
+               vol[3] = tofHit->GetPadx();
+               vol[4] = tofHit->GetPadz();
+
+               // 95% of efficiency to be inserted here
+               // edge effect to be inserted here
+               // cross talk  to be inserted here
+
+               Float_t idealtime = tofHit->GetTof(); // unit s
+               idealtime *= 1.E+12;  // conversion from s to ps
+                              // fTimeRes is given usually in ps
+               Float_t tdctime   = gRandom->Gaus(idealtime, fTimeRes); 
+               digit[0] = tdctime;
+
+               // typical Landau Distribution to be inserted here
+               // instead of Gaussian Distribution
+               Float_t idealcharge = tofHit->GetEdep();
+               Float_t adccharge = gRandom->Gaus(idealcharge, fChrgRes);
+               digit[1] = adccharge;
+               Int_t tracknum = tofHit->GetTrack();
+               tracks[0] = tracknum;
+               tracks[1] = 0;
+               tracks[2] = 0;
+
+               // check if two digit are on the same pad; in that case we sum
+               // the two or more digits
+               Bool_t overlap = CheckOverlap(vol, digit, tracknum);
+               if(!overlap) 
+                       new ((*fSDigits)[nSdigits++]) AliTOFdigit(tracks, vol, digit);
+               //cout << "nSdigits" << endl; 
+               } // end loop on hits for the current track
+
+            } // end loop on ntracks
+
+         } // close if TOF switched ON
+
+      gAlice->TreeS()->Fill();
+      gAlice->TreeS()->Print();
+
+    }                          //event loop
 
-        vol[0] = currentHit->GetSector();
-        vol[1] = currentHit->GetPlate();
-        vol[2] = currentHit->GetPad_x();
-        vol[3] = currentHit->GetPad_z();
-        vol[4] = currentHit->GetStrip();
+}
 
-        Float_t IdealTime = currentHit->GetTof();
-        Float_t TDCTime   = rnd->Gaus(IdealTime, fTimeRes);    
-        digit[0] = TDCTime;
+//---------------------------------------------------------------------
 
-        Float_t IdealCharge = currentHit->GetEdep();
-        Float_t ADCcharge = rnd->Gaus(IdealCharge, fChrgRes);
-        digit[1] = ADCcharge;
+void AliTOF::Hits2SDigits()
+{
+//
+// Use the TOF SDigitizer to make TOF SDigits
+//
+//
+  //#ifdef DEBUG
+  cout<<"ALiTOF::Hits2SDigits> start...\n";
+  //#endif
+  
+  char * fileSDigits = 0 ;
+  char * fileHeader = 0;
+  AliTOFSDigitizer * sd = new AliTOFSDigitizer(fileHeader,fileSDigits) ;
 
-        Int_t Track = currentHit -> GetTrack();
-        tracks[0] = Track;
-       tracks[1] = 0;
-       tracks[2] = 0;
+  sd->Exec("") ;
+  sd->Print("");
 
-        Bool_t Overlap = CheckOverlap(vol, digit, Track);
-        if(!Overlap) AddDigit(tracks, vol, digit);
-     }
-  }
-  TD->Fill();
-  TD->Write();  
+  delete sd ;
+  
 }
-
 //___________________________________________________________________________
 Bool_t AliTOF::CheckOverlap(Int_t* vol, Float_t* digit,Int_t Track)
+{
 //
 // Checks if 2 or more hits belong to the same pad.
 // In this case the data assigned to the digit object
 // are the ones of the first hit in order of Time.
+// 2 hits from the same track on the same pad are collected.
+// Called only by Hits2SDigits.
+// This procedure has to be optimized in the next TOF release.
 //
-// Called only by Hits2Digits.
-//
-{
-        Bool_t Overlap = 0;
+
+        Bool_t overlap = kFALSE;
         Int_t  vol2[5];
 
-        for (Int_t ndig=0; ndig<fNdigits; ndig++){
-          AliTOFdigit* currentDigit = (AliTOFdigit*)(fDigits->UncheckedAt(ndig));
+        for (Int_t ndig=0; ndig<fSDigits->GetEntries(); ndig++){
+          AliTOFdigit* currentDigit = (AliTOFdigit*)(fSDigits->UncheckedAt(ndig));
            currentDigit->GetLocation(vol2);
-           Bool_t Idem=1;
+           Bool_t idem= kTRUE;
+          // check on digit volume
            for (Int_t i=0;i<=4;i++){
-               if (vol[i]!=vol2[i]) Idem=0;}
-           if (Idem){
-             Float_t TDC2 = digit[0];
-              Float_t TDC1 = currentDigit->GetTdc();
-              if (TDC1>TDC2){
-                  currentDigit->SetTdc(TDC2); 
-                  currentDigit->SetAdc(digit[1]);
-              }
-              currentDigit->AddTrack(Track);
-              Overlap = 1;
-           }
-        }
-        return Overlap;
+              if (!idem) break;
+               if (vol[i]!=vol2[i]) idem=kFALSE;}
+
+           if (idem){  // same pad fired
+             Float_t tdc2 = digit[0];
+              Float_t tdc1 = currentDigit->GetTdc();
+
+             // we separate two digits on the same pad if
+             // they are separated in time by at least 25 ns
+             // remember that tdc time is given in ps
+
+              if (TMath::Abs(tdc1-tdc2)<25000){
+                 // in case of overlap we take the earliest
+                 if (tdc1>tdc2){
+                   currentDigit->SetTdc(tdc2); 
+                   currentDigit->SetAdc(digit[1]);
+                 }
+                 else {
+                  currentDigit->SetTdc(tdc1);
+                  currentDigit->SetAdc(digit[1]);
+                 }
+                  currentDigit->AddTrack(Track); // add track number in the track array
+                  overlap = kTRUE;
+                 return overlap;
+              } else 
+               overlap= kFALSE;
+
+           } // close if (idem) -> two digits on the same TOF pad
+
+        } // end loop on existing sdigits
+
+        return overlap;
 }
 
 
 //____________________________________________________________________________
 void AliTOF::Digits2Raw(Int_t evNumber)
+{
 //
 // Starting from digits, writes the 
 // Raw Data objects, i.e. a 
 // TClonesArray of 18 AliTOFRawSector objects
 //
 
-{
-  TTree* TD;
+  TTree* tD;
 
+  // do nothing if no particles
   Int_t nparticles = gAlice->GetEvent(evNumber); 
   if (nparticles <= 0) return;
 
-  TD = gAlice->TreeD();
+  tD = gAlice->TreeD();
   
-  TClonesArray* TOFdigits = this->Digits();
-  Int_t ndigits = TOFdigits->GetEntriesFast();
+  TClonesArray* tofdigits = this->Digits();
+  Int_t ndigits = tofdigits->GetEntriesFast();
 
-  TClonesArray* Raw = new TClonesArray("AliTOFRawSector",fNTof+2); 
+  TClonesArray* rawsectors = new TClonesArray("AliTOFRawSector",fNTof+2); 
 
   for (Int_t isect=1;isect<=fNTof;isect++){
-     AliTOFRawSector* currentSector = (AliTOFRawSector*)Raw->UncheckedAt(isect);
-     TClonesArray* RocData = (TClonesArray*)currentSector->GetRocData();
+     AliTOFRawSector* currentSector = (AliTOFRawSector*)rawsectors->UncheckedAt(isect);
+     TClonesArray* rocData = (TClonesArray*)currentSector->GetRocData();
 
      for (Int_t digit=0; digit<ndigits; digit++){
-        AliTOFdigit* currentDigit = (AliTOFdigit*)TOFdigits->UncheckedAt(digit);
+        AliTOFdigit* currentDigit = (AliTOFdigit*)tofdigits->UncheckedAt(digit);
         Int_t sector = currentDigit->GetSector();
         if (sector==isect){
-           Int_t   Pad    = currentDigit -> GetTotPad();
-           Int_t   Roc    = (Int_t)(Pad/fNPadXRoc)-1;
-           if (Roc>=fNRoc) printf("Wrong n. of ROC ! Roc = %i",Roc);
-            Int_t   PadRoc = (Int_t) Pad%fNPadXRoc;
-           Int_t   Fec    = (Int_t)(PadRoc/fNFec)-1;
-            Int_t   Tdc    = (Int_t)(PadRoc%fNFec)-1;
-            Float_t Time   = currentDigit->GetTdc();
-            Float_t Charge = currentDigit->GetAdc();
-           AliTOFRoc* currentROC = (AliTOFRoc*)RocData->UncheckedAt(Roc);
-           Int_t Error    = 0;
-            currentROC->AddItem(Fec, Tdc, Error, Charge, Time);
-       }
-     }
+           Int_t   pad    = currentDigit -> GetTotPad();
+           Int_t   roc    = (Int_t)(pad/fNPadXRoc)-1;
+           if (roc>=fNRoc) printf("Wrong n. of ROC ! Roc = %i",roc);
+            Int_t   padRoc = (Int_t) pad%fNPadXRoc;
+           Int_t   fec    = (Int_t)(padRoc/fNFec)-1;
+            Int_t   tdc    = (Int_t)(padRoc%fNFec)-1;
+            Float_t time   = currentDigit->GetTdc();
+            Float_t charge = currentDigit->GetAdc();
+           AliTOFRoc* currentROC = (AliTOFRoc*)rocData->UncheckedAt(roc);
+           Int_t error    = 0;
+            currentROC->AddItem(fec, tdc, error, charge, time);
+       } // close if (sector==isect) i.e. end loop on digits for the current sector
+     } // end loop on TOF digits
      
-     UInt_t TotSize=16,RocSize=0;
-     UInt_t RocHead[14],RocChek[14];
-     UInt_t GlobalCheckSum=0;
+     UInt_t totSize=16,rocSize=0;
+     UInt_t rocHead[14],rocChek[14];
+     UInt_t globalCheckSum=0;
 
      for (UInt_t iRoc = 1; iRoc<(UInt_t)fNRoc; iRoc++){
-        AliTOFRoc* currentRoc = (AliTOFRoc*)RocData->UncheckedAt(iRoc); 
-       RocSize  = currentRoc->Items*2+1;
-       TotSize += RocSize*4;
-       if (RocSize>=TMath::Power(2,16)) RocSize=0;
-       RocHead[iRoc]   = iRoc<<28;
-       RocHead[iRoc]  += RocSize;
-       RocChek[iRoc]   = currentRoc->GetCheckSum();
-        Int_t HeadCheck = currentRoc->BitCount(RocHead[iRoc]);
-       GlobalCheckSum += HeadCheck;
-       GlobalCheckSum += RocChek[iRoc];
+        AliTOFRoc* currentRoc = (AliTOFRoc*)rocData->UncheckedAt(iRoc); 
+       rocSize  = currentRoc->GetItems()*2+1;
+       totSize += rocSize*4;
+       if (rocSize>=TMath::Power(2,16)) rocSize=0;
+       rocHead[iRoc]   = iRoc<<28;
+       rocHead[iRoc]  += rocSize;
+       rocChek[iRoc]   = currentRoc->GetCheckSum();
+        Int_t headCheck = currentRoc->BitCount(rocHead[iRoc]);
+       globalCheckSum += headCheck;
+       globalCheckSum += rocChek[iRoc];
      }
      
-     AliTOFRoc* DummyRoc = new AliTOFRoc();
-     TotSize *= 4;
-     if (TotSize>=TMath::Power(2,24)) TotSize=0;
-     UInt_t Header = TotSize;
-     UInt_t SectId = ((UInt_t)isect)<<24;
-     Header += SectId;
-     GlobalCheckSum += DummyRoc->BitCount(Header);
-     currentSector->SetGlobalCS(GlobalCheckSum);
-     currentSector->SetHeader(Header);
+     AliTOFRoc* dummyRoc = new AliTOFRoc();
+     totSize *= 4;
+     if (totSize>=TMath::Power(2,24)) totSize=0;
+     UInt_t header = totSize;
+     UInt_t sectId = ((UInt_t)isect)<<24;
+     header += sectId;
+     globalCheckSum += dummyRoc->BitCount(header);
+     currentSector->SetGlobalCS(globalCheckSum);
+     currentSector->SetHeader(header);
   }  
 }
  
 //____________________________________________________________________________
 void AliTOF::Raw2Digits(Int_t evNumber)
+{
 //
 //  Converts Raw Data objects into digits objects.
 //  We schematize the raw data with a 
 //  TClonesArray of 18 AliTOFRawSector objects
 //
-{
-  TTree    *TD;
+
+  TTree    *tD;
   Int_t    vol[5];
   Int_t    tracks[3];
   Float_t  digit[2];
@@ -621,15 +1015,15 @@ void AliTOF::Raw2Digits(Int_t evNumber)
   Int_t nparticles = gAlice->GetEvent(evNumber); 
   if (nparticles <= 0) return;
 
-  TD = gAlice->TreeD();
+  tD = gAlice->TreeD();
   
-  TClonesArray* Raw = new TClonesArray("AliTOFRawSector",fNTof+2);
+  TClonesArray* rawsectors = new TClonesArray("AliTOFRawSector",fNTof+2);
   
   for(Int_t nSec=1; nSec<=fNTof; nSec++){
-     AliTOFRawSector* currentSector = (AliTOFRawSector*)Raw->UncheckedAt(nSec);
-     TClonesArray* RocData = (TClonesArray*)currentSector->GetRocData();
+     AliTOFRawSector* currentSector = (AliTOFRawSector*)rawsectors->UncheckedAt(nSec);
+     TClonesArray* rocData = (TClonesArray*)currentSector->GetRocData();
      for(Int_t nRoc=1; nRoc<=14; nRoc++){
-        AliTOFRoc* currentRoc = (AliTOFRoc*)RocData->UncheckedAt(nRoc);
+        AliTOFRoc* currentRoc = (AliTOFRoc*)rocData->UncheckedAt(nRoc);
         Int_t currentItems = currentRoc->GetItems();
         for(Int_t item=1; item<currentItems; item++){ 
            Int_t nPad = currentRoc->GetTotPad(item);        
@@ -653,155 +1047,22 @@ void AliTOF::Raw2Digits(Int_t evNumber)
                   break;
           }
            vol[2] = nStrip;
-           Int_t Pad = nPad%fPadXStr;
-          if (Pad==0) Pad=fPadXStr;
+           Int_t pad = nPad%fPadXStr;
+          if (pad==0) pad=fPadXStr;
           Int_t nPadX=0, nPadZ=0;
-          (Pad>fNpadX)? nPadX -= fNpadX : nPadX = Pad ;
+          (pad>fNpadX)? nPadX -= fNpadX : nPadX = pad ;
           vol[3] = nPadX;
-          (Pad>fNpadX)? nPadZ = 2 : nPadZ = 1 ;
+          (pad>fNpadX)? nPadZ = 2 : nPadZ = 1 ;
           vol[4] = nPadZ;
           UInt_t error=0;
-          Float_t TDC = currentRoc->GetTime(item,error);
-          if (!error) digit[0]=TDC;
+          Float_t tdc = currentRoc->GetTime(item,error);
+          if (!error) digit[0]=tdc;
           digit[1] = currentRoc->GetCharge(item);
           AddDigit(tracks,vol,digit);
         }
      }
   }
-  TD->Fill();
-  TD->Write();
+  tD->Fill();
+  tD->Write(0,TObject::kOverwrite);
 } 
 
-
-/******************************************************************************/
-
-ClassImp(AliTOFhit)
-//______________________________________________________________________________
-AliTOFhit::AliTOFhit(Int_t shunt, Int_t track, Int_t *vol,
-                     Float_t *hits)
-:AliHit(shunt, track)
-//
-// Constructor of hit object
-//
-{
-  //
-  // Store a TOF hit
-  // _______________
-  //
-  // Hit Volume
-  // 
-  fSector= vol[0];
-  fPlate = vol[1];
-  fStrip = vol[2];
-  fPad_x = vol[3];
-  fPad_z = vol[4];
-  //
-  //Position
-  fX = hits[0];
-  fY = hits[1];
-  fZ = hits[2];
-  //
-  // Momentum
-  fPx  = hits[3];
-  fPy  = hits[4];
-  fPz  = hits[5];
-  fPmom= hits[6];
-  //
-  // Time Of Flight
-  fTof = hits[7];   //TOF[s]
-  //
-  // Other Data
-  fDx  = hits[8];   //Distance from the edge along x axis
-  fDy  = hits[9];   //Y cohordinate of the hit
-  fDz  = hits[10];  //Distance from the edge along z axis
-  fIncA= hits[11];  //Incidence angle
-  fEdep= hits[12];  //Energy loss in TOF pad
-}
-
-//******************************************************************************
-
-ClassImp(AliTOFdigit)
-
-//______________________________________________________________________________
-AliTOFdigit::AliTOFdigit(Int_t *tracks, Int_t *vol,Float_t *digit)
-:AliDigit(tracks)
-//
-// Constructor of digit object
-//
-{
-  fSector = vol[0];
-  fPlate  = vol[1];
-  fStrip  = vol[2];
-  fPad_x  = vol[3];
-  fPad_z  = vol[4];
-  fTdc    = digit[0];
-  fAdc    = digit[1];
-}
-
-//______________________________________________________________________________
-void AliTOFdigit::GetLocation(Int_t *Loc)
-//
-// Get the cohordinates of the digit
-// in terms of Sector - Plate - Strip - Pad
-//
-{
-   Loc[0]=fSector;
-   Loc[1]=fPlate;
-   Loc[2]=fStrip;
-   Loc[3]=fPad_x;
-   Loc[4]=fPad_z;
-}
-
-//______________________________________________________________________________
-Int_t AliTOFdigit::GetTotPad()
-//
-// Get the "total" index of the pad inside a Sector
-// starting from the digits data.
-//
-{
-  AliTOF* TOF;
-  
-  if(gAlice){
-     TOF =(AliTOF*) gAlice->GetDetector("TOF");
-  }else{
-     printf("AliTOFdigit::GetTotPad - No AliRun object present, exiting");
-     return 0;
-  }
-  
-  Int_t Pad = fPad_x+TOF->fNpadX*(fPad_z-1);
-  Int_t Before=0;
-
-  switch(fPlate){ 
-  case 1: Before = 0;
-          break;
-  case 2: Before = TOF->fNStripC;
-          break;
-  case 3: Before = TOF->fNStripB + TOF->fNStripC;
-          break;
-  case 4: Before = TOF->fNStripA + TOF->fNStripB + TOF->fNStripC;
-          break;
-  case 5: Before = TOF->fNStripA + 2*TOF->fNStripB + TOF->fNStripC;
-          break;
-  }
-  
-  Int_t Strip = fStrip+Before;
-  Int_t PadTot = TOF->fPadXStr*(Strip-1)+Pad;
-  return PadTot;
-}
-
-//______________________________________________________________________________
-void AliTOFdigit::AddTrack(Int_t track)
-//
-// Add a track to the digit 
-//
-{
-  if (fTracks[1]==0){
-     fTracks[1] = track;
-  }else if (fTracks[2]==0){
-     fTracks[2] = track;
-  }else{
-  //   printf("AliTOFdigit::AddTrack ERROR: Too many Tracks (>3) \n");
-  }
-}
-