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