]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSsimulationSDD.cxx
Code cleanup (F. Prino)
[u/mrichter/AliRoot.git] / ITS / AliITSsimulationSDD.cxx
index bdb9306e289e5bb96b52bf6adcb6f2aa0da47e05..bddc8383275d113ab61c71035b9561502244bed3 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-#include <TSystem.h>
-#include <TROOT.h>
-#include <TStopwatch.h>
 #include <TCanvas.h>
 #include <TF1.h>
-#include <TRandom.h>
 #include <TH1.h>
 #include <TFile.h>
-#include <TVector.h>
-#include <TArrayI.h>
-#include <TArrayF.h>
-
-#include "AliRun.h"
+#include <TRandom.h>
+#include <TROOT.h>
 #include "AliITS.h"
-#include "AliITShit.h"
-#include "AliITSdigitSDD.h"
+#include "AliITSMapA2.h"
+#include "AliITSRawData.h"
 #include "AliITSdigitSPD.h"
+#include "AliITSetfSDD.h"
 #include "AliITSmodule.h"
 #include "AliITSpList.h"
-#include "AliITSMapA1.h"
-#include "AliITSMapA2.h"
-#include "AliITSetfSDD.h"
-#include "AliITSRawData.h"
-#include "AliITSHuffman.h"
-#include "AliITSgeom.h"
-#include "AliITSsegmentation.h"
-#include "AliITSresponse.h"
-#include "AliITSsegmentationSDD.h"
 #include "AliITSresponseSDD.h"
+#include "AliITSCalibrationSDD.h"
+#include "AliITSsegmentationSDD.h"
 #include "AliITSsimulationSDD.h"
+#include "AliLog.h"
+#include "AliRun.h"
 
 ClassImp(AliITSsimulationSDD)
 ////////////////////////////////////////////////////////////////////////
-// Version: 0
-// Written by Piergiorgio Cerello
-// November 23 1999
-//
-// AliITSsimulationSDD is the simulation of SDDs.
-  //
-//Begin_Html
-/*
-  <img src="picts/ITS/AliITShit_Class_Diagram.gif">
-  </pre>
-  <br clear=left>
-  <font size=+2 color=red>
-  <p>This show the relasionships between the ITS hit class and the 
-  rest of Aliroot.
-  </font>
-  <pre>
-*/
-//End_Html
- //______________________________________________________________________
+// Version: 0                                                         //
+// Written by Piergiorgio Cerello                                     //
+// November 23 1999                                                   //
+//                                                                    //
+// AliITSsimulationSDD is the simulation of SDDs.                     //
+////////////////////////////////////////////////////////////////////////
+
+//______________________________________________________________________
 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;
@@ -154,10 +132,10 @@ fAnodeFire(0),
 fHis(0),
 fD(),
 fT1(),
+fT2(),
 fTol(),
-fBaseline(),
-fNoise(),
 fTreeB(0),
+fParam(0),
 fFileName(),
 fFlag(kFALSE),
 fCheckNoise(kFALSE),
@@ -174,14 +152,36 @@ fScaleSize(0){
     SetCheckNoise();
 }
 //______________________________________________________________________
-AliITSsimulationSDD::AliITSsimulationSDD(AliITSsimulationSDD &source) :
-    AliITSsimulation(source){
+AliITSsimulationSDD::AliITSsimulationSDD(const AliITSsimulationSDD &source) :
+    AliITSsimulation(source),
+fITS(source.fITS),
+fHitMap2(source.fHitMap2),
+fHitSigMap2(source.fHitSigMap2),
+fHitNoiMap2(source.fHitNoiMap2),
+fStream(source.fStream),
+fElectronics(source.fElectronics),
+fInZR(source.fInZR),
+fInZI(source.fInZI),
+fOutZR(source.fOutZR),
+fOutZI(source.fOutZI),
+fAnodeFire(source.fAnodeFire),
+fHis(source.fHis),
+fD(source.fD),
+fT1(source.fT1),
+fT2(source.fT2),
+fTol(source.fTol),
+fTreeB(source.fTreeB),
+fParam(source.fParam),
+fFileName(source.fFileName),
+fFlag(source.fFlag),
+fCheckNoise(source.fCheckNoise),
+fCrosstalkFlag(source.fCrosstalkFlag),
+fDoFFT(source.fDoFFT),
+fNofMaps(source.fNofMaps),
+fMaxNofSamples(source.fMaxNofSamples),
+fScaleSize(source.fScaleSize){
     // Copy constructor to satify Coding roules only.
 
-    if(this==&source) return;
-    Error("AliITSsimulationSDD","Not allowed to make a copy of "
-          "AliITSsimulationSDD Using default creater instead");
-    AliITSsimulationSDD();
 }
 //______________________________________________________________________
 AliITSsimulationSDD& AliITSsimulationSDD::operator=(const AliITSsimulationSDD &src){
@@ -193,7 +193,7 @@ AliITSsimulationSDD& AliITSsimulationSDD::operator=(const AliITSsimulationSDD &s
     return *this ;
 }
 //______________________________________________________________________
-AliITSsimulationSDD& AliITSsimulationSDD::operator=(const AliITSsimulation &src){
+AliITSsimulation& AliITSsimulationSDD::operator=(const AliITSsimulation &src){
     // Assignment operator to satify Coding roules only.
 
     if(this==&src) return *this;
@@ -203,9 +203,8 @@ AliITSsimulationSDD& AliITSsimulationSDD::operator=(const AliITSsimulation &src)
 }
 
 //______________________________________________________________________
-AliITSsimulationSDD::AliITSsimulationSDD(AliITSsegmentation *seg,
-                                         AliITSresponse *resp):
-AliITSsimulation(seg,resp),
+AliITSsimulationSDD::AliITSsimulationSDD(AliITSDetTypeSim* dettyp):
+AliITSsimulation(dettyp),
 fITS(0),
 fHitMap2(0),
 fHitSigMap2(0),
@@ -220,10 +219,10 @@ fAnodeFire(0),
 fHis(0),
 fD(),
 fT1(),
+fT2(),
 fTol(),
-fBaseline(),
-fNoise(),
 fTreeB(0),
+fParam(),
 fFileName(),
 fFlag(kFALSE),
 fCheckNoise(kFALSE),
@@ -232,9 +231,8 @@ fDoFFT(1),
 fNofMaps(0),
 fMaxNofSamples(0),
 fScaleSize(0){
-    // Standard Constructor
-
-    Init();
+    // Default Constructor
+  Init();
 }
 //______________________________________________________________________
 void AliITSsimulationSDD::Init(){
@@ -246,23 +244,22 @@ void AliITSsimulationSDD::Init(){
     SetDoFFT();
     SetCheckNoise();
 
-    fpList = new AliITSpList( GetSeg()->Npz(),
-                              fScaleSize*GetSeg()->Npx() );
-    fHitSigMap2 = new AliITSMapA2(GetSeg(),fScaleSize,1);
-    fHitNoiMap2 = new AliITSMapA2(GetSeg(),fScaleSize,1);
+    AliITSsegmentationSDD* seg = (AliITSsegmentationSDD*)GetSegmentationModel(1);
+    
+    AliITSresponseSDD* res = (AliITSresponseSDD*)fDetType->GetResponse(1);
+    fpList = new AliITSpList( seg->Npz(),
+                              fScaleSize*seg->Npx() );
+    fHitSigMap2 = new AliITSMapA2(seg,fScaleSize,1);
+    fHitNoiMap2 = new AliITSMapA2(seg,fScaleSize,1);
     fHitMap2 = fHitSigMap2;
 
-    fNofMaps = GetSeg()->Npz();
-    fMaxNofSamples = GetSeg()->Npx();
+    fNofMaps = seg->Npz();
+    fMaxNofSamples = seg->Npx();
     fAnodeFire = new Bool_t [fNofMaps];
     
-    Float_t sddLength = GetSeg()->Dx();
-    Float_t sddWidth  = GetSeg()->Dz();
-
-    Int_t dummy        = 0;
-    Float_t anodePitch = GetSeg()->Dpz(dummy);
-    Double_t timeStep  = (Double_t)GetSeg()->Dpx(dummy);
-    Float_t driftSpeed = GetResp()->DriftSpeed();
+    Float_t sddWidth  = seg->Dz();
+    Float_t anodePitch = seg->Dpz(0);
+    Double_t timeStep  = (Double_t)seg->Dpx(0);
 
     if(anodePitch*(fNofMaps/2) > sddWidth) {
         Warning("AliITSsimulationSDD",
@@ -270,54 +267,23 @@ void AliITSsimulationSDD::Init(){
                 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,
-                                    GetResp()->Electronics());
+                                    res->Electronics());
 
     char opt1[20], opt2[20];
-    GetResp()->ParamOptions(opt1,opt2);
+    res->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=GetResp()->ZeroSuppOption();
-    if (strstr(fParam.Data(),"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"))  {
-            fT2.Set(2);
-            fTol.Set(2);
-            Init1D();      // desactivate if param change module by module
-        } // end if strstr
-    } else {
-        fD.Set(2);
-        fTol.Set(2);
-        fT1.Set(2);
-        fT2.Set(2);
-        SetCompressParam();
-    } // end if else strstr
-
-    Bool_t write = GetResp()->OutputOption();
+    const char *kopt=res->ZeroSuppOption();
+    fD.Set(fNofMaps);
+    fT1.Set(fNofMaps);
+    fT2.Set(fNofMaps);
+    fTol.Set(fNofMaps);
+    Bool_t write = res->OutputOption();
     if(write && strstr(kopt,"2D")) MakeTreeB();
-
-    // call here if baseline does not change by module
-    // ReadBaseline();
-
+  
     fITS       = (AliITS*)gAlice->GetModule("ITS");
     Int_t size = fNofMaps*fMaxNofSamples;
     fStream    = new AliITSInStream(size);
@@ -375,10 +341,10 @@ void AliITSsimulationSDD::SDigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
     if( !nhits ) return;
 
     InitSimulationModule( md, ev );
-    HitsToAnalogDigits( mod );
-    ChargeToSignal( kFALSE ); // - Process signal without add noise
+    HitsToAnalogDigits( mod );  // fills fHitMap2 which is = fHitSigmap2
+    ChargeToSignal( fModule,kFALSE,kTRUE ); // - Process signal adding gain without adding noise
     fHitMap2 = fHitNoiMap2;   // - Swap to noise map
-    ChargeToSignal( kTRUE );  // - Process only noise
+    ChargeToSignal( fModule,kTRUE,kFALSE );  // - Process only noise
     fHitMap2 = fHitSigMap2;   // - Return to signal map
     WriteSDigits();
     ClearMaps();
@@ -387,8 +353,9 @@ void AliITSsimulationSDD::SDigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
 Bool_t AliITSsimulationSDD::AddSDigitsToModule(TClonesArray *pItemArray,
                                                Int_t mask ) {
     // Add Summable digits to module maps.
+   AliITSresponseSDD* res = (AliITSresponseSDD*)fDetType->GetResponse(1);
     Int_t    nItems = pItemArray->GetEntries();
-    Double_t maxadc = GetResp()->MaxAdc();
+    Double_t maxadc = res->MaxAdc();
     Bool_t sig = kFALSE;
     
     // cout << "Adding "<< nItems <<" SDigits to module " << fModule << endl;
@@ -432,7 +399,7 @@ void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
     InitSimulationModule( md, ev );
 
     if( !nhits && fCheckNoise ) {
-        ChargeToSignal( kTRUE );  // process noise
+        ChargeToSignal( fModule,kTRUE,kFALSE );  // process noise
         GetNoise();
         ClearMaps();
         return;
@@ -440,7 +407,7 @@ void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
         if( !nhits ) return;
         
     HitsToAnalogDigits( mod );
-    ChargeToSignal( kTRUE );  // process signal + noise
+    ChargeToSignal( fModule,kTRUE,kTRUE );  // process signal + noise
 
     for( Int_t i=0; i<fNofMaps; i++ ) {
         for( Int_t j=0; j<fMaxNofSamples; j++ ) {
@@ -464,35 +431,36 @@ void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
 void AliITSsimulationSDD::FinishDigits() {
     // introduce the electronics effects and do zero-suppression if required
 
-    ApplyDeadChannels();
-    if( fCrosstalkFlag ) ApplyCrosstalk();
+    if( fCrosstalkFlag ) ApplyCrosstalk(fModule);
 
-    const char *kopt = GetResp()->ZeroSuppOption();
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+    const char *kopt = res->GetZeroSuppOption();
     ZeroSuppression( kopt );
 }
 //______________________________________________________________________
 void AliITSsimulationSDD::HitsToAnalogDigits( AliITSmodule *mod ) {
     // create maps to build the lists of tracks for each digit
-
-    TObjArray *hits     = mod->GetHits();
+  AliITSsegmentationSDD* seg = (AliITSsegmentationSDD*)GetSegmentationModel(1);
+  AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+  TObjArray *hits     = mod->GetHits();
     Int_t      nhits    = hits->GetEntriesFast();
+
     //    Int_t      arg[6]   = {0,0,0,0,0,0};
-    Int_t    dummy      = 0;
-    Int_t    nofAnodes  = fNofMaps/2;
-    Float_t  sddLength  = GetSeg()->Dx();
-    Float_t  sddWidth   = GetSeg()->Dz();
-    Float_t  anodePitch = GetSeg()->Dpz(dummy);
-    Float_t  timeStep   = GetSeg()->Dpx(dummy);
-    Float_t  driftSpeed = GetResp()->DriftSpeed();
-    Float_t  maxadc     = GetResp()->MaxAdc();    
-    Float_t  topValue   = GetResp()->DynamicRange();
-    Float_t  cHloss     = GetResp()->ChargeLoss();
-    Float_t  norm       = maxadc/topValue;
-    Double_t dfCoeff, s1; GetResp()->DiffCoeff(dfCoeff,s1); // Signal 2d Shape
-    Double_t eVpairs    = GetResp()->GetGeVToCharge()*1.0E9; // 3.6 eV by def.
-    Float_t  nsigma     = GetResp()->NSigmaIntegration(); //
-    Int_t    nlookups   = GetResp()->GausNLookUp();       //
-    Float_t  jitter     = ((AliITSresponseSDD*)GetResp())->JitterError(); // 
+    Int_t     nofAnodes  = fNofMaps/2;
+    Double_t  sddLength  = seg->Dx();
+    Double_t  sddWidth   = seg->Dz();
+    Double_t  anodePitch = seg->Dpz(0);
+    Double_t  timeStep   = seg->Dpx(0);
+    Double_t  driftSpeed ;  // drift velocity (anode dependent)
+    //Float_t   maxadc     = res->GetMaxAdc();    
+    //Float_t   topValue   = res->GetDynamicRange();
+    Double_t  norm       = res->GetMaxAdc()/res->GetDynamicRange(); //   maxadc/topValue;
+    Double_t  cHloss     = res->GetChargeLoss();
+    Float_t   dfCoeff, s1; res->DiffCoeff(dfCoeff,s1); // Signal 2d Shape
+    Double_t  eVpairs    = res->GetGeVToCharge()*1.0E9; // 3.6 eV by def.
+    Double_t  nsigma     = res->GetNSigmaIntegration(); //
+    Int_t     nlookups   = res->GetGausNLookUp();       //
+    Float_t   jitter     = res->GetJitterError(); // 
 
     // Piergiorgio's part (apart for few variables which I made float
     // when i thought that can be done
@@ -500,339 +468,227 @@ void AliITSsimulationSDD::HitsToAnalogDigits( AliITSmodule *mod ) {
     // loop over hits in the module
 
     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.
+    Int_t     itrack      = 0;
+    Int_t     iWing;       // which detector wing/side.
+    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.
+    Double_t  pathInSDD; // Track length in SDD.
+    Double_t  drPath; // average position of track in detector. in microns
+    Double_t  drTime; // Drift time
+    Double_t  nmul;   // drift time window multiplication factor.
+    Double_t  avDrft;  // x position of path length segment in cm.
+    Double_t  avAnode; // Anode for path length segment in Anode number (float)
+    Double_t  zAnode;  // Floating point anode number.
+    Double_t  driftPath; // avDrft in microns.
+    Double_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  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++) {
-        if(!mod->LineSegmentL(ii,xL[0],dxL[0],xL[1],dxL[1],xL[2],dxL[2],
+      if(!mod->LineSegmentL(ii,xL[0],dxL[0],xL[1],dxL[1],xL[2],dxL[2],
                               depEnergy,itrack)) continue;
-        xL[0] += 0.0001*gRandom->Gaus( 0, jitter ); //
-        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.
+      Float_t xloc=xL[0];
+      if(xloc>0) iWing=0; // left side, carlos channel 0
+      else iWing=1; // right side
+
+      Float_t zloc=xL[2]+0.5*dxL[2];
+      zAnode=seg->GetAnodeFromLocal(xloc,zloc); // anode number in the range 0.-511.
+      driftSpeed = res->GetDriftSpeedAtAnode(zAnode);
+      if(timeStep*fMaxNofSamples < sddLength/driftSpeed) {
+       AliWarning("Time Interval > Allowed Time Interval\n");
+      }
+      depEnergy  *= kconv;
         
-        // scale path to simulate a perpendicular track
-        // continue if the particle did not lose energy
-        // passing through detector
-        if (!depEnergy) {
-            if(GetDebug()){ 
-                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) {
-            if(GetDebug()){ // this should be fixed at geometry level
-                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
+      // scale path to simulate a perpendicular track
+      // continue if the particle did not lose energy
+      // passing through detector
+      if (!depEnergy) {
+       AliDebug(1,
+                Form("fTrack = %d hit=%d module=%d This particle has passed without losing energy!",
+                     itrack,ii,mod->GetIndex()));
+       continue;
+      } // end if !depEnergy
+      
+      xL[0] += 0.0001*gRandom->Gaus( 0, jitter ); //
+      pathInSDD = TMath::Sqrt(dxL[0]*dxL[0]+dxL[1]*dxL[1]+dxL[2]*dxL[2]);
+
+      if (fFlag && pathInSDD) { depEnergy *= (0.03/pathInSDD); }
+      drPath = TMath::Abs(10000.*(dxL[0]+2.*xL[0])*0.5);
+      drPath = sddLength-drPath;
+      if(drPath < 0) {
+       AliDebug(1, // this should be fixed at geometry level
+                Form("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;
-
-        // 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) {
-                if(GetDebug()){ // this should be fixed at geometry level
-                    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*GetResp()->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*
-                            GetResp()->GausLookUp(dummy);
-                    } // end if TMath::Abs(tExpo) > nsigma
-                    // build the list of Sdigits 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);
-                    Double_t charge = timeAmplitude;
-                    charge += fHitMap2->GetSignal(index,it-1);
-                    fHitMap2->SetHit(index, it-1, charge);
-                    fpList->AddSignal(index,it-1,itrack,ii-1,
-                                      mod->GetIndex(),timeAmplitude);
-                    fAnodeFire[index] = kTRUE;                 
-                } // end if anodeAmplitude and loop over time in window
-            } // loop over anodes in window
-        } // end loop over "sub-hits"
+      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;
+      
+      // loop over path segments, init. some variables.
+      depEnergy /= nOfSplits;
+      nOfSplitsF = (Float_t) nOfSplits;
+      Float_t theAverage=0.,theSteps=0.;
+      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;
+       theSteps+=1.;
+       theAverage+=avAnode;
+       zAnode = seg->GetAnodeFromLocal(avDrft,avAnode);
+       driftSpeed = res->GetDriftSpeedAtAnode(zAnode); 
+       driftPath = TMath::Abs(10000.*avDrft);
+       driftPath = sddLength-driftPath;
+       if(driftPath < 0) {
+         AliDebug(1, // this should be fixed at geometry level
+                  Form("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
+       drTime     = driftPath/driftSpeed; // drift time for segment.
+       timeSample = (Int_t) (fScaleSize*drTime/timeStep + 1); // time bin in range 1-256 !!!
+       if(timeSample > fScaleSize*fMaxNofSamples) {
+         AliWarning(Form("Wrong Time Sample: %e",timeSample));
+         continue;
+       } // end if timeSample > fScaleSize*fMaxNoofSamples
+       
+       if(zAnode>nofAnodes) zAnode-=nofAnodes;  // to have the anode number between 0. and 256.
+       if(zAnode*anodePitch > sddWidth || zAnode*anodePitch < 0.) 
+         AliWarning(Form("Exceeding sddWidth=%e Z = %e",sddWidth,zAnode*anodePitch));
+       iAnode = (Int_t) (1.+zAnode); // iAnode in range 1-256 !!!!
+       if(iAnode < 1 || iAnode > nofAnodes) {
+         AliWarning(Form("Wrong iAnode: 1<%d>%d  (xanode=%e)",iAnode,nofAnodes, zAnode));
+         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 = zAnode*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 - 2)*fScaleSize*nsplit +1;
+       jamax = (iAnode + anodeWindow/ndiv + 1)*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 {
+           Int_t theBin = (Int_t) ((aExpo+nsigma)/width+0.5);
+           anodeAmplitude = amplitude*res->GetGausLookUp(theBin);
+         } // end if TMath::Abs(aEspo) > nsigma
+         // index starts from 0
+         index = iWing*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 {
+               Int_t theBin = (Int_t) ((tExpo+nsigma)/width+0.5);
+               timeAmplitude = anodeAmplitude*res->GetGausLookUp(theBin);
+             } // end if TMath::Abs(tExpo) > nsigma
+             // build the list of Sdigits 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);
+             Double_t charge = timeAmplitude;
+             charge += fHitMap2->GetSignal(index,it-1);
+             fHitMap2->SetHit(index, it-1, charge);
+             fpList->AddSignal(index,it-1,itrack,ii-1,
+                               mod->GetIndex(),timeAmplitude);
+             fAnodeFire[index] = kTRUE;
+           }  // end loop over time in window               
+         } // end if anodeAmplitude 
+       } // loop over anodes in window
+      } // end loop over "sub-hits"
     } // end loop over hits
 }
-/*
-//______________________________________________________________________
-void AliITSsimulationSDD::ListOfFiredCells(Int_t *arg,Double_t timeAmplitude,
-                                          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
-
-        if(GetDebug()){
-            // 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
-        } // end if GetDebug()
-    } //  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.
-    Int_t size = AliITSdigitSPD::GetNTracks();
+    Int_t size = AliITSdigit::GetNTracks();
+
     Int_t digits[3];
     Int_t * tracks = new Int_t[size];
     Int_t * hits = new Int_t[size];
@@ -873,126 +729,88 @@ void AliITSsimulationSDD::AddDigit( Int_t i, Int_t j, Int_t signal ) {
     delete [] charges;
 }
 //______________________________________________________________________
-void AliITSsimulationSDD::ChargeToSignal(Bool_t bAddNoise) {
-    // add baseline, noise, electronics and ADC saturation effects
-
-    char opt1[20], opt2[20];
-    GetResp()->ParamOptions(opt1,opt2);
-    char *read = strstr(opt1,"file");
-    Double_t baseline, noise; 
-
-    if (read) {
-        static Bool_t readfile=kTRUE;
-        //read baseline and noise from file
-        if (readfile) ReadBaseline();
-        readfile=kFALSE;
-    } else GetResp()->GetNoiseParam(noise,baseline);
-
-    Float_t contrib=0;
-    Int_t i,k,kk;
-    Float_t maxadc = GetResp()->MaxAdc();    
+void AliITSsimulationSDD::ChargeToSignal(Int_t mod,Bool_t bAddNoise, Bool_t bAddGain) {
+  // add baseline, noise, gain, electronics and ADC saturation effects
+  // apply dead channels
+
+  char opt1[20], opt2[20];
+  AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(mod);
+  res->GetParamOptions(opt1,opt2);
+  Double_t baseline=0; 
+  Double_t noise=0; 
+  Double_t gain=0; 
+  Float_t contrib=0;
+  Int_t i,k,kk;
+  Float_t maxadc = res->GetMaxAdc();    
+
+  for (i=0;i<fNofMaps;i++) {
+    if( !fAnodeFire[i] ) continue;
+    baseline = res->GetBaseline(i);
+    noise = res->GetNoise(i);
+    gain = res->GetChannelGain(i);
+    if(res->IsBad()) gain=0.;
+    if( res->IsChipBad(res->GetChip(i)) ){ 
+      printf("Chip bad mod %d  chip %d anode %d\n",mod,res->GetChip(i),i);
+      gain=0.;
+    }
+    for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
+      fInZR[k]  = fHitMap2->GetSignal(i,k);
+      if(bAddGain) fInZR[k]*=gain;
+      if( bAddNoise ) {
+       contrib   = (baseline + noise*gRandom->Gaus());
+       fInZR[k] += contrib;
+      }
+      fInZI[k]  = 0.;
+    } // end for k
     if(!fDoFFT) {
-        for (i=0;i<fNofMaps;i++) {
-            if( !fAnodeFire[i] ) continue;
-            if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
-            for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
-                fInZR[k]  = fHitMap2->GetSignal(i,k);
-                if( bAddNoise ) {
-                    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
-
-    for (i=0;i<fNofMaps;i++) {
-        if( !fAnodeFire[i] ) continue;
-        if  (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
-        for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
-            fInZR[k]  = fHitMap2->GetSignal(i,k);
-            if( bAddNoise ) {
-                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++) {
-            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::ApplyDeadChannels() {    
-    // Set dead channel signal to zero
-    AliITSresponseSDD * response = (AliITSresponseSDD *)GetResp();
-    
-    // nothing to do
-    if( response->GetDeadModules() == 0 && 
-        response->GetDeadChips() == 0 && 
-        response->GetDeadChannels() == 0 )
-        return;  
-    
-    static AliITS *iTS = (AliITS*)gAlice->GetModule( "ITS" );
-
-    Int_t fMaxNofSamples = GetSeg()->Npx();    
-    AliITSgeom *geom = iTS->GetITSgeom();
-    Int_t firstSDDMod = geom->GetStartDet( 1 );
-    // loop over wings
-    for( Int_t j=0; j<2; j++ ) {
-        Int_t mod = (fModule-firstSDDMod)*2 + j;
-        for( Int_t u=0; u<response->Chips(); u++ )
-            for( Int_t v=0; v<response->Channels(); v++ ) {
-                Float_t Gain = response->Gain( mod, u, v );
-                for( Int_t k=0; k<fMaxNofSamples; k++ ) {
-                    Int_t i = j*response->Chips()*response->Channels() +
-                        u*response->Channels() + 
-                        v;
-                    Double_t signal =  Gain * fHitMap2->GetSignal( i, k );
-                    fHitMap2->SetHit( i, k, signal );  ///
-                }
-            }
-    }    
+      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
+    }else{
+      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++) {
+       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::ApplyCrosstalk() {
+void AliITSsimulationSDD::ApplyCrosstalk(Int_t mod) {
     // function add the crosstalk effect to signal
     // temporal function, should be checked...!!!
-
-    Int_t fNofMaps = GetSeg()->Npz();
-    Int_t fMaxNofSamples = GetSeg()->Npx();
+    AliITSsegmentationSDD* seg = (AliITSsegmentationSDD*)GetSegmentationModel(1);
+  
+    Int_t fNofMaps = seg->Npz();
+    Int_t fMaxNofSamples = seg->Npx();
 
     // create and inizialice crosstalk map
     Float_t* ctk = new Float_t[fNofMaps*fMaxNofSamples+1];
@@ -1001,11 +819,9 @@ void AliITSsimulationSDD::ApplyCrosstalk() {
         return;
     }
     memset( ctk, 0, sizeof(Float_t)*(fNofMaps*fMaxNofSamples+1) );
-
-    Double_t noise, baseline;
-    GetResp()->GetNoiseParam( noise, baseline );
-
+    AliITSCalibrationSDD* calibr = (AliITSCalibrationSDD*)GetCalibrationModel(mod);
     for( Int_t z=0; z<fNofMaps; z++ ) {
+      Double_t baseline = calibr->GetBaseline(z);
         Bool_t on = kFALSE;
         Int_t tstart = 0;
         Int_t tstop = 0;
@@ -1073,97 +889,33 @@ void AliITSsimulationSDD::ApplyCrosstalk() {
 
     delete [] ctk;
 }
-//______________________________________________________________________
-void AliITSsimulationSDD::GetAnodeBaseline(Int_t i,Double_t &baseline,
-                                           Double_t &noise){
-    // 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){
+                                           Int_t &th) const{
     // 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
+  db=fD[i]; 
+  tl=fT1[i]; 
+  th=fT2[i];
 }
 //______________________________________________________________________
-void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl){
+void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl) const{
     // 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
-        //     Warning("CompressionParam",
-        //             "Size= %d . Values i=%d ; db= %d ; tl= %d",
-        //              size,i,db,tl);
-    } // end if size > 2
+    db=fD[i]; 
+    tl=fT1[i];
 }
 //______________________________________________________________________
-void AliITSsimulationSDD::SetCompressParam(){
+void AliITSsimulationSDD::SetCompressParam(){ 
     // Sets the compression alogirthm parameters  
-    Int_t cp[8],i;
-
-    GetResp()->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
-
-    Int_t na,pos;
-    Float_t bl,n;
-    char input[100], base[100], param[100];
-    char *filtmp;
-
-    GetResp()->Filenames(input,base,param);
-    fFileName=base;
-    //
-    filtmp = gSystem->ExpandPathName(fFileName.Data());
-    FILE *bline = fopen(filtmp,"r");
-    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);
-            } // 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);
-    } // end if(bline)
-
-    fclose(bline);
-    delete [] filtmp;
+    AliITSCalibrationSDD* calibr = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+    for(Int_t ian = 0; ian<fNofMaps;ian++){
+      fD[ian] = (Int_t)(calibr->GetBaseline(ian));
+      fT1[ian] = (Int_t)(2.*calibr->GetNoiseAfterElectronics(ian)+0.5);
+      fT2[ian] = 0;   // used by 2D clustering - not defined yet
+      fTol[ian] = 0; // used by 2D clustering - not defined yet
+    }
 }
 //______________________________________________________________________
 Int_t AliITSsimulationSDD::Convert10to8(Int_t signal) const {
@@ -1187,7 +939,6 @@ AliITSMap*   AliITSsimulationSDD::HitMap(Int_t i){
 //______________________________________________________________________
 void AliITSsimulationSDD::ZeroSuppression(const char *option) {
     // perform the zero suppresion
-
     if (strstr(option,"2D")) {
         //Init2D();              // activate if param change module by module
         Compress2D();
@@ -1216,10 +967,12 @@ void AliITSsimulationSDD::Init2D(){
     char input[100],basel[100],par[100];
     char *filtmp;
     Double_t tmp1,tmp2;
-    GetResp()->Thresholds(tmp1,tmp2);
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+
+    res->Thresholds(tmp1,tmp2);
     Int_t minval = static_cast<Int_t>(tmp1);
 
-    GetResp()->Filenames(input,basel,par);
+    res->Filenames(input,basel,par);
     fFileName = par;
     //
     filtmp = gSystem->ExpandPathName(fFileName.Data());
@@ -1262,12 +1015,14 @@ void AliITSsimulationSDD::Compress2D(){
 
     Int_t db,tl,th; 
     Double_t tmp1,tmp2;
-    GetResp()->Thresholds(tmp1,tmp2); 
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+
+    res->Thresholds(tmp1,tmp2); 
     Int_t minval   = static_cast<Int_t>(tmp1);
-    Bool_t write   = GetResp()->OutputOption();   
-    Bool_t do10to8 = GetResp()->Do10to8();
+    Bool_t write   = res->OutputOption();   
+    Bool_t do10to8 = res->Do10to8();
     Int_t nz, nl, nh, low, i, j; 
-
+    SetCompressParam();
     for (i=0; i<fNofMaps; i++) {
         CompressionParam(i,db,tl,th);
         nz  = 0; 
@@ -1305,7 +1060,10 @@ void AliITSsimulationSDD::Compress2D(){
 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 = GetResp()->Do10to8();
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+    AliITSsegmentationSDD* seg = (AliITSsegmentationSDD*)GetSegmentationModel(1);
+  
+    Bool_t do10to8 = res->Do10to8();
     Bool_t high    = kFALSE;
 
     fHitMap2->FlagHit(i,j);
@@ -1316,7 +1074,7 @@ void  AliITSsimulationSDD::FindCluster(Int_t i,Int_t j,Int_t signal,
     Int_t nn;
     Int_t dbx,tlx,thx;  
     Int_t xList[kMaxNeighbours], yList[kMaxNeighbours];
-    GetSeg()->Neighbours(i,j,&nn,xList,yList);
+    seg->Neighbours(i,j,&nn,xList,yList);
     Int_t in,ix,iy,qns;
     for (in=0; in<nn; in++) {
         ix=xList[in];
@@ -1358,10 +1116,12 @@ void AliITSsimulationSDD::Init1D(){
     char input[100],basel[100],par[100];
     char *filtmp;
     Double_t tmp1,tmp2;
-    GetResp()->Thresholds(tmp1,tmp2);
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+
+    res->Thresholds(tmp1,tmp2);
     Int_t minval = static_cast<Int_t>(tmp1);
 
-    GetResp()->Filenames(input,basel,par);
+    res->Filenames(input,basel,par);
     fFileName=par;
 
     //  set first the disable and tol param
@@ -1405,10 +1165,12 @@ void AliITSsimulationSDD::Compress1D(){
     Int_t    dis,tol,thres,decr,diff;
     UChar_t *str=fStream->Stream();
     Int_t    counter=0;
-    Bool_t   do10to8=GetResp()->Do10to8();
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+
+    Bool_t   do10to8=res->Do10to8();
     Int_t    last=0;
     Int_t    k,i,j;
-
+    SetCompressParam();
     for (k=0; k<2; k++) {
         tol = Tolerance(k);
         dis = Disable(k);  
@@ -1460,7 +1222,7 @@ void AliITSsimulationSDD::Compress1D(){
     // open file and write out the stream of diff's
     static Bool_t open=kTRUE;
     static TFile *outFile;
-    Bool_t write = GetResp()->OutputOption();
+    Bool_t write = res->OutputOption();
     TDirectory *savedir = gDirectory;
  
     if (write ) {
@@ -1483,7 +1245,9 @@ void AliITSsimulationSDD::Compress1D(){
 //______________________________________________________________________
 void AliITSsimulationSDD::StoreAllDigits(){
     // if non-zero-suppressed data
-    Bool_t do10to8 = GetResp()->Do10to8();
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+
+    Bool_t do10to8 = res->Do10to8();
     Int_t i, j, digits[3];
 
     for (i=0; i<fNofMaps; i++) {
@@ -1494,7 +1258,7 @@ void AliITSsimulationSDD::StoreAllDigits(){
             digits[1] = j;
             digits[2] = signal;
             fITS->AddRealDigit(1,digits);
-        } // end for j
+       } // end for j
     } // end for i
 } 
 //______________________________________________________________________
@@ -1573,19 +1337,12 @@ Float_t AliITSsimulationSDD::GetNoise() {
     Int_t decr;
     Int_t threshold = fT1[0];
     char opt1[20], opt2[20];
-
-    GetResp()->ParamOptions(opt1,opt2);
+    AliITSCalibrationSDD* res = (AliITSCalibrationSDD*)GetCalibrationModel(fModule);
+    SetCompressParam();
+    res->GetParamOptions(opt1,opt2);
     fParam=opt2;
-    char *same = strstr(opt1,"same");
     Double_t noise,baseline;
-    if (same) {
-        GetResp()->GetNoiseParam(noise,baseline);
-    } else {
-        static Bool_t readfile=kTRUE;
-        //read baseline and noise from file
-        if (readfile) ReadBaseline();
-        readfile=kFALSE;
-    } // end if same
+    //GetBaseline(fModule);
 
     TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
     if(c2) delete c2->GetPrimitive("noisehist");
@@ -1600,7 +1357,8 @@ Float_t AliITSsimulationSDD::GetNoise() {
     Int_t i,k;
     for (i=0;i<fNofMaps;i++) {
         CompressionParam(i,decr,threshold); 
-        if  (!same) GetAnodeBaseline(i,baseline,noise);
+       baseline = res->GetBaseline(i);
+       noise = res->GetNoise(i);
         anode->Reset();
         for (k=0;k<fMaxNofSamples;k++) {
             Float_t signal=(Float_t)fHitMap2->GetSignal(i,k);
@@ -1629,7 +1387,7 @@ void AliITSsimulationSDD::WriteSDigits(){
 
     for( Int_t i=0; i<fNofMaps; i++ ) {
         if( !fAnodeFire[i] ) continue;
-        for( Int_t j=0; j<fMaxNofSamples; j++ ) {
+       for( Int_t j=0; j<fMaxNofSamples; j++ ) {
             Double_t sig = fHitMap2->GetSignal( i, j );
             if( sig > 0.2 ) {
                 Int_t jdx = j*fScaleSize;