Correct treatment of tracks with pT<pTmin
[u/mrichter/AliRoot.git] / ITS / AliITSDetTypeSim.cxx
index 0f695a7..8be79cd 100644 (file)
 #include "AliCDBStorage.h"
 #include "AliCDBEntry.h"
 #include "AliCDBMetaData.h"
-
 #include "AliITSdigit.h"
 #include "AliITSdigitSPD.h"
 #include "AliITSdigitSDD.h"
 #include "AliITSdigitSSD.h"
+#include "AliITSgeom.h"
 #include "AliITSDetTypeSim.h"
 #include "AliITSpListItem.h"
-#include "AliITSresponseSDD.h"
+#include "AliITSCalibration.h"
 #include "AliITSCalibrationSDD.h"
+#include "AliITSMapSDD.h"
+#include "AliITSCorrMapSDD.h"
+#include "AliITSDriftSpeedArraySDD.h"
+#include "AliITSDriftSpeedSDD.h"
+#include "AliITSCalibrationSSD.h"
+#include "AliITSNoiseSSDv2.h"
+#include "AliITSGainSSDv2.h"
+#include "AliITSBadChannelsSSDv2.h"
+#include "AliITSNoiseSSD.h"
+#include "AliITSGainSSD.h"
+#include "AliITSBadChannelsSSD.h"
 #include "AliITSCalibrationSSD.h"
 #include "AliITSsegmentationSPD.h"
 #include "AliITSsegmentationSDD.h"
 #include "AliITSsimulationSPD.h"
 #include "AliITSsimulationSDD.h"
 #include "AliITSsimulationSSD.h"
-
+#include "AliITSresponseSDD.h"
+#include "AliITSDDLModuleMapSDD.h"
+#include "AliITSTriggerConditions.h"
+#include "AliBaseLoader.h"
 
 const Int_t AliITSDetTypeSim::fgkNdettypes = 3;
 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSPD =  240;
 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSDD =  260;
 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSSD = 1698;
 
+using std::endl;
+using std::cout;
 ClassImp(AliITSDetTypeSim)
 
 //----------------------------------------------------------------------
@@ -67,18 +83,24 @@ TObject(),
 fSimulation(),   // [NDet]
 fSegmentation(), // [NDet]
 fCalibration(),     // [NMod]
-fPreProcess(),   // [] e.g. Fill fHitModule with hits
-fPostProcess(),  // [] e.g. Wright Raw data
+fSSDCalibration(0),
+fSPDNoisy(0),
+fSPDSparseDead(0),
 fNSDigits(0),    //! number of SDigits
-fSDigits(),      //! [NMod][NSDigits]
+fSDigits("AliITSpListItem",1000),   
 fNDigits(0),     //! number of Digits
 fRunNumber(0),   //! Run number (to access DB)
 fDigits(),       //! [NMod][NDigits]
-fHitClassName(), // String with Hit class name.
-fSDigClassName(),// String with SDigit class name.
-fDigClassName(), // String with digit class name.
+fSimuPar(0),
+fDDLMapSDD(0),
+fRespSDD(0),
+fAveGainSDD(0),
+fkDigClassName(), // String with digit class name.
 fLoader(0),      // local pointer to loader
-fFirstcall(kTRUE){ // flag
+fFirstcall(kTRUE),
+fFOGenerator(),
+fTriggerConditions(NULL)
+{ 
     // Default Constructor
     // Inputs:
     //    none.
@@ -90,12 +112,11 @@ fFirstcall(kTRUE){ // flag
   fSimulation = new TObjArray(fgkNdettypes);
   fSegmentation = new TObjArray(fgkNdettypes);
   fSegmentation->SetOwner(kTRUE);
-  fSDigits = new TClonesArray("AliITSpListItem",1000);
   fDigits = new TObjArray(fgkNdettypes);
   fNDigits = new Int_t[fgkNdettypes];
-  fNMod[0] = fgkDefaultNModulesSPD;
-  fNMod[1] = fgkDefaultNModulesSDD;
-  fNMod[2] = fgkDefaultNModulesSSD;
+  fDDLMapSDD=new AliITSDDLModuleMapSDD();
+  fSimuPar= new AliITSSimuParam();
+  fSSDCalibration=new AliITSCalibrationSSD();
   SetRunNumber();
 }
 //----------------------------------------------------------------------
@@ -119,38 +140,44 @@ AliITSDetTypeSim::~AliITSDetTypeSim(){
     }
     fSegmentation = 0;
     if(fCalibration && fRunNumber<0){
-       AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
-                               GetITSgeom()->GetStartSPD()))->GetResponse();
-       AliITSresponse* rsdd = ((AliITSCalibration*)fCalibration->At(
-                               GetITSgeom()->GetStartSDD()))->GetResponse();
-       AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
-                               GetITSgeom()->GetStartSSD()))->GetResponse();
-       if(rspd) delete rspd;
-       if(rsdd) delete rsdd;
-       if(rssd) delete rssd;
+
        fCalibration->Delete();
        delete fCalibration;
     }
     fCalibration = 0;
-    if(fPreProcess){
-       fPreProcess->Delete();
-       delete fPreProcess;
+    if(fSSDCalibration) {
+      if(!(AliCDBManager::Instance()->GetCacheFlag())) {
+       delete fSSDCalibration;
+       fSSDCalibration = NULL;
+      }
+    }
+    if(fSPDNoisy){
+    if(!(AliCDBManager::Instance()->GetCacheFlag())) {
+       fSPDNoisy->Delete();
+       delete fSPDNoisy;
+       fSPDNoisy = 0;
+      }
     }
-    fPreProcess = 0;
-    if(fPostProcess){
-       fPostProcess->Delete();
-       delete fPostProcess;
+    if(fSPDSparseDead){
+    if(!(AliCDBManager::Instance()->GetCacheFlag())) {
+       fSPDSparseDead->Delete();
+       delete fSPDSparseDead;
+       fSPDSparseDead = 0;
+      }
+    }
+    if(fSimuPar) delete fSimuPar;
+    if(fRespSDD){
+      if(!(AliCDBManager::Instance()->GetCacheFlag())){
+       delete fRespSDD;
+       fRespSDD=0;
+      }
     }
-    fPostProcess = 0;
+    if(fDDLMapSDD) delete fDDLMapSDD;
     if(fNDigits) delete [] fNDigits;
     fNDigits = 0;
     if (fLoader)fLoader->GetModulesFolder()->Remove(this);
     fLoader = 0; // Not deleting it.
-    if (fSDigits) {
-       fSDigits->Delete();
-       delete fSDigits;
-    }
-    fSDigits=0;
+    fSDigits.Delete();
     if (fDigits) {
       fDigits->Delete();
       delete fDigits;
@@ -158,24 +185,41 @@ AliITSDetTypeSim::~AliITSDetTypeSim(){
     fDigits=0;
 }
 //----------------------------------------------------------------------
-AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source){
-    // Copy Constructor for object AliITSDetTypeSim not allowed
-
-    if(this==&source) return;
-    Error("Copy constructor",
-         "You are not allowed to make a copy of the AliITSDetTypeSim");
-    exit(1);
-}
-//----------------------------------------------------------------------
-AliITSDetTypeSim& AliITSDetTypeSim::operator=(const AliITSDetTypeSim &source){
-    // The = operator for object AliITSDetTypeSim
+// AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source),
+// fSimulation(source.fSimulation),   // [NDet]
+// fSegmentation(source.fSegmentation), // [NDet]
+// fCalibration(source.fCalibration),     // [NMod]
+// fSSDCalibration(source.fSSDCalibration),
+// fSPDNoisy(source.fSPDNoisy),
+// fSPDSparseDead(source.fSPDSparseDead),
+// fNSDigits(source.fNSDigits),    //! number of SDigits
+// fSDigits(*((TClonesArray*)source.fSDigits.Clone())),
+// fNDigits(source.fNDigits),     //! number of Digits
+// fRunNumber(source.fRunNumber),   //! Run number (to access DB)
+// fDigits(source.fDigits),       //! [NMod][NDigits]
+// fSimuPar(source.fSimuPar),
+// fDDLMapSDD(source.fDDLMapSDD),
+// fRespSDD(source.fRespSDD),
+// fAveGainSDD(source.fAveGainSDD),
+// fkDigClassName(), // String with digit class name.
+// fLoader(source.fLoader),      // local pointer to loader
+// fFirstcall(source.fFirstcall),
+// fFOGenerator(source.fFOGenerator),
+// fTriggerConditions(source.fTriggerConditions) 
+// {
+//     // Copy Constructor for object AliITSDetTypeSim not allowed
+//   for(Int_t i=0;i<fgkNdettypes;i++){
+//     fkDigClassName[i] = source.fkDigClassName[i];
+//   }
+// }
+// //----------------------------------------------------------------------
+// AliITSDetTypeSim& AliITSDetTypeSim::operator=(const AliITSDetTypeSim &source){
+//     // The = operator for object AliITSDetTypeSim
  
-    if(&source==this) return *this;
-    Error("operator=",
-         "You are not allowed to make a copy of the AliITSDetTypeSIm");
-    exit(1);
-    return *this;
-}
+//   this->~AliITSDetTypeSim();
+//   new(this) AliITSDetTypeSim(source);
+//   return *this;
+// }
 
 //______________________________________________________________________
 void AliITSDetTypeSim::SetITSgeom(AliITSgeom *geom){
@@ -224,7 +268,7 @@ void AliITSDetTypeSim::SetSimulationModel(Int_t dettype,AliITSsimulation *sim){
   fSimulation->AddAt(sim,dettype);
 }
 //______________________________________________________________________
-AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype){
+AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype) const { 
 
   //Get simulation model for detector type
   if(fSimulation==0)  {
@@ -234,7 +278,7 @@ AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype){
   return (AliITSsimulation*)(fSimulation->At(dettype));
 }
 //______________________________________________________________________
-AliITSsimulation* AliITSDetTypeSim::GetSimulationModelByModule(Int_t module){
+AliITSsimulation* AliITSDetTypeSim::GetSimulationModelByModule(Int_t module) const {
 
   //Get simulation model by module number
   if(GetITSgeom()==0) {
@@ -256,13 +300,16 @@ void AliITSDetTypeSim::SetDefaultSegmentation(Int_t idet){
     if(GetSegmentationModel(idet))
        delete (AliITSsegmentation*)fSegmentation->At(idet);
     if(idet==0){
-       seg = new AliITSsegmentationSPD(GetITSgeom());
+       seg = new AliITSsegmentationSPD();
     }else if(idet==1){
-       AliITSCalibration* res=GetCalibrationModel(
-           GetITSgeom()->GetStartSDD());
-       seg = new AliITSsegmentationSDD(GetITSgeom(),res);
+      seg = new AliITSsegmentationSDD();
+      AliITSCalibrationSDD* cal=(AliITSCalibrationSDD*)GetCalibrationModel(fgkDefaultNModulesSPD+1);
+      if(cal->IsAMAt20MHz()){ 
+       seg->SetPadSize(seg->Dpz(0),20.);
+       seg->SetNPads(seg->Npz()/2,128);
+      }
     }else {
-       seg = new AliITSsegmentationSSD(GetITSgeom());
+       seg = new AliITSsegmentationSSD();
     }
     SetSegmentationModel(idet,seg);
 }
@@ -278,7 +325,7 @@ void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,
   fSegmentation->AddAt(seg,dettype);
 }
 //______________________________________________________________________
-AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype){
+AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype) const{
   //Get segmentation model for detector type
    
    if(fSegmentation==0) {
@@ -288,7 +335,7 @@ AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype){
    return (AliITSsegmentation*)(fSegmentation->At(dettype));
 }
 //_______________________________________________________________________
-AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module){
+AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module) const{
     //Get segmentation model by module number
     if(GetITSgeom()==0){
        Warning("GetSegmentationModelByModule","GetITSgeom() is 0!");
@@ -320,19 +367,44 @@ void AliITSDetTypeSim::SetCalibrationModel(Int_t iMod, AliITSCalibration *resp){
        delete (AliITSCalibration*) fCalibration->At(iMod);
     fCalibration->AddAt(resp, iMod);
 }
+//_______________________________________________________________________
+void AliITSDetTypeSim::SetSPDNoisyModel(Int_t iMod, AliITSCalibration *cal){
+  //Set noisy pixel info for the SPD module iMod
+  if (fSPDNoisy==0) {
+    fSPDNoisy = new TObjArray(fgkDefaultNModulesSPD);
+    fSPDNoisy->SetOwner(kTRUE);
+    fSPDNoisy->Clear();
+  }
+
+  if (fSPDNoisy->At(iMod) != 0)
+    delete (AliITSCalibration*) fSPDNoisy->At(iMod);
+  fSPDNoisy->AddAt(cal,iMod);
+}
+//_______________________________________________________________________
+void AliITSDetTypeSim::SetSPDSparseDeadModel(Int_t iMod, AliITSCalibration *cal){
+  //Set sparse dead pixel info for the SPD module iMod
+  if (fSPDSparseDead==0) {
+    fSPDSparseDead = new TObjArray(fgkDefaultNModulesSPD);
+    fSPDSparseDead->SetOwner(kTRUE);
+    fSPDSparseDead->Clear();
+  }
+
+  if (fSPDNoisy->At(iMod) != 0)
+    delete (AliITSCalibration*) fSPDNoisy->At(iMod);
+  fSPDNoisy->AddAt(cal,iMod);
+}
 //______________________________________________________________________
 void AliITSDetTypeSim::ResetCalibrationArray(){
     //resets response array
     if(fCalibration && fRunNumber<0){  // if fRunNumber<0 fCalibration is owner
+      /*
        AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
                                 GetITSgeom()->GetStartSPD()))->GetResponse();
-       AliITSresponse* rsdd = ((AliITSCalibration*)fCalibration->At(
-                                GetITSgeom()->GetStartSDD()))->GetResponse();
        AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
                                 GetITSgeom()->GetStartSSD()))->GetResponse();
        if(rspd) delete rspd;
-       if(rsdd) delete rsdd;
        if(rssd) delete rssd;
+      */
        fCalibration->Clear();
     }else if (fCalibration && fRunNumber>=0){
        fCalibration->Clear();
@@ -344,14 +416,30 @@ void AliITSDetTypeSim::ResetSegmentation(){
     if(fSegmentation) fSegmentation->Clear();
 }
 //_______________________________________________________________________
-AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod){
+AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod) const {
     //Get response model for module number iMod 
  
     if(fCalibration==0) {
        AliError("fCalibration is 0!");
        return 0; 
     }
-  return (AliITSCalibration*)(fCalibration->At(iMod));
+  if(iMod<fgkDefaultNModulesSPD+fgkDefaultNModulesSDD){
+    return (AliITSCalibration*)fCalibration->At(iMod);
+  }else{
+    Int_t i=iMod-(fgkDefaultNModulesSPD+fgkDefaultNModulesSDD);
+    fSSDCalibration->SetModule(i);
+    return (AliITSCalibration*)fSSDCalibration;
+  }
+
+}
+//_______________________________________________________________________
+AliITSCalibration* AliITSDetTypeSim::GetSPDNoisyModel(Int_t iMod) const {
+  //Get SPD noisy calib for module iMod 
+  if(fSPDNoisy==0) {
+    AliWarning("fSPDNoisy is 0!");
+    return 0; 
+  }
+  return (AliITSCalibration*)fSPDNoisy->At(iMod);
 }
 //_______________________________________________________________________
 void AliITSDetTypeSim::SetDefaults(){
@@ -368,43 +456,13 @@ void AliITSDetTypeSim::SetDefaults(){
     ResetSegmentation();
     if(!GetCalibration()){AliFatal("Exit"); exit(0);}
 
+    SetDigitClassName(0,"AliITSdigitSPD");
+    SetDigitClassName(1,"AliITSdigitSDD");
+    SetDigitClassName(2,"AliITSdigitSSD");
+
     for(Int_t idet=0;idet<fgkNdettypes;idet++){
-       //SPD
-       if(idet==0){
-           if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
-           const char *kData0=(GetCalibrationModel(GetITSgeom()->GetStartSPD()))->DataType();
-           if (strstr(kData0,"real")) {
-               SetDigitClassName(idet,"AliITSdigit");
-           }else {
-               SetDigitClassName(idet,"AliITSdigitSPD");
-           } // end if
-       } // end if idet==0
-       //SDD
-       if(idet==1){
-           if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
-           AliITSCalibrationSDD* rsp = 
-               (AliITSCalibrationSDD*)GetCalibrationModel(
-                   GetITSgeom()->GetStartSDD());
-           const char *kopt = ((AliITSresponseSDD*)rsp->GetResponse())->
-               ZeroSuppOption();
-           if((!strstr(kopt,"2D"))&&(!strstr(kopt,"1D"))) {
-               SetDigitClassName(idet,"AliITSdigit");
-           }else {
-               SetDigitClassName(idet,"AliITSdigitSDD");
-           } // end if
-       } // end if idet==1
-       //SSD
-       if(idet==2){
-           if(!GetSegmentationModel(idet))SetDefaultSegmentation(idet);
-           const char *kData2 = (GetCalibrationModel(
-                                  GetITSgeom()->GetStartSSD())->DataType());
-           if (strstr(kData2,"real")) {
-               SetDigitClassName(idet,"AliITSdigit");
-           }else {
-               SetDigitClassName(idet,"AliITSdigitSSD");
-           } // end if
-       } // end if idet==2
-    }// end for idet
+      if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
+    }
 }
 //______________________________________________________________________
 Bool_t AliITSDetTypeSim::GetCalibration() {
@@ -420,7 +478,6 @@ Bool_t AliITSDetTypeSim::GetCalibration() {
 
   SetRunNumber((Int_t)AliCDBManager::Instance()->GetRun());
   Int_t run=GetRunNumber();
-  if(run<0)run=0;   // if the run number is not yet set, use fake run # 0
 
   Bool_t origCacheStatus = AliCDBManager::Instance()->GetCacheFlag();
   Bool_t isCacheActive = kTRUE;
@@ -434,38 +491,44 @@ Bool_t AliITSDetTypeSim::GetCalibration() {
 
   AliCDBManager::Instance()->SetCacheFlag(isCacheActive);
 
-  AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSPD", run);
+  AliCDBEntry *deadSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDDead", run);
+  AliCDBEntry *noisySPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDNoisy", run);
+  AliCDBEntry *foEffSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDFOEfficiency", run);
+  AliCDBEntry *foNoiSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDFONoise", run);
   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", run);
-  AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", run);
-  AliCDBEntry *entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", run);
-  AliCDBEntry *entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD", run);
-  AliCDBEntry *entry2SSD = AliCDBManager::Instance()->Get("ITS/Calib/RespSSD", run);
-
-  if(!entrySPD || !entrySDD || !entrySSD || !entry2SPD || !entry2SDD || !entry2SSD){
-       AliWarning("Calibration object retrieval failed! Dummy calibration will be used.");
-       AliCDBStorage *localStor = 
-               AliCDBManager::Instance()->GetStorage("local://$ALICE_ROOT");
+  AliCDBEntry *entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD");
+  AliCDBEntry *drSpSDD = AliCDBManager::Instance()->Get("ITS/Calib/DriftSpeedSDD",run);
+  AliCDBEntry *ddlMapSDD = AliCDBManager::Instance()->Get("ITS/Calib/DDLMapSDD",run);
+  //AliCDBEntry *mapASDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsAnodeSDD",run);
+  AliCDBEntry *mapTSDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsTimeSDD",run);
+  // AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", run);
+  AliCDBEntry *entryNoiseSSD = AliCDBManager::Instance()->Get("ITS/Calib/NoiseSSD");
+  AliCDBEntry *entryGainSSD = AliCDBManager::Instance()->Get("ITS/Calib/GainSSD");
+  AliCDBEntry *entryBadChannelsSSD = AliCDBManager::Instance()->Get("ITS/Calib/BadChannelsSSD");
+
+if(!deadSPD || !noisySPD || !foEffSPD || !foNoiSPD 
+     || !entrySDD  || !entry2SDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD 
+     || !drSpSDD || !ddlMapSDD || !mapTSDD){
+    AliFatal("Calibration object retrieval failed! ");
+    return kFALSE;
+  }            
        
-       entrySPD = localStor->Get("ITS/Calib/CalibSPD", run);
-       entrySDD = localStor->Get("ITS/Calib/CalibSDD", run);
-       entrySSD = localStor->Get("ITS/Calib/CalibSSD", run);
-       entry2SPD = localStor->Get("ITS/Calib/RespSPD", run);
-       entry2SDD = localStor->Get("ITS/Calib/RespSDD", run);
-       entry2SSD = localStor->Get("ITS/Calib/RespSSD", run);
-  }
 
-  if(!entrySPD || !entrySDD || !entrySSD || !entry2SPD || !entry2SDD || !entry2SSD){
-    AliError("Calibration data was not found in $ALICE_ROOT!");
-    return kFALSE;
-  }
+  TObjArray *calDeadSPD = (TObjArray*) deadSPD->GetObject();
+  if (!isCacheActive) deadSPD->SetObject(NULL);
+  deadSPD->SetOwner(kTRUE);
 
-  TObjArray *calSPD = (TObjArray *)entrySPD->GetObject();
-  if(!isCacheActive)entrySPD->SetObject(NULL);
-  entrySPD->SetOwner(kTRUE);
+  TObjArray *calNoisySPD = (TObjArray*) noisySPD->GetObject();
+  if (!isCacheActive) noisySPD->SetObject(NULL);
+  noisySPD->SetOwner(kTRUE);
 
-  AliITSresponseSPD *pSPD = (AliITSresponseSPD*)entry2SPD->GetObject();
-  if(!isCacheActive)entry2SPD->SetObject(NULL);
-  entry2SPD->SetOwner(kTRUE);
+  AliITSFOEfficiencySPD *calFoEffSPD = (AliITSFOEfficiencySPD*) foEffSPD->GetObject();
+  if (!isCacheActive) foEffSPD->SetObject(NULL);
+  foEffSPD->SetOwner(kTRUE);
+
+  AliITSFONoiseSPD *calFoNoiSPD = (AliITSFONoiseSPD*) foNoiSPD->GetObject();
+  if (!isCacheActive) foNoiSPD->SetObject(NULL);
+  foNoiSPD->SetOwner(kTRUE);
    
   TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
   if(!isCacheActive)entrySDD->SetObject(NULL);
@@ -475,57 +538,189 @@ Bool_t AliITSDetTypeSim::GetCalibration() {
   if(!isCacheActive)entry2SDD->SetObject(NULL);
   entry2SDD->SetOwner(kTRUE);
 
+  TObjArray *drSp = (TObjArray *)drSpSDD->GetObject();
+  if(!isCacheActive)drSpSDD->SetObject(NULL);
+  drSpSDD->SetOwner(kTRUE);
+
+  AliITSDDLModuleMapSDD *ddlsdd=(AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
+  if(!isCacheActive)ddlMapSDD->SetObject(NULL);
+  ddlMapSDD->SetOwner(kTRUE);
+
+//   TObjArray *mapAn = (TObjArray *)mapASDD->GetObject();
+//   if(!isCacheActive)mapASDD->SetObject(NULL);
+//   mapASDD->SetOwner(kTRUE);
+
+  TObjArray *mapT = (TObjArray *)mapTSDD->GetObject();
+  if(!isCacheActive)mapTSDD->SetObject(NULL);
+  mapTSDD->SetOwner(kTRUE);
+
+  /*
   TObjArray *calSSD = (TObjArray *)entrySSD->GetObject();
   if(!isCacheActive)entrySSD->SetObject(NULL);
   entrySSD->SetOwner(kTRUE);
+  */
+
+  TObject *emptyssd = 0; TString ssdobjectname;
+  AliITSNoiseSSDv2 *noiseSSD = NULL; 
+  emptyssd = (TObject *)entryNoiseSSD->GetObject();
+  ssdobjectname = emptyssd->GetName();
+  if(ssdobjectname=="TObjArray") {
+    noiseSSD = new AliITSNoiseSSDv2(); 
+    TObjArray *noiseSSDOld = (TObjArray *)entryNoiseSSD->GetObject();
+    ReadOldSSDNoise(noiseSSDOld, noiseSSD);
+  }
+  else if(ssdobjectname=="AliITSNoiseSSDv2")
+    noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
+  if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
+  entryNoiseSSD->SetOwner(kTRUE);
+
+  AliITSGainSSDv2 *gainSSD = NULL;
+  emptyssd = (TObject *)entryGainSSD->GetObject();
+  ssdobjectname = emptyssd->GetName();
+  if(ssdobjectname=="Gain") {
+    TObjArray *gainSSDOld = (TObjArray *)entryGainSSD->GetObject();
+    gainSSD = new AliITSGainSSDv2();
+    ReadOldSSDGain(gainSSDOld, gainSSD);
+  }
+  else if(ssdobjectname=="AliITSGainSSDv2")
+    gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
+  if(!isCacheActive)entryGainSSD->SetObject(NULL);
+  entryGainSSD->SetOwner(kTRUE);
+
+  AliITSBadChannelsSSDv2 *badChannelsSSD = NULL;
+  emptyssd = (TObject *)entryBadChannelsSSD->GetObject();
+  ssdobjectname = emptyssd->GetName();
+  if(ssdobjectname=="TObjArray") {
+    TObjArray *badChannelsSSDOld = (TObjArray *)entryBadChannelsSSD->GetObject();
+    badChannelsSSD = new AliITSBadChannelsSSDv2();
+    ReadOldSSDBadChannels(badChannelsSSDOld, badChannelsSSD);
+  }
+  else if(ssdobjectname=="AliITSBadChannelsSSDv2")
+    badChannelsSSD = (AliITSBadChannelsSSDv2*)entryBadChannelsSSD->GetObject();
+  if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
+  entryBadChannelsSSD->SetOwner(kTRUE);
+
+  /*AliITSNoiseSSDv2 *noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
+  if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
+  entryNoiseSSD->SetOwner(kTRUE);
+
+  AliITSGainSSDv2 *gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
+  if(!isCacheActive)entryGainSSD->SetObject(NULL);
+  entryGainSSD->SetOwner(kTRUE);
+
+  AliITSBadChannelsSSDv2 *badchannelsSSD = 
+    (AliITSBadChannelsSSDv2 *)entryBadChannelsSSD->GetObject();
+  if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
+  entryBadChannelsSSD->SetOwner(kTRUE);*/
 
-  AliITSresponseSSD *pSSD = (AliITSresponseSSD*)entry2SSD->GetObject();
-  if(!isCacheActive)entry2SSD->SetObject(NULL);
-  entry2SSD->SetOwner(kTRUE);
-  
   // DB entries are deleted. In this way metadeta objects are deleted as well
   if(!isCacheActive){
-    delete entrySPD;
+    delete deadSPD;
+    delete noisySPD;
+    delete foEffSPD;
+    delete foNoiSPD;
     delete entrySDD;
-    delete entrySSD;
-    delete entry2SPD;
     delete entry2SDD;
-    delete entry2SSD;
+    delete entryNoiseSSD;
+    delete entryGainSSD;
+    delete entryBadChannelsSSD;
+//    delete mapASDD;   
+    delete mapTSDD;
+    delete drSpSDD;
+    delete ddlMapSDD;
   }
   
   AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
 
-  if ((!pSPD)||(!pSDD)||(!pSSD)) {
-    AliWarning("Can not get calibration from calibration database !");
-    return kFALSE;
-  }
-  if ((! calSPD)||(! calSDD)||(! calSSD)) {
+ if ((!calDeadSPD) || (!calNoisySPD) || (!calFoEffSPD) || (!calFoNoiSPD) 
+      || (!calSDD) || (!pSDD)|| (!drSp) || (!ddlsdd)  
+      || (!mapT) || (!noiseSSD)|| (!gainSSD)|| (!badChannelsSSD)) {
     AliWarning("Can not get calibration from calibration database !");
     return kFALSE;
   }
-  fNMod[0] = calSPD->GetEntries();
-  fNMod[1] = calSDD->GetEntries();
-  fNMod[2] = calSSD->GetEntries();
-  AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
-              fNMod[0], fNMod[1], fNMod[2]));
+
+
+  Int_t nmods0 = calDeadSPD->GetEntries();
+  Int_t nmods1 = calSDD->GetEntries();
+
+  AliDebug(1,Form("%i SPD, %i SDD and %i SSD in calibration database",
+                 nmods0,nmods1,fgkDefaultNModulesSSD));
   AliITSCalibration* cal;
-  for (Int_t i=0; i<fNMod[0]; i++) {
-    cal = (AliITSCalibration*) calSPD->At(i);
-    cal->SetResponse(pSPD);
+  for (Int_t i=0; i<nmods0; i++) {
+    cal = (AliITSCalibration*) calDeadSPD->At(i);
     SetCalibrationModel(i, cal);
- }
-  for (Int_t i=0; i<fNMod[1]; i++) {
-    cal = (AliITSCalibration*) calSDD->At(i);
-    cal->SetResponse(pSDD);
-    Int_t iMod = i + fNMod[0];
-    SetCalibrationModel(iMod, cal);
- }
-  for (Int_t i=0; i<fNMod[2]; i++) {
-    cal = (AliITSCalibration*) calSSD->At(i);
-    cal->SetResponse(pSSD);
-    Int_t iMod = i + fNMod[0] + fNMod[1];
-    SetCalibrationModel(iMod, cal);
- }
+    cal = (AliITSCalibration*) calNoisySPD->At(i);
+    SetSPDNoisyModel(i, cal);
+  }
+
+  fFOGenerator.SetEfficiency(calFoEffSPD); // this cal object is used only by the generator
+  fFOGenerator.SetNoise(calFoNoiSPD); // this cal object is used only by the generator
+  
+  fDDLMapSDD->SetDDLMap(ddlsdd);
+  fRespSDD=pSDD;
+  Float_t avegain=0.;
+  Float_t nGdAnodes=0;
+  Bool_t oldMapFormat=kFALSE;
+  TObject* objmap=(TObject*)mapT->At(0);
+  TString cname(objmap->ClassName());
+  if(cname.CompareTo("AliITSMapSDD")==0){ 
+    oldMapFormat=kTRUE;
+    AliInfo("SDD Maps converted to new format");
+  }
+
+  for (Int_t i=0; i<fgkDefaultNModulesSDD; i++) {
+    Int_t iddl,icarlos;
+    Int_t iMod = i + fgkDefaultNModulesSPD;
+    fDDLMapSDD->FindInDDLMap(iMod,iddl,icarlos);
+    if(iddl<0){ 
+      AliITSCalibrationSDD* calsdddead=new AliITSCalibrationSDD();
+      calsdddead->SetBad();      
+      AliITSDriftSpeedSDD* driftspdef = new AliITSDriftSpeedSDD();
+      AliITSDriftSpeedArraySDD* arrdrsp=new AliITSDriftSpeedArraySDD(1);
+      arrdrsp->AddDriftSpeed(driftspdef);
+      calsdddead->SetDriftSpeed(0,arrdrsp);
+      calsdddead->SetDriftSpeed(1,arrdrsp);
+      SetCalibrationModel(iMod, calsdddead);
+      AliWarning(Form("SDD module %d not present in DDL map: set it as dead",iMod));
+    }else{
+      cal = (AliITSCalibration*) calSDD->At(i);
+      for(Int_t iAnode=0;iAnode< ((AliITSCalibrationSDD*)cal)->NOfAnodes(); iAnode++){
+       if(((AliITSCalibrationSDD*)cal)->IsBadChannel(iAnode)) continue;
+       avegain+= ((AliITSCalibrationSDD*)cal)->GetChannelGain(iAnode);
+       nGdAnodes++;
+      }
+      Int_t i0=2*i;
+      Int_t i1=1+2*i;
+      AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
+      AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
+
+      AliITSCorrMapSDD* mt0 = 0;
+      AliITSCorrMapSDD* mt1 = 0;
+      if(oldMapFormat){ 
+       AliITSMapSDD* oldmap0=(AliITSMapSDD*)mapT->At(i0);
+       AliITSMapSDD* oldmap1=(AliITSMapSDD*)mapT->At(i1);
+       mt0=oldmap0->ConvertToNewFormat();
+       mt1=oldmap1->ConvertToNewFormat();
+      }else{
+       mt0=(AliITSCorrMapSDD*)mapT->At(i0);
+       mt1=(AliITSCorrMapSDD*)mapT->At(i1);
+      }
+      cal->SetDriftSpeed(0,arr0);
+      cal->SetDriftSpeed(1,arr1);
+      cal->SetMapT(0,mt0);
+      cal->SetMapT(1,mt1);
+      SetCalibrationModel(iMod, cal);
+    }
+  }
+  if(nGdAnodes) fAveGainSDD=avegain/nGdAnodes;
+  AliDebug(3,Form("SDD average gain=%f\n",fAveGainSDD));
+  fSSDCalibration->SetNoise(noiseSSD);
+  fSSDCalibration->SetGain(gainSSD);
+  fSSDCalibration->SetBadChannels(badChannelsSSD);
+  //fSSDCalibration->FillBadChipMap();
+
+
+
   return kTRUE;
 }
 //_______________________________________________________________________
@@ -579,30 +774,26 @@ void AliITSDetTypeSim::SetDefaultSimulation(){
   }
 }
 //___________________________________________________________________
-void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, Char_t* name){
+void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, const Char_t* name){
   // Set branch address for the ITS summable digits Trees.  
-  char branchname[30];
 
   if(!treeS){
     return;
   }
-  if (fSDigits ==  0x0){
-    fSDigits = new TClonesArray("AliITSpListItem",1000);
-  }
   TBranch *branch;
-  sprintf(branchname,"%s",name);
-  branch = treeS->GetBranch(branchname);
-  if (branch) branch->SetAddress(&fSDigits);
+  branch = treeS->GetBranch(name);
+  TClonesArray *sdigi = &fSDigits;
+  if (branch) branch->SetAddress(&sdigi);
 
 }
 //___________________________________________________________________
-void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
+void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, const Char_t* name){
   // Set branch address for the digit Trees.
   
   const char *det[3] = {"SPD","SDD","SSD"};
   TBranch *branch;
   
-  char branchname[30];
+  TString branchname;
   
   if(!treeD){
     return;
@@ -611,7 +802,7 @@ void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
     fDigits = new TObjArray(fgkNdettypes); 
   }
   for(Int_t i=0;i<fgkNdettypes;i++){
-    Char_t* digclass = GetDigitClassName(i);
+    const Char_t* digclass = GetDigitClassName(i);
     if(digclass==0x0){
       if(i==0) SetDigitClassName(i,"AliITSdigitSPD");
       if(i==1) SetDigitClassName(i,"AliITSdigitSDD");
@@ -625,14 +816,11 @@ void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
       ResetDigits(i);
     }
     
-    if(fgkNdettypes==3) sprintf(branchname,"%sDigits%s",name,det[i]);
-    else sprintf(branchname,"%sDigits%d",name,i+1);
-    if(fDigits){
-      branch = treeD->GetBranch(branchname);
-      if(branch) branch->SetAddress(&((*fDigits)[i]));
-    }
+    if(fgkNdettypes==3) branchname.Form("%sDigits%s",name,det[i]);
+    else branchname.Form("%sDigits%d",name,i+1);
+    branch = treeD->GetBranch(branchname.Data());
+    if(branch) branch->SetAddress(&((*fDigits)[i]));    
   }
-
 }
 //___________________________________________________________________
 void AliITSDetTypeSim::ResetDigits(){
@@ -670,8 +858,8 @@ void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
   const char *all = strstr(opt,"All");
   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
                        strstr(opt,"SSD")};
-  if( !det[0] && !det[1] && !det[2] ) all = "All";
-  else all = 0;
+  if(!all && !det[0] && !det[1] && !det[2] ) all = "All";
+
   static Bool_t setDef = kTRUE;
   if(setDef) SetDefaultSimulation();
   setDef = kFALSE;
@@ -682,7 +870,7 @@ void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
     Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
     return;
   } 
-  sprintf(name,"%s",name);
+
   TBranch* brchSDigits = trees->GetBranch(name);
   
   Int_t id;
@@ -698,13 +886,15 @@ void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
     }
     sim->InitSimulationModule(module,gAlice->GetEvNumber());
     
-    fSDigits->Clear();
+    fSDigits.Clear();
     brchSDigits->GetEvent(module);
-    sim->AddSDigitsToModule(fSDigits,0);
+    sim->AddSDigitsToModule(&fSDigits,0);
     sim->FinishSDigitiseModule();
     fLoader->TreeD()->Fill();
     ResetDigits();
   }
+
+  WriteFOSignals(); 
   fLoader->TreeD()->GetEntries();
   fLoader->TreeD()->AutoSave();
   fLoader->TreeD()->Reset();
@@ -713,18 +903,10 @@ void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
 void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){  
   //Adds the module full of summable digits to the summable digits tree.
 
-  TClonesArray &lsdig = *fSDigits;
-  new(lsdig[fNSDigits++]) AliITSpListItem(sdig);
+  new(fSDigits[fNSDigits++]) AliITSpListItem(sdig);
 }
 //__________________________________________________________
-void AliITSDetTypeSim::AddRealDigit(Int_t branch, Int_t *digits){
-  //   Add a real digit - as coming from data.
-
-  TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
-  new(ldigits[fNDigits[branch]++]) AliITSdigit(digits); 
-}
-//__________________________________________________________
-void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){  
+void AliITSDetTypeSim::AddSimDigit(Int_t branch, const AliITSdigit* d){  
   //    Add a simulated digit.
 
   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
@@ -742,20 +924,18 @@ void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){
 }
 //______________________________________________________________________
 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
-                                  Int_t *tracks,Int_t *hits,Float_t *charges){
+                                  Int_t *tracks,Int_t *hits,Float_t *charges, 
+                                  Int_t sigexpanded){
   //   Add a simulated digit to the list.
 
   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
-  AliITSCalibrationSDD *resp = 0;
   switch(branch){
   case 0:
     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
     break;
   case 1:
-    resp = (AliITSCalibrationSDD*)GetCalibrationModel(
-       GetITSgeom()->GetStartSDD());
     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
-                                                  hits,charges,resp);
+                                                  hits,charges,sigexpanded);
     break;
   case 2:
     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(digits,tracks,hits);
@@ -763,51 +943,125 @@ void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
   } 
 }
 //______________________________________________________________________
-void AliITSDetTypeSim::StoreCalibration(Int_t firstRun, Int_t lastRun,
-                                       AliCDBMetaData &md) {
-  // Store calibration in the calibration database
-  // The database must be created in an external piece of code (i.e. 
-  // a configuration macro )
+void AliITSDetTypeSim::ReadOldSSDNoise(const TObjArray *array, 
+                                      AliITSNoiseSSDv2 *noiseSSD) {
+  //Reads the old SSD calibration object and converts it to the new format
+  const Int_t fgkSSDSTRIPSPERMODULE = 1536;
+  const Int_t fgkSSDPSIDESTRIPSPERMODULE = 768;
+
+  Int_t gNMod = array->GetEntries();
+  cout<<"Converting old calibration object for noise..."<<endl;
+
+  //NOISE
+  Double_t noise = 0.0;
+  for (Int_t iModule = 0; iModule < gNMod; iModule++) {
+    AliITSNoiseSSD *noiseModule = (AliITSNoiseSSD*) (array->At(iModule));
+    for(Int_t iStrip = 0; iStrip < fgkSSDSTRIPSPERMODULE; iStrip++) {
+      noise = (iStrip < fgkSSDPSIDESTRIPSPERMODULE) ? noiseModule->GetNoiseP(iStrip) : noiseModule->GetNoiseN(1535 - iStrip);
+      if(iStrip < fgkSSDPSIDESTRIPSPERMODULE)
+       noiseSSD->AddNoiseP(iModule,iStrip,noise);
+      if(iStrip >= fgkSSDPSIDESTRIPSPERMODULE)
+       noiseSSD->AddNoiseN(iModule,1535 - iStrip,noise);
+    }//loop over strips
+  }//loop over modules      
+}
 
-  if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
-    AliWarning("No storage set! Will use dummy one");
-    AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
+//______________________________________________________________________
+void AliITSDetTypeSim::ReadOldSSDBadChannels(const TObjArray *array, 
+                                            AliITSBadChannelsSSDv2 *badChannelsSSD) {
+  //Reads the old SSD calibration object and converts it to the new format
+  Int_t nMod = array->GetEntries();
+  cout<<"Converting old calibration object for bad channels..."<<endl;
+  for (Int_t iModule = 0; iModule < nMod; iModule++) {
+    //for (Int_t iModule = 0; iModule < 1; iModule++) {
+    AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (array->At(iModule));
+    TArrayI arrayPSide = bad->GetBadPChannelsList();
+    for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++) 
+      badChannelsSSD->AddBadChannelP(iModule,
+                                    iPCounter,
+                                    (Char_t)arrayPSide.At(iPCounter));
+        
+    TArrayI arrayNSide = bad->GetBadNChannelsList();
+    for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++) 
+      badChannelsSSD->AddBadChannelN(iModule,
+                                    iNCounter,
+                                    (Char_t)arrayNSide.At(iNCounter));
+    
+  }//loop over modules      
+}
+
+//______________________________________________________________________
+void AliITSDetTypeSim::ReadOldSSDGain(const TObjArray *array, 
+                                     AliITSGainSSDv2 *gainSSD) {
+  //Reads the old SSD calibration object and converts it to the new format
+
+  Int_t nMod = array->GetEntries();
+  cout<<"Converting old calibration object for gain..."<<endl;
+
+  //GAIN
+  for (Int_t iModule = 0; iModule < nMod; iModule++) {
+    AliITSGainSSD *gainModule = (AliITSGainSSD*) (array->At(iModule));
+    TArrayF arrayPSide = gainModule->GetGainP();
+    for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++)
+      gainSSD->AddGainP(iModule,
+                       iPCounter,
+                       arrayPSide.At(iPCounter));
+    TArrayF arrayNSide = gainModule->GetGainN();
+    for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++)
+      gainSSD->AddGainN(iModule,
+                       iNCounter,
+                       arrayNSide.At(iNCounter));
+  }//loop over modules 
+}
+//______________________________________________________________________
+void AliITSDetTypeSim::ProcessSPDDigitForFastOr(UInt_t module, UInt_t colM, UInt_t rowM) {
+  // Processes wether a single fired pixel will give rise to a fast-or signal
+  fFOGenerator.ProcessPixelHitM(module,colM,rowM);
+}
+//_______________________________________________________________________
+AliITSTriggerConditions* AliITSDetTypeSim::GetTriggerConditions() {
+  // Get Pixel Trigger Conditions (separate method since it is used only when simulating trigger)
+  if (fTriggerConditions==NULL) { // read from db
+    fRunNumber = ((Int_t)AliCDBManager::Instance()->GetRun());
+    Bool_t origCacheStatus = AliCDBManager::Instance()->GetCacheFlag();
+    Bool_t isCacheActive;
+    if (fRunNumber<0) isCacheActive=kFALSE;
+    else              isCacheActive=kTRUE;
+    AliCDBManager::Instance()->SetCacheFlag(isCacheActive);
+    AliCDBEntry *pitCond = AliCDBManager::Instance()->Get("TRIGGER/SPD/PITConditions", fRunNumber);
+    if (!pitCond) {
+      AliError("Trigger conditions retrieval failed! ");
+      return NULL;
+    }
+    fTriggerConditions = (AliITSTriggerConditions*) pitCond->GetObject();
+    if (!isCacheActive) pitCond->SetObject(NULL);
+    pitCond->SetOwner(kTRUE);
+    if (!isCacheActive) {
+      delete pitCond;
+    }
+    AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
+    if (fTriggerConditions==NULL) {
+      AliWarning("fTriggerConditions is NULL!");
+    }
   }
+  return fTriggerConditions;
+}
+//_______________________________________________________________________
+void AliITSDetTypeSim::WriteFOSignals() {
+  // write fo signals to event
 
-  if (!fCalibration) {
-    AliError("AliITSCalibration classes are not defined - nothing done");
+  if (!fLoader) {
+    AliError("ITS loader is NULL.");
     return;
   }
-  AliCDBId idRespSPD("ITS/Calib/CalibSPD",firstRun, lastRun);
-  AliCDBId idRespSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
-  AliCDBId idRespSSD("ITS/Calib/CalibSSD",firstRun, lastRun);
 
-  TObjArray respSPD(fNMod[0]);
-  TObjArray respSDD(fNMod[1]-fNMod[0]);
-  TObjArray respSSD(fNMod[2]-fNMod[1]);
-  respSPD.SetOwner(kFALSE);
-  respSSD.SetOwner(kFALSE);
-  respSSD.SetOwner(kFALSE);
-
-  Int_t index[fgkNdettypes];
-  for (Int_t i = 0; i<fgkNdettypes; i++ ) {
-    index[i] = 0;
-    for (Int_t j = 0; j<=i; j++ )
-      index[i]+=fNMod[j];
+  if(!fLoader->TreeD()){
+   AliError("No TreeD available");
+   return;
   }
-
-  for (Int_t i = 0; i<index[0]; i++ )
-    respSPD.Add(fCalibration->At(i));
-
-  for (Int_t i = index[0]; i<index[1]; i++ )
-    respSDD.Add(fCalibration->At(i));
-
-  for (Int_t i = index[1]; i<index[2]; i++ )
-    respSSD.Add(fCalibration->At(i));
-
-  AliCDBManager::Instance()->Put(&respSPD, idRespSPD, &md);
-  AliCDBManager::Instance()->Put(&respSDD, idRespSDD, &md);
-  AliCDBManager::Instance()->Put(&respSSD, idRespSSD, &md);
+  TTree *tree = fLoader->TreeD();
+  AliITSFOSignalsSPD *foSignals = new AliITSFOSignalsSPD(*GetFOSignals()); 
+  tree->GetUserInfo()->Add(foSignals);
+  fFOGenerator.ResetSignals();
 }
 
-