]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSDetTypeSim.cxx
Switch off global QA for the moment
[u/mrichter/AliRoot.git] / ITS / AliITSDetTypeSim.cxx
index d809e8f1ad6c2c848809e940a488c8c8e618655f..e7f17f05eee0df3be2b0857c54ff5b0b3d7ea954 100644 (file)
@@ -17,6 +17,7 @@
  $Id$
 */
 
+
 /////////////////////////////////////////////////////////////////////
 // Base simulation functions for ITS                               //
 //                                                                 //
 #include "TTree.h"
 
 #include "AliRun.h"
+
+#include "AliCDBManager.h"
+#include "AliCDBId.h"
+#include "AliCDBStorage.h"
+#include "AliCDBEntry.h"
+#include "AliCDBMetaData.h"
+
 #include "AliITSdigit.h"
 #include "AliITSdigitSPD.h"
 #include "AliITSdigitSDD.h"
 #include "AliITSdigitSSD.h"
 #include "AliITSDetTypeSim.h"
-#include "AliITSgeom.h"
 #include "AliITSpListItem.h"
-#include "AliITSresponseSPD.h"
 #include "AliITSresponseSDD.h"
-#include "AliITSresponseSSD.h"
+#include "AliITSCalibrationSDD.h"
+#include "AliITSMapSDD.h"
+#include "AliITSDriftSpeedArraySDD.h"
+#include "AliITSDriftSpeedSDD.h"
+#include "AliITSCalibrationSSD.h"
+#include "AliITSNoiseSSD.h"
+#include "AliITSGainSSD.h"
+#include "AliITSBadChannelsSSD.h"
+#include "AliITSCalibrationSSD.h"
 #include "AliITSsegmentationSPD.h"
 #include "AliITSsegmentationSDD.h"
 #include "AliITSsegmentationSSD.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;
 
 ClassImp(AliITSDetTypeSim)
 
 //----------------------------------------------------------------------
 AliITSDetTypeSim::AliITSDetTypeSim():
 TObject(),
-fGeom(),         //
 fSimulation(),   // [NDet]
 fSegmentation(), // [NDet]
-fResponse(),     // [NMod]
+fCalibration(),     // [NMod]
 fPreProcess(),   // [] e.g. Fill fHitModule with hits
 fPostProcess(),  // [] e.g. Wright Raw data
 fNSDigits(0),    //! number of SDigits
 fSDigits(),      //! [NMod][NSDigits]
 fNDigits(0),     //! number of Digits
+fRunNumber(0),   //! Run number (to access DB)
 fDigits(),       //! [NMod][NDigits]
+fDDLMapSDD(0),
 fHitClassName(), // String with Hit class name.
 fSDigClassName(),// String with SDigit class name.
-fDigClassName(){ // String with digit class name.
+fDigClassName(), // String with digit class name.
+fLoader(0),      // local pointer to loader
+fFirstcall(kTRUE){ // flag
     // Default Constructor
     // Inputs:
     //    none.
@@ -74,17 +94,18 @@ fDigClassName(){ // String with digit class name.
     //    none.
     // Return:
     //    A properly zero-ed AliITSDetTypeSim class.
-  fGeom = 0;
+
   fSimulation = new TObjArray(fgkNdettypes);
   fSegmentation = new TObjArray(fgkNdettypes);
-  fResponse = 0;
-  fPreProcess = 0;
-  fPostProcess = 0;
-  fNSDigits = 0;
+  fSegmentation->SetOwner(kTRUE);
   fSDigits = new TClonesArray("AliITSpListItem",1000);
   fDigits = new TObjArray(fgkNdettypes);
   fNDigits = new Int_t[fgkNdettypes];
-  fLoader = 0;
+  fDDLMapSDD=new AliITSDDLModuleMapSDD();
+  fNMod[0] = fgkDefaultNModulesSPD;
+  fNMod[1] = fgkDefaultNModulesSDD;
+  fNMod[2] = fgkDefaultNModulesSSD;
+  SetRunNumber();
 }
 //----------------------------------------------------------------------
 AliITSDetTypeSim::~AliITSDetTypeSim(){
@@ -95,77 +116,128 @@ AliITSDetTypeSim::~AliITSDetTypeSim(){
     //    none.
     // Return:
     //    Nothing.
-  
-    if(fGeom) delete fGeom;
+
     if(fSimulation){
-      fSimulation->Delete();
-      delete fSimulation;
-      fSimulation = 0;
+       fSimulation->Delete();
+       delete fSimulation;
     }
-    
+    fSimulation = 0;
     if(fSegmentation){
-      fSegmentation->Delete();
-      delete fSegmentation;
-      fSegmentation = 0;
+       fSegmentation->Delete();
+       delete fSegmentation;
     }
-    
-    if(fResponse){
-      fResponse->Delete();
-      delete fResponse;
-      fResponse = 0;
+    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;
-      fPreProcess = 0;
+       fPreProcess->Delete();
+       delete fPreProcess;
     }
-    
+    fPreProcess = 0;
     if(fPostProcess){
-      fPostProcess->Delete();
-      delete fPostProcess;
-      fPostProcess = 0;
+       fPostProcess->Delete();
+       delete fPostProcess;
     }
-    
-    if (fLoader)
-      {
-       fLoader->GetModulesFolder()->Remove(this);
-      }
-    
-        
+    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();
+       delete fSDigits;
     }
+    fSDigits=0;
     if (fDigits) {
       fDigits->Delete();
       delete fDigits;
-      fDigits=0;
     }
-  
+    fDigits=0;
 }
 //----------------------------------------------------------------------
-AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source){
+AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source),
+fSimulation(source.fSimulation),   // [NDet]
+fSegmentation(source.fSegmentation), // [NDet]
+fCalibration(source.fCalibration),     // [NMod]
+fPreProcess(source.fPreProcess),   // [] e.g. Fill fHitModule with hits
+fPostProcess(source.fPostProcess),  // [] e.g. Wright Raw data
+fNSDigits(source.fNSDigits),    //! number of SDigits
+fSDigits(source.fSDigits),      //! [NMod][NSDigits]
+fNDigits(source.fNDigits),     //! number of Digits
+fRunNumber(source.fRunNumber),   //! Run number (to access DB)
+fDigits(source.fDigits),       //! [NMod][NDigits]
+fDDLMapSDD(source.fDDLMapSDD),
+fHitClassName(source.fHitClassName), // String with Hit class name.
+fSDigClassName(source.fSDigClassName),// String with SDigit class name.
+fDigClassName(), // String with digit class name.
+fLoader(source.fLoader),      // local pointer to loader
+fFirstcall(source.fFirstcall)
+{
     // 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);
-
-        
+  for(Int_t i=0;i<fgkNdettypes;i++){
+    fDigClassName[i] = source.fDigClassName[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){
+    // Sets/replaces the existing AliITSgeom object kept in AliITSLoader
+    // 
+    // Inputs:
+    //   AliITSgoem   *geom  The AliITSgeom object to be used.
+    // Output:
+    //   none.
+    // Return:
+    //   none.
+  if(!fLoader){
+    Error("SetITSgeom","No pointer to loader - nothing done");
+    return;
+  }
+  else {
+    fLoader->SetITSgeom(geom);  // protections in AliITSLoader::SetITSgeom
+  }
+}
+//______________________________________________________________________
+void AliITSDetTypeSim::SetLoader(AliITSLoader *loader){
+    // Sets the local copy of the AliITSLoader, and passes on the
+    // AliITSgeom object as needed.
+    // Inputs
+    //   AliITSLoader  *loader pointer to AliITSLoader for local use
+    // Outputs:
+    //   none.
+    // Return:
+    //  none.
+
+    if(fLoader==loader) return; // Same do nothing
+    if(fLoader){ // alread have an existing loader
+       Error("SetLoader",
+               "Already have an exisiting loader ptr=%p Nothing done",
+               fLoader);
+    } // end if
+    fLoader = loader;
+}
 //______________________________________________________________________
 void AliITSDetTypeSim::SetSimulationModel(Int_t dettype,AliITSsimulation *sim){
 
@@ -188,228 +260,418 @@ AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype){
 AliITSsimulation* AliITSDetTypeSim::GetSimulationModelByModule(Int_t module){
 
   //Get simulation model by module number
-  if(fGeom==0) {
-    Warning("GetSimulationModelByModule","fGeom is 0!");
+  if(GetITSgeom()==0) {
+    Warning("GetSimulationModelByModule","GetITSgeom() is 0!");
     return 0;
   }
   
-  return GetSimulationModel(fGeom->GetModuleType(module));
+  return GetSimulationModel(GetITSgeom()->GetModuleType(module));
+}
+//_______________________________________________________________________
+void AliITSDetTypeSim::SetDefaultSegmentation(Int_t idet){
+    // Set default segmentation model objects
+    AliITSsegmentation *seg;
+
+    if(fSegmentation==0x0){
+       fSegmentation = new TObjArray(fgkNdettypes);
+       fSegmentation->SetOwner(kTRUE);
+    }
+    if(GetSegmentationModel(idet))
+       delete (AliITSsegmentation*)fSegmentation->At(idet);
+    if(idet==0){
+       seg = new AliITSsegmentationSPD();
+    }else if(idet==1){
+       seg = new AliITSsegmentationSDD();
+    }else {
+       seg = new AliITSsegmentationSSD();
+    }
+    SetSegmentationModel(idet,seg);
 }
 //______________________________________________________________________
-void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,AliITSsegmentation *seg){
+void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,
+                                           AliITSsegmentation *seg){
    
   //Set segmentation model for detector type
-  if(fSegmentation==0x0) fSegmentation = new TObjArray(fgkNdettypes);
+  if(fSegmentation==0x0){
+    fSegmentation = new TObjArray(fgkNdettypes);
+    fSegmentation->SetOwner(kTRUE);
+  }
   fSegmentation->AddAt(seg,dettype);
-
 }
 //______________________________________________________________________
 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype){
-
   //Get segmentation model for detector type
    
    if(fSegmentation==0) {
-     Warning("GetSegmentationModel","fSegmentation is 0!");
-     return 0; 
+       Warning("GetSegmentationModel","fSegmentation is 0!");
+       return 0; 
    } 
    return (AliITSsegmentation*)(fSegmentation->At(dettype));
-
 }
 //_______________________________________________________________________
 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module){
-  
-  //Get segmentation model by module number
-   if(fGeom==0){
-     Warning("GetSegmentationModelByModule","fGeom is 0!");
-     return 0;
-   }     
-   return GetSegmentationModel(fGeom->GetModuleType(module));
+    //Get segmentation model by module number
+    if(GetITSgeom()==0){
+       Warning("GetSegmentationModelByModule","GetITSgeom() is 0!");
+       return 0;
+    }     
+    return GetSegmentationModel(GetITSgeom()->GetModuleType(module));
+}
+//_______________________________________________________________________
+void AliITSDetTypeSim::CreateCalibrationArray() {
+    //Create the container of calibration functions with correct size
+    if (fCalibration) {
+       Warning("CreateCalibration","pointer to calibration object exists\n");
+       fCalibration->Delete();
+       delete fCalibration;
+    }
 
+    Int_t nModTot = GetITSgeom()->GetIndexMax();
+    fCalibration = new TObjArray(nModTot);
+    fCalibration->SetOwner(kTRUE);
+    fCalibration->Clear();
 }
 //_______________________________________________________________________
-void AliITSDetTypeSim::SetResponseModel(Int_t dettype,AliITSresponse *resp){
+void AliITSDetTypeSim::SetCalibrationModel(Int_t iMod, AliITSCalibration *resp){
+    //Set response model for modules
 
-  
-  //Set segmentation model for module number
-  if(fResponse==0) fResponse = new TObjArray(fgkNdettypes);
-  fResponse->AddAt(resp,dettype);
+    if (fCalibration==0) CreateCalibrationArray();
+    if (fCalibration->At(iMod)!=0)
+       delete (AliITSCalibration*) fCalibration->At(iMod);
+    fCalibration->AddAt(resp, iMod);
 }
 //______________________________________________________________________
-void AliITSDetTypeSim::ResetResponse(){
-
-  //resets response array
-  if(fResponse){
-    for(Int_t i=0;i<fResponse->GetEntries();i++){
-      if(fResponse->At(i)) delete (AliITSresponse*)fResponse->At(i);
+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();
     }
-  }
 }
 //______________________________________________________________________
 void AliITSDetTypeSim::ResetSegmentation(){
- //Resets segmentation array
-  if(fSegmentation){
-    for(Int_t i=0;i<fgkNdettypes;i++){
-      if(fSegmentation->At(i)) delete (AliITSsegmentation*)fSegmentation->At(i);
-    }
-  }
+    //Resets segmentation array
+    if(fSegmentation) fSegmentation->Clear();
 }
-
 //_______________________________________________________________________
-AliITSresponse* AliITSDetTypeSim::GetResponseModel(Int_t dettype){
-  
-  //Get segmentation model for module number
-  
-  if(fResponse==0) {
-    Warning("GetResponseModel","fResponse is 0!");
-    return 0; 
-  }  
-  return (AliITSresponse*)(fResponse->At(dettype));
+AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod){
+    //Get response model for module number iMod 
+    if(fCalibration==0) {
+       AliError("fCalibration is 0!");
+       return 0; 
+    }
+  return (AliITSCalibration*)(fCalibration->At(iMod));
 }
 //_______________________________________________________________________
 void AliITSDetTypeSim::SetDefaults(){
+    //Set defaults for segmentation and response
+
+    if(GetITSgeom()==0){
+       Warning("SetDefaults","GetITSgeom() is 0!");
+       return;
+    } // end if
+    if (fCalibration==0) {
+       CreateCalibrationArray();
+    } // end if
+
+    ResetSegmentation();
+    if(!GetCalibration()){AliFatal("Exit"); exit(0);}
+
+    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,"ZS")) {
+             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
+}
+//______________________________________________________________________
+Bool_t AliITSDetTypeSim::GetCalibration() {
+  // Get Default calibration if a storage is not defined.
 
-  //Set defaults for segmentation and response
-  
-
-  if(fGeom==0){
-    Warning("SetDefaults","fGeom is 0!");
-    return;
+  if(!fFirstcall){
+    AliITSCalibration* cal = GetCalibrationModel(0);
+    if(cal)return kTRUE;
+  }
+  else {
+    fFirstcall = kFALSE;
   }
 
-  if(!fResponse) fResponse = new TObjArray(fgkNdettypes);  
+  SetRunNumber((Int_t)AliCDBManager::Instance()->GetRun());
+  Int_t run=GetRunNumber();
 
-  AliITSsegmentation* seg;
-  AliITSresponse* res;
-  ResetResponse();
-  ResetSegmentation();
+  Bool_t origCacheStatus = AliCDBManager::Instance()->GetCacheFlag();
+  Bool_t isCacheActive = kTRUE;
+  if(GetRunNumber()<0){
+    isCacheActive=kFALSE;
+    fCalibration->SetOwner(kTRUE);
+  }
+  else{
+    fCalibration->SetOwner(kFALSE);
+  }
 
-  for(Int_t idet=0;idet<fgkNdettypes;idet++){
-    //SPD
-    if(idet==0){
-      if(!GetSegmentationModel(idet)){
-       seg = new AliITSsegmentationSPD(fGeom);
-       SetSegmentationModel(idet,seg);
-      }
-      if(!GetResponseModel(idet)){
-       res = new AliITSresponseSPD();
-       SetResponseModel(idet,res);
-      }
-      const char *kData0=(GetResponseModel(idet))->DataType();
-      if (strstr(kData0,"real")) {
-       SetDigitClassName(idet,"AliITSdigit");
-      } else SetDigitClassName(idet,"AliITSdigitSPD");
-    }
-    //SDD
-    if(idet==1){
-      if(!GetResponseModel(idet)){
-       SetResponseModel(idet,new AliITSresponseSDD("simulated"));
-      }
-      if(!GetSegmentationModel(idet)){
-       res = GetResponseModel(idet);
-       seg = new AliITSsegmentationSDD(fGeom,res);
-       SetSegmentationModel(idet,seg);
-      }
-      const char *kopt = GetResponseModel(idet)->ZeroSuppOption();
-      if((!strstr(kopt,"2D"))&&(!strstr(kopt,"1D"))) SetDigitClassName(idet,"AliITSdigit");
-      else SetDigitClassName(idet,"AliITSdigitSDD");
-    }
-    //SSD
-    if(idet==2){
-      if(!GetSegmentationModel(idet)){
-       seg = new AliITSsegmentationSSD(fGeom);
-       SetSegmentationModel(idet,seg);
-      }
-      if(!GetResponseModel(idet)){
-       SetResponseModel(idet,new AliITSresponseSSD("simulated"));
-      }
-      const char *kData2 = (GetResponseModel(idet))->DataType();
-      if (strstr(kData2,"real")) {
-       SetDigitClassName(idet,"AliITSdigit");
-      } else SetDigitClassName(idet,"AliITSdigitSSD");
-      
-    }
+  AliCDBManager::Instance()->SetCacheFlag(isCacheActive);
+
+  AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDDead", run);
+  AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", run);
+  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");
+
+  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 || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
+     !entry2SPD || !entry2SDD || !entry2SSD || !drSpSDD || !ddlMapSDD || !mapASDD ||!mapTSDD){
+    AliFatal("Calibration object retrieval failed! ");
+    return kFALSE;
+  }    
+
+//   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
+//      !entry2SPD || !entry2SDD || !entry2SSD){
+//     AliFatal("Calibration object retrieval failed! ");
+//     return kFALSE;
+//   }         
+
+  TObjArray *calSPD = (TObjArray *)entrySPD->GetObject();
+  if(!isCacheActive)entrySPD->SetObject(NULL);
+  entrySPD->SetOwner(kTRUE);
+
+  AliITSresponseSPD *pSPD = (AliITSresponseSPD*)entry2SPD->GetObject();
+  if(!isCacheActive)entry2SPD->SetObject(NULL);
+  entry2SPD->SetOwner(kTRUE);
+   
+  TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
+  if(!isCacheActive)entrySDD->SetObject(NULL);
+  entrySDD->SetOwner(kTRUE);
+
+  AliITSresponseSDD *pSDD = (AliITSresponseSDD*)entry2SDD->GetObject();
+  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);
+  */
+
+  TObjArray *noiseSSD = (TObjArray *)entryNoiseSSD->GetObject();
+  if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
+  entryNoiseSSD->SetOwner(kTRUE);
+
+  TObjArray *gainSSD = (TObjArray *)entryGainSSD->GetObject();
+  if(!isCacheActive)entryGainSSD->SetObject(NULL);
+  entryGainSSD->SetOwner(kTRUE);
+
+  TObjArray *badchannelsSSD = (TObjArray *)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 entrySDD;
+    delete entryNoiseSSD;
+    delete entryGainSSD;
+    delete entryBadChannelsSSD;
+    delete entry2SPD;
+    delete entry2SDD;
+    delete entry2SSD;
+    delete mapASDD;   
+    delete mapTSDD;
+    delete drSpSDD;
+    delete ddlMapSDD;
+  }
+  
+  AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
 
+  if ((!pSPD)||(!pSDD)||(!pSSD) || (!calSPD) || (!calSDD) || (!drSp) || (!ddlsdd)
+      || (!mapAn) || (!mapT) || (!noiseSSD)|| (!gainSSD)|| (!badchannelsSSD)) {
+    AliWarning("Can not get calibration from calibration database !");
+    return kFALSE;
   }
 
-}
+  fNMod[0] = calSPD->GetEntries();
+  fNMod[1] = calSDD->GetEntries();
+  fNMod[2] = noiseSSD->GetEntries();
+  AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
+              fNMod[0], fNMod[1], fNMod[2]));
+  AliITSCalibration* cal;
+  for (Int_t i=0; i<fNMod[0]; i++) {
+    cal = (AliITSCalibration*) calSPD->At(i);
+    cal->SetResponse(pSPD);
+    SetCalibrationModel(i, cal);
+  }
 
+  fDDLMapSDD->SetDDLMap(ddlsdd);
+  for(Int_t iddl=0; iddl<AliITSDDLModuleMapSDD::GetNDDLs(); iddl++){
+    for(Int_t icar=0; icar<AliITSDDLModuleMapSDD::GetNModPerDDL();icar++){
+      Int_t iMod=fDDLMapSDD->GetModuleNumber(iddl,icar);
+      if(iMod==-1) continue;
+      Int_t i=iMod - fgkDefaultNModulesSPD;
+      cal = (AliITSCalibration*) calSDD->At(i);
+      cal->SetResponse((AliITSresponse*)pSDD);
+      Int_t i0=2*i;
+      Int_t i1=1+2*i;
+      AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
+      AliITSMapSDD* ma0 = (AliITSMapSDD*)mapAn->At(i0);
+      AliITSMapSDD* mt0 = (AliITSMapSDD*)mapT->At(i0);
+      AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
+      AliITSMapSDD* ma1 = (AliITSMapSDD*)mapAn->At(i1);
+      AliITSMapSDD* mt1 = (AliITSMapSDD*)mapT->At(i1);
+      cal->SetDriftSpeed(0,arr0);
+      cal->SetDriftSpeed(1,arr1);
+      cal->SetMapA(0,ma0);
+      cal->SetMapA(1,ma1);
+      cal->SetMapT(0,mt0);
+      cal->SetMapT(1,mt1);
+      SetCalibrationModel(iMod, cal);
+    }
+  }
+  for (Int_t i=0; i<fNMod[2]; i++) {
+    AliITSCalibrationSSD *calibSSD = new AliITSCalibrationSSD();
+    calibSSD->SetResponse((AliITSresponse*)pSSD);
+    
+    AliITSNoiseSSD *noise = (AliITSNoiseSSD*) (noiseSSD->At(i));
+    calibSSD->SetNoise(noise);
+    AliITSGainSSD *gain = (AliITSGainSSD*) (gainSSD->At(i));
+    calibSSD->SetGain(gain);
+    AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (badchannelsSSD->At(i));
+    calibSSD->SetBadChannels(bad);
+
+    Int_t iMod = i + fgkDefaultNModulesSPD + fgkDefaultNModulesSDD;
+    SetCalibrationModel(iMod, calibSSD);
+
+ }
+  return kTRUE;
+}
 //_______________________________________________________________________
 void AliITSDetTypeSim::SetDefaultSimulation(){
-
   //Set default simulation for detector type
 
-  if(fGeom==0){
-    Warning("SetDefaults","fGeom is 0!");
+  if(GetITSgeom()==0){
+    Warning("SetDefaultSimulation","GetITSgeom() is 0!");
     return;
   }
-  
-  if(!fResponse) fResponse = new TObjArray(fgkNdettypes);
-
-  AliITSsegmentation* seg;
-  AliITSresponse* res;
+  if(fCalibration==0){
+    Warning("SetDefaultSimulation","fCalibration is 0!");
+    return;
+  }
+  if(fSegmentation==0){
+    Warning("SetDefaultSimulation","fSegmentation is 0!");
+    for(Int_t i=0;i<fgkNdettypes;i++) SetDefaultSegmentation(i);
+  }else for(Int_t i=0;i<fgkNdettypes;i++) if(!GetSegmentationModel(i)){
+      Warning("SetDefaultSimulation",
+             "Segmentation not defined for det %d - Default taken\n!",i);
+      SetDefaultSegmentation(i);
+  }
   AliITSsimulation* sim;
 
   for(Int_t idet=0;idet<fgkNdettypes;idet++){
    //SPD
     if(idet==0){
-      sim = GetSimulationModel(idet);
+      sim = GetSimulationModel(idet); 
       if(!sim){
-       seg = (AliITSsegmentationSPD*)GetSegmentationModel(idet);
-       res = (AliITSresponseSPD*)GetResponseModel(idet);      
-       sim = new AliITSsimulationSPD(seg,res);
+       sim = new AliITSsimulationSPD(this);
        SetSimulationModel(idet,sim);
-      } else{
-       sim->SetResponseModel(GetResponseModel(idet));
-       sim->SetSegmentationModel((AliITSsegmentationSPD*)GetSegmentationModel(idet));
-       sim->Init();
       }
     }
     //SDD
     if(idet==1){
       sim = GetSimulationModel(idet);
       if(!sim){
-       seg = (AliITSsegmentationSDD*)GetSegmentationModel(idet);
-       res = (AliITSresponseSDD*)GetResponseModel(idet);
-       sim = new AliITSsimulationSDD(seg,res);
+       sim = new AliITSsimulationSDD(this);
        SetSimulationModel(idet,sim);
-      } else {
-       sim->SetResponseModel((AliITSresponseSDD*)GetResponseModel(idet));
-       sim->SetSegmentationModel((AliITSsegmentationSDD*)GetSegmentationModel(idet));
-       sim->Init();
-      }
-      
+      }      
     }
     //SSD
     if(idet==2){
       sim = GetSimulationModel(idet);
       if(!sim){
-       seg = (AliITSsegmentationSSD*)GetSegmentationModel(idet);
-       res = (AliITSresponseSSD*)GetResponseModel(idet);
-       sim = new AliITSsimulationSSD(seg,res);
+       sim = new AliITSsimulationSSD(this);
        SetSimulationModel(idet,sim);
-      } else{
-       sim->SetResponseModel((AliITSresponseSSD*)GetResponseModel(idet));
-       sim->SetSegmentationModel((AliITSsegmentationSSD*)GetSegmentationModel(idet));
-       sim->Init();
       }
-
     }
 
   }
 }
-
 //___________________________________________________________________
 void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, Char_t* name){
-  // Set branch address for the ITS summable digits Trees.
-  
+  // Set branch address for the ITS summable digits Trees.  
   char branchname[30];
 
   if(!treeS){
     return;
   }
-  if (fSDigits == 0x0){
+  if (fSDigits ==  0x0){
     fSDigits = new TClonesArray("AliITSpListItem",1000);
   }
   TBranch *branch;
@@ -459,8 +721,7 @@ void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
 }
 //___________________________________________________________________
 void AliITSDetTypeSim::ResetDigits(){
-  // Reset number of digits and the digits array for the ITS detector.
-  
+  // Reset number of digits and the digits array for the ITS detector.  
 
   if(!fDigits){
     Error("ResetDigits","fDigits is null!");
@@ -486,8 +747,8 @@ void AliITSDetTypeSim::ResetDigits(Int_t branch){
 //_______________________________________________________________________
 void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
   // Standard Summable digits to Digits function.
-  if(!fGeom){
-    Warning("SDigitsToDigits","fGeom is null!!");
+  if(!GetITSgeom()){
+    Warning("SDigitsToDigits","GetITSgeom() is null!!");
     return;
   }
   
@@ -510,14 +771,14 @@ void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
   TBranch* brchSDigits = trees->GetBranch(name);
   
   Int_t id;
-  for(Int_t module=0;module<fGeom->GetIndexMax();module++){
-     id = fGeom->GetModuleType(module);
+  for(Int_t module=0;module<GetITSgeom()->GetIndexMax();module++){
+     id = GetITSgeom()->GetModuleType(module);
     if (!all && !det[id]) continue;
     sim = (AliITSsimulation*)GetSimulationModel(id);
     if(!sim){
       Error("SDigit2Digits","The simulation class was not "
            "instanciated for module %d type %s!",module,
-           fGeom->GetModuleTypeName(module));
+           GetITSgeom()->GetModuleTypeName(module));
       exit(1);
     }
     sim->InitSimulationModule(module,gAlice->GetEvNumber());
@@ -533,26 +794,24 @@ void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
   fLoader->TreeD()->AutoSave();
   fLoader->TreeD()->Reset();
 }
-
-
-
 //_________________________________________________________
-void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){
-  
+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);
 }
 //__________________________________________________________
 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, AliITSdigit* d){  
   //    Add a simulated digit.
+
   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
   switch(branch){
   case 0:
@@ -564,24 +823,22 @@ void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){
   case 2:
     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
     break;
-  } 
-  
-
+  }  
 }
-
 //______________________________________________________________________
 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
                                   Int_t *tracks,Int_t *hits,Float_t *charges){
   //   Add a simulated digit to the list.
 
   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
-  AliITSresponseSDD *resp = 0;
+  AliITSCalibrationSDD *resp = 0;
   switch(branch){
   case 0:
     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
     break;
   case 1:
-    resp = (AliITSresponseSDD*)GetResponseModel(branch);
+    resp = (AliITSCalibrationSDD*)GetCalibrationModel(
+       GetITSgeom()->GetStartSDD());
     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
                                                   hits,charges,resp);
     break;
@@ -590,3 +847,52 @@ void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
     break;
   } 
 }
+//______________________________________________________________________
+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 )
+
+  if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
+    AliWarning("No storage set! Will use dummy one");
+    AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
+  }
+
+  if (!fCalibration) {
+    AliError("AliITSCalibration classes are not defined - nothing done");
+    return;
+  }
+  AliCDBId idRespSPD("ITS/Calib/SPDDead",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];
+  }
+
+  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);
+}
+
+