]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeSim.cxx
First set of modifications for simulating and reconstructing the SDDs with the analog...
[u/mrichter/AliRoot.git] / ITS / AliITSDetTypeSim.cxx
1 /***************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17  $Id$
18 */
19
20
21 /////////////////////////////////////////////////////////////////////
22 // Base simulation functions for ITS                               //
23 //                                                                 //
24 //                                                                 //
25 /////////////////////////////////////////////////////////////////////          
26 #include "TBranch.h"
27 #include "TClonesArray.h"
28 #include "TObjArray.h"
29 #include "TTree.h"
30
31 #include "AliRun.h"
32
33 #include "AliCDBManager.h"
34 #include "AliCDBId.h"
35 #include "AliCDBStorage.h"
36 #include "AliCDBEntry.h"
37 #include "AliCDBMetaData.h"
38
39 #include "AliITSdigit.h"
40 #include "AliITSdigitSPD.h"
41 #include "AliITSdigitSDD.h"
42 #include "AliITSdigitSSD.h"
43 #include "AliITSDetTypeSim.h"
44 #include "AliITSpListItem.h"
45 #include "AliITSCalibrationSDD.h"
46 #include "AliITSMapSDD.h"
47 #include "AliITSDriftSpeedArraySDD.h"
48 #include "AliITSDriftSpeedSDD.h"
49 #include "AliITSCalibrationSSD.h"
50 #include "AliITSNoiseSSDv2.h"
51 #include "AliITSGainSSDv2.h"
52 #include "AliITSBadChannelsSSDv2.h"
53 #include "AliITSNoiseSSD.h"
54 #include "AliITSGainSSD.h"
55 #include "AliITSBadChannelsSSD.h"
56 #include "AliITSCalibrationSSD.h"
57 #include "AliITSsegmentationSPD.h"
58 #include "AliITSsegmentationSDD.h"
59 #include "AliITSsegmentationSSD.h"
60 #include "AliITSsimulation.h"
61 #include "AliITSsimulationSPD.h"
62 #include "AliITSsimulationSDD.h"
63 #include "AliITSsimulationSSD.h"
64
65
66 const Int_t AliITSDetTypeSim::fgkNdettypes = 3;
67 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSPD =  240;
68 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSDD =  260;
69 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSSD = 1698;
70
71 ClassImp(AliITSDetTypeSim)
72
73 //----------------------------------------------------------------------
74 AliITSDetTypeSim::AliITSDetTypeSim():
75 TObject(),
76 fSimulation(),   // [NDet]
77 fSegmentation(), // [NDet]
78 fCalibration(),     // [NMod]
79 fSSDCalibration(0),
80 fPreProcess(),   // [] e.g. Fill fHitModule with hits
81 fPostProcess(),  // [] e.g. Wright Raw data
82 fNSDigits(0),    //! number of SDigits
83 fSDigits("AliITSpListItem",1000),   
84 fNDigits(0),     //! number of Digits
85 fRunNumber(0),   //! Run number (to access DB)
86 fDigits(),       //! [NMod][NDigits]
87 fSimuPar(0),
88 fDDLMapSDD(0),
89 fHitClassName(), // String with Hit class name.
90 fSDigClassName(),// String with SDigit class name.
91 fDigClassName(), // String with digit class name.
92 fLoader(0),      // local pointer to loader
93 fFirstcall(kTRUE){ // flag
94     // Default Constructor
95     // Inputs:
96     //    none.
97     // Outputs:
98     //    none.
99     // Return:
100     //    A properly zero-ed AliITSDetTypeSim class.
101
102   fSimulation = new TObjArray(fgkNdettypes);
103   fSegmentation = new TObjArray(fgkNdettypes);
104   fSegmentation->SetOwner(kTRUE);
105   fDigits = new TObjArray(fgkNdettypes);
106   fNDigits = new Int_t[fgkNdettypes];
107   fDDLMapSDD=new AliITSDDLModuleMapSDD();
108   fSimuPar= new AliITSSimuParam();
109   fSSDCalibration=new AliITSCalibrationSSD();
110   fNMod[0] = fgkDefaultNModulesSPD;
111   fNMod[1] = fgkDefaultNModulesSDD;
112   fNMod[2] = fgkDefaultNModulesSSD;
113   SetRunNumber();
114 }
115 //----------------------------------------------------------------------
116 AliITSDetTypeSim::~AliITSDetTypeSim(){
117     // Destructor
118     // Inputs:
119     //    none.
120     // Outputs:
121     //    none.
122     // Return:
123     //    Nothing.
124
125     if(fSimulation){
126         fSimulation->Delete();
127         delete fSimulation;
128     }
129     fSimulation = 0;
130     if(fSegmentation){
131         fSegmentation->Delete();
132         delete fSegmentation;
133     }
134     fSegmentation = 0;
135     if(fCalibration && fRunNumber<0){
136         AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
137                                GetITSgeom()->GetStartSPD()))->GetResponse();
138         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
139                                GetITSgeom()->GetStartSSD()))->GetResponse();
140         if(rspd) delete rspd;
141         if(rssd) delete rssd;
142         fCalibration->Delete();
143         delete fCalibration;
144     }
145     fCalibration = 0;
146     if(fSSDCalibration) delete fSSDCalibration;
147      if(fPreProcess){
148         fPreProcess->Delete();
149         delete fPreProcess;
150     }
151     fPreProcess = 0;
152     if(fPostProcess){
153         fPostProcess->Delete();
154         delete fPostProcess;
155     }
156     fPostProcess = 0;
157     if(fSimuPar) delete fSimuPar;
158     if(fDDLMapSDD) delete fDDLMapSDD;
159     if(fNDigits) delete [] fNDigits;
160     fNDigits = 0;
161     if (fLoader)fLoader->GetModulesFolder()->Remove(this);
162     fLoader = 0; // Not deleting it.
163     fSDigits.Delete();
164     if (fDigits) {
165       fDigits->Delete();
166       delete fDigits;
167     }
168     fDigits=0;
169 }
170 //----------------------------------------------------------------------
171 AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source),
172 fSimulation(source.fSimulation),   // [NDet]
173 fSegmentation(source.fSegmentation), // [NDet]
174 fCalibration(source.fCalibration),     // [NMod]
175 fSSDCalibration(source.fSSDCalibration),
176 fPreProcess(source.fPreProcess),   // [] e.g. Fill fHitModule with hits
177 fPostProcess(source.fPostProcess),  // [] e.g. Wright Raw data
178 fNSDigits(source.fNSDigits),    //! number of SDigits
179 fSDigits(*((TClonesArray*)source.fSDigits.Clone())),
180 fNDigits(source.fNDigits),     //! number of Digits
181 fRunNumber(source.fRunNumber),   //! Run number (to access DB)
182 fDigits(source.fDigits),       //! [NMod][NDigits]
183 fSimuPar(source.fSimuPar),
184 fDDLMapSDD(source.fDDLMapSDD),
185 fHitClassName(source.fHitClassName), // String with Hit class name.
186 fSDigClassName(source.fSDigClassName),// String with SDigit class name.
187 fDigClassName(), // String with digit class name.
188 fLoader(source.fLoader),      // local pointer to loader
189 fFirstcall(source.fFirstcall)
190 {
191     // Copy Constructor for object AliITSDetTypeSim not allowed
192   for(Int_t i=0;i<fgkNdettypes;i++){
193     fDigClassName[i] = source.fDigClassName[i];
194   }
195 }
196 //----------------------------------------------------------------------
197 AliITSDetTypeSim& AliITSDetTypeSim::operator=(const AliITSDetTypeSim &source){
198     // The = operator for object AliITSDetTypeSim
199  
200   this->~AliITSDetTypeSim();
201   new(this) AliITSDetTypeSim(source);
202   return *this;
203 }
204
205 //______________________________________________________________________
206 void AliITSDetTypeSim::SetITSgeom(AliITSgeom *geom){
207     // Sets/replaces the existing AliITSgeom object kept in AliITSLoader
208     // 
209     // Inputs:
210     //   AliITSgoem   *geom  The AliITSgeom object to be used.
211     // Output:
212     //   none.
213     // Return:
214     //   none.
215   if(!fLoader){
216     Error("SetITSgeom","No pointer to loader - nothing done");
217     return;
218   }
219   else {
220     fLoader->SetITSgeom(geom);  // protections in AliITSLoader::SetITSgeom
221   }
222  
223 }
224 //______________________________________________________________________
225 void AliITSDetTypeSim::SetLoader(AliITSLoader *loader){
226     // Sets the local copy of the AliITSLoader, and passes on the
227     // AliITSgeom object as needed.
228     // Inputs
229     //   AliITSLoader  *loader pointer to AliITSLoader for local use
230     // Outputs:
231     //   none.
232     // Return:
233     //  none.
234
235     if(fLoader==loader) return; // Same do nothing
236     if(fLoader){ // alread have an existing loader
237         Error("SetLoader",
238                 "Already have an exisiting loader ptr=%p Nothing done",
239                 fLoader);
240     } // end if
241     fLoader = loader;
242 }
243 //______________________________________________________________________
244 void AliITSDetTypeSim::SetSimulationModel(Int_t dettype,AliITSsimulation *sim){
245
246   //Set simulation model for detector type
247
248   if(fSimulation==0) fSimulation = new TObjArray(fgkNdettypes);
249   fSimulation->AddAt(sim,dettype);
250 }
251 //______________________________________________________________________
252 AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype){
253
254   //Get simulation model for detector type
255   if(fSimulation==0)  {
256     Warning("GetSimulationModel","fSimulation is 0!");
257     return 0;     
258   }
259   return (AliITSsimulation*)(fSimulation->At(dettype));
260 }
261 //______________________________________________________________________
262 AliITSsimulation* AliITSDetTypeSim::GetSimulationModelByModule(Int_t module){
263
264   //Get simulation model by module number
265   if(GetITSgeom()==0) {
266     Warning("GetSimulationModelByModule","GetITSgeom() is 0!");
267     return 0;
268   }
269   
270   return GetSimulationModel(GetITSgeom()->GetModuleType(module));
271 }
272 //_______________________________________________________________________
273 void AliITSDetTypeSim::SetDefaultSegmentation(Int_t idet){
274     // Set default segmentation model objects
275     AliITSsegmentation *seg;
276
277     if(fSegmentation==0x0){
278         fSegmentation = new TObjArray(fgkNdettypes);
279         fSegmentation->SetOwner(kTRUE);
280     }
281     if(GetSegmentationModel(idet))
282         delete (AliITSsegmentation*)fSegmentation->At(idet);
283     if(idet==0){
284         seg = new AliITSsegmentationSPD();
285     }else if(idet==1){
286       seg = new AliITSsegmentationSDD();
287       AliITSCalibrationSDD* cal=(AliITSCalibrationSDD*)GetCalibrationModel(fgkDefaultNModulesSPD+1);
288       if(cal->IsAMAt20MHz()){ 
289         seg->SetPadSize(seg->Dpz(0),20.);
290         seg->SetNPads(seg->Npz()/2,128);
291       }
292     }else {
293         seg = new AliITSsegmentationSSD();
294     }
295     SetSegmentationModel(idet,seg);
296 }
297 //______________________________________________________________________
298 void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,
299                                             AliITSsegmentation *seg){
300    
301   //Set segmentation model for detector type
302   if(fSegmentation==0x0){
303     fSegmentation = new TObjArray(fgkNdettypes);
304     fSegmentation->SetOwner(kTRUE);
305   }
306   fSegmentation->AddAt(seg,dettype);
307 }
308 //______________________________________________________________________
309 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype){
310   //Get segmentation model for detector type
311    
312    if(fSegmentation==0) {
313        Warning("GetSegmentationModel","fSegmentation is 0!");
314        return 0; 
315    } 
316    return (AliITSsegmentation*)(fSegmentation->At(dettype));
317 }
318 //_______________________________________________________________________
319 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module){
320     //Get segmentation model by module number
321     if(GetITSgeom()==0){
322         Warning("GetSegmentationModelByModule","GetITSgeom() is 0!");
323         return 0;
324     }     
325     return GetSegmentationModel(GetITSgeom()->GetModuleType(module));
326 }
327 //_______________________________________________________________________
328 void AliITSDetTypeSim::CreateCalibrationArray() {
329     //Create the container of calibration functions with correct size
330     if (fCalibration) {
331         Warning("CreateCalibration","pointer to calibration object exists\n");
332         fCalibration->Delete();
333         delete fCalibration;
334     }
335
336     Int_t nModTot = GetITSgeom()->GetIndexMax();
337     fCalibration = new TObjArray(nModTot);
338     fCalibration->SetOwner(kTRUE);
339     fCalibration->Clear();
340 }
341 //_______________________________________________________________________
342 void AliITSDetTypeSim::SetCalibrationModel(Int_t iMod, AliITSCalibration *resp){
343     //Set response model for modules
344
345     if (fCalibration==0) CreateCalibrationArray();
346  
347     if (fCalibration->At(iMod)!=0)
348         delete (AliITSCalibration*) fCalibration->At(iMod);
349     fCalibration->AddAt(resp, iMod);
350 }
351 //______________________________________________________________________
352 void AliITSDetTypeSim::ResetCalibrationArray(){
353     //resets response array
354     if(fCalibration && fRunNumber<0){  // if fRunNumber<0 fCalibration is owner
355         AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
356                                 GetITSgeom()->GetStartSPD()))->GetResponse();
357         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
358                                 GetITSgeom()->GetStartSSD()))->GetResponse();
359         if(rspd) delete rspd;
360         if(rssd) delete rssd;
361         fCalibration->Clear();
362     }else if (fCalibration && fRunNumber>=0){
363         fCalibration->Clear();
364     }
365 }
366 //______________________________________________________________________
367 void AliITSDetTypeSim::ResetSegmentation(){
368     //Resets segmentation array
369     if(fSegmentation) fSegmentation->Clear();
370 }
371 //_______________________________________________________________________
372 AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod){
373     //Get response model for module number iMod 
374  
375     if(fCalibration==0) {
376         AliError("fCalibration is 0!");
377         return 0; 
378     }
379   if(iMod<fgkDefaultNModulesSPD+fgkDefaultNModulesSDD){
380     return (AliITSCalibration*)fCalibration->At(iMod);
381   }else{
382     Int_t i=iMod-(fgkDefaultNModulesSPD+fgkDefaultNModulesSDD);
383     fSSDCalibration->SetModule(i);
384     return (AliITSCalibration*)fSSDCalibration;
385   }
386
387 }
388 //_______________________________________________________________________
389 void AliITSDetTypeSim::SetDefaults(){
390     //Set defaults for segmentation and response
391
392     if(GetITSgeom()==0){
393         Warning("SetDefaults","GetITSgeom() is 0!");
394         return;
395     } // end if
396     if (fCalibration==0) {
397         CreateCalibrationArray();
398     } // end if
399
400     ResetSegmentation();
401     if(!GetCalibration()){AliFatal("Exit"); exit(0);}
402
403     SetDigitClassName(0,"AliITSdigitSPD");
404     SetDigitClassName(1,"AliITSdigitSDD");
405     SetDigitClassName(2,"AliITSdigitSSD");
406
407     for(Int_t idet=0;idet<fgkNdettypes;idet++){
408       if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
409     }
410 }
411 //______________________________________________________________________
412 Bool_t AliITSDetTypeSim::GetCalibration() {
413   // Get Default calibration if a storage is not defined.
414
415   if(!fFirstcall){
416     AliITSCalibration* cal = GetCalibrationModel(0);
417     if(cal)return kTRUE;
418   }
419   else {
420     fFirstcall = kFALSE;
421   }
422
423   SetRunNumber((Int_t)AliCDBManager::Instance()->GetRun());
424   Int_t run=GetRunNumber();
425
426   Bool_t origCacheStatus = AliCDBManager::Instance()->GetCacheFlag();
427   Bool_t isCacheActive = kTRUE;
428   if(GetRunNumber()<0){
429     isCacheActive=kFALSE;
430     fCalibration->SetOwner(kTRUE);
431   }
432   else{
433     fCalibration->SetOwner(kFALSE);
434   }
435
436   AliCDBManager::Instance()->SetCacheFlag(isCacheActive);
437
438   AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDDead", run);
439   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", run);
440   AliCDBEntry *drSpSDD = AliCDBManager::Instance()->Get("ITS/Calib/DriftSpeedSDD",run);
441   AliCDBEntry *ddlMapSDD = AliCDBManager::Instance()->Get("ITS/Calib/DDLMapSDD",run);
442   AliCDBEntry *mapASDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsAnodeSDD",run);
443   AliCDBEntry *mapTSDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsTimeSDD",run);
444   // AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", run);
445   AliCDBEntry *entryNoiseSSD = AliCDBManager::Instance()->Get("ITS/Calib/NoiseSSD");
446   AliCDBEntry *entryGainSSD = AliCDBManager::Instance()->Get("ITS/Calib/GainSSD");
447   AliCDBEntry *entryBadChannelsSSD = AliCDBManager::Instance()->Get("ITS/Calib/BadChannelsSSD");
448
449   AliCDBEntry *entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", run);
450   AliCDBEntry *entry2SSD = AliCDBManager::Instance()->Get("ITS/Calib/RespSSD", run);
451
452   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
453      !entry2SPD || !entry2SSD || !drSpSDD || !ddlMapSDD || !mapASDD ||!mapTSDD){
454     AliFatal("Calibration object retrieval failed! ");
455     return kFALSE;
456   }     
457
458 //   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
459 //      !entry2SPD || !entry2SSD){
460 //     AliFatal("Calibration object retrieval failed! ");
461 //     return kFALSE;
462 //   }          
463
464   TObjArray *calSPD = (TObjArray *)entrySPD->GetObject();
465   if(!isCacheActive)entrySPD->SetObject(NULL);
466   entrySPD->SetOwner(kTRUE);
467
468   AliITSresponseSPD *pSPD = (AliITSresponseSPD*)entry2SPD->GetObject();
469   if(!isCacheActive)entry2SPD->SetObject(NULL);
470   entry2SPD->SetOwner(kTRUE);
471    
472   TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
473   if(!isCacheActive)entrySDD->SetObject(NULL);
474   entrySDD->SetOwner(kTRUE);
475
476   TObjArray *drSp = (TObjArray *)drSpSDD->GetObject();
477   if(!isCacheActive)drSpSDD->SetObject(NULL);
478   drSpSDD->SetOwner(kTRUE);
479
480   AliITSDDLModuleMapSDD *ddlsdd=(AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
481   if(!isCacheActive)ddlMapSDD->SetObject(NULL);
482   ddlMapSDD->SetOwner(kTRUE);
483
484   TObjArray *mapAn = (TObjArray *)mapASDD->GetObject();
485   if(!isCacheActive)mapASDD->SetObject(NULL);
486   mapASDD->SetOwner(kTRUE);
487
488   TObjArray *mapT = (TObjArray *)mapTSDD->GetObject();
489   if(!isCacheActive)mapTSDD->SetObject(NULL);
490   mapTSDD->SetOwner(kTRUE);
491
492   /*
493   TObjArray *calSSD = (TObjArray *)entrySSD->GetObject();
494   if(!isCacheActive)entrySSD->SetObject(NULL);
495   entrySSD->SetOwner(kTRUE);
496   */
497
498   TObject *emptyssd = 0; TString ssdobjectname = 0;
499   AliITSNoiseSSDv2 *noiseSSD = new AliITSNoiseSSDv2();  
500   emptyssd = (TObject *)entryNoiseSSD->GetObject();
501   ssdobjectname = emptyssd->GetName();
502   if(ssdobjectname=="TObjArray") {
503     TObjArray *noiseSSDOld = (TObjArray *)entryNoiseSSD->GetObject();
504     ReadOldSSDNoise(noiseSSDOld, noiseSSD);
505   }
506   else if(ssdobjectname=="AliITSNoiseSSDv2")
507     noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
508   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
509   entryNoiseSSD->SetOwner(kTRUE);
510
511   AliITSGainSSDv2 *gainSSD = new AliITSGainSSDv2();
512   emptyssd = (TObject *)entryGainSSD->GetObject();
513   ssdobjectname = emptyssd->GetName();
514   if(ssdobjectname=="Gain") {
515     TObjArray *gainSSDOld = (TObjArray *)entryGainSSD->GetObject();
516     ReadOldSSDGain(gainSSDOld, gainSSD);
517   }
518   else if(ssdobjectname=="AliITSGainSSDv2")
519     gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
520   if(!isCacheActive)entryGainSSD->SetObject(NULL);
521   entryGainSSD->SetOwner(kTRUE);
522
523   AliITSBadChannelsSSDv2 *badChannelsSSD = new AliITSBadChannelsSSDv2();
524   emptyssd = (TObject *)entryBadChannelsSSD->GetObject();
525   ssdobjectname = emptyssd->GetName();
526   if(ssdobjectname=="TObjArray") {
527     TObjArray *badChannelsSSDOld = (TObjArray *)entryBadChannelsSSD->GetObject();
528     ReadOldSSDBadChannels(badChannelsSSDOld, badChannelsSSD);
529   }
530   else if(ssdobjectname=="AliITSBadChannelsSSDv2")
531     badChannelsSSD = (AliITSBadChannelsSSDv2*)entryBadChannelsSSD->GetObject();
532   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
533   entryBadChannelsSSD->SetOwner(kTRUE);
534
535   /*AliITSNoiseSSDv2 *noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
536   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
537   entryNoiseSSD->SetOwner(kTRUE);
538
539   AliITSGainSSDv2 *gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
540   if(!isCacheActive)entryGainSSD->SetObject(NULL);
541   entryGainSSD->SetOwner(kTRUE);
542
543   AliITSBadChannelsSSDv2 *badchannelsSSD = 
544     (AliITSBadChannelsSSDv2 *)entryBadChannelsSSD->GetObject();
545   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
546   entryBadChannelsSSD->SetOwner(kTRUE);*/
547
548   AliITSresponseSSD *pSSD = (AliITSresponseSSD*)entry2SSD->GetObject();
549   if(!isCacheActive)entry2SSD->SetObject(NULL);
550   entry2SSD->SetOwner(kTRUE);
551   
552   // DB entries are deleted. In this way metadeta objects are deleted as well
553   if(!isCacheActive){
554     delete entrySPD;
555     delete entrySDD;
556     delete entryNoiseSSD;
557     delete entryGainSSD;
558     delete entryBadChannelsSSD;
559     delete entry2SPD;
560     delete entry2SSD;
561     delete mapASDD;   
562     delete mapTSDD;
563     delete drSpSDD;
564     delete ddlMapSDD;
565   }
566   
567   AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
568
569   if ((!pSPD)||(!pSSD) || (!calSPD) || (!calSDD) || (!drSp) || (!ddlsdd)
570       || (!mapAn) || (!mapT) || (!noiseSSD)|| (!gainSSD)|| (!badChannelsSSD)) {
571     AliWarning("Can not get calibration from calibration database !");
572     return kFALSE;
573   }
574
575   fNMod[0] = calSPD->GetEntries();
576   fNMod[1] = calSDD->GetEntries();
577   //  fNMod[2] = noiseSSD->GetEntries();
578   AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
579                fNMod[0], fNMod[1], fNMod[2]));
580   AliITSCalibration* cal;
581   for (Int_t i=0; i<fNMod[0]; i++) {
582     cal = (AliITSCalibration*) calSPD->At(i);
583     cal->SetResponse(pSPD);
584     SetCalibrationModel(i, cal);
585   }
586
587   fDDLMapSDD->SetDDLMap(ddlsdd);
588
589   for (Int_t i=0; i<fgkDefaultNModulesSDD; i++) {
590     Int_t iddl,icarlos;
591     Int_t iMod = i + fgkDefaultNModulesSPD;
592     fDDLMapSDD->FindInDDLMap(iMod,iddl,icarlos);
593     if(iddl<0){ 
594       AliITSCalibrationSDD* calsdddead=new AliITSCalibrationSDD();
595       calsdddead->SetBad();      
596       AliITSDriftSpeedSDD* driftspdef = new AliITSDriftSpeedSDD();
597       AliITSDriftSpeedArraySDD* arrdrsp=new AliITSDriftSpeedArraySDD(1);
598       arrdrsp->AddDriftSpeed(driftspdef);
599       calsdddead->SetDriftSpeed(0,arrdrsp);
600       calsdddead->SetDriftSpeed(1,arrdrsp);
601       SetCalibrationModel(iMod, calsdddead);
602       AliWarning(Form("SDD module %d not present in DDL map: set it as dead",iMod));
603     }else{
604       cal = (AliITSCalibration*) calSDD->At(i);
605       Int_t i0=2*i;
606       Int_t i1=1+2*i;
607       AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
608       AliITSMapSDD* ma0 = (AliITSMapSDD*)mapAn->At(i0);
609       AliITSMapSDD* mt0 = (AliITSMapSDD*)mapT->At(i0);
610       AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
611       AliITSMapSDD* ma1 = (AliITSMapSDD*)mapAn->At(i1);
612       AliITSMapSDD* mt1 = (AliITSMapSDD*)mapT->At(i1);
613       cal->SetDriftSpeed(0,arr0);
614       cal->SetDriftSpeed(1,arr1);
615       cal->SetMapA(0,ma0);
616       cal->SetMapA(1,ma1);
617       cal->SetMapT(0,mt0);
618       cal->SetMapT(1,mt1);
619       SetCalibrationModel(iMod, cal);
620     }
621   }
622
623   fSSDCalibration->SetResponse((AliITSresponse*)pSSD);
624   fSSDCalibration->SetNoise(noiseSSD);
625   fSSDCalibration->SetGain(gainSSD);
626   fSSDCalibration->SetBadChannels(badChannelsSSD);
627   //fSSDCalibration->FillBadChipMap();
628
629
630   /*
631   for (Int_t i=0; i<fNMod[2]; i++) {
632     AliITSCalibrationSSD *calibSSD = new AliITSCalibrationSSD();
633     calibSSD->SetResponse((AliITSresponse*)pSSD);
634     
635     AliITSNoiseSSD *noise = (AliITSNoiseSSD*) (noiseSSD->At(i));
636     calibSSD->SetNoise(noise);
637     AliITSGainSSD *gain = (AliITSGainSSD*) (gainSSD->At(i));
638     calibSSD->SetGain(gain);
639     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (badchannelsSSD->At(i));
640     calibSSD->SetBadChannels(bad);
641
642     Int_t iMod = i + fgkDefaultNModulesSPD + fgkDefaultNModulesSDD;
643     SetCalibrationModel(iMod, calibSSD);
644
645     }
646   */
647
648
649   return kTRUE;
650 }
651 //_______________________________________________________________________
652 void AliITSDetTypeSim::SetDefaultSimulation(){
653   //Set default simulation for detector type
654
655   if(GetITSgeom()==0){
656     Warning("SetDefaultSimulation","GetITSgeom() is 0!");
657     return;
658   }
659   if(fCalibration==0){
660     Warning("SetDefaultSimulation","fCalibration is 0!");
661     return;
662   }
663   if(fSegmentation==0){
664     Warning("SetDefaultSimulation","fSegmentation is 0!");
665     for(Int_t i=0;i<fgkNdettypes;i++) SetDefaultSegmentation(i);
666   }else for(Int_t i=0;i<fgkNdettypes;i++) if(!GetSegmentationModel(i)){
667       Warning("SetDefaultSimulation",
668               "Segmentation not defined for det %d - Default taken\n!",i);
669       SetDefaultSegmentation(i);
670   }
671   AliITSsimulation* sim;
672
673   for(Int_t idet=0;idet<fgkNdettypes;idet++){
674    //SPD
675     if(idet==0){
676       sim = GetSimulationModel(idet); 
677       if(!sim){
678         sim = new AliITSsimulationSPD(this);
679         SetSimulationModel(idet,sim);
680       }
681     }
682     //SDD
683     if(idet==1){
684       sim = GetSimulationModel(idet);
685       if(!sim){
686         sim = new AliITSsimulationSDD(this);
687         SetSimulationModel(idet,sim);
688       }      
689     }
690     //SSD
691     if(idet==2){
692       sim = GetSimulationModel(idet);
693       if(!sim){
694         sim = new AliITSsimulationSSD(this);
695         SetSimulationModel(idet,sim);
696       }
697     }
698
699   }
700 }
701 //___________________________________________________________________
702 void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, Char_t* name){
703   // Set branch address for the ITS summable digits Trees.  
704   char branchname[30];
705
706   if(!treeS){
707     return;
708   }
709   TBranch *branch;
710   sprintf(branchname,"%s",name);
711   branch = treeS->GetBranch(branchname);
712   TClonesArray *sdigi = &fSDigits;
713   if (branch) branch->SetAddress(&sdigi);
714
715 }
716 //___________________________________________________________________
717 void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
718   // Set branch address for the digit Trees.
719   
720   const char *det[3] = {"SPD","SDD","SSD"};
721   TBranch *branch;
722   
723   char branchname[30];
724   
725   if(!treeD){
726     return;
727   }
728   if(!fDigits){
729     fDigits = new TObjArray(fgkNdettypes); 
730   }
731   for(Int_t i=0;i<fgkNdettypes;i++){
732     Char_t* digclass = GetDigitClassName(i);
733     if(digclass==0x0){
734       if(i==0) SetDigitClassName(i,"AliITSdigitSPD");
735       if(i==1) SetDigitClassName(i,"AliITSdigitSDD");
736       if(i==2) SetDigitClassName(i,"AliITSdigitSSD");
737       digclass = GetDigitClassName(i);
738     }
739     TString classn = digclass;
740     if(!(fDigits->At(i))){
741       fDigits->AddAt(new TClonesArray(classn.Data(),1000),i);
742     }else{
743       ResetDigits(i);
744     }
745     
746     if(fgkNdettypes==3) sprintf(branchname,"%sDigits%s",name,det[i]);
747     else sprintf(branchname,"%sDigits%d",name,i+1);
748     if(fDigits){
749       branch = treeD->GetBranch(branchname);
750       if(branch) branch->SetAddress(&((*fDigits)[i]));
751     }
752   }
753
754 }
755 //___________________________________________________________________
756 void AliITSDetTypeSim::ResetDigits(){
757   // Reset number of digits and the digits array for the ITS detector.  
758
759   if(!fDigits){
760     Error("ResetDigits","fDigits is null!");
761     return;
762   }
763   for(Int_t i=0;i<fgkNdettypes;i++){
764     ResetDigits(i);
765   }
766 }
767 //___________________________________________________________________
768 void AliITSDetTypeSim::ResetDigits(Int_t branch){
769   // Reset number of digits and the digits array for this branch.
770
771   if(fDigits->At(branch)){
772     ((TClonesArray*)fDigits->At(branch))->Clear();
773   }
774   if(fNDigits) fNDigits[branch]=0;
775
776 }
777
778
779
780 //_______________________________________________________________________
781 void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
782   // Standard Summable digits to Digits function.
783   if(!GetITSgeom()){
784     Warning("SDigitsToDigits","GetITSgeom() is null!!");
785     return;
786   }
787   
788   const char *all = strstr(opt,"All");
789   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
790                         strstr(opt,"SSD")};
791   if( !det[0] && !det[1] && !det[2] ) all = "All";
792   else all = 0;
793   static Bool_t setDef = kTRUE;
794   if(setDef) SetDefaultSimulation();
795   setDef = kFALSE;
796   
797   AliITSsimulation *sim =0;
798   TTree* trees = fLoader->TreeS();
799   if( !(trees && GetSDigits()) ){
800     Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
801     return;
802   } 
803   sprintf(name,"%s",name);
804   TBranch* brchSDigits = trees->GetBranch(name);
805   
806   Int_t id;
807   for(Int_t module=0;module<GetITSgeom()->GetIndexMax();module++){
808      id = GetITSgeom()->GetModuleType(module);
809     if (!all && !det[id]) continue;
810     sim = (AliITSsimulation*)GetSimulationModel(id);
811     if(!sim){
812       Error("SDigit2Digits","The simulation class was not "
813             "instanciated for module %d type %s!",module,
814             GetITSgeom()->GetModuleTypeName(module));
815       exit(1);
816     }
817     sim->InitSimulationModule(module,gAlice->GetEvNumber());
818     
819     fSDigits.Clear();
820     brchSDigits->GetEvent(module);
821     sim->AddSDigitsToModule(&fSDigits,0);
822     sim->FinishSDigitiseModule();
823     fLoader->TreeD()->Fill();
824     ResetDigits();
825   }
826   fLoader->TreeD()->GetEntries();
827   fLoader->TreeD()->AutoSave();
828   fLoader->TreeD()->Reset();
829 }
830 //_________________________________________________________
831 void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){  
832   //Adds the module full of summable digits to the summable digits tree.
833
834   new(fSDigits[fNSDigits++]) AliITSpListItem(sdig);
835 }
836 //__________________________________________________________
837 void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){  
838   //    Add a simulated digit.
839
840   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
841   switch(branch){
842   case 0:
843     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
844     break;
845   case 1:
846     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
847     break;
848   case 2:
849     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
850     break;
851   }  
852 }
853 //______________________________________________________________________
854 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
855                                    Int_t *tracks,Int_t *hits,Float_t *charges, 
856                                    Int_t sigexpanded){
857   //   Add a simulated digit to the list.
858
859   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
860   switch(branch){
861   case 0:
862     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
863     break;
864   case 1:
865     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
866                                                    hits,charges,sigexpanded);
867     break;
868   case 2:
869     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(digits,tracks,hits);
870     break;
871   } 
872 }
873 //______________________________________________________________________
874 void AliITSDetTypeSim::StoreCalibration(Int_t firstRun, Int_t lastRun,
875                                         AliCDBMetaData &md) {
876   // Store calibration in the calibration database
877   // The database must be created in an external piece of code (i.e. 
878   // a configuration macro )
879
880   if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
881     AliWarning("No storage set! Will use dummy one");
882     AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
883   }
884
885   if (!fCalibration) {
886     AliError("AliITSCalibration classes are not defined - nothing done");
887     return;
888   }
889   AliCDBId idRespSPD("ITS/Calib/SPDDead",firstRun, lastRun);
890   AliCDBId idRespSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
891   AliCDBId idRespSSD("ITS/Calib/CalibSSD",firstRun, lastRun);
892
893   TObjArray respSPD(fNMod[0]);
894   TObjArray respSDD(fNMod[1]-fNMod[0]);
895   TObjArray respSSD(fNMod[2]-fNMod[1]);
896   respSPD.SetOwner(kFALSE);
897   respSSD.SetOwner(kFALSE);
898   respSSD.SetOwner(kFALSE);
899
900   Int_t index[fgkNdettypes];
901   for (Int_t i = 0; i<fgkNdettypes; i++ ) {
902     index[i] = 0;
903     for (Int_t j = 0; j<=i; j++ )
904       index[i]+=fNMod[j];
905   }
906
907   for (Int_t i = 0; i<index[0]; i++ )
908     respSPD.Add(fCalibration->At(i));
909
910   for (Int_t i = index[0]; i<index[1]; i++ )
911     respSDD.Add(fCalibration->At(i));
912
913   for (Int_t i = index[1]; i<index[2]; i++ )
914     respSSD.Add(fCalibration->At(i));
915
916   AliCDBManager::Instance()->Put(&respSPD, idRespSPD, &md);
917   AliCDBManager::Instance()->Put(&respSDD, idRespSDD, &md);
918   AliCDBManager::Instance()->Put(&respSSD, idRespSSD, &md);
919 }
920
921 //______________________________________________________________________
922 void AliITSDetTypeSim::ReadOldSSDNoise(TObjArray *array, 
923                                        AliITSNoiseSSDv2 *noiseSSD) {
924   //Reads the old SSD calibration object and converts it to the new format
925   const Int_t fgkSSDSTRIPSPERMODULE = 1536;
926   const Int_t fgkSSDPSIDESTRIPSPERMODULE = 768;
927
928   Int_t fNMod = array->GetEntries();
929   cout<<"Converting old calibration object for noise..."<<endl;
930
931   //NOISE
932   Double_t noise = 0.0;
933   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
934     AliITSNoiseSSD *noiseModule = (AliITSNoiseSSD*) (array->At(iModule));
935     for(Int_t iStrip = 0; iStrip < fgkSSDSTRIPSPERMODULE; iStrip++) {
936       noise = (iStrip < fgkSSDPSIDESTRIPSPERMODULE) ? noiseModule->GetNoiseP(iStrip) : noiseModule->GetNoiseN(1535 - iStrip);
937       if(iStrip < fgkSSDPSIDESTRIPSPERMODULE)
938         noiseSSD->AddNoiseP(iModule,iStrip,noise);
939       if(iStrip >= fgkSSDPSIDESTRIPSPERMODULE)
940         noiseSSD->AddNoiseN(iModule,1535 - iStrip,noise);
941     }//loop over strips
942   }//loop over modules      
943 }
944
945 //______________________________________________________________________
946 void AliITSDetTypeSim::ReadOldSSDBadChannels(TObjArray *array, 
947                                              AliITSBadChannelsSSDv2 *badChannelsSSD) {
948   //Reads the old SSD calibration object and converts it to the new format
949   Int_t fNMod = array->GetEntries();
950   cout<<"Converting old calibration object for bad channels..."<<endl;
951   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
952     //for (Int_t iModule = 0; iModule < 1; iModule++) {
953     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (array->At(iModule));
954     TArrayI arrayPSide = bad->GetBadPChannelsList();
955     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++) 
956       badChannelsSSD->AddBadChannelP(iModule,
957                                      iPCounter,
958                                      (Char_t)arrayPSide.At(iPCounter));
959         
960     TArrayI arrayNSide = bad->GetBadNChannelsList();
961     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++) 
962       badChannelsSSD->AddBadChannelN(iModule,
963                                      iNCounter,
964                                      (Char_t)arrayNSide.At(iNCounter));
965     
966   }//loop over modules      
967 }
968
969 //______________________________________________________________________
970 void AliITSDetTypeSim::ReadOldSSDGain(TObjArray *array, 
971                                       AliITSGainSSDv2 *gainSSD) {
972   //Reads the old SSD calibration object and converts it to the new format
973
974   Int_t fNMod = array->GetEntries();
975   cout<<"Converting old calibration object for gain..."<<endl;
976
977   //GAIN
978   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
979     AliITSGainSSD *gainModule = (AliITSGainSSD*) (array->At(iModule));
980     TArrayF arrayPSide = gainModule->GetGainP();
981     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++)
982       gainSSD->AddGainP(iModule,
983                         iPCounter,
984                         arrayPSide.At(iPCounter));
985     TArrayF arrayNSide = gainModule->GetGainN();
986     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++)
987       gainSSD->AddGainN(iModule,
988                         iNCounter,
989                         arrayNSide.At(iNCounter));
990   }//loop over modules 
991 }
992