]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSsimulationSDD.cxx
Typo in the calculation of procetile-target distance is corrected
[u/mrichter/AliRoot.git] / ITS / AliITSsimulationSDD.cxx
index 2d702cb1cebf990bbf65f09f0715668756922c8e..5b737e4f87adab9fb23a3f1d8e4b15faa3c65c0f 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-
 #include <iostream.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
+#include <TSystem.h>
+#include <TROOT.h>
 #include <TStopwatch.h>
 #include <TCanvas.h>
 #include <TF1.h>
@@ -34,6 +35,7 @@
 #include "AliITShit.h"
 #include "AliITSdigit.h"
 #include "AliITSmodule.h"
+#include "AliITSpList.h"
 #include "AliITSMapA1.h"
 #include "AliITSMapA2.h"
 #include "AliITSetfSDD.h"
@@ -41,6 +43,8 @@
 #include "AliITSHuffman.h"
 #include "AliITSsegmentation.h"
 #include "AliITSresponse.h"
+#include "AliITSsegmentationSDD.h"
+#include "AliITSresponseSDD.h"
 #include "AliITSsimulationSDD.h"
 
 ClassImp(AliITSsimulationSDD)
@@ -62,259 +66,290 @@ ClassImp(AliITSsimulationSDD)
 <pre>
 */
 //End_Html
-//_____________________________________________________________________________
-
+//______________________________________________________________________
 Int_t power(Int_t b, Int_t e) {
-  // compute b to the e power, where both b and e are Int_ts.
-  Int_t power = 1,i;
-  for(i=0; i<e; i++) power *= b;
-  return power;
-}
-
-//_____________________________________________
+    // compute b to the e power, where both b and e are Int_ts.
+    Int_t power = 1,i;
 
+    for(i=0; i<e; i++) power *= b;
+    return power;
+}
+//______________________________________________________________________
 void FastFourierTransform(AliITSetfSDD *alisddetf,Double_t *real,
                           Double_t *imag,Int_t direction) {
-  // Do a Fast Fourier Transform
-  //printf("FFT: direction %d\n",direction);
-
-  Int_t samples = alisddetf->GetSamples();
-  Int_t l = (Int_t) ((log((Float_t) samples)/log(2.))+0.5);
-  Int_t m1 = samples;
-  Int_t m  = samples/2;
-  Int_t m2 = samples/m1;
-  Int_t i,j,k;
-  for(i=1; i<=l; i++) {
-    for(j=0; j<samples; j += m1) {
-      Int_t p = 0;
-      for(k=j; k<= j+m-1; k++) {
-       Double_t wsr = alisddetf->GetWeightReal(p);
-        Double_t wsi = alisddetf->GetWeightImag(p);
-       if(direction == -1) wsi = -wsi;
-       Double_t xr = *(real+k+m);
-       Double_t xi = *(imag+k+m);
-       *(real+k+m) = wsr*(*(real+k)-xr) - wsi*(*(imag+k)-xi);
-       *(imag+k+m) = wsr*(*(imag+k)-xi) + wsi*(*(real+k)-xr);
-       *(real+k) += xr;
-       *(imag+k) += xi;
-       p += m2;
-      }
-    }
-    m1 = m;
-    m /= 2;
-    m2 += m2;
-  } 
+    // Do a Fast Fourier Transform
+
+    Int_t samples = alisddetf->GetSamples();
+    Int_t l = (Int_t) ((log((Float_t) samples)/log(2.))+0.5);
+    Int_t m1 = samples;
+    Int_t m  = samples/2;
+    Int_t m2 = samples/m1;
+    Int_t i,j,k;
+    for(i=1; i<=l; i++) {
+       for(j=0; j<samples; j += m1) {
+           Int_t p = 0;
+           for(k=j; k<= j+m-1; k++) {
+               Double_t wsr = alisddetf->GetWeightReal(p);
+               Double_t wsi = alisddetf->GetWeightImag(p);
+               if(direction == -1) wsi = -wsi;
+               Double_t xr = *(real+k+m);
+               Double_t xi = *(imag+k+m);
+               *(real+k+m) = wsr*(*(real+k)-xr) - wsi*(*(imag+k)-xi);
+               *(imag+k+m) = wsr*(*(imag+k)-xi) + wsi*(*(real+k)-xr);
+               *(real+k) += xr;
+               *(imag+k) += xi;
+               p += m2;
+           } // end for k
+       } // end for j
+       m1 = m;
+       m /= 2;
+       m2 += m2;
+    } // end for i
   
-  for(j=0; j<samples; j++) {
-    Int_t j1 = j;
-    Int_t p = 0;
-    Int_t i1;
-    for(i1=1; i1<=l; i1++) {
-      Int_t j2 = j1;
-      j1 /= 2;
-      p = p + p + j2 - j1 - j1;
-    }
-    if(p >= j) {
-      Double_t xr = *(real+j);
-      Double_t xi = *(imag+j);
-      *(real+j) = *(real+p);
-      *(imag+j) = *(imag+p);
-      *(real+p) = xr;
-      *(imag+p) = xi;
-    }
-  }
-  if(direction == -1) {
-    for(i=0; i<samples; i++) {
-      *(real+i) /= samples;
-      *(imag+i) /= samples;
-    }
-  }
+    for(j=0; j<samples; j++) {
+       Int_t j1 = j;
+       Int_t p = 0;
+       Int_t i1;
+       for(i1=1; i1<=l; i1++) {
+           Int_t j2 = j1;
+           j1 /= 2;
+           p = p + p + j2 - j1 - j1;
+       } // end for i1
+       if(p >= j) {
+           Double_t xr = *(real+j);
+           Double_t xi = *(imag+j);
+           *(real+j) = *(real+p);
+           *(imag+j) = *(imag+p);
+           *(real+p) = xr;
+           *(imag+p) = xi;
+       } // end if p>=j
+    } // end for j
+    if(direction == -1) {
+       for(i=0; i<samples; i++) {
+           *(real+i) /= samples;
+           *(imag+i) /= samples;
+       } // end for i
+    } // end if direction == -1
   return;
 }
-//_____________________________________________________________________________
-
+//______________________________________________________________________
 AliITSsimulationSDD::AliITSsimulationSDD(){
-  // Default constructor
-
-  fResponse = 0;
-  fSegmentation = 0;
-  fHis = 0;
-  fHitMap1 = 0;
-  fHitMap2 = 0;
-  fElectronics = 0;
-  fStream = 0;
-  fD.Set(0);
-  fT1.Set(0);
-  fT2.Set(0);
-  fTol.Set(0);
-  fNoise.Set(0);
-  fBaseline.Set(0);
-  SetScaleFourier();
-  SetPerpendTracksFlag();
-  SetDoFFT();
-  SetCheckNoise();
-  fInZR = 0;
-  fInZI = 0;
-  fOutZR = 0;
-  fOutZI = 0;
-  fNofMaps = 0;
-  fMaxNofSamples = 0;
-  fITS = 0;
-  fTreeB=0;
+    // Default constructor
+
+    fResponse      = 0;
+    fSegmentation  = 0;
+    fHis           = 0;
+    fHitMap1       = 0;
+    fHitMap2       = 0;
+    fElectronics   = 0;
+    fStream        = 0;
+    fInZR          = 0;
+    fInZI          = 0;
+    fOutZR         = 0;
+    fOutZI         = 0;
+    fNofMaps       = 0;
+    fMaxNofSamples = 0;
+    fITS           = 0;
+    fTreeB         = 0;
+    SetScaleFourier();
+    SetPerpendTracksFlag();
+    SetDoFFT();
+    SetCheckNoise();
+}
+//______________________________________________________________________
+AliITSsimulationSDD::AliITSsimulationSDD(AliITSsimulationSDD &source){
+    // Copy constructor to satify Coding roules only.
+
+    if(this==&source) return;
+    Error("AliITSsimulationSSD","Not allowed to make a copy of "
+         "AliITSsimulationSDD Using default creater instead");
+    AliITSsimulationSDD();
 }
-//_____________________________________________________________________________
-AliITSsimulationSDD::AliITSsimulationSDD(AliITSsimulationSDD &source)
-{
-  // Copy constructor to satify Coding roules only.
-  if(this==&source) return;
-  printf("Not allowed to make a copy of AliITSsimulationSDD "
-         "Using default creater instead\n");
-  AliITSsimulationSDD();
+//______________________________________________________________________
+AliITSsimulationSDD& AliITSsimulationSDD::operator=(AliITSsimulationSDD &src){
+    // Assignment operator to satify Coding roules only.
+
+    if(this==&src) return *this;
+    Error("AliITSsimulationSSD","Not allowed to make a = with "
+         "AliITSsimulationSDD Using default creater instead");
+    return *this ;
 }
-//_____________________________________________________________________________
-AliITSsimulationSDD& AliITSsimulationSDD::operator=(AliITSsimulationSDD &source)
-{
-  // Assignment operator to satify Coding roules only.
-  if(this==&source) return *this;
-  printf("Not allowed to make a = with AliITSsimulationSDD "
-         "Using default creater instead\n");
-  return *this ;
+//______________________________________________________________________
+AliITSsimulationSDD::AliITSsimulationSDD(AliITSsegmentation *seg,
+                                        AliITSresponse *resp){
+    // Standard Constructor
+
+
+    fResponse      = 0;
+    fSegmentation  = 0;
+    fHis           = 0;
+    fHitMap1       = 0;
+    fHitMap2       = 0;
+    fElectronics   = 0;
+    fStream        = 0;
+    fInZR          = 0;
+    fInZI          = 0;
+    fOutZR         = 0;
+    fOutZI         = 0;
+    fNofMaps       = 0;
+    fMaxNofSamples = 0;
+    fITS           = 0;
+    fTreeB         = 0;
+
+    Init((AliITSsegmentationSDD*)seg,(AliITSresponseSDD*)resp);
 }
-//_____________________________________________________________________________
-
-AliITSsimulationSDD::AliITSsimulationSDD(AliITSsegmentation *seg,AliITSresponse *resp) 
-{
-  // Standard Constructor
-
-      fHis=0;
-      fTreeB=0;
-      fResponse = resp;
-      fSegmentation = seg;
-      SetScaleFourier();
-      SetPerpendTracksFlag();
-      SetDoFFT();
-      SetCheckNoise();
-
-      fHitMap2 = new AliITSMapA2(fSegmentation,fScaleSize,1);
-      fHitMap1 = new AliITSMapA1(fSegmentation);
-
-      //
-      fNofMaps=fSegmentation->Npz();
-      fMaxNofSamples=fSegmentation->Npx();
-
-      Float_t sddLength = fSegmentation->Dx();
-      Float_t sddWidth = fSegmentation->Dz();
-
-      Int_t dummy=0;
-      Float_t anodePitch = fSegmentation->Dpz(dummy);
-      Double_t timeStep = (Double_t)fSegmentation->Dpx(dummy);
-      Float_t driftSpeed=fResponse->DriftSpeed();    
-
-      if(anodePitch*(fNofMaps/2) > sddWidth) {
-         Warning("AliITSsimulationSDD",
-           "Too many anodes %d or too big pitch %f \n",fNofMaps/2,anodePitch);
-      }
-
-      if(timeStep*fMaxNofSamples < sddLength/driftSpeed) {
-         Error("AliITSsimulationSDD",
-                             "Time Interval > Allowed Time Interval: exit\n");
-         return;
-      }
-
-      fElectronics = new AliITSetfSDD(timeStep/fScaleSize);
-
-      char opt1[20], opt2[20];
-      fResponse->ParamOptions(opt1,opt2);
-      fParam=opt2;
-      char *same = strstr(opt1,"same");
-      if (same) {
-         fNoise.Set(0);
-         fBaseline.Set(0);
-      } else {
-         fNoise.Set(fNofMaps);
-         fBaseline.Set(fNofMaps);
-      }
-      
-      //
-      const char *kopt=fResponse->ZeroSuppOption();
-        if (strstr(fParam,"file") ) {
-         fD.Set(fNofMaps);
-         fT1.Set(fNofMaps);
-          if (strstr(kopt,"2D")) {
+//______________________________________________________________________
+void AliITSsimulationSDD::Init(AliITSsegmentationSDD *seg,
+                              AliITSresponseSDD *resp){
+    // Standard Constructor
+
+    fResponse     = resp;
+    fSegmentation = seg;
+    SetScaleFourier();
+    SetPerpendTracksFlag();
+    SetDoFFT();
+    SetCheckNoise();
+
+    fHitMap2 = new AliITSMapA2(fSegmentation,fScaleSize,1);
+    fHitMap1 = new AliITSMapA1(fSegmentation);
+
+    fNofMaps = fSegmentation->Npz();
+    fMaxNofSamples = fSegmentation->Npx();
+
+    Float_t sddLength = fSegmentation->Dx();
+    Float_t sddWidth  = fSegmentation->Dz();
+
+    Int_t dummy        = 0;
+    Float_t anodePitch = fSegmentation->Dpz(dummy);
+    Double_t timeStep  = (Double_t)fSegmentation->Dpx(dummy);
+    Float_t driftSpeed = fResponse->DriftSpeed();
+
+    if(anodePitch*(fNofMaps/2) > sddWidth) {
+       Warning("AliITSsimulationSDD",
+               "Too many anodes %d or too big pitch %f \n",
+               fNofMaps/2,anodePitch);
+    } // end if
+
+    if(timeStep*fMaxNofSamples < sddLength/driftSpeed) {
+       Error("AliITSsimulationSDD",
+             "Time Interval > Allowed Time Interval: exit\n");
+       return;
+    } // end if
+
+    fElectronics = new AliITSetfSDD(timeStep/fScaleSize,
+                                   fResponse->Electronics());
+
+    char opt1[20], opt2[20];
+    fResponse->ParamOptions(opt1,opt2);
+    fParam = opt2;
+    char *same = strstr(opt1,"same");
+    if (same) {
+       fNoise.Set(0);
+       fBaseline.Set(0);
+    } else {
+       fNoise.Set(fNofMaps);
+       fBaseline.Set(fNofMaps);
+    } // end if
+
+    const char *kopt=fResponse->ZeroSuppOption();
+    if (strstr(fParam,"file") ) {
+       fD.Set(fNofMaps);
+       fT1.Set(fNofMaps);
+       if (strstr(kopt,"2D")) {
            fT2.Set(fNofMaps);
             fTol.Set(0);
             Init2D();       // desactivate if param change module by module
-          } else if(strstr(kopt,"1D"))  {
+       } else if(strstr(kopt,"1D"))  {
             fT2.Set(2);
             fTol.Set(2);
             Init1D();      // desactivate if param change module by module
-         }
-       } else {
-          fD.Set(2);
-         fTol.Set(2);
-         fT1.Set(2);
-         fT2.Set(2);
-         SetCompressParam();
-       }
+       } // end if strstr
+    } else {
+       fD.Set(2);
+       fTol.Set(2);
+       fT1.Set(2);
+       fT2.Set(2);
+       SetCompressParam();
+    } // end if else strstr
 
+    Bool_t write = fResponse->OutputOption();
+    if(write && strstr(kopt,"2D")) MakeTreeB();
 
-       Bool_t write=fResponse->OutputOption();
-       if(write && strstr(kopt,"2D")) MakeTreeB();
+    // call here if baseline does not change by module
+    // ReadBaseline();
 
-        // call here if baseline does not change by module
-        // ReadBaseline();
+    fITS       = (AliITS*)gAlice->GetModule("ITS");
+    Int_t size = fNofMaps*fMaxNofSamples;
+    fStream    = new AliITSInStream(size);
 
-        fITS = (AliITS*)gAlice->GetModule("ITS");
-        Int_t size=fNofMaps*fMaxNofSamples;
-       fStream = new AliITSInStream(size); 
-       
-       fInZR = new Double_t [fScaleSize*fMaxNofSamples];
-       fInZI = new Double_t [fScaleSize*fMaxNofSamples];
-       fOutZR = new Double_t [fScaleSize*fMaxNofSamples];
-       fOutZI = new Double_t [fScaleSize*fMaxNofSamples];  
+    fInZR  = new Double_t [fScaleSize*fMaxNofSamples];
+    fInZI  = new Double_t [fScaleSize*fMaxNofSamples];
+    fOutZR = new Double_t [fScaleSize*fMaxNofSamples];
+    fOutZI = new Double_t [fScaleSize*fMaxNofSamples];  
 
 }
+//______________________________________________________________________
+AliITSsimulationSDD::~AliITSsimulationSDD() { 
+    // destructor
+
+    delete fHitMap1;
+    delete fHitMap2;
+    delete fStream;
+    delete fElectronics;
+
+    fITS = 0;
+
+    if (fHis) {
+       fHis->Delete(); 
+       delete fHis;     
+    } // end if fHis
+    if(fTreeB) delete fTreeB;           
+    if(fInZR)  delete [] fInZR;
+    if(fInZI)  delete [] fInZI;        
+    if(fOutZR) delete [] fOutZR;
+    if(fOutZI) delete [] fOutZI;
+}
+//______________________________________________________________________
+void AliITSsimulationSDD::SDigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
+    // create maps to build the lists of tracks for each summable digit
 
+    TObjArray *fHits = mod->GetHits();
+    Int_t nhits      = fHits->GetEntriesFast();
+    fModule          = md;
+    fEvent           = ev;
 
-//_____________________________________________________________________________
+    if(!nhits) return;
 
-AliITSsimulationSDD::~AliITSsimulationSDD() { 
-  // destructor
-
-  delete fHitMap1;
-  delete fHitMap2;
-  delete fStream;
-  delete fElectronics;
-
-  fD.Set(0);
-  fT1.Set(0);
-  fT2.Set(0);
-  fTol.Set(0);
-  fNoise.Set(0);
-  fBaseline.Set(0);
-  fITS = 0;
-
-  if (fHis) {
-     fHis->Delete(); 
-     delete fHis;     
-  }     
-  if(fTreeB) delete fTreeB;           
-  if(fInZR) delete [] fInZR;
-  if(fInZI) delete [] fInZI;   
-  if(fOutZR) delete [] fOutZR;
-  if(fOutZI) delete [] fOutZI;
-}
-//_____________________________________________________________________________
+    AliITSpList *pList = new AliITSpList(2*fSegmentation->Npz(),
+                                        fScaleSize*fSegmentation->Npx());
 
-void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
-  // create maps to build the lists of tracks
-  // for each digit
+    // inputs to ListOfFiredCells.
+    TObjArray          *alist = new TObjArray();
+    fHitMap1->SetArray(alist);
+    static TClonesArray *padr = 0;
+    if(!padr)            padr = new TClonesArray("TVector",1000);
+
+    HitsToAnalogDigits(mod,alist,padr,pList);
 
-    fModule=md;
-    fEvent=ev;
+    WriteSDigits(pList);
+
+    // clean memory
+    delete pList;
+    alist->Delete();
+    delete alist;
+    padr->Delete();
+    fHitMap1->ClearMap();
+    fHitMap2->ClearMap();
+}
+//______________________________________________________________________
+void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
+    // create maps to build the lists of tracks for each digit
 
     TObjArray *fHits = mod->GetHits();
-    Int_t nhits = fHits->GetEntriesFast();
+    Int_t nhits      = fHits->GetEntriesFast();
+    fModule          = md;
+    fEvent           = ev;
+
     if (!nhits && fCheckNoise) {
         ChargeToSignal();
         GetNoise();
@@ -322,350 +357,423 @@ void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
         return;
     } else if (!nhits) return;
 
-    //printf("simSDD: module nhits %d %d\n",md,nhits);
-
+    AliITSpList *pList = new AliITSpList(2*fSegmentation->Npz(),
+                                        fScaleSize*fSegmentation->Npx());
 
-    TObjArray *list=new TObjArray;
-    static TClonesArray *padr=0;
-    if(!padr) padr=new TClonesArray("TVector",1000);
-    Int_t arg[6] = {0,0,0,0,0,0}; 
-    fHitMap1->SetArray(list);
+    // inputs to ListOfFiredCells.
+    TObjArray          *alist = new TObjArray();
+    fHitMap1->SetArray(alist);
+    static TClonesArray *padr = 0;
+    if(!padr)            padr = new TClonesArray("TVector",1000);
 
+    HitsToAnalogDigits(mod,alist,padr,pList);
 
-    Int_t nofAnodes=fNofMaps/2;
+    FinishDigits(alist);
 
-    Float_t sddLength = fSegmentation->Dx();
-    Float_t sddWidth = fSegmentation->Dz();
-
-    Int_t dummy=0;
-    Float_t anodePitch = fSegmentation->Dpz(dummy);
-    Float_t timeStep = fSegmentation->Dpx(dummy);
+    // clean memory
+    delete pList;
+    alist->Delete();
+    delete alist;
+    padr->Delete();
+    fHitMap1->ClearMap();
+    fHitMap2->ClearMap();
+}
+//______________________________________________________________________
+void AliITSsimulationSDD::SDigitsToDigits(AliITSpList *pList){
+    // Take Summable digits and create Digits.
+
+    // inputs to ListOfFiredCells.
+    TObjArray          *alist = new TObjArray();
+    fHitMap1->SetArray(alist);
+    static TClonesArray *padr = 0;
+    if(!padr)            padr = new TClonesArray("TVector",1000);
+    Int_t              arg[6] = {0,0,0,0,0,0};
+    Double_t  timeAmplitude;
+    Int_t i,j;
+
+    // Fill maps from pList.
+    for(i=0;i<pList->GetMaxIndex();i++){
+       pList->GetMapIndex(i,arg[0],arg[1]);
+       for(j=0;j<pList->GetNEnteries();j++){
+           timeAmplitude = pList->GetTSignal(arg[0],arg[1],j);
+           if(timeAmplitude>0.0) continue;
+           arg[2] = pList->GetTrack(arg[0],arg[1],j);
+           arg[3] = pList->GetHit(arg[0],arg[1],j);
+           ListOfFiredCells(arg,timeAmplitude,alist,padr);
+       } // end for j
+       // Make sure map has full signal in it.
+       fHitMap2->SetHit(arg[0],arg[1],pList->GetSignal(arg[0],arg[1]));
+    } // end for i
+
+    FinishDigits(alist);
+
+    // clean memory
+    alist->Delete();
+    delete alist;
+    padr->Delete();
+    fHitMap1->ClearMap();
+    fHitMap2->ClearMap();
+}
+//______________________________________________________________________
+void AliITSsimulationSDD::FinishDigits(TObjArray *alist){
+    // introduce the electronics effects and do zero-suppression if required
+    Int_t nentries=alist->GetEntriesFast();
 
-    Float_t driftSpeed=fResponse->DriftSpeed();    
+    if(!nentries) return;
+    ChargeToSignal();
+    const char *kopt=fResponse->ZeroSuppOption();
+    ZeroSuppression(kopt);
+}
+//______________________________________________________________________
+void AliITSsimulationSDD::HitsToAnalogDigits(AliITSmodule *mod,TObjArray *alst,
+                                            TClonesArray *padr,
+                                            AliITSpList *pList){
+    // create maps to build the lists of tracks for each digit
+
+    TObjArray *fHits    = mod->GetHits();
+    Int_t      nhits    = fHits->GetEntriesFast();
+    Int_t      arg[6]   = {0,0,0,0,0,0};
+    Int_t    dummy      = 0;
+    Int_t    nofAnodes  = fNofMaps/2;
+    Float_t  sddLength  = fSegmentation->Dx();
+    Float_t  sddWidth   = fSegmentation->Dz();
+    Float_t  anodePitch = fSegmentation->Dpz(dummy);
+    Float_t  timeStep   = fSegmentation->Dpx(dummy);
+    Float_t  driftSpeed = fResponse->DriftSpeed();
+    Float_t  maxadc     = fResponse->MaxAdc();    
+    Float_t  topValue   = fResponse->DynamicRange();
+    Float_t  cHloss     = fResponse->ChargeLoss();
+    Float_t  norm       = maxadc/topValue;
+    Float_t  dfCoeff, s1; fResponse->DiffCoeff(dfCoeff,s1); // Signal 2d Shape
+    Double_t eVpairs    = 3.6;  // electron pair energy eV.
+    Float_t  nsigma     = fResponse->NSigmaIntegration(); //
+    Int_t    nlookups   = fResponse->GausNLookUp();       //
 
     // Piergiorgio's part (apart for few variables which I made float
     // when i thought that can be done
-
     // Fill detector maps with GEANT hits
     // loop over hits in the module
 
-    const Float_t kconv=1.0e+6;  // GeV->KeV
-    Int_t ii;
-    Int_t idhit=-1;
+    const Float_t kconv = 1.0e+6;  // GeV->KeV
+    Int_t    itrack      = 0;
+    Int_t    hitDetector; // detector number (lay,lad,hitDetector)
+    Int_t    iWing;       // which detector wing/side.
+    Int_t    detector;    // 2*(detector-1)+iWing
+    Int_t    ii,kk,ka,kt; // loop indexs
+    Int_t    ia,it,index; // sub-pixel integration indexies
+    Int_t    iAnode;      // anode number.
+    Int_t    timeSample;  // time buckett.
+    Int_t    anodeWindow; // anode direction charge integration width
+    Int_t    timeWindow;  // time direction charge integration width
+    Int_t    jamin,jamax; // anode charge integration window
+    Int_t    jtmin,jtmax; // time charge integration window
+    Int_t    ndiv;        // Anode window division factor.
+    Int_t    nsplit;      // the number of splits in anode and time windows==1.
+    Int_t    nOfSplits;   // number of times track length is split into
+    Float_t  nOfSplitsF;  // Floating point version of nOfSplits.
+    Float_t  kkF;         // Floating point version of loop index kk.
+    Float_t  pathInSDD; // Track length in SDD.
+    Float_t  drPath; // average position of track in detector. in microns
+    Float_t  drTime; // Drift time
+    Float_t  nmul;   // drift time window multiplication factor.
+    Float_t  avDrft;  // x position of path length segment in cm.
+    Float_t  avAnode; // Anode for path length segment in Anode number (float)
+    Float_t  xAnode;  // Floating point anode number.
+    Float_t  driftPath; // avDrft in microns.
+    Float_t  width;     // width of signal at anodes.
+    Double_t  depEnergy; // Energy deposited in this GEANT step.
+    Double_t  xL[3],dxL[3]; // local hit coordinates and diff.
+    Double_t sigA; // sigma of signal at anode.
+    Double_t sigT; // sigma in time/drift direction for track segment
+    Double_t aStep,aConst; // sub-pixel size and offset anode
+    Double_t tStep,tConst; // sub-pixel size and offset time
+    Double_t amplitude; // signal amplitude for track segment in nanoAmpere
+    Double_t chargeloss; // charge loss for track segment.
+    Double_t anodeAmplitude; // signal amplitude in anode direction
+    Double_t aExpo;          // exponent of Gaussian anode direction
+    Double_t timeAmplitude;  // signal amplitude in time direction
+    Double_t tExpo;          // exponent of Gaussian time direction
+//  Double_t tof;            // Time of flight in ns of this step.    
+
     for(ii=0; ii<nhits; ii++) {
-       AliITShit *hit = (AliITShit*) fHits->At(ii);
-       Float_t xL[3];
-       hit = (AliITShit*) fHits->At(ii);
-       hit->GetPositionL(xL[0],xL[1],xL[2]);
-       Int_t hitDetector = hit->GetDetector();
-
-        if(hit->StatusEntering()) idhit=ii;
-       
-       Int_t nOfSplits = 5;
-       if(fFlag) nOfSplits = 1;
-       // Deposited energy in keV
-       Float_t avpath = 0.;
-       Float_t avanod = 0.;
-       Float_t depEnergy = kconv*hit->GetIonization()/nOfSplits;
-       AliITShit *hit1 = 0;
-       Float_t xL1[3];
-       if(fFlag && depEnergy != 0.) continue;
-       if(depEnergy == 0.) {   
-         ii++;
-         hit1 = (AliITShit*) fHits->At(ii);
-         hit1->GetPositionL(xL1[0],xL1[1],xL1[2]);
-       } else {
-         xL1[0] = xL[0];
-         xL1[1] = xL[1];
-         xL1[2] = xL[2];
-       }
+       if(!mod->LineSegmentL(ii,xL[0],dxL[0],xL[1],dxL[1],xL[2],dxL[2],
+                             depEnergy,itrack)) continue;
+       depEnergy  *= kconv;
+       hitDetector = mod->GetDet();
+       //tof         = 1.E+09*(mod->GetHit(ii)->GetTOF()); // tof in ns.
+       //if(tof>sddLength/driftSpeed) continue; // hit happed too late.
 
        // scale path to simulate a perpendicular track
-
-       if(depEnergy == 0.) depEnergy = kconv*hit1->GetIonization()/nOfSplits;
-       if (fFlag) {
-         Float_t pathInSDD = TMath::Sqrt((xL[0]-xL1[0])*(xL[0]-xL1[0])+(xL[1]-xL1[1])*(xL[1]-xL1[1])+(xL[2]-xL1[2])*(xL[2]-xL1[2]));
-         depEnergy *= (0.03/pathInSDD); 
-       }
-       
-        for(Int_t kk=0;kk<nOfSplits;kk++) {
-         Float_t avDrft =  xL[0]+(xL1[0]-xL[0])*((kk+0.5)/((Float_t) nOfSplits));
-        Float_t avAnode = xL[2]+(xL1[2]-xL[2])*((kk+0.5)/((Float_t) nOfSplits));
-        Float_t driftPath = 10000.*avDrft;
-         
-       avpath = xL1[0];
-       avanod = xL1[2];
-       depEnergy = kconv*hit1->GetIonization();
-       
-       // added 11.09.2000 - continue if the particle did not lose energy
+       // continue if the particle did not lose energy
        // passing through detector
        if (!depEnergy) {
-         printf("This particle has passed without losing energy!\n");
-         continue;
-       }
-       // end add
-
-       Int_t iWing = 2;
-       if(driftPath < 0) {
-         iWing = 1;
-         driftPath = -driftPath;
-       }
-       driftPath = sddLength-driftPath;
-       Int_t detector = 2*(hitDetector-1) + iWing;
-       if(driftPath < 0) {
-         cout << "Warning: negative drift path " << driftPath << endl;
-         continue;
-       }
-        
-       //   Drift Time
-       Float_t driftTime = driftPath/driftSpeed;
-       Int_t timeSample = (Int_t) (fScaleSize*driftTime/timeStep + 1);
-       if(timeSample > fScaleSize*fMaxNofSamples) {
-         cout << "Warning: Wrong Time Sample: " << timeSample << endl;
-         continue;
-       }
-
-       //   Anode
-       Float_t xAnode = 10000.*(avAnode)/anodePitch + nofAnodes/2;  // +1?
-       if((xAnode+1)*anodePitch > sddWidth || xAnode*anodePitch < 0.) 
-             { cout << "Warning: Z = " << xAnode*anodePitch << endl; }
-       Int_t iAnode = (Int_t) (1.+xAnode); // xAnode?
-       if(iAnode < 0 || iAnode > nofAnodes) {
-         cout << "Warning: Wrong iAnode: " << iAnode << endl;
-         continue;
-       } 
-
-
-       // work with the idtrack=entry number in the TreeH for the moment
-       //Int_t idhit,idtrack;
-       //mod->GetHitTrackAndHitIndex(ii,idtrack,idhit);    
-       //Int_t idtrack=mod->GetHitTrackIndex(ii);  
-        // or store straight away the particle position in the array
-       // of particles and take idhit=ii only when part is entering (this
-       // requires FillModules() in the macro for analysis) : 
-        Int_t itrack = hit->GetTrack();
-
-       //  Signal 2d Shape
-       Float_t diffCoeff, s0;
-       fResponse->DiffCoeff(diffCoeff,s0);
-    
-       // Squared Sigma along the anodes
-       Double_t sigma2A = 2.*diffCoeff*driftTime+s0*s0;
-       Double_t sigmaA  = TMath::Sqrt(sigma2A);
-       Double_t sigmaT  = sigmaA/driftSpeed;
-    
-       // Peak amplitude in nanoAmpere
-       Double_t eVpairs = 3.6;
-       Double_t amplitude = fScaleSize*160.*depEnergy/(timeStep*eVpairs*2.*acos(-1.)*sigmaT*sigmaA);
-    
-       Float_t nsigma=fResponse->NSigmaIntegration();
-       // Spread the charge 
-       // Pixel index
-       Int_t ja = iAnode;
-       Int_t jt = timeSample;
-       // Sub-pixel index
-       Int_t nsplit = 4; // hard-wired
-       nsplit = (nsplit+1)/2*2;
-       // Sub-pixel size
-       Double_t aStep = anodePitch/(nsplit*fScaleSize);
-       Double_t tStep = timeStep/(nsplit*fScaleSize);
-       // Define SDD window corresponding to the hit
-       Int_t anodeWindow = (Int_t) (fScaleSize*nsigma*sigmaA/anodePitch + 1);
-       Int_t timeWindow = (Int_t) (fScaleSize*nsigma*sigmaT/timeStep + 1);
-       Int_t jamin = (ja - anodeWindow/2 - 1)*fScaleSize*nsplit + 1;
-       Int_t jamax = (ja + anodeWindow/2)*fScaleSize*nsplit;
-       if(jamin <= 0) jamin = 1;
-       if(jamax > fScaleSize*nofAnodes*nsplit) jamax = fScaleSize*nofAnodes*nsplit;
-       Int_t jtmin = (jt - timeWindow*3 - 1)*nsplit + 1; //hard-wired
-       Int_t jtmax = (jt + timeWindow*3)*nsplit; //hard-wired
-       if(jtmin <= 0) jtmin = 1;
-       if(jtmax > fScaleSize*fMaxNofSamples*nsplit) jtmax = fScaleSize*fMaxNofSamples*nsplit;
-
-       Double_t rlAnode = log(aStep*amplitude);
-
-       // Spread the charge in the anode-time window
-        Int_t ka;
-       for(ka=jamin; ka <=jamax; ka++) {
-         Int_t ia = (ka-1)/(fScaleSize*nsplit) + 1;
-         if(ia <= 0) { cout << "Warning: ia < 1: " << endl; continue; }
-         if(ia > nofAnodes) ia = nofAnodes;
-         Double_t aExpo = (aStep*(ka-0.5)-xAnode*anodePitch)/sigmaA;
-         Double_t anodeAmplitude = rlAnode - 0.5*aExpo*aExpo;
-         // Protect against overflows
-         if(anodeAmplitude > -87.3)
-           anodeAmplitude = exp(anodeAmplitude);
-         else
-           anodeAmplitude = 0;
-         Int_t index = ((detector+1)%2)*nofAnodes+ia-1; // index starts from 0
-         if(anodeAmplitude) {
-           Double_t rlTime = log(tStep*anodeAmplitude);
-            Int_t kt;
-           for(kt=jtmin; kt<=jtmax; kt++) {
-             Int_t it = (kt-1)/nsplit+1;  // it starts from 1
-             if(it<=0) { cout << "Warning: it < 1: " << endl; continue; } 
-             if(it>fScaleSize*fMaxNofSamples) it = fScaleSize*fMaxNofSamples;
-             Double_t tExpo = (tStep*(kt-0.5)-driftTime)/sigmaT;
-             Double_t timeAmplitude = rlTime - 0.5*tExpo*tExpo;
-             // Protect against overflows
-             if(timeAmplitude > -87.3){
-               timeAmplitude = exp(timeAmplitude);
-             } else
-               timeAmplitude = 0;
-
-             // build the list of digits for this module       
-             arg[0]=index;
-             arg[1]=it;
-             arg[2]=itrack;
-             arg[3]=idhit;
-             ListOfFiredCells(arg,timeAmplitude,list,padr);
-       } // loop over time in window 
-      } // end if anodeAmplitude
-    } // loop over anodes in window
-   } // end loop over "sub-hits"
-  } // end loop over hits
-
-  // introduce the electronics effects and do zero-suppression if required
-  Int_t nentries=list->GetEntriesFast();
-  if (nentries) {
-
-    //TStopwatch timer;
-    ChargeToSignal(); 
-    //timer.Stop(); timer.Print();
-
-    const char *kopt=fResponse->ZeroSuppOption();
-    ZeroSuppression(kopt);
-  } 
-
-  // clean memory
-  list->Delete();
-  delete list; 
-                      
-  padr->Delete(); 
-
-  fHitMap1->ClearMap();
-  fHitMap2->ClearMap();
+           Warning("HitsToAnalogDigits", 
+                   "fTrack = %d hit=%d module=%d This particle has"
+                   " passed without losing energy!",
+                   itrack,ii,mod->GetIndex());
+           continue;
+       } // end if !depEnergy
+
+       pathInSDD = TMath::Sqrt(dxL[0]*dxL[0]+dxL[1]*dxL[1]+dxL[2]*dxL[2]);
+
+       if (fFlag && pathInSDD) { depEnergy *= (0.03/pathInSDD); }
+       drPath = 10000.*(dxL[0]+2.*xL[0])*0.5;
+       if(drPath < 0) drPath = -drPath;
+       drPath = sddLength-drPath;
+       if(drPath < 0) {
+           Warning("HitsToAnalogDigits",
+                   "negative drift path drPath=%e sddLength=%e dxL[0]=%e "
+                   "xL[0]=%e",
+                   drPath,sddLength,dxL[0],xL[0]);
+           continue;
+       } // end if drPath < 0
+
+       // Compute number of segments to brake step path into
+       drTime = drPath/driftSpeed;  //   Drift Time
+       sigA   = TMath::Sqrt(2.*dfCoeff*drTime+s1*s1);// Sigma along the anodes
+       // calcuate the number of time the path length should be split into.
+       nOfSplits = (Int_t) (1. + 10000.*pathInSDD/sigA);
+       if(fFlag) nOfSplits = 1;
 
-  //gObjectTable->Print();
+       // loop over path segments, init. some variables.
+       depEnergy /= nOfSplits;
+       nOfSplitsF = (Float_t) nOfSplits;
+       for(kk=0;kk<nOfSplits;kk++) { // loop over path segments
+           kkF       = (Float_t) kk + 0.5;
+           avDrft    = xL[0]+dxL[0]*kkF/nOfSplitsF;
+           avAnode   = xL[2]+dxL[2]*kkF/nOfSplitsF;
+           driftPath = 10000.*avDrft;
+
+           iWing = 2;  // Assume wing is 2
+           if(driftPath < 0) { // if wing is not 2 it is 1.
+               iWing     = 1;
+               driftPath = -driftPath;
+           } // end if driftPath < 0
+           driftPath = sddLength-driftPath;
+           detector  = 2*(hitDetector-1) + iWing;
+           if(driftPath < 0) {
+               Warning("HitsToAnalogDigits","negative drift path "
+                       "driftPath=%e sddLength=%e avDrft=%e dxL[0]=%e "
+                       "xL[0]=%e",driftPath,sddLength,avDrft,dxL[0],xL[0]);
+               continue;
+           } // end if driftPath < 0
+
+           //   Drift Time
+           drTime     = driftPath/driftSpeed; // drift time for segment.
+           timeSample = (Int_t) (fScaleSize*drTime/timeStep + 1);
+           // compute time Sample including tof information. The tof only 
+           // effects the time of the signal is recoreded and not the
+           // the defusion.
+           // timeSample = (Int_t) (fScaleSize*(drTime+tof)/timeStep + 1);
+           if(timeSample > fScaleSize*fMaxNofSamples) {
+               Warning("HItsToAnalogDigits","Wrong Time Sample: %e",
+                       timeSample);
+               continue;
+           } // end if timeSample > fScaleSize*fMaxNoofSamples
+
+           //   Anode
+           xAnode = 10000.*(avAnode)/anodePitch + nofAnodes/2;  // +1?
+           if(xAnode*anodePitch > sddWidth || xAnode*anodePitch < 0.) 
+                         Warning("HitsToAnalogDigits",
+                                 "Exceedubg sddWidth=%e Z = %e",
+                                 sddWidth,xAnode*anodePitch);
+           iAnode = (Int_t) (1.+xAnode); // xAnode?
+           if(iAnode < 1 || iAnode > nofAnodes) {
+               Warning("HitToAnalogDigits","Wrong iAnode: 1<%d>%d",
+                       iAnode,nofAnodes);
+               continue;
+           } // end if iAnode < 1 || iAnode > nofAnodes
+
+           // store straight away the particle position in the array
+           // of particles and take idhit=ii only when part is entering (this
+           // requires FillModules() in the macro for analysis) :
+    
+           // Sigma along the anodes for track segment.
+           sigA       = TMath::Sqrt(2.*dfCoeff*drTime+s1*s1);
+           sigT       = sigA/driftSpeed;
+           // Peak amplitude in nanoAmpere
+           amplitude  = fScaleSize*160.*depEnergy/
+                                (timeStep*eVpairs*2.*acos(-1.)*sigT*sigA);
+           amplitude *= timeStep/25.; // WARNING!!!!! Amplitude scaling to 
+                                      // account for clock variations 
+                                       // (reference value: 40 MHz)
+           chargeloss = 1.-cHloss*driftPath/1000;
+           amplitude *= chargeloss;
+           width  = 2.*nsigma/(nlookups-1);
+           // Spread the charge 
+           // Pixel index
+           ndiv = 2;
+           nmul = 3.; 
+           if(drTime > 1200.) { 
+               ndiv = 4;
+               nmul = 1.5;
+           } // end if drTime > 1200.
+           // Sub-pixel index
+           nsplit = 4; // hard-wired //nsplit=4;nsplit = (nsplit+1)/2*2;
+           // Sub-pixel size see computation of aExpo and tExpo.
+           aStep  = anodePitch/(nsplit*fScaleSize*sigA);
+           aConst = xAnode*anodePitch/sigA;
+           tStep  = timeStep/(nsplit*fScaleSize*sigT);
+           tConst = drTime/sigT;
+           // Define SDD window corresponding to the hit
+           anodeWindow = (Int_t)(fScaleSize*nsigma*sigA/anodePitch+1);
+           timeWindow  = (Int_t) (fScaleSize*nsigma*sigT/timeStep+1.);
+           jamin = (iAnode - anodeWindow/ndiv - 1)*fScaleSize*nsplit +1;
+           jamax = (iAnode + anodeWindow/ndiv)*fScaleSize*nsplit;
+           if(jamin <= 0) jamin = 1;
+           if(jamax > fScaleSize*nofAnodes*nsplit) 
+                                        jamax = fScaleSize*nofAnodes*nsplit;
+           // jtmin and jtmax are Hard-wired
+           jtmin = (Int_t)(timeSample-timeWindow*nmul-1)*nsplit+1;
+           jtmax = (Int_t)(timeSample+timeWindow*nmul)*nsplit;
+           if(jtmin <= 0) jtmin = 1;
+           if(jtmax > fScaleSize*fMaxNofSamples*nsplit) 
+                                     jtmax = fScaleSize*fMaxNofSamples*nsplit;
+           // Spread the charge in the anode-time window
+           for(ka=jamin; ka <=jamax; ka++) {
+               ia = (ka-1)/(fScaleSize*nsplit) + 1;
+               if(ia <= 0) {
+                   Warning("HitsToAnalogDigits","ia < 1: ");
+                   continue;
+               } // end if
+               if(ia > nofAnodes) ia = nofAnodes;
+               aExpo     = (aStep*(ka-0.5)-aConst);
+               if(TMath::Abs(aExpo) > nsigma)  anodeAmplitude = 0.;
+               else {
+                   dummy          = (Int_t) ((aExpo+nsigma)/width);
+                   anodeAmplitude = amplitude*fResponse->GausLookUp(dummy);
+               } // end if TMath::Abs(aEspo) > nsigma
+               // index starts from 0
+               index = ((detector+1)%2)*nofAnodes+ia-1;
+               if(anodeAmplitude) for(kt=jtmin; kt<=jtmax; kt++) {
+                   it = (kt-1)/nsplit+1;  // it starts from 1
+                   if(it<=0){
+                       Warning("HitsToAnalogDigits","it < 1:");
+                       continue;
+                   } // end if 
+                   if(it>fScaleSize*fMaxNofSamples)
+                                               it = fScaleSize*fMaxNofSamples;
+                   tExpo    = (tStep*(kt-0.5)-tConst);
+                   if(TMath::Abs(tExpo) > nsigma) timeAmplitude = 0.;
+                   else {
+                       dummy         = (Int_t) ((tExpo+nsigma)/width);
+                       timeAmplitude = anodeAmplitude*
+                                       fResponse->GausLookUp(dummy);
+                   } // end if TMath::Abs(tExpo) > nsigma
+                   // build the list of digits for this module 
+                   arg[0]         = index;
+                   arg[1]         = it;
+                   arg[2]         = itrack; // track number
+                   arg[3]         = ii-1; // hit number.
+                   timeAmplitude *= norm;
+                   timeAmplitude *= 10;
+                   ListOfFiredCells(arg,timeAmplitude,alst,padr);
+                   pList->AddSignal(index,it,itrack,ii-1,
+                                    mod->GetIndex(),timeAmplitude);
+               } // end if anodeAmplitude and loop over time in window
+           } // loop over anodes in window
+       } // end loop over "sub-hits"
+    } // end loop over hits
 }
-
-
-//____________________________________________
-
+//______________________________________________________________________
 void AliITSsimulationSDD::ListOfFiredCells(Int_t *arg,Double_t timeAmplitude,
-                                           TObjArray *list,TClonesArray *padr){
-  // Returns the list of "fired" cells.
-
-                    Int_t index=arg[0];
-                    Int_t ik=arg[1];
-                    Int_t idtrack=arg[2];
-                    Int_t idhit=arg[3];
-                    Int_t counter=arg[4];
-                    Int_t countadr=arg[5];
-                   
-                    Double_t charge=timeAmplitude;
-                   charge += fHitMap2->GetSignal(index,ik-1);
-                   fHitMap2->SetHit(index, ik-1, charge);
-
-                    Int_t digits[3];
-                   Int_t it=(Int_t)((ik-1)/fScaleSize);
-                   
-                   digits[0]=index;
-                   digits[1]=it;
-                   digits[2]=(Int_t)timeAmplitude;
-                    Float_t phys;
-                   if (idtrack >= 0) phys=(Float_t)timeAmplitude;
-                   else phys=0;
-                   
-                   Double_t cellcharge=0.;
-                   AliITSTransientDigit* pdigit;
-                   // build the list of fired cells and update the info
-                   if (!fHitMap1->TestHit(index, it)) {
-                     
-                       new((*padr)[countadr++]) TVector(3);
-                       TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
-                       trinfo(0)=(Float_t)idtrack;
-                       trinfo(1)=(Float_t)idhit;
-                       trinfo(2)=(Float_t)timeAmplitude;
-
-                       list->AddAtAndExpand(
-                           new AliITSTransientDigit(phys,digits),counter);
-                       
-                       fHitMap1->SetHit(index, it, counter);
-                       counter++;
-                       pdigit=(AliITSTransientDigit*)list->
-                                                      At(list->GetLast());
-                       // list of tracks
-                       TObjArray *trlist=(TObjArray*)pdigit->TrackList();
-                       trlist->Add(&trinfo);
-
-                   } else {
-                       pdigit=
-                         (AliITSTransientDigit*) fHitMap1->GetHit(index, it);
-                       for(Int_t kk=0;kk<fScaleSize;kk++) {
-                         cellcharge += fHitMap2->GetSignal(index,fScaleSize*it+kk);
-                       }
-                       // update charge
-                       (*pdigit).fSignal=(Int_t)cellcharge;
-                       (*pdigit).fPhysics+=phys;                       
-                       // update list of tracks
-                       TObjArray* trlist=(TObjArray*)pdigit->TrackList();
-                       Int_t lastentry=trlist->GetLast();
-                       TVector *ptrkp=(TVector*)trlist->At(lastentry);
-                       TVector &trinfo=*ptrkp;
-                       Int_t lasttrack=Int_t(trinfo(0));
-                       //Int_t lasthit=Int_t(trinfo(1));
-                       Float_t lastcharge=(trinfo(2));
-                       
-                       if (lasttrack==idtrack ) {
-                           lastcharge+=(Float_t)timeAmplitude;
-                           trlist->RemoveAt(lastentry);
-                           trinfo(0)=lasttrack;
-                           //trinfo(1)=lasthit; // or idhit
-                           trinfo(1)=idhit;
-                           trinfo(2)=lastcharge;
-                           trlist->AddAt(&trinfo,lastentry);
-                       } else {
-                         
-                           new((*padr)[countadr++]) TVector(3);
-                           TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
-                           trinfo(0)=(Float_t)idtrack;
-                           trinfo(1)=(Float_t)idhit;
-                           trinfo(2)=(Float_t)timeAmplitude;
-                         
-                           trlist->Add(&trinfo);
-                       }
+                                          TObjArray *alist,TClonesArray *padr){
+    // Returns the list of "fired" cells.
+
+    Int_t index     = arg[0];
+    Int_t ik        = arg[1];
+    Int_t idtrack   = arg[2];
+    Int_t idhit     = arg[3];
+    Int_t counter   = arg[4];
+    Int_t countadr  = arg[5];
+    Double_t charge = timeAmplitude;
+    charge += fHitMap2->GetSignal(index,ik-1);
+    fHitMap2->SetHit(index, ik-1, charge);
+
+    Int_t digits[3];
+    Int_t it = (Int_t)((ik-1)/fScaleSize);
+    digits[0] = index;
+    digits[1] = it;
+    digits[2] = (Int_t)timeAmplitude;
+    Float_t phys;
+    if (idtrack >= 0) phys = (Float_t)timeAmplitude;
+    else phys = 0;
+
+    Double_t cellcharge = 0.;
+    AliITSTransientDigit* pdigit;
+    // build the list of fired cells and update the info
+    if (!fHitMap1->TestHit(index, it)) {
+       new((*padr)[countadr++]) TVector(3);
+       TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
+       trinfo(0) = (Float_t)idtrack;
+       trinfo(1) = (Float_t)idhit;
+       trinfo(2) = (Float_t)timeAmplitude;
+
+       alist->AddAtAndExpand(new AliITSTransientDigit(phys,digits),counter);
+       fHitMap1->SetHit(index, it, counter);
+       counter++;
+       pdigit=(AliITSTransientDigit*)alist->At(alist->GetLast());
+       // list of tracks
+       TObjArray *trlist=(TObjArray*)pdigit->TrackList();
+       trlist->Add(&trinfo);
+    } else {
+       pdigit = (AliITSTransientDigit*) fHitMap1->GetHit(index, it);
+       for(Int_t kk=0;kk<fScaleSize;kk++) {
+           cellcharge += fHitMap2->GetSignal(index,fScaleSize*it+kk);
+       }  // end for kk
+       // update charge
+       (*pdigit).fSignal = (Int_t)cellcharge;
+       (*pdigit).fPhysics += phys;                     
+       // update list of tracks
+       TObjArray* trlist = (TObjArray*)pdigit->TrackList();
+       Int_t lastentry = trlist->GetLast();
+       TVector *ptrkp = (TVector*)trlist->At(lastentry);
+       TVector &trinfo = *ptrkp;
+       Int_t lasttrack = Int_t(trinfo(0));
+       Float_t lastcharge=(trinfo(2));
+       if (lasttrack==idtrack ) {
+           lastcharge += (Float_t)timeAmplitude;
+           trlist->RemoveAt(lastentry);
+           trinfo(0) = lasttrack;
+           trinfo(1) = idhit;
+           trinfo(2) = lastcharge;
+           trlist->AddAt(&trinfo,lastentry);
+       } else {                  
+           new((*padr)[countadr++]) TVector(3);
+           TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
+           trinfo(0) = (Float_t)idtrack;
+           trinfo(1) = (Float_t)idhit;
+           trinfo(2) = (Float_t)timeAmplitude;
+           trlist->Add(&trinfo);
+       } // end if lasttrack==idtrack
 
 #ifdef print
-                       // check the track list - debugging
-                        Int_t trk[20], htrk[20];
-                        Float_t chtrk[20];  
-                       Int_t nptracks=trlist->GetEntriesFast();
-                       if (nptracks > 2) {
-                            Int_t tr;
-                           for (tr=0;tr<nptracks;tr++) {
-                               TVector *pptrkp=(TVector*)trlist->At(tr);
-                               TVector &pptrk=*pptrkp;
-                               trk[tr]=Int_t(pptrk(0));
-                               htrk[tr]=Int_t(pptrk(1));
-                               chtrk[tr]=(pptrk(2));
-                                printf("nptracks %d \n",nptracks);
-                               // set printings
-                           }
-                       } // end if nptracks
+       // check the track list - debugging
+       Int_t trk[20], htrk[20];
+       Float_t chtrk[20];  
+       Int_t nptracks = trlist->GetEntriesFast();
+       if (nptracks > 2) {
+           Int_t tr;
+           for (tr=0;tr<nptracks;tr++) {
+               TVector *pptrkp = (TVector*)trlist->At(tr);
+               TVector &pptrk  = *pptrkp;
+               trk[tr]   = Int_t(pptrk(0));
+               htrk[tr]  = Int_t(pptrk(1));
+               chtrk[tr] = (pptrk(2));
+               cout << "nptracks "<<nptracks << endl;
+           } // end for tr
+       } // end if nptracks
 #endif
-                   } //  end if pdigit
-
-                    arg[4]=counter;
-                    arg[5]=countadr;
-
+    } //  end if pdigit
 
+    // update counter and countadr for next call.
+    arg[4] = counter;
+    arg[5] = countadr;
 }
-
-
 //____________________________________________
 
 void AliITSsimulationSDD::AddDigit(Int_t i, Int_t j, Int_t signal){
-  // Adds a Digit.
+    // Adds a Digit.
     // tag with -1 signals coming from background tracks
     // tag with -2 signals coming from pure electronic noise
 
@@ -679,281 +787,241 @@ void AliITSsimulationSDD::AddDigit(Int_t i, Int_t j, Int_t signal){
 
     if(do10to8) signal=Convert8to10(signal); 
     AliITSTransientDigit *obj = (AliITSTransientDigit*)fHitMap1->GetHit(i,j);
-    digits[0]=i;
-    digits[1]=j;
-    digits[2]=signal;
+    digits[0] = i;
+    digits[1] = j;
+    digits[2] = signal;
     if (!obj) {
         phys=0;
         Int_t k;
         for (k=0;k<3;k++) {
-         tracks[k]=-2;
-          charges[k]=0;
-          hits[k]=-1;
-       }
+           tracks[k]=-2;
+           charges[k]=0;
+           hits[k]=-1;
+       } // end for k
         fITS->AddSimDigit(1,phys,digits,tracks,hits,charges); 
     } else {
-      phys=obj->fPhysics;
-      TObjArray* trlist=(TObjArray*)obj->TrackList();
-      Int_t nptracks=trlist->GetEntriesFast();
-
-      if (nptracks > 20) {
-        cout<<"Attention - nptracks > 20 "<<nptracks<<endl;
-        nptracks=20;
-      }
-      Int_t tr;
-      for (tr=0;tr<nptracks;tr++) {
-         TVector &pp  =*((TVector*)trlist->At(tr));
-         trk[tr]=Int_t(pp(0));
-         htrk[tr]=Int_t(pp(1));
-         chtrk[tr]=(pp(2));
-      }
-      if (nptracks > 1) {
-       //printf("nptracks > 2  -- %d\n",nptracks);
-         SortTracks(trk,chtrk,htrk,nptracks);
-      }
-      Int_t i;
-      if (nptracks < 3 ) {
-        for (i=0; i<nptracks; i++) {
-            tracks[i]=trk[i];
-            charges[i]=chtrk[i];
-            hits[i]=htrk[i];
-        }
-        for (i=nptracks; i<3; i++) {
-            tracks[i]=-3;
-            hits[i]=-1;
-            charges[i]=0;
-        }
-      } else {
-        for (i=0; i<3; i++) {
-            tracks[i]=trk[i];
-            charges[i]=chtrk[i];
-            hits[i]=htrk[i];
-        }
-      }
-
-      fITS->AddSimDigit(1,phys,digits,tracks,hits,charges); 
+       phys=obj->fPhysics;
+       TObjArray* trlist=(TObjArray*)obj->TrackList();
+       Int_t nptracks=trlist->GetEntriesFast();
+       if (nptracks > 20) {
+           Warning("AddDigit","nptracks=%d > 20 nptracks set to 20",nptracks);
+           nptracks=20;
+       } // end if nptracks > 20
+       Int_t tr;
+       for (tr=0;tr<nptracks;tr++) {
+           TVector &pp  =*((TVector*)trlist->At(tr));
+           trk[tr]=Int_t(pp(0));
+           htrk[tr]=Int_t(pp(1));
+           chtrk[tr]=(pp(2));
+       } // end for tr
+       if (nptracks > 1) {
+           SortTracks(trk,chtrk,htrk,nptracks);
+       } // end if nptracks > 1
+       Int_t i;
+       if (nptracks < 3 ) {
+           for (i=0; i<nptracks; i++) {
+               tracks[i]=trk[i];
+               charges[i]=chtrk[i];
+               hits[i]=htrk[i];
+           } // end for i
+           for (i=nptracks; i<3; i++) {
+               tracks[i]=-3;
+               hits[i]=-1;
+               charges[i]=0;
+           } // end for i
+       } else {
+           for (i=0; i<3; i++) {
+               tracks[i]=trk[i];
+               charges[i]=chtrk[i];
+               hits[i]=htrk[i];
+           } // end for i
+       } // end if/else nptracks < 3
+
+       fITS->AddSimDigit(1,phys,digits,tracks,hits,charges); 
  
-    }
-
+    } // end if/else !obj
 }
-
-//____________________________________________
-
-void AliITSsimulationSDD::SortTracks(Int_t *tracks,Float_t *charges,Int_t *hits,Int_t ntr){
-  //
-  // Sort the list of tracks contributing to a given digit
-  // Only the 3 most significant tracks are acctually sorted
-  //
-  
-  //
-  //  Loop over signals, only 3 times
-  //
-
-  
-  Float_t qmax;
-  Int_t jmax;
-  Int_t idx[3] = {-3,-3,-3};
-  Float_t jch[3] = {-3,-3,-3};
-  Int_t jtr[3] = {-3,-3,-3};
-  Int_t jhit[3] = {-3,-3,-3};
-  Int_t i,j,imax;
-  
-  if (ntr<3) imax=ntr;
-  else imax=3;
-  for(i=0;i<imax;i++){
-    qmax=0;
-    jmax=0;
-    
-    for(j=0;j<ntr;j++){
-      
-      if((i == 1 && j == idx[i-1] )
-        ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
-      
-      if(charges[j] > qmax) {
-       qmax = charges[j];
-       jmax=j;
-      }       
-    } 
-    
-    if(qmax > 0) {
-      idx[i]=jmax;
-      jch[i]=charges[jmax]; 
-      jtr[i]=tracks[jmax]; 
-      jhit[i]=hits[jmax]; 
-    }
-    
-  } 
-  
-  for(i=0;i<3;i++){
-    if (jtr[i] == -3) {
-         charges[i]=0;
-         tracks[i]=-3;
-         hits[i]=-1;
-    } else {
-         charges[i]=jch[i];
-         tracks[i]=jtr[i];
-         hits[i]=jhit[i];
-    }
-  }
-
+//______________________________________________________________________
+void AliITSsimulationSDD::SortTracks(Int_t *tracks,Float_t *charges,
+                                    Int_t *hits,Int_t ntr){
+    // Sort the list of tracks contributing to a given digit
+    // Only the 3 most significant tracks are acctually sorted
+    //  Loop over signals, only 3 times
+
+    Float_t qmax;
+    Int_t   jmax;
+    Int_t   idx[3]  = {-3,-3,-3};
+    Float_t jch[3]  = {-3,-3,-3};
+    Int_t   jtr[3]  = {-3,-3,-3};
+    Int_t   jhit[3] = {-3,-3,-3};
+    Int_t   i,j,imax;
+
+    if (ntr<3) imax = ntr;
+    else imax = 3;
+    for(i=0;i<imax;i++){
+       qmax = 0;
+       jmax = 0;
+       for(j=0;j<ntr;j++){
+           if((i == 1 && j == idx[i-1] )
+              ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
+           if(charges[j] > qmax) {
+               qmax = charges[j];
+               jmax=j;
+           } // end if charges[j]>qmax
+       } // end for j
+       if(qmax > 0) {
+           idx[i]  = jmax;
+           jch[i]  = charges[jmax]; 
+           jtr[i]  = tracks[jmax]; 
+           jhit[i] = hits[jmax]; 
+       } // end if qmax > 0
+    } // end for i
+
+    for(i=0;i<3;i++){
+       if (jtr[i] == -3) {
+           charges[i] = 0;
+           tracks[i]  = -3;
+           hits[i]    = -1;
+       } else {
+           charges[i] = jch[i];
+           tracks[i]  = jtr[i];
+           hits[i]    = jhit[i];
+       } // end if jtr[i] == -3
+    } // end for i
 }
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::ChargeToSignal() {
-  // add baseline, noise, electronics and ADC saturation effects
-
-
-  Float_t maxadc = fResponse->MaxAdc();    
-  Float_t topValue = fResponse->MagicValue();
-  Float_t norm = maxadc/topValue;
-
-  char opt1[20], opt2[20];
-  fResponse->ParamOptions(opt1,opt2);
-  char *read = strstr(opt1,"file");
-
-  Float_t baseline, noise; 
-
-  if (read) {
-      static Bool_t readfile=kTRUE;
-      //read baseline and noise from file
-      if (readfile) ReadBaseline();
-      readfile=kFALSE;
-  } else fResponse->GetNoiseParam(noise,baseline);
+    // add baseline, noise, electronics and ADC saturation effects
+
+    char opt1[20], opt2[20];
+    fResponse->ParamOptions(opt1,opt2);
+    char *read = strstr(opt1,"file");
+    Float_t baseline, noise; 
+
+    if (read) {
+       static Bool_t readfile=kTRUE;
+       //read baseline and noise from file
+       if (readfile) ReadBaseline();
+       readfile=kFALSE;
+    } else fResponse->GetNoiseParam(noise,baseline);
+
+    Float_t contrib=0;
+    Int_t i,k,kk;
+    Float_t maxadc = fResponse->MaxAdc();    
+    if(!fDoFFT) {
+       for (i=0;i<fNofMaps;i++) {
+           if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
+           for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
+               fInZR[k]  = fHitMap2->GetSignal(i,k);
+               contrib   = (baseline + noise*gRandom->Gaus());
+               fInZR[k] += contrib;
+           } // end for k
+           for(k=0; k<fMaxNofSamples; k++) {
+               Double_t newcont = 0.;
+               Double_t maxcont = 0.;
+               for(kk=0;kk<fScaleSize;kk++) {
+                   newcont = fInZR[fScaleSize*k+kk];
+                   if(newcont > maxcont) maxcont = newcont;
+               } // end for kk
+               newcont = maxcont;
+               if (newcont >= maxadc) newcont = maxadc -1;
+               if(newcont >= baseline){
+                   Warning("","newcont=%d>=baseline=%d",newcont,baseline);
+               } // end if
+               // back to analog: ?
+               fHitMap2->SetHit(i,k,newcont);
+           }  // end for k
+       } // end for i loop over anodes
+       return;
+    } // end if DoFFT
 
-  Float_t contrib=0;
-
-  TRandom random; 
-  Int_t i,k,kk; 
-
-  if(!fDoFFT) {
     for (i=0;i<fNofMaps;i++) {
-        if  (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
+       if  (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
        for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
-          fInZR[k] = fHitMap2->GetSignal(i,k);
-          contrib = baseline + noise*random.Gaus();
-          fInZR[k] += contrib;
-       }
+           fInZR[k]  = fHitMap2->GetSignal(i,k);
+           contrib   = (baseline + noise*gRandom->Gaus());
+           fInZR[k] += contrib;
+           fInZI[k]  = 0.;
+       } // end for k
+       FastFourierTransform(fElectronics,&fInZR[0],&fInZI[0],1);
+       for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
+           Double_t rw = fElectronics->GetTraFunReal(k);
+           Double_t iw = fElectronics->GetTraFunImag(k);
+           fOutZR[k]   = fInZR[k]*rw - fInZI[k]*iw;
+           fOutZI[k]   = fInZR[k]*iw + fInZI[k]*rw;
+       } // end for k
+       FastFourierTransform(fElectronics,&fOutZR[0],&fOutZI[0],-1);
        for(k=0; k<fMaxNofSamples; k++) {
-          Float_t newcont = 0.;
-          Float_t maxcont = 0.;
-          for(kk=0;kk<fScaleSize;kk++) {
-            newcont = fInZR[fScaleSize*k+kk];
-            if(newcont > maxcont) maxcont = newcont;
-          }
-          newcont = maxcont;
-          Double_t signal = newcont*norm;
-          if (signal >= maxadc) signal = maxadc -1;
-          // back to analog: ?
-          signal /=norm;
-          fHitMap2->SetHit(i,k,signal);
-       }  
-    } // loop over anodes
-    return;
-  } // end if DoFFT
-
-  for (i=0;i<fNofMaps;i++) {
-      if  (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
-      for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
-       fInZR[k] = fHitMap2->GetSignal(i,k);
-       contrib = baseline + noise*random.Gaus();
-       fInZR[k] += contrib;
-       fInZI[k] = 0.;
-      }
-      FastFourierTransform(fElectronics,&fInZR[0],&fInZI[0],1);
-      for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
-       Double_t rw = fElectronics->GetTraFunReal(k);
-       Double_t iw = fElectronics->GetTraFunImag(k);
-       fOutZR[k] = fInZR[k]*rw - fInZI[k]*iw;
-       fOutZI[k] = fInZR[k]*iw + fInZI[k]*rw;
-      }
-      FastFourierTransform(fElectronics,&fOutZR[0],&fOutZI[0],-1);
-      for(k=0; k<fMaxNofSamples; k++) {
-       Float_t newcont = 0.;
-       //Float_t totcont = 0.;
-       Float_t maxcont = 0.;
-       for(kk=0;kk<fScaleSize;kk++) {
-         newcont = fOutZR[fScaleSize*k+kk];
-         if(newcont > maxcont) maxcont = newcont;
-         //      totcont += (0.25*Out_ZR[4*k+kk]);
-       }
-       newcont = maxcont;
-       Double_t signal = newcont*norm;
-       if (signal >= maxadc) signal = maxadc -1;
-       // back to analog: ?
-       // comment the line below because you want to keep the signal in ADCs
-       // convert back to nA in cluster finder
-       signal /=norm;
-       fHitMap2->SetHit(i,k,signal);
-      }      
-  } // loop over anodes
+           Double_t newcont1 = 0.;
+           Double_t maxcont1 = 0.;
+           for(kk=0;kk<fScaleSize;kk++) {
+               newcont1 = fOutZR[fScaleSize*k+kk];
+               if(newcont1 > maxcont1) maxcont1 = newcont1;
+           } // end for kk
+           newcont1 = maxcont1;
+           if (newcont1 >= maxadc) newcont1 = maxadc -1;
+           fHitMap2->SetHit(i,k,newcont1);
+       } // end for k
+    } // end for i loop over anodes
   return;
-
 }
-
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::GetAnodeBaseline(Int_t i,Float_t &baseline,
                                            Float_t &noise){
-  // Returns the Baseline for a particular anode.
-    baseline=fBaseline[i];
-    noise=fNoise[i];
-
+    // Returns the Baseline for a particular anode.
+    baseline = fBaseline[i];
+    noise    = fNoise[i];
 }
-
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl,
                                            Int_t &th){
-  // Returns the compression alogirthm parameters
-   Int_t size = fD.GetSize();
-   if (size > 2 ) {
-      db=fD[i]; tl=fT1[i]; th=fT2[i];
-   } else {
-      if (size <= 2 && i>=fNofMaps/2) {
-       db=fD[1]; tl=fT1[1]; th=fT2[1];
-      } else {
-        db=fD[0]; tl=fT1[0]; th=fT2[0];
-      }
-   }
+    // Returns the compression alogirthm parameters
+    Int_t size = fD.GetSize();
+    if (size > 2 ) {
+       db=fD[i]; tl=fT1[i]; th=fT2[i];
+    } else {
+       if (size <= 2 && i>=fNofMaps/2) {
+           db=fD[1]; tl=fT1[1]; th=fT2[1];
+       } else {
+           db=fD[0]; tl=fT1[0]; th=fT2[0];
+       } // end if size <=2 && i>=fNofMaps/2
+    } // end if size >2
 }
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl){
-  // returns the compression alogirthm parameters
-   Int_t size = fD.GetSize();
-   if (size > 2 ) {
-      db=fD[i]; tl=fT1[i];
-   } else {
-      if (size <= 2 && i>=fNofMaps/2) {
-       db=fD[1]; tl=fT1[1]; 
-      } else {
-        db=fD[0]; tl=fT1[0]; 
-      }
-   }
+    // returns the compression alogirthm parameters
+    Int_t size = fD.GetSize();
 
+    if (size > 2 ) {
+       db=fD[i]; tl=fT1[i];
+    } else {
+       if (size <= 2 && i>=fNofMaps/2) {
+           db=fD[1]; tl=fT1[1]; 
+       } else {
+           db=fD[0]; tl=fT1[0]; 
+       } // end if size <=2 && i>=fNofMaps/2
+    } // end if size > 2
 }
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::SetCompressParam(){
-  // Sets the compression alogirthm parameters  
-   Int_t cp[8],i;
-   
-   fResponse->GiveCompressParam(cp);
-   for (i=0; i<2; i++) {
-       fD[i]  =cp[i];
-       fT1[i] =cp[i+2];
-       fT2[i] =cp[i+4];
-       fTol[i]=cp[i+6];
-       printf("\n i, fD, fT1, fT2, fTol %d %d %d %d %d\n",
-                                      i,fD[i],fT1[i],fT2[i],fTol[i]);
-   }
+    // Sets the compression alogirthm parameters  
+    Int_t cp[8],i;
+
+    fResponse->GiveCompressParam(cp);
+    for (i=0; i<2; i++) {
+       fD[i]   = cp[i];
+       fT1[i]  = cp[i+2];
+       fT2[i]  = cp[i+4];
+       fTol[i] = cp[i+6];
+    } // end for i
 }
-
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::ReadBaseline(){
-      // read baseline and noise from file - either a .root file and in this
-      // case data should be organised in a tree with one entry for each
-      // module => reading should be done accordingly
-      // or a classic file and do smth. like this:
-  //
-  // Read baselines and noise for SDD
-  //
-
+    // read baseline and noise from file - either a .root file and in this
+    // case data should be organised in a tree with one entry for each
+    // module => reading should be done accordingly
+    // or a classic file and do smth. like this:
+    // Read baselines and noise for SDD
 
     Int_t na,pos;
     Float_t bl,n;
@@ -965,129 +1033,113 @@ void AliITSsimulationSDD::ReadBaseline(){
 //
     filtmp = gSystem->ExpandPathName(fFileName.Data());
     FILE *bline = fopen(filtmp,"r");
-    printf("filtmp %s\n",filtmp);
     na = 0;
 
     if(bline) {
-       while(fscanf(bline,"%d %f %f",&pos, &bl, &n) != EOF) {
-          if (pos != na+1) {
-             Error("ReadBaseline","Anode number not in increasing order!",
-                   filtmp);
-             exit(1);
-         }
-          fBaseline[na]=bl;
-          fNoise[na]=n;
-          na++;
-       }
+       while(fscanf(bline,"%d %f %f",&pos, &bl, &n) != EOF) {
+           if (pos != na+1) {
+               Error("ReadBaseline","Anode number not in increasing order!",
+                     filtmp);
+               exit(1);
+           } // end if pos != na+1
+           fBaseline[na]=bl;
+           fNoise[na]=n;
+           na++;
+       } // end while
     } else {
-      Error("ReadBaseline"," THE BASELINE FILE %s DOES NOT EXIST !",
-         filtmp);
-      exit(1);
+       Error("ReadBaseline"," THE BASELINE FILE %s DOES NOT EXIST !",filtmp);
+       exit(1);
     } // end if(bline)
-    
+
     fclose(bline);
     delete [] filtmp;
-} 
-
-//____________________________________________
-Int_t AliITSsimulationSDD::Convert10to8(Int_t signal) {
-  // To the 10 to 8 bit lossive compression.
-  // code from Davide C. and Albert W.
-
-   if (signal < 128)  return signal;
-   if (signal < 256)  return (128+((signal-128)>>1));
-   if (signal < 512)  return (192+((signal-256)>>3));
-   if (signal < 1024) return (224+((signal-512)>>4));
-   return 0;
-
 }
-
-//____________________________________________
-Int_t AliITSsimulationSDD::Convert8to10(Int_t signal) {
-  // Undo the lossive 10 to 8 bit compression.
-  // code from Davide C. and Albert W.
-   if (signal < 0 || signal > 255) {
-       printf("<Convert8to10> out of range %d \n",signal);
-       return 0;
-   }
-   
-   if (signal < 128) return signal;
-   if (signal < 192) {
-     if (TMath::Odd(signal)) return (128+((signal-128)<<1));
-     else  return (128+((signal-128)<<1)+1);
-   }
-   if (signal < 224) {
-     if (TMath::Odd(signal)) return (256+((signal-192)<<3)+3);
-     else  return (256+((signal-192)<<3)+4);
-   }
-   if (TMath::Odd(signal)) return (512+((signal-224)<<4)+7);
-   else  return (512+((signal-224)<<4)+7);
-   return 0;
-
+//______________________________________________________________________
+Int_t AliITSsimulationSDD::Convert10to8(Int_t signal) const {
+    // To the 10 to 8 bit lossive compression.
+    // code from Davide C. and Albert W.
+
+    if (signal < 128)  return signal;
+    if (signal < 256)  return (128+((signal-128)>>1));
+    if (signal < 512)  return (192+((signal-256)>>3));
+    if (signal < 1024) return (224+((signal-512)>>4));
+    return 0;
 }
-
-//____________________________________________
+//______________________________________________________________________
+Int_t AliITSsimulationSDD::Convert8to10(Int_t signal) const {
+    // Undo the lossive 10 to 8 bit compression.
+    // code from Davide C. and Albert W.
+    if (signal < 0 || signal > 255) {
+       Warning("Convert8to10","out of range signal=%d",signal);
+       return 0;
+    } // end if signal <0 || signal >255
+
+    if (signal < 128) return signal;
+    if (signal < 192) {
+       if (TMath::Odd(signal)) return (128+((signal-128)<<1));
+       else  return (128+((signal-128)<<1)+1);
+    } // end if signal < 192
+    if (signal < 224) {
+       if (TMath::Odd(signal)) return (256+((signal-192)<<3)+3);
+       else  return (256+((signal-192)<<3)+4);
+    } // end if signal < 224
+    if (TMath::Odd(signal)) return (512+((signal-224)<<4)+7);
+    return (512+((signal-224)<<4)+7);
+}
+//______________________________________________________________________
 AliITSMap*   AliITSsimulationSDD::HitMap(Int_t i){
-  //Return the correct map.
+    //Return the correct map.
+
     return ((i==0)? fHitMap1 : fHitMap2);
 }
-
-
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::ZeroSuppression(const char *option) {
-  // perform the zero suppresion
-  if (strstr(option,"2D")) {
-    //Init2D();              // activate if param change module by module
-    Compress2D();
-  } else if (strstr(option,"1D")) {
-    //Init1D();              // activate if param change module by module
-    Compress1D();  
-  } else StoreAllDigits();  
-
+    // perform the zero suppresion
+
+    if (strstr(option,"2D")) {
+       //Init2D();              // activate if param change module by module
+       Compress2D();
+    } else if (strstr(option,"1D")) {
+       //Init1D();              // activate if param change module by module
+       Compress1D();  
+    } else StoreAllDigits();
 }
-
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::Init2D(){
-     // read in and prepare arrays: fD, fT1, fT2
-     //                         savemu[nanodes], savesigma[nanodes] 
-      // read baseline and noise from file - either a .root file and in this
-      // case data should be organised in a tree with one entry for each
-      // module => reading should be done accordingly
-      // or a classic file and do smth. like this ( code from Davide C. and
-      // Albert W.) :
-  //
-  // Read 2D zero-suppression parameters for SDD
-  //
+    // read in and prepare arrays: fD, fT1, fT2
+    //                         savemu[nanodes], savesigma[nanodes] 
+    // read baseline and noise from file - either a .root file and in this
+    // case data should be organised in a tree with one entry for each
+    // module => reading should be done accordingly
+    // or a classic file and do smth. like this ( code from Davide C. and
+    // Albert W.) :
+    // Read 2D zero-suppression parameters for SDD
 
     if (!strstr(fParam,"file")) return;
 
     Int_t na,pos,tempTh;
     Float_t mu,sigma;
-    Float_t *savemu = new Float_t [fNofMaps];
+    Float_t *savemu    = new Float_t [fNofMaps];
     Float_t *savesigma = new Float_t [fNofMaps];
     char input[100],basel[100],par[100];
     char *filtmp;
-
-
     Int_t minval = fResponse->MinVal();
 
     fResponse->Filenames(input,basel,par);
-    fFileName=par;
-
+    fFileName = par;
 //
     filtmp = gSystem->ExpandPathName(fFileName.Data());
     FILE *param = fopen(filtmp,"r");
     na = 0;
 
     if(param) {
-       while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
-          if (pos != na+1) {
-             Error("Init2D ","Anode number not in increasing order!",
-                   filtmp);
-             exit(1);
-         }
-          savemu[na]=mu;
-          savesigma[na]=sigma;
+       while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
+           if (pos != na+1) {
+               Error("Init2D","Anode number not in increasing order!",filtmp);
+               exit(1);
+           } // end if pos != na+1
+           savemu[na] = mu;
+          savesigma[na] = sigma;
           if ((2.*sigma) < mu) {
               fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
               mu = 2.0 * sigma;
@@ -1099,40 +1151,33 @@ void AliITSsimulationSDD::Init2D(){
           if (tempTh < 0) tempTh=0;
           fT2[na] = tempTh;
           na++;
-       } // end while
-
+       } // end while
     } else {
-      Error("Init2D "," THE FILE %s DOES NOT EXIST !",
-         filtmp);
-      exit(1);
+       Error("Init2D","THE FILE %s DOES NOT EXIST !",filtmp);
+       exit(1);
     } // end if(param)
-    
+
     fclose(param);
     delete [] filtmp;
     delete [] savemu;
     delete [] savesigma;
-} 
-
-//____________________________________________
+}
+//______________________________________________________________________
 void AliITSsimulationSDD::Compress2D(){
-  //
-  // simple ITS cluster finder -- online zero-suppression conditions
-  // 
-  //
+    // simple ITS cluster finder -- online zero-suppression conditions
 
     Int_t db,tl,th;  
-    Int_t minval = fResponse->MinVal();
-    Bool_t write=fResponse->OutputOption();   
-    Bool_t do10to8=fResponse->Do10to8();
-
+    Int_t minval   = fResponse->MinVal();
+    Bool_t write   = fResponse->OutputOption();   
+    Bool_t do10to8 = fResponse->Do10to8();
     Int_t nz, nl, nh, low, i, j; 
 
     for (i=0; i<fNofMaps; i++) {
         CompressionParam(i,db,tl,th);
-        nz=0; 
-        nl=0;
-        nh=0;
-        low=0;
+        nz  = 0; 
+        nl  = 0;
+        nh  = 0;
+        low = 0;
        for (j=0; j<fMaxNofSamples; j++) {
            Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
            signal -= db; // if baseline eq. is done here
@@ -1142,93 +1187,82 @@ void AliITSsimulationSDD::Compress2D(){
                nh++;
                Bool_t cond=kTRUE;
                FindCluster(i,j,signal,minval,cond);
-               if (cond && ((TMath::Abs(fHitMap2->GetSignal(i,j-1))-th)>=minval)) {
-                 if(do10to8) signal = Convert10to8(signal);
-                 AddDigit(i,j,signal);
-               }
+               if(cond && j &&
+                  ((TMath::Abs(fHitMap2->GetSignal(i,j-1))-th)>=minval)){
+                   if(do10to8) signal = Convert10to8(signal);
+                   AddDigit(i,j,signal);
+               } // end if cond&&j&&()
            } else if ((signal - tl) >= minval) nl++;
-       } // loop time samples
-       if (write) TreeB()->Fill(nz,nl,nh,low,i+1);
-    } // loop anodes  
+       } // end for j loop time samples
+       if (write) TreeB()->Fill(nz,nl,nh,low,i+1);
+    } //end for i loop anodes
 
-      char hname[30];
-      if (write) {
+    char hname[30];
+    if (write) {
        sprintf(hname,"TNtuple%d_%d",fModule,fEvent);
        TreeB()->Write(hname);
        // reset tree
         TreeB()->Reset();
-      }
-
-} 
-
-//_____________________________________________________________________________
+    } // end if write
+}
+//______________________________________________________________________
 void  AliITSsimulationSDD::FindCluster(Int_t i,Int_t j,Int_t signal,
                                        Int_t minval,Bool_t &cond){
-//
-//  Find clusters according to the online 2D zero-suppression algorithm
-//
-
-    Bool_t do10to8=fResponse->Do10to8();
-
-    Bool_t high=kFALSE;
+    // Find clusters according to the online 2D zero-suppression algorithm
+    Bool_t do10to8 = fResponse->Do10to8();
+    Bool_t high    = kFALSE;
 
     fHitMap2->FlagHit(i,j);
 //
 //  check the online zero-suppression conditions
 //  
-    const Int_t maxNeighbours = 4;
-
+    const Int_t kMaxNeighbours = 4;
     Int_t nn;
     Int_t dbx,tlx,thx;  
-    Int_t xList[maxNeighbours], yList[maxNeighbours];
+    Int_t xList[kMaxNeighbours], yList[kMaxNeighbours];
     fSegmentation->Neighbours(i,j,&nn,xList,yList);
     Int_t in,ix,iy,qns;
     for (in=0; in<nn; in++) {
        ix=xList[in];
         iy=yList[in];
         if (fHitMap2->TestHit(ix,iy)==kUnused) {
-          CompressionParam(ix,dbx,tlx,thx);
-           Int_t qn = (Int_t)(fHitMap2->GetSignal(ix,iy));
-          qn -= dbx; // if baseline eq. is done here
-          if ((qn-tlx) < minval) {
-             fHitMap2->FlagHit(ix,iy);
-             continue;
-          } else {
-              if ((qn - thx) >= minval) high=kTRUE;
-              if (cond) {
-                 if(do10to8) signal = Convert10to8(signal);
-                AddDigit(i,j,signal);
-             }
-             if(do10to8) qns = Convert10to8(qn);
-             else qns=qn;
-             if (!high) AddDigit(ix,iy,qns);
-             cond=kFALSE;
-             if(!high) fHitMap2->FlagHit(ix,iy);
-          }
-       } // TestHit
-    } // loop over neighbours
-
+           CompressionParam(ix,dbx,tlx,thx);
+           Int_t qn = (Int_t)(fHitMap2->GetSignal(ix,iy));
+           qn -= dbx; // if baseline eq. is done here
+           if ((qn-tlx) < minval) {
+               fHitMap2->FlagHit(ix,iy);
+               continue;
+           } else {
+               if ((qn - thx) >= minval) high=kTRUE;
+               if (cond) {
+                   if(do10to8) signal = Convert10to8(signal);
+                   AddDigit(i,j,signal);
+               } // end if cond
+               if(do10to8) qns = Convert10to8(qn);
+               else qns=qn;
+               if (!high) AddDigit(ix,iy,qns);
+               cond=kFALSE;
+               if(!high) fHitMap2->FlagHit(ix,iy);
+           } // end if qn-tlx < minval
+       } // end if  TestHit
+    } // end for in loop over neighbours
 }
-
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::Init1D(){
-  // this is just a copy-paste of input taken from 2D algo
-  // Torino people should give input
-  //
-  // Read 1D zero-suppression parameters for SDD
-  //
+    // this is just a copy-paste of input taken from 2D algo
+    // Torino people should give input
+    // Read 1D zero-suppression parameters for SDD
 
     if (!strstr(fParam,"file")) return;
 
     Int_t na,pos,tempTh;
     Float_t mu,sigma;
-    Float_t *savemu = new Float_t [fNofMaps];
+    Float_t *savemu    = new Float_t [fNofMaps];
     Float_t *savesigma = new Float_t [fNofMaps];
     char input[100],basel[100],par[100];
     char *filtmp;
-
-
     Int_t minval = fResponse->MinVal();
+
     fResponse->Filenames(input,basel,par);
     fFileName=par;
 
@@ -1240,79 +1274,69 @@ void AliITSsimulationSDD::Init1D(){
     na = 0;
 
     if (param) {
-          fscanf(param,"%d %d %d %d ", &fT2[0], &fT2[1], &fTol[0], &fTol[1]);
-          while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
-              if (pos != na+1) {
-                 Error("Init1D ","Anode number not in increasing order!",
-                   filtmp);
-                 exit(1);
-              }
-              savemu[na]=mu;
-              savesigma[na]=sigma;
-              if ((2.*sigma) < mu) {
-                fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
-                mu = 2.0 * sigma;
-              } else fD[na] = 0;
-              tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
-              if (tempTh < 0) tempTh=0;
-              fT1[na] = tempTh;
-              na++;
-         } // end while
+       fscanf(param,"%d %d %d %d ", &fT2[0], &fT2[1], &fTol[0], &fTol[1]);
+       while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
+           if (pos != na+1) {
+               Error("Init1D","Anode number not in increasing order!",filtmp);
+               exit(1);
+           } // end if pos != na+1
+           savemu[na]=mu;
+           savesigma[na]=sigma;
+           if ((2.*sigma) < mu) {
+               fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
+               mu = 2.0 * sigma;
+           } else fD[na] = 0;
+           tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
+           if (tempTh < 0) tempTh=0;
+           fT1[na] = tempTh;
+           na++;
+       } // end while
     } else {
-      Error("Init1D "," THE FILE %s DOES NOT EXIST !",
-         filtmp);
-      exit(1);
+       Error("Init1D","THE FILE %s DOES NOT EXIST !",filtmp);
+       exit(1);
     } // end if(param)
-    
+
     fclose(param);
     delete [] filtmp;
     delete [] savemu;
     delete [] savesigma;
-
-
-
-}
-//____________________________________________
+} 
+//______________________________________________________________________
 void AliITSsimulationSDD::Compress1D(){
     // 1D zero-suppression algorithm (from Gianluca A.)
-
-    Int_t dis,tol,thres,decr,diff;  
-
+    Int_t    dis,tol,thres,decr,diff;
     UChar_t *str=fStream->Stream();
-    Int_t counter=0;
+    Int_t    counter=0;
+    Bool_t   do10to8=fResponse->Do10to8();
+    Int_t    last=0;
+    Int_t    k,i,j;
 
-    Bool_t do10to8=fResponse->Do10to8();
-
-    Int_t last=0;
-    Int_t k,i,j;
     for (k=0; k<2; k++) {
-         tol = Tolerance(k);
-         dis = Disable(k);  
-        for (i=0; i<fNofMaps/2; i++) {
-            Bool_t firstSignal=kTRUE;
-             Int_t idx=i+k*fNofMaps/2;
-            CompressionParam(idx,decr,thres); 
-            for (j=0; j<fMaxNofSamples; j++) {
-                Int_t signal=(Int_t)(fHitMap2->GetSignal(idx,j));
-                 signal -= decr;  // if baseline eq.
-                if(do10to8) signal = Convert10to8(signal);
-                if (signal <= thres) {
-                     signal=0;
-                     diff=128; 
-                     last=0; 
-                     // write diff in the buffer for HuffT
-                     str[counter]=(UChar_t)diff;
-                     counter++;
-                     continue;
-                }
-                 diff=signal-last;
-                 if (diff > 127) diff=127;
-                 if (diff < -128) diff=-128;
-   
-                if (signal < dis) {
-                  // tol has changed to 8 possible cases ? - one can write
-                  // this if(TMath::Abs(diff)<tol) ... else ...
+       tol = Tolerance(k);
+       dis = Disable(k);  
+       for (i=0; i<fNofMaps/2; i++) {
+           Bool_t firstSignal=kTRUE;
+           Int_t idx=i+k*fNofMaps/2;
+           CompressionParam(idx,decr,thres); 
+           for (j=0; j<fMaxNofSamples; j++) {
+               Int_t signal=(Int_t)(fHitMap2->GetSignal(idx,j));
+               signal -= decr;  // if baseline eq.
+               if(do10to8) signal = Convert10to8(signal);
+               if (signal <= thres) {
+                   signal=0;
+                   diff=128; 
+                   last=0; 
+                   // write diff in the buffer for HuffT
+                   str[counter]=(UChar_t)diff;
+                   counter++;
+                   continue;
+               } // end if signal <= thres
+               diff=signal-last;
+               if (diff > 127) diff=127;
+               if (diff < -128) diff=-128;
+               if (signal < dis) {
+                   // tol has changed to 8 possible cases ? - one can write
+                   // this if(TMath::Abs(diff)<tol) ... else ...
                    if(TMath::Abs(diff)<tol) diff=0;
                    // or keep it as it was before
                    /*
@@ -1321,30 +1345,28 @@ void AliITSsimulationSDD::Compress1D(){
                     if (tol==3 && (diff >= -16 && diff <= 15)) diff=0;
                    */
                     AddDigit(idx,j,last+diff);
-                } else {
-                   AddDigit(idx,j,signal);
-                }
-                 
-                 diff += 128;
-                 // write diff in the buffer used to compute Huffman tables
-                 if (firstSignal) str[counter]=(UChar_t)signal;
-                else str[counter]=(UChar_t)diff;
-                counter++;
-
-                 last=signal;
-                firstSignal=kFALSE;
-            } // loop time samples
-        } // loop anodes  one half of detector 
-    }
+               } else {
+                   AddDigit(idx,j,signal);
+               } // end if singal < dis
+               diff += 128;
+               // write diff in the buffer used to compute Huffman tables
+               if (firstSignal) str[counter]=(UChar_t)signal;
+               else str[counter]=(UChar_t)diff;
+               counter++;
+               last=signal;
+               firstSignal=kFALSE;
+           } // end for j loop time samples
+       } // end for i loop anodes  one half of detector 
+    } //  end for k
 
     // check
     fStream->CheckCount(counter);
 
     // open file and write out the stream of diff's
-   
     static Bool_t open=kTRUE;
     static TFile *outFile;
     Bool_t write = fResponse->OutputOption();
+    TDirectory *savedir = gDirectory;
  
     if (write ) {
        if(open) {
@@ -1352,175 +1374,186 @@ void AliITSsimulationSDD::Compress1D(){
            cout<<"filename "<<fFileName<<endl;
            outFile=new TFile(fFileName,"recreate");
            cout<<"I have opened "<<fFileName<<" file "<<endl;
-       }           
-       open=kFALSE;
+       } // end if open
+       open = kFALSE;
        outFile->cd();
         fStream->Write();
     }  // endif write  
 
-     fStream->ClearStream();
-
-     // back to galice.root file
-
-     TTree *fAli=gAlice->TreeK();
-     TFile *file = 0;
-           
-     if (fAli) file =fAli->GetCurrentFile();
-     file->cd();
+    fStream->ClearStream();
 
-
-} 
-//____________________________________________
+    // back to galice.root file
+    if(savedir) savedir->cd();
+}
+//______________________________________________________________________
 void AliITSsimulationSDD::StoreAllDigits(){
-  // if non-zero-suppressed data 
-
-    Bool_t do10to8=fResponse->Do10to8();
-
+    // if non-zero-suppressed data
+    Bool_t do10to8 = fResponse->Do10to8();
     Int_t i, j, digits[3];
+
     for (i=0; i<fNofMaps; i++) {
         for (j=0; j<fMaxNofSamples; j++) {
-             Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
-            if(do10to8) signal = Convert10to8(signal);
-            if(do10to8) signal = Convert8to10(signal); 
-             digits[0]=i;
-             digits[1]=j;
-             digits[2]=signal;
-            fITS->AddRealDigit(1,digits);
-       }
-    }
+           Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
+           if(do10to8) signal = Convert10to8(signal);
+           if(do10to8) signal = Convert8to10(signal); 
+           digits[0] = i;
+           digits[1] = j;
+           digits[2] = signal;
+           fITS->AddRealDigit(1,digits);
+       } // end for j
+    } // end for i
 } 
-//____________________________________________
-
+//______________________________________________________________________
 void AliITSsimulationSDD::CreateHistograms(Int_t scale){
-  // Creates histograms of maps for debugging
-
-      Int_t i;
+    // Creates histograms of maps for debugging
+    Int_t i;
 
       fHis=new TObjArray(fNofMaps);
-      TString sddName("sdd_");
       for (i=0;i<fNofMaps;i++) {
+          TString sddName("sdd_");
           Char_t candNum[4];
           sprintf(candNum,"%d",i+1);
           sddName.Append(candNum);
-          (*fHis)[i] = new TH1F(sddName.Data(),"SDD maps",
-                scale*fMaxNofSamples,0.,(Float_t) scale*fMaxNofSamples);
-      }
-
+          fHis->AddAt(new TH1F(sddName.Data(),"SDD maps",scale*fMaxNofSamples,
+                               0.,(Float_t) scale*fMaxNofSamples), i);
+      } // end for i
 }
-//____________________________________________
+//______________________________________________________________________
 void AliITSsimulationSDD::FillHistograms(){
-  // fill 1D histograms from map
-  if (!fHis) return; 
-  
-  for( Int_t i=0; i<fNofMaps; i++) {
-    TH1F *hist =(TH1F *)fHis->UncheckedAt(i);
-    Int_t nsamples = hist->GetNbinsX();
-    for( Int_t j=0; j<nsamples; j++) {
-      Double_t signal=fHitMap2->GetSignal(i,j);
-      hist->Fill((Float_t)j,signal);
-    }
-  }
+    // fill 1D histograms from map
+
+    if (!fHis) return;
+
+    for( Int_t i=0; i<fNofMaps; i++) {
+       TH1F *hist =(TH1F *)fHis->UncheckedAt(i);
+       Int_t nsamples = hist->GetNbinsX();
+       for( Int_t j=0; j<nsamples; j++) {
+           Double_t signal=fHitMap2->GetSignal(i,j);
+           hist->Fill((Float_t)j,signal);
+       } // end for j
+    } // end for i
 }
-
-//____________________________________________
-
+//______________________________________________________________________
 void AliITSsimulationSDD::ResetHistograms(){
-    //
     // Reset histograms for this detector
-    //
     Int_t i;
-    for (i=0;i<fNofMaps;i++ ) {
-       if ((*fHis)[i])    ((TH1F*)(*fHis)[i])->Reset();
-    }
 
+    for (i=0;i<fNofMaps;i++ ) {
+       if (fHis->At(i))    ((TH1F*)fHis->At(i))->Reset();
+    } // end for i
 }
+//______________________________________________________________________
+TH1F *AliITSsimulationSDD::GetAnode(Int_t wing, Int_t anode) { 
+    // Fills a histogram from a give anode.  
 
+    if (!fHis) return 0;
 
-//____________________________________________
+    if(wing <=0 || wing > 2) {
+       Warning("GetAnode","Wrong wing number: %d",wing);
+       return NULL;
+    } // end if wing <=0 || wing >2
+    if(anode <=0 || anode > fNofMaps/2) {
+       Warning("GetAnode","Wrong anode number: %d",anode);
+       return NULL;
+    } // end if ampde <=0 || andoe > fNofMaps/2
 
-TH1F *AliITSsimulationSDD::GetAnode(Int_t wing, Int_t anode) { 
-  // Fills a histogram from a give anode.  
-  if (!fHis) return 0;
-
-  if(wing <=0 || wing > 2) {
-    cout << "Wrong wing number: " << wing << endl;
-    return NULL;
-  }
-  if(anode <=0 || anode > fNofMaps/2) {
-    cout << "Wrong anode number: " << anode << endl;
-    return NULL;
-  }
-
-  Int_t index = (wing-1)*fNofMaps/2 + anode-1;
-  return (TH1F*)((*fHis)[index]); 
+    Int_t index = (wing-1)*fNofMaps/2 + anode-1;
+    return (TH1F*)(fHis->At(index));
 }
-
-//____________________________________________
-
+//______________________________________________________________________
 void AliITSsimulationSDD::WriteToFile(TFile *hfile) {
-  // Writes the histograms to a file 
-  if (!fHis) return;
+    // Writes the histograms to a file
 
-  hfile->cd();
-  Int_t i;
-  for(i=0; i<fNofMaps; i++)  (*fHis)[i]->Write(); //fAdcs[i]->Write();
-  return;
+    if (!fHis) return;
+
+    hfile->cd();
+    Int_t i;
+    for(i=0; i<fNofMaps; i++)  fHis->At(i)->Write(); //fAdcs[i]->Write();
+    return;
 }
-//____________________________________________
+//______________________________________________________________________
 Float_t AliITSsimulationSDD::GetNoise() {  
-  // Returns the noise value
-
-  //Bool_t do10to8=fResponse->Do10to8();
-  //noise will always be in the liniar part of the signal
-
-  Int_t decr;
-  Int_t threshold=fT1[0];
-
-  char opt1[20], opt2[20];
-  fResponse->ParamOptions(opt1,opt2);
-  fParam=opt2;
-  char *same = strstr(opt1,"same");
-  Float_t noise,baseline;
-  if (same) {
-    fResponse->GetNoiseParam(noise,baseline);
-  } else {
-     static Bool_t readfile=kTRUE;
-     //read baseline and noise from file
-     if (readfile) ReadBaseline();
-     readfile=kFALSE;
-  }
-
-   TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
-   if(c2) delete c2->GetPrimitive("noisehist");
-   if(c2) delete c2->GetPrimitive("anode");
-   else     c2=new TCanvas("c2");
-   c2->cd();
-   c2->SetFillColor(0);
-
-   TH1F *noisehist = new TH1F("noisehist","noise",100,0.,(float)2*threshold);
-   TH1F *anode = new TH1F("anode","Anode Projection",fMaxNofSamples,0.,(float)fMaxNofSamples);
-  Int_t i,k;
-  for (i=0;i<fNofMaps;i++) {
-    CompressionParam(i,decr,threshold); 
-    if  (!same) GetAnodeBaseline(i,baseline,noise);
-    anode->Reset();
-    for (k=0;k<fMaxNofSamples;k++) {
-      Float_t signal=(Float_t)fHitMap2->GetSignal(i,k);
-      //if (signal <= (float)threshold) noisehist->Fill(signal-baseline);
-      if (signal <= (float)threshold) noisehist->Fill(signal);
-      anode->Fill((float)k,signal);
-    }
-    anode->Draw();
+    // Returns the noise value
+    //Bool_t do10to8=fResponse->Do10to8();
+    //noise will always be in the liniar part of the signal
+    Int_t decr;
+    Int_t threshold = fT1[0];
+    char opt1[20], opt2[20];
+
+    fResponse->ParamOptions(opt1,opt2);
+    fParam=opt2;
+    char *same = strstr(opt1,"same");
+    Float_t noise,baseline;
+    if (same) {
+       fResponse->GetNoiseParam(noise,baseline);
+    } else {
+       static Bool_t readfile=kTRUE;
+       //read baseline and noise from file
+       if (readfile) ReadBaseline();
+       readfile=kFALSE;
+    } // end if same
+
+    TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
+    if(c2) delete c2->GetPrimitive("noisehist");
+    if(c2) delete c2->GetPrimitive("anode");
+    else     c2=new TCanvas("c2");
+    c2->cd();
+    c2->SetFillColor(0);
+
+    TH1F *noisehist = new TH1F("noisehist","noise",100,0.,(float)2*threshold);
+    TH1F *anode = new TH1F("anode","Anode Projection",fMaxNofSamples,0.,
+                          (float)fMaxNofSamples);
+    Int_t i,k;
+    for (i=0;i<fNofMaps;i++) {
+       CompressionParam(i,decr,threshold); 
+       if  (!same) GetAnodeBaseline(i,baseline,noise);
+       anode->Reset();
+       for (k=0;k<fMaxNofSamples;k++) {
+           Float_t signal=(Float_t)fHitMap2->GetSignal(i,k);
+           //if (signal <= (float)threshold) noisehist->Fill(signal-baseline);
+           if (signal <= (float)threshold) noisehist->Fill(signal);
+           anode->Fill((float)k,signal);
+       } // end for k
+       anode->Draw();
+       c2->Update();
+    } // end for i
+    TF1 *gnoise = new TF1("gnoise","gaus",0.,threshold);
+    noisehist->Fit("gnoise","RQ");
+    noisehist->Draw();
     c2->Update();
-  }
-  TF1 *gnoise = new TF1("gnoise","gaus",0.,threshold);
-  noisehist->Fit("gnoise","RQ");
-  noisehist->Draw();
-  c2->Update();
-  Float_t mnoise = gnoise->GetParameter(1);
-  cout << "mnoise : " << mnoise << endl;
-  Float_t rnoise = gnoise->GetParameter(2);
-  cout << "rnoise : " << rnoise << endl;
-  delete noisehist;
-  return rnoise;
+    Float_t mnoise = gnoise->GetParameter(1);
+    cout << "mnoise : " << mnoise << endl;
+    Float_t rnoise = gnoise->GetParameter(2);
+    cout << "rnoise : " << rnoise << endl;
+    delete noisehist;
+    return rnoise;
+}//______________________________________________________________________
+void AliITSsimulationSDD::WriteSDigits(AliITSpList *pList){
+    // Fills the Summable digits Tree
+    Int_t i,ni,j,nj;
+    static AliITS *aliITS = (AliITS*)gAlice->GetModule("ITS");
+
+    pList->GetMaxMapIndex(ni,nj);
+    for(i=0;i<ni;i++)for(j=0;j<nj;j++){
+       if(pList->GetSignalOnly(i,j)>0.5*fT1[0]){ // above small threshold.
+           aliITS->AddSumDigit(*(pList->GetpListItem(i,j)));
+//         cout << "pListSDD: " << *(pList->GetpListItem(i,j)) << endl;
+       } // end if
+    } // end for i,j
+    return;
+}
+//______________________________________________________________________
+void AliITSsimulationSDD::Print() {
+    // Print SDD simulation Parameters
+
+    cout << "**************************************************" << endl;
+    cout << "   Silicon Drift Detector Simulation Parameters   " << endl;
+    cout << "**************************************************" << endl;
+    cout << "Flag for Perpendicular tracks: " << (Int_t) fFlag << endl;
+    cout << "Flag for noise checking: " << (Int_t) fCheckNoise << endl;
+    cout << "Flag to switch off electronics: " << (Int_t) fDoFFT << endl;
+    cout << "Number pf Anodes used: " << fNofMaps << endl;
+    cout << "Number of Time Samples: " << fMaxNofSamples << endl;
+    cout << "Scale size factor: " << fScaleSize << endl;
+    cout << "**************************************************" << endl;
 }