]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFSDigitizer.cxx
Coding conventions (Annalisa)
[u/mrichter/AliRoot.git] / TOF / AliTOFSDigitizer.cxx
index 38d3788a56a2cc0d7c06293ccf3f6f869f3e977a..aeec5069e2ede2c1b8fc501eb164cf49467a1a23 100644 (file)
@@ -13,6 +13,8 @@
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+/* $Id$ */
+
 //_________________________________________________________________________
 // This is a TTask that constructs SDigits out of Hits
 // A Summable Digits is the "sum" of all hits in a pad
 // Use case: see AliTOFhits2sdigits.C macro in the CVS
 //////////////////////////////////////////////////////////////////////////////
 
-#include "TBenchmark.h"
-#include "TTask.h"
-#include "TTree.h"
-#include "TSystem.h"
-#include "TFile.h"
-#include "TParticle.h"
-#include "TH1.h"
+#include <Riostream.h>
+#include <stdlib.h>
 
-#include "AliTOFHitMap.h"
-#include "AliTOFSDigit.h"
-#include "AliTOFConstants.h"
-#include "AliTOFhit.h"
-#include "AliTOF.h"
-#include "AliTOFv1.h"
-#include "AliTOFv2.h"
-#include "AliTOFv3.h"
-#include "AliTOFv4.h"
-#include "AliTOFSDigitizer.h"
-#include "AliRun.h"
+#include <TBenchmark.h>
+#include <TF1.h>
+#include <TFile.h>
+#include <TFolder.h>
+#include <TH1.h>
+#include <TParticle.h>
+#include <TROOT.h>
+#include <TSystem.h>
+#include <TTask.h>
+#include <TTree.h>
+
+#include "AliLog.h"
 #include "AliDetector.h"
+#include "AliLoader.h"
 #include "AliMC.h"
+#include "AliRun.h"
+#include "AliRunLoader.h"
 
-#include "TFile.h"
-#include "TTask.h"
-#include "TTree.h"
-#include "TSystem.h"
-#include "TROOT.h"
-#include "TFolder.h"
-#include <TF1.h>
-#include <stdlib.h>
-#include <iostream.h>
-#include <fstream.h>
+#include "AliTOF.h"
+#include "AliTOFGeometry.h"
+#include "AliTOFHitMap.h"
+#include "AliTOFSDigit.h"
+#include "AliTOFSDigitizer.h"
+#include "AliTOFhit.h"
+#include "AliTOFhitT0.h"
 
 ClassImp(AliTOFSDigitizer)
 
 //____________________________________________________________________________ 
-  AliTOFSDigitizer::AliTOFSDigitizer():TTask("AliTOFSDigitizer","") 
+  AliTOFSDigitizer::AliTOFSDigitizer():TTask("TOFSDigitizer","") 
 {
   // ctor
-  fSDigits = 0 ;
-  fEvent1=0;
-  fEvent2=0;
-  ftail    = 0;
+
+  fRunLoader      = 0;
+  fTOFLoader      = 0;
+
+  fEvent1         = 0;
+  fEvent2         = 0;
+  ftail           = 0;
+  fSelectedSector = -1;
+  fSelectedPlate  = -1;
+
+  fTOFGeometry = new AliTOFGeometry();
+
 }
-           
+
+//------------------------------------------------------------------------
+AliTOFSDigitizer::AliTOFSDigitizer(const AliTOFSDigitizer &source)
+  :TTask(source)
+{
+  // copy constructor
+  this->fTOFGeometry=source.fTOFGeometry;
+
+}
+
 //____________________________________________________________________________ 
-  AliTOFSDigitizer::AliTOFSDigitizer(char* HeaderFile,char *SdigitsFile, Int_t evNumber1, Int_t nEvents):TTask("AliTOFSDigitizer","") 
+AliTOFSDigitizer& AliTOFSDigitizer::operator=(const AliTOFSDigitizer &source)
 {
-  fEvent1=evNumber1;
-  fEvent2=fEvent1+nEvents;
-  fSDigits = 0;
-  ftail    = 0;
+  // ass. op.
+  this->fTOFGeometry=source.fTOFGeometry;
+  return *this;
 
+}
+
+//____________________________________________________________________________ 
+AliTOFSDigitizer::AliTOFSDigitizer(const char* HeaderFile, Int_t evNumber1, Int_t nEvents):TTask("TOFSDigitizer","")
+{
+  //ctor, reading from input file 
+  ftail    = 0;
+  fSelectedSector=-1; // by default we sdigitize all sectors
+  fSelectedPlate =-1; // by default we sdigitize all plates in all sectors
+  
   fHeadersFile = HeaderFile ; // input filename (with hits)
-  fSDigitsFile = SdigitsFile; // output filename for sdigits
-  TFile * file = (TFile*) gROOT->GetFile(fHeadersFile.Data() ) ;
-
-  //File was not opened yet
-  // open file and get alirun object
-  if(file == 0){
-      file =    TFile::Open(fHeadersFile.Data(),"update") ;
-      gAlice = (AliRun *) file->Get("gAlice") ;
+  TFile * file = (TFile*) gROOT->GetFile(fHeadersFile.Data());
+  
+  //File was not opened yet open file and get alirun object
+  if (file == 0) {
+    file   = TFile::Open(fHeadersFile.Data(),"update") ;
+    gAlice = (AliRun *) file->Get("gAlice") ;
+  }
+  
+  // add Task to //root/Tasks folder
+  TString evfoldname = AliConfig::GetDefaultEventFolderName();
+  fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
+  if (!fRunLoader)
+    fRunLoader = AliRunLoader::Open(HeaderFile);//open session and mount on default event folder
+  if (fRunLoader == 0x0)
+    {
+      AliFatal("Event is not loaded. Exiting");
+      return;
+    }
+
+  fRunLoader->CdGAFile();
+  TDirectory *savedir=gDirectory;
+  TFile *in=(TFile*)gFile;
+
+  if (!in->IsOpen()) {
+    AliWarning("Geometry file is not open default TOF geometry will be used");
+    fTOFGeometry = new AliTOFGeometry();
+  }
+  else {
+    in->cd();
+    fTOFGeometry = (AliTOFGeometry*)in->Get("TOFgeometry");
   }
 
+  savedir->cd();
+
+  if (fRunLoader->TreeE() == 0x0) fRunLoader->LoadHeader();
+  
+  if (evNumber1>=0) fEvent1 = evNumber1;
+  else fEvent1=0;
+  
+  if (nEvents==0) fEvent2 = (Int_t)(fRunLoader->GetNumberOfEvents());
+  else if (nEvents>0) fEvent2 = evNumber1+nEvents;
+  else fEvent2 = 1;
+  
+  if (!(fEvent2>fEvent1)) {
+    AliError(Form("fEvent2 = %d <= fEvent1 = %d", fEvent2, fEvent1));
+    fEvent1 = 0;
+    fEvent2 = 1;
+    AliError(Form("Correction: fEvent2 = %d <= fEvent1 = %d", fEvent2, fEvent1));
+  }
+  
   // init parameters for sdigitization
   InitParameters();
-
-  // add Task to //root/Tasks folder
-  TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ; 
-  roottasks->Add(this) ; 
+  
+  fTOFLoader = fRunLoader->GetLoader("TOFLoader");
+  if (fTOFLoader == 0x0)
+    {
+      AliFatal("Can not find TOF loader in event. Exiting.");
+      return;
+    }
+  fTOFLoader->PostSDigitizer(this);
 }
 
 //____________________________________________________________________________ 
-  AliTOFSDigitizer::~AliTOFSDigitizer()
+AliTOFSDigitizer::~AliTOFSDigitizer()
 {
   // dtor
-  if (ftail)
-    {
-      delete ftail;
-      ftail = 0;
-    }
+  fTOFLoader->CleanSDigitizer();
+
+  delete fTOFGeometry;
+
 }
 
 //____________________________________________________________________________ 
 void AliTOFSDigitizer::InitParameters()
 {
   // set parameters for detector simulation
-
-  fTimeResolution =0.120;
-  fpadefficiency  =0.99 ;
+  
+  fTimeResolution = 0.080; //0.120; OLD
+  fpadefficiency  = 0.99 ;
   fEdgeEffect     = 2   ;
   fEdgeTails      = 0   ;
   fHparameter     = 0.4 ;
@@ -123,9 +190,12 @@ void AliTOFSDigitizer::InitParameters()
   fEffBoundary    = 0.65;
   fEff2Boundary   = 0.90;
   fEff3Boundary   = 0.08;
-  fResCenter      = 50. ;
+  fAddTRes        = 68. ; // \sqrt{2x20^2 + 15^2 + 2x10^2 + 30^2 + 50^2} (p-p)
+  //fAddTRes      = 48. ; // \sqrt{2x20^2 + 15^2 + 2x10^2 + 30^2 + 15^2} (Pb-Pb)
+  // 30^2+20^2+40^2+50^2+50^2+50^2 = 10400 ps^2 (very old value)
+  fResCenter      = 35. ; //50. ; // OLD
   fResBoundary    = 70. ;
-  fResSlope       = 40. ;
+  fResSlope       = 37. ; //40. ; // OLD
   fTimeWalkCenter = 0.  ;
   fTimeWalkBoundary=0.  ;
   fTimeWalkSlope  = 0.  ;
@@ -138,8 +208,8 @@ void AliTOFSDigitizer::InitParameters()
   fLogChargeSmearing=0.13;
   fTimeSmearing   =0.022;
   fAverageTimeFlag=0    ;
-  fTdcBin   = 50.;      // 1 TDC bin = 50 ps
-  fAdcBin   = 0.25;     // 1 ADC bin = 0.25 pC (or 0.03 pC)
+
+  fAdcBin   = 0.25;    // 1 ADC bin = 0.25 pC (or 0.03 pC)
   fAdcMean  = 50.;     // ADC distribution mpv value for Landau (in bins)
                        // it corresponds to a mean value of ~100 bins
   fAdcRms   = 25.;     // ADC distribution rms value (in bins)
@@ -162,22 +232,14 @@ Double_t TimeWithTail(Double_t* x, Double_t* par)
   return f;
 }
 
-
 //____________________________________________________________________________
-void AliTOFSDigitizer::Exec(Option_t *verboseOption, Option_t *allEvents) { 
-
-  if(strstr(verboseOption,"tim") || strstr(verboseOption,"all"))
+void AliTOFSDigitizer::Exec(Option_t *verboseOption) { 
+  //execute TOF sdigitization
+  if (strstr(verboseOption,"tim") || strstr(verboseOption,"all"))
     gBenchmark->Start("TOFSDigitizer");
 
-  AliTOF *TOF = (AliTOF *) gAlice->GetDetector ("TOF");
-
-  if (!TOF) {
-    Error("AliTOFSDigitizer","TOF not found");
-    return;
-  }
-
   if (fEdgeTails) ftail = new TF1("tail",TimeWithTail,-2,2,3);
-
+  
   Int_t nselectedHits=0;
   Int_t ntotalsdigits=0;
   Int_t ntotalupdates=0;
@@ -187,14 +249,47 @@ void AliTOFSDigitizer::Exec(Option_t *verboseOption, Option_t *allEvents) {
   Int_t nHitsFromSec=0;
   Int_t nlargeTofDiff=0;
 
-  if (strstr(allEvents,"all")){
-    fEvent1=0;
-    fEvent2= (Int_t) gAlice->TreeE()->GetEntries();
+  Bool_t thereIsNotASelection=(fSelectedSector==-1) && (fSelectedPlate==-1);
+
+  if (fRunLoader->GetAliRun() == 0x0) fRunLoader->LoadgAlice();
+  gAlice = fRunLoader->GetAliRun();
+
+  fRunLoader->LoadKinematics();
+  
+  AliTOF *tof = (AliTOF *) gAlice->GetDetector("TOF");
+  
+  if (!tof) {
+    AliError("TOF not found");
+    return;
   }
+  
+  fTOFLoader->LoadHits("read");
+  fTOFLoader->LoadSDigits("recreate");
+  
+  for (Int_t iEvent=fEvent1; iEvent<fEvent2; iEvent++) {
+//     cout << "------------------- "<< GetName() << " ------------- \n";
+//     cout << "Sdigitizing event " << iEvent << endl;
+
+    fRunLoader->GetEvent(iEvent);
+
+    TTree *hitTree = fTOFLoader->TreeH ();
+    if (!hitTree) return;
+
+    if (fTOFLoader->TreeS () == 0) fTOFLoader->MakeTree ("S");
+    
+    //Make branch for digits
+    tof->MakeBranch("S");
+    
+    // recreate TClonesArray fSDigits - for backward compatibility
+    if (tof->SDigits() == 0) {
+      tof->CreateSDigitsArray();
+    } else {
+      tof->RecreateSDigitsArray();
+    }
 
-  for (Int_t ievent = fEvent1; ievent < fEvent2; ievent++) {
-    cout << "------------------- "<< GetName() << " -------------" << endl ;
-    cout << "Sdigitizing event " << ievent << endl;
+    tof->SetTreeAddress();
+
+    Int_t version=tof->IsVersion();
 
     Int_t nselectedHitsinEv=0;
     Int_t ntotalsdigitsinEv=0;
@@ -202,164 +297,176 @@ void AliTOFSDigitizer::Exec(Option_t *verboseOption, Option_t *allEvents) {
     Int_t nnoisesdigitsinEv=0;
     Int_t nsignalsdigitsinEv=0;
 
-    gAlice->GetEvent(ievent);
-    TTree *TH = gAlice->TreeH ();
-    if (!TH)
-      return;
-    if (gAlice->TreeS () == 0)
-      gAlice->MakeTree ("S");
-
-      
-    //Make branches
-    char branchname[20];
-    sprintf (branchname, "%s", TOF->GetName ());
-    //Make branch for digits
-    TOF->MakeBranch ("S");
-    
-    //Now made SDigits from hits
-
-    Int_t    vol[5];       // location for a digit
-    Float_t  digit[2];     // TOF digit variables
     TParticle *particle;
-    AliTOFhit *tofHit;
-    TClonesArray *TOFhits = TOF->Hits();
+    //AliTOFhit *tofHit;
+    TClonesArray *tofHitArray = tof->Hits();
 
     // create hit map
-    AliTOFHitMap *hitMap = new AliTOFHitMap(TOF->SDigits());
+    AliTOFHitMap *hitMap = new AliTOFHitMap(tof->SDigits(), fTOFGeometry);
 
-    Int_t ntracks = static_cast<Int_t>(TH->GetEntries());
+    TBranch * tofHitsBranch = hitTree->GetBranch("TOF");
+
+    Int_t ntracks = static_cast<Int_t>(hitTree->GetEntries());
     for (Int_t track = 0; track < ntracks; track++)
     {
       gAlice->ResetHits();
-      TH->GetEvent(track);
-      particle = gAlice->Particle(track);
-      Int_t nhits = TOFhits->GetEntriesFast();
+      tofHitsBranch->GetEvent(track);
+      particle = gAlice->GetMCApp()->Particle(track);
+      Int_t nhits = tofHitArray->GetEntriesFast();
       // cleaning all hits of the same track in the same pad volume
       // it is a rare event, however it happens
 
-      Int_t previousTrack =0;
-      Int_t previousSector=0;
-      Int_t previousPlate =0;
-      Int_t previousStrip =0;
-      Int_t previousPadX  =0;
-      Int_t previousPadZ  =0;
-
-      for (Int_t hit = 0; hit < nhits; hit++)
-      {
-       tofHit = (AliTOFhit *) TOFhits->UncheckedAt(hit);
-       Int_t tracknum = tofHit->GetTrack();
-       vol[0] = tofHit->GetSector();
-       vol[1] = tofHit->GetPlate();
-       vol[2] = tofHit->GetStrip();
-       vol[3] = tofHit->GetPadx();
-       vol[4] = tofHit->GetPadz();
-
-       Bool_t dummy=((tracknum==previousTrack) && (vol[0]==previousSector) && (vol[1]==previousPlate) && (vol[2]==previousStrip));
-
-       Bool_t isCloneOfThePrevious=dummy && ((vol[3]==previousPadX) && (vol[4]==previousPadZ));
-
-       // much stronger check to be inserted here
-
-       if(!isCloneOfThePrevious){
-         // update "previous" values
-         // in fact, we are yet in the future, so the present is past
-         previousTrack=tracknum;
-         previousSector=vol[0];
-         previousPlate=vol[1];
-         previousStrip=vol[2];
-         previousPadX=vol[3];
-         previousPadZ=vol[4];
-
-         nselectedHits++;
-         nselectedHitsinEv++;
-         if (particle->GetFirstMother() < 0){
-           nHitsFromPrim++;
-         } // counts hits due to primary particles
-
-         Float_t Xpad = tofHit->GetDx();
-         Float_t Zpad = tofHit->GetDz();
-         Float_t xStrip=AliTOFConstants::fgkXPad*(vol[3]-0.5-0.5*AliTOFConstants::fgkNpadX)+Xpad;
-         Float_t zStrip=AliTOFConstants::fgkZPad*(vol[4]-0.5-0.5*AliTOFConstants::fgkNpadZ)+Zpad;
-         Float_t geantTime = tofHit->GetTof(); // unit [s]
-         geantTime *= 1.e+09;  // conversion from [s] to [ns]
-
-         //cout << "geantTime " << geantTime << " [ns]" << endl;
-         Int_t nActivatedPads = 0, nFiredPads = 0;
-         Bool_t isFired[4] = {kFALSE, kFALSE, kFALSE, kFALSE};
-         Float_t tofAfterSimul[4] = {0., 0., 0., 0.};
-         Float_t qInduced[4] = {0.,0.,0.,0.};
-         Int_t nPlace[4] = {0, 0, 0, 0};
-         Float_t averageTime = 0.;
-         SimulateDetectorResponse(zStrip,xStrip,geantTime,nActivatedPads,nFiredPads,isFired,nPlace,qInduced,tofAfterSimul,averageTime);
-         if(nFiredPads) {
-           for(Int_t indexOfPad=0; indexOfPad<nActivatedPads; indexOfPad++) {
-             if(isFired[indexOfPad]){ // the pad has fired
-               Float_t timediff=geantTime-tofAfterSimul[indexOfPad];
-
-               if(timediff>=0.2) nlargeTofDiff++;
-
-               digit[0] = (Int_t) ((tofAfterSimul[indexOfPad]*1.e+03)/fTdcBin); // TDC bin number (each bin -> 25. ps)
-
-               Float_t landauFactor = gRandom->Landau(fAdcMean, fAdcRms); 
-               digit[1] = (Int_t) (qInduced[indexOfPad] * landauFactor); // ADC bins (each bin -> 0.25 (or 0.03) pC)
-
-               // recalculate the volume only for neighbouring pads
-               if(indexOfPad){
-                 (nPlace[indexOfPad]<=AliTOFConstants::fgkNpadX) ? vol[4] = 1 : vol[4] = 2;
-                 (nPlace[indexOfPad]<=AliTOFConstants::fgkNpadX) ? vol[3] = nPlace[indexOfPad] : vol[3] = nPlace[indexOfPad] - AliTOFConstants::fgkNpadX;
-               }
-
-               // check if two sdigit are on the same pad; in that case we sum
-               // the two or more sdigits
-               if (hitMap->TestHit(vol) != kEmpty) {
-                 AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(hitMap->GetHit(vol));
-                 Int_t tdctime = (Int_t) digit[0];
-                 Int_t adccharge = (Int_t) digit[1];
-                 sdig->Update(tdctime,adccharge,tracknum);
-                 ntotalupdatesinEv++;
-                 ntotalupdates++;
-               } else {
-
-                 TOF->AddSDigit(tracknum, vol, digit);
-
+      Int_t previousTrack =-1;
+      Int_t previousSector=-1;
+      Int_t previousPlate =-1;
+      Int_t previousStrip =-1;
+      Int_t previousPadX  =-1;
+      Int_t previousPadZ  =-1;
+
+      for (Int_t hit = 0; hit < nhits; hit++) {
+       Int_t    vol[5];       // location for a digit
+       Float_t  digit[2];     // TOF digit variables
+       Int_t tracknum;
+       Float_t dxPad;
+       Float_t dzPad;
+       Float_t geantTime;
+
+       // fp: really sorry for this, it is a temporary trick to have
+       // track length too
+       if(version!=6 && version!=7){
+         AliTOFhit *tofHit = (AliTOFhit *) tofHitArray->UncheckedAt(hit);
+         tracknum = tofHit->GetTrack();
+         vol[0] = tofHit->GetSector();
+         vol[1] = tofHit->GetPlate();
+         vol[2] = tofHit->GetStrip();
+         vol[3] = tofHit->GetPadx();
+         vol[4] = tofHit->GetPadz();
+         dxPad = tofHit->GetDx();
+         dzPad = tofHit->GetDz();
+         geantTime = tofHit->GetTof(); // unit [s]
+       } else {
+         AliTOFhitT0 *tofHit = (AliTOFhitT0 *) tofHitArray->UncheckedAt(hit);
+         tracknum = tofHit->GetTrack();
+         vol[0] = tofHit->GetSector();
+         vol[1] = tofHit->GetPlate();
+         vol[2] = tofHit->GetStrip();
+         vol[3] = tofHit->GetPadx();
+         vol[4] = tofHit->GetPadz();
+         dxPad = tofHit->GetDx();
+         dzPad = tofHit->GetDz();
+         geantTime = tofHit->GetTof(); // unit [s]
+       }
+       
+       geantTime *= 1.e+09;  // conversion from [s] to [ns]
+       
+       // selection case for sdigitizing only hits in a given plate of a given sector
+       if(thereIsNotASelection || (vol[0]==fSelectedSector && vol[1]==fSelectedPlate)){
+         
+         Bool_t dummy=((tracknum==previousTrack) && (vol[0]==previousSector) && (vol[1]==previousPlate) && (vol[2]==previousStrip));
+         
+         Bool_t isCloneOfThePrevious=dummy && ((vol[3]==previousPadX) && (vol[4]==previousPadZ));
+         
+         Bool_t isNeighOfThePrevious=dummy && ((((vol[3]==previousPadX-1) || (vol[3]==previousPadX+1)) && (vol[4]==previousPadZ)) || ((vol[3]==previousPadX) && ((vol[4]==previousPadZ+1) || (vol[4]==previousPadZ-1))));
+         
+         if(!isCloneOfThePrevious && !isNeighOfThePrevious){
+           // update "previous" values
+           // in fact, we are yet in the future, so the present is past
+           previousTrack=tracknum;
+           previousSector=vol[0];
+           previousPlate=vol[1];
+           previousStrip=vol[2];
+           previousPadX=vol[3];
+           previousPadZ=vol[4];
+           
+           nselectedHits++;
+           nselectedHitsinEv++;
+           if (particle->GetFirstMother() < 0) nHitsFromPrim++; // counts hits due to primary particles
+           
+           Float_t xStrip=AliTOFGeometry::XPad()*(vol[3]+0.5-0.5*AliTOFGeometry::NpadX())+dxPad;
+           Float_t zStrip=AliTOFGeometry::ZPad()*(vol[4]+0.5-0.5*AliTOFGeometry::NpadZ())+dzPad;
+
+           Int_t nActivatedPads = 0, nFiredPads = 0;
+           Bool_t isFired[4] = {kFALSE, kFALSE, kFALSE, kFALSE};
+           Float_t tofAfterSimul[4] = {0., 0., 0., 0.};
+           Float_t qInduced[4] = {0.,0.,0.,0.};
+           Int_t nPlace[4] = {0, 0, 0, 0};
+           Float_t averageTime = 0.;
+           SimulateDetectorResponse(zStrip,xStrip,geantTime,nActivatedPads,nFiredPads,isFired,nPlace,qInduced,tofAfterSimul,averageTime);
+           if(nFiredPads) {
+             for(Int_t indexOfPad=0; indexOfPad<nActivatedPads; indexOfPad++) {
+               if(isFired[indexOfPad]){ // the pad has fired
+                 Float_t timediff=geantTime-tofAfterSimul[indexOfPad];
+                 
+                 if(timediff>=0.2) nlargeTofDiff++;
+                 
+                 digit[0] = (Int_t) ((tofAfterSimul[indexOfPad]*1.e+03)/AliTOFGeometry::TdcBinWidth()); // TDC bin number (each bin -> 24.4 ps)
+                 
+                 Float_t landauFactor = gRandom->Landau(fAdcMean, fAdcRms); 
+                 digit[1] = (Int_t) (qInduced[indexOfPad] * landauFactor); // ADC bins (each bin -> 0.25 (or 0.03) pC)
+
+                 // recalculate the volume only for neighbouring pads
                  if(indexOfPad){
-                   nnoisesdigits++;
-                   nnoisesdigitsinEv++;
-                 } else {
-                   nsignalsdigits++;
-                   nsignalsdigitsinEv++;
+                   (nPlace[indexOfPad]<=AliTOFGeometry::NpadX()) ? vol[4] = 0 : vol[4] = 1;
+                   (nPlace[indexOfPad]<=AliTOFGeometry::NpadX()) ? vol[3] = nPlace[indexOfPad] - 1 : vol[3] = nPlace[indexOfPad] - AliTOFGeometry::NpadX() - 1;
                  }
-                 ntotalsdigitsinEv++;  
-                 ntotalsdigits++;
-                 hitMap->SetHit(vol);
-               } // if (hitMap->TestHit(vol) != kEmpty)
-             } // if(isFired[indexOfPad])
-           } // end loop on nActivatedPads
-         } // if(nFiredPads) i.e. if some pads has fired
-       } // close if(!isCloneOfThePrevious)
+                 // check if two sdigit are on the same pad;
+                 // in that case we sum the two or more sdigits
+                 if (hitMap->TestHit(vol) != kEmpty) {
+                   AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(hitMap->GetHit(vol));
+                   Int_t tdctime = (Int_t) digit[0];
+                   Int_t adccharge = (Int_t) digit[1];
+                   sdig->Update(AliTOFGeometry::TdcBinWidth(),tdctime,adccharge,tracknum);
+                   ntotalupdatesinEv++;
+                   ntotalupdates++;
+                 } else {
+                   
+                   tof->AddSDigit(tracknum, vol, digit);
+                   
+                   if(indexOfPad){
+                     nnoisesdigits++;
+                     nnoisesdigitsinEv++;
+                   } else {
+                     nsignalsdigits++;
+                     nsignalsdigitsinEv++;
+                   }
+                   ntotalsdigitsinEv++;  
+                   ntotalsdigits++;
+                   hitMap->SetHit(vol);
+                 } // if (hitMap->TestHit(vol) != kEmpty)
+               } // if(isFired[indexOfPad])
+             } // end loop on nActivatedPads
+           } // if(nFiredPads) i.e. if some pads has fired
+         } // close if(!isCloneOfThePrevious)
+       } // close the selection on sector and plate
       } // end loop on hits for the current track
     } // end loop on ntracks
-
+    
     delete hitMap;
-      
-    gAlice->TreeS()->Reset();
-    gAlice->TreeS()->Fill();
-    //gAlice->TreeS()->Write(0,TObject::kOverwrite) ;
-    gAlice->TreeS()->AutoSave();
-
-    if(strstr(verboseOption,"all")){
-      cout << "----------------------------------------" << endl;
-      cout << "       <AliTOFSDigitizer>     " << endl;
-      cout << "After sdigitizing " << nselectedHitsinEv << " hits" << " in event " << ievent << endl;
+    
+    fTOFLoader->TreeS()->Reset();
+    fTOFLoader->TreeS()->Fill();
+    fTOFLoader->WriteSDigits("OVERWRITE");
+    
+    if (tof->SDigits()) tof->ResetSDigits();
+    
+    if (strstr(verboseOption,"all")) {
+      AliInfo("----------------------------------------");
+      AliInfo("       <AliTOFSDigitizer>    ");
+      AliInfo(Form("After sdigitizing %d hits in event %d", nselectedHitsinEv, iEvent));
       //" (" << nHitsFromPrim << " from primaries and " << nHitsFromSec << " from secondaries) TOF hits, " 
-      cout << ntotalsdigitsinEv << " digits have been created " << endl;
-      cout << "(" << nsignalsdigitsinEv << " due to signals and " <<  nnoisesdigitsinEv << " due to border effect)" << endl;
-      cout << ntotalupdatesinEv << " total updates of the hit map have been performed in current event" << endl;
-      cout << "----------------------------------------" << endl;
+      AliInfo(Form("%d digits have been created", ntotalsdigitsinEv));
+      AliInfo(Form("(%d due to signals and %d due to border effect)", nsignalsdigitsinEv, nnoisesdigitsinEv));
+      AliInfo(Form("%d total updates of the hit map have been performed in current event", ntotalupdatesinEv));
+      AliInfo("----------------------------------------");
     }
 
   } //event loop on events
 
+    fTOFLoader->UnloadSDigits();
+    fTOFLoader->UnloadHits();
+    fRunLoader->UnloadKinematics();
+    //fRunLoader->UnloadgAlice();
+
   // free used memory
   if (ftail){
     delete ftail;
@@ -368,47 +475,52 @@ void AliTOFSDigitizer::Exec(Option_t *verboseOption, Option_t *allEvents) {
   
   nHitsFromSec=nselectedHits-nHitsFromPrim;
   if(strstr(verboseOption,"all")){
-    cout << "----------------------------------------" << endl;
-    cout << "----------------------------------------" << endl;
-    cout << "-----------SDigitization Summary--------" << endl;
-    cout << "       <AliTOFSDigitizer>     " << endl;
-    cout << "After sdigitizing " << nselectedHits << " hits" << endl;
-    cout << "in " << (fEvent2-fEvent1) << " events" << endl;
+    AliInfo("----------------------------------------");
+    AliInfo("----------------------------------------");
+    AliInfo("-----------SDigitization Summary--------");
+    AliInfo("       <AliTOFSDigitizer>     ");
+    AliInfo(Form("After sdigitizing %d hits", nselectedHits));
+    AliInfo(Form("in %d events", fEvent2-fEvent1));
 //" (" << nHitsFromPrim << " from primaries and " << nHitsFromSec << " from secondaries) TOF hits, " 
-    cout << ntotalsdigits << " sdigits have been created " << endl;
-    cout << "(" << nsignalsdigits << " due to signals and " <<  nnoisesdigits << " due to border effect)" << endl;
-    cout << ntotalupdates << " total updates of the hit map have been performed" << endl;
-    cout << "in " << nlargeTofDiff << " cases the time of flight difference is greater than 200 ps" << endl;
+    AliInfo(Form("%d sdigits have been created", ntotalsdigits));
+    AliInfo(Form("(%d due to signals and " 
+                "%d due to border effect)", nsignalsdigits, nnoisesdigits));
+    AliInfo(Form("%d total updates of the hit map have been performed", ntotalupdates));
+    AliInfo(Form("in %d cases the time of flight difference is greater than 200 ps", nlargeTofDiff));
   }
 
 
   if(strstr(verboseOption,"tim") || strstr(verboseOption,"all")){
     gBenchmark->Stop("TOFSDigitizer");
-    cout << "AliTOFSDigitizer:" << endl ;
-    cout << "   took " << gBenchmark->GetCpuTime("TOFSDigitizer") << " seconds in order to make sdigits " 
-        <<  gBenchmark->GetCpuTime("TOFSDigitizer")/(fEvent2-fEvent1) << " seconds per event " << endl ;
-    cout << endl ;
+    AliInfo("AliTOFSDigitizer:");
+    AliInfo(Form("   took %f seconds in order to make sdigits " 
+        "%f seconds per event", gBenchmark->GetCpuTime("TOFSDigitizer"), gBenchmark->GetCpuTime("TOFSDigitizer")/(fEvent2-fEvent1)));
+    AliInfo(" +++++++++++++++++++++++++++++++++++++++++++++++++++ ");
   }
 
-  Print("");
 }
+
 //__________________________________________________________________
-void AliTOFSDigitizer::SetSDigitsFile(char * file ){
-  if(!fSDigitsFile.IsNull())
-    cout << "Changing SDigits file from " <<(char *)fSDigitsFile.Data() << " to " << file << endl ;
-  fSDigitsFile=file ;
+void AliTOFSDigitizer::Print(Option_t* /*opt*/)const
+{
+  cout << "------------------- "<< GetName() << " ------------- \n";
 }
 
 //__________________________________________________________________
-void AliTOFSDigitizer::Print(Option_t* opt)const
+void AliTOFSDigitizer::SelectSectorAndPlate(Int_t sector, Int_t plate)
 {
-  cout << "------------------- "<< GetName() << " -------------" << endl ;
-  if(fSDigitsFile.IsNull())
-    cout << " Writing SDigits to file with hits "<< endl ;
-  else
-    cout << "    Writing SDigits to file  " << (char*) fSDigitsFile.Data() << endl ;
-  cout << "--------------------------------------------------" << endl;
+  //Select sector and plate
+  Bool_t isaWrongSelection=(sector < 0) || (sector >= AliTOFGeometry::NSectors()) || (plate < 0) || (plate >= AliTOFGeometry::NPlates());
+  if(isaWrongSelection){
+    AliError("You have selected an invalid value for sector or plate ");
+    AliError(Form("The correct range for sector is [0,%d]", AliTOFGeometry::NSectors()-1));
+    AliError(Form("The correct range for plate  is [0,%d]",  AliTOFGeometry::NPlates()-1));
+    AliError("By default we continue sdigitizing all hits in all plates of all sectors");
+  } else {
+    fSelectedSector=sector;
+    fSelectedPlate =plate;
+    AliInfo(Form("SDigitizing only hits in plate %d of the sector %d", fSelectedPlate, fSelectedSector));
+  }
 }
 
 //__________________________________________________________________
@@ -442,7 +554,7 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
   //         qCenterPad - charge extimated for each pad, arb. units
   //         weightsSum - sum of weights extimated for each pad fired, arb. units
   
-  const Float_t kSigmaForTail[2] = {AliTOFConstants::fgkSigmaForTail1,AliTOFConstants::fgkSigmaForTail2}; //for tail                                                   
+  const Float_t kSigmaForTail[2] = {AliTOFGeometry::SigmaForTail1(),AliTOFGeometry::SigmaForTail2()}; //for tail                                                   
   Int_t iz = 0, ix = 0;
   Float_t dX = 0., dZ = 0., x = 0., z = 0.;
   Float_t h = fHparameter, h2 = fH2parameter, k = fKparameter, k2 = fK2parameter;
@@ -462,17 +574,17 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
   nFiredPads = 0;
   
   (z0 <= 0) ? iz = 0 : iz = 1;
-  dZ = z0 + (0.5 * AliTOFConstants::fgkNpadZ - iz - 0.5) * AliTOFConstants::fgkZPad; // hit position in the pad frame, (0,0) - center of the pad
-  z = 0.5 * AliTOFConstants::fgkZPad - TMath::Abs(dZ);                               // variable for eff., res. and timeWalk. functions
-  iz++;                                                                              // z row: 1, ..., AliTOFConstants::fgkNpadZ = 2
-  ix = (Int_t)((x0 + 0.5 * AliTOFConstants::fgkNpadX * AliTOFConstants::fgkXPad) / AliTOFConstants::fgkXPad);
-  dX = x0 + (0.5 * AliTOFConstants::fgkNpadX - ix - 0.5) * AliTOFConstants::fgkXPad; // hit position in the pad frame, (0,0) - center of the pad
-  x = 0.5 * AliTOFConstants::fgkXPad - TMath::Abs(dX);                               // variable for eff., res. and timeWalk. functions;
-  ix++;                                                                              // x row: 1, ..., AliTOFConstants::fgkNpadX = 48
+  dZ = z0 + (0.5 * AliTOFGeometry::NpadZ() - iz - 0.5) * AliTOFGeometry::ZPad(); // hit position in the pad frame, (0,0) - center of the pad
+  z = 0.5 * AliTOFGeometry::ZPad() - TMath::Abs(dZ);                               // variable for eff., res. and timeWalk. functions
+  iz++;                                                                              // z row: 1, ..., AliTOFGeometry::NpadZ = 2
+  ix = (Int_t)((x0 + 0.5 * AliTOFGeometry::NpadX() * AliTOFGeometry::XPad()) / AliTOFGeometry::XPad());
+  dX = x0 + (0.5 * AliTOFGeometry::NpadX() - ix - 0.5) * AliTOFGeometry::XPad(); // hit position in the pad frame, (0,0) - center of the pad
+  x = 0.5 * AliTOFGeometry::XPad() - TMath::Abs(dX);                               // variable for eff., res. and timeWalk. functions;
+  ix++;                                                                              // x row: 1, ..., AliTOFGeometry::NpadX = 48
   
   ////// Pad A:
   nActivatedPads++;
-  nPlace[nActivatedPads-1] = (iz - 1) * AliTOFConstants::fgkNpadX + ix;
+  nPlace[nActivatedPads-1] = (iz - 1) * AliTOFGeometry::NpadX() + ix;
   qInduced[nActivatedPads-1] = qCenterPad;
   padId[nActivatedPads-1] = 1;
   
@@ -480,7 +592,7 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
     eff[nActivatedPads-1] = fEffCenter;
     if (gRandom->Rndm() < eff[nActivatedPads-1]) {
       nFiredPads = 1;
-      res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + fResCenter * fResCenter); // 10400=30^2+20^2+40^2+50^2+50^2+50^2  ns;
+      res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + fResCenter * fResCenter); // ns
       isFired[nActivatedPads-1] = kTRUE;
       tofTime[nActivatedPads-1] = gRandom->Gaus(geantTime + fTimeWalkCenter, res[0]);
       averageTime = tofTime[nActivatedPads-1];
@@ -518,7 +630,7 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
     }
     
     (effZ<effX) ? eff[nActivatedPads-1] = effZ : eff[nActivatedPads-1] = effX;
-    (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2  ns
+    (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resZ * resZ); // ns
     (timeWalkZ<timeWalkX) ? timeWalk[nActivatedPads-1] = 0.001 *  timeWalkZ : timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
 
 
@@ -534,9 +646,9 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
     if(z < k && z > 0) {
       if( (iz == 1 && dZ > 0) || (iz == 2 && dZ < 0) ) {
        nActivatedPads++;
-       nPlace[nActivatedPads-1] = nPlace[0] + (3 - 2 * iz) * AliTOFConstants::fgkNpadX;
+       nPlace[nActivatedPads-1] = nPlace[0] + (3 - 2 * iz) * AliTOFGeometry::NpadX();
        eff[nActivatedPads-1] = effZ;
-       res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns 
+       res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resZ * resZ); // ns 
        timeWalk[nActivatedPads-1] = 0.001 * timeWalkZ; // ns
        nTail[nActivatedPads-1] = 2;
        if (fTimeDelayFlag) {
@@ -568,7 +680,7 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
        nActivatedPads++;
        nPlace[nActivatedPads-1] = nPlace[0] - 1;
        eff[nActivatedPads-1] = effX;
-       res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns 
+       res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resX * resX); // ns 
        timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
        nTail[nActivatedPads-1] = 2;
        if (fTimeDelayFlag) {
@@ -586,9 +698,9 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
        if(z < k && z > 0) {
          if( (iz == 1 && dZ > 0) || (iz == 2 && dZ < 0) ) {
            nActivatedPads++;
-           nPlace[nActivatedPads-1] = nPlace[0] + (3 - 2 * iz) * AliTOFConstants::fgkNpadX - 1;
+           nPlace[nActivatedPads-1] = nPlace[0] + (3 - 2 * iz) * AliTOFGeometry::NpadX() - 1;
            eff[nActivatedPads-1] = effX * effZ;
-           (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns
+           (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resZ * resZ); // ns
            (timeWalkZ<timeWalkX) ? timeWalk[nActivatedPads-1] = 0.001 * timeWalkZ : timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
            
            nTail[nActivatedPads-1] = 2;
@@ -614,11 +726,11 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
       }  // end C
       
       //   E:
-      if(ix < AliTOFConstants::fgkNpadX && dX > 0) {
+      if(ix < AliTOFGeometry::NpadX() && dX > 0) {
        nActivatedPads++;
        nPlace[nActivatedPads-1] = nPlace[0] + 1;
        eff[nActivatedPads-1] = effX;
-       res[nActivatedPads-1] = 0.001 * (TMath::Sqrt(10400 + resX * resX)); // ns
+       res[nActivatedPads-1] = 0.001 * (TMath::Sqrt(fAddTRes*fAddTRes + resX * resX)); // ns
        timeWalk[nActivatedPads-1] = 0.001 * timeWalkX; // ns
        nTail[nActivatedPads-1] = 2;
        if (fTimeDelayFlag) {
@@ -637,9 +749,9 @@ void AliTOFSDigitizer::SimulateDetectorResponse(Float_t z0, Float_t x0, Float_t
        if(z < k && z > 0) {
          if( (iz == 1 && dZ > 0) || (iz == 2 && dZ < 0) ) {
            nActivatedPads++;
-           nPlace[nActivatedPads - 1] = nPlace[0] + (3 - 2 * iz) * AliTOFConstants::fgkNpadX + 1;
+           nPlace[nActivatedPads - 1] = nPlace[0] + (3 - 2 * iz) * AliTOFGeometry::NpadX() + 1;
            eff[nActivatedPads - 1] = effX * effZ;
-           (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(10400 + resZ * resZ); // 10400=30^2+20^2+40^2+50^2+50^2+50^2 ns
+           (resZ<resX) ? res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resX * resX) : res[nActivatedPads-1] = 0.001 * TMath::Sqrt(fAddTRes*fAddTRes + resZ * resZ); // ns
            (timeWalkZ<timeWalkX) ? timeWalk[nActivatedPads-1] = 0.001 * timeWalkZ : timeWalk[nActivatedPads-1] = 0.001*timeWalkX; // ns
            nTail[nActivatedPads-1] = 2;
            if (fTimeDelayFlag) {
@@ -722,7 +834,7 @@ void AliTOFSDigitizer::PrintParameters()const
   cout << " Time walk (ps) at the boundary of the pad       : "<< fTimeWalkBoundary<< endl;
   cout << " Slope (ps/K) for neighbouring pad               : "<< fTimeWalkSlope<<endl;
   cout << " Pulse Heigth Simulation Parameters " << endl;
-  cout << " Flag for delay due to the PulseHeightEffect: "<< fTimeDelayFlag <<endl;
+  cout << " Flag for delay due to the PulseHeightEffect  : "<< fTimeDelayFlag <<endl;
   cout << " Pulse Height Slope                           : "<< fPulseHeightSlope<<endl;
   cout << " Time Delay Slope                             : "<< fTimeDelaySlope<<endl;
   cout << " Minimum charge amount which could be induced : "<< fMinimumCharge<<endl;