]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeSim.cxx
From Alex: updated and new macros for TRD.
[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 "AliITSresponseSDD.h"
46 #include "AliITSCalibrationSDD.h"
47 #include "AliITSMapSDD.h"
48 #include "AliITSDriftSpeedArraySDD.h"
49 #include "AliITSDriftSpeedSDD.h"
50 #include "AliITSCalibrationSSD.h"
51 #include "AliITSNoiseSSD.h"
52 #include "AliITSGainSSD.h"
53 #include "AliITSBadChannelsSSD.h"
54 #include "AliITSCalibrationSSD.h"
55 #include "AliITSsegmentationSPD.h"
56 #include "AliITSsegmentationSDD.h"
57 #include "AliITSsegmentationSSD.h"
58 #include "AliITSsimulation.h"
59 #include "AliITSsimulationSPD.h"
60 #include "AliITSsimulationSDD.h"
61 #include "AliITSsimulationSSD.h"
62
63
64 const Int_t AliITSDetTypeSim::fgkNdettypes = 3;
65 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSPD =  240;
66 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSDD =  260;
67 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSSD = 1698;
68
69 ClassImp(AliITSDetTypeSim)
70
71 //----------------------------------------------------------------------
72 AliITSDetTypeSim::AliITSDetTypeSim():
73 TObject(),
74 fSimulation(),   // [NDet]
75 fSegmentation(), // [NDet]
76 fCalibration(),     // [NMod]
77 fPreProcess(),   // [] e.g. Fill fHitModule with hits
78 fPostProcess(),  // [] e.g. Wright Raw data
79 fNSDigits(0),    //! number of SDigits
80 fSDigits("AliITSpListItem",1000),   
81 fNDigits(0),     //! number of Digits
82 fRunNumber(0),   //! Run number (to access DB)
83 fDigits(),       //! [NMod][NDigits]
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   fNMod[0] = fgkDefaultNModulesSPD;
105   fNMod[1] = fgkDefaultNModulesSDD;
106   fNMod[2] = fgkDefaultNModulesSSD;
107   SetRunNumber();
108 }
109 //----------------------------------------------------------------------
110 AliITSDetTypeSim::~AliITSDetTypeSim(){
111     // Destructor
112     // Inputs:
113     //    none.
114     // Outputs:
115     //    none.
116     // Return:
117     //    Nothing.
118
119     if(fSimulation){
120         fSimulation->Delete();
121         delete fSimulation;
122     }
123     fSimulation = 0;
124     if(fSegmentation){
125         fSegmentation->Delete();
126         delete fSegmentation;
127     }
128     fSegmentation = 0;
129     if(fCalibration && fRunNumber<0){
130         AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
131                                GetITSgeom()->GetStartSPD()))->GetResponse();
132         AliITSresponse* rsdd = ((AliITSCalibration*)fCalibration->At(
133                                GetITSgeom()->GetStartSDD()))->GetResponse();
134         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
135                                GetITSgeom()->GetStartSSD()))->GetResponse();
136         if(rspd) delete rspd;
137         if(rsdd) delete rsdd;
138         if(rssd) delete rssd;
139         fCalibration->Delete();
140         delete fCalibration;
141     }
142     fCalibration = 0;
143     if(fPreProcess){
144         fPreProcess->Delete();
145         delete fPreProcess;
146     }
147     fPreProcess = 0;
148     if(fPostProcess){
149         fPostProcess->Delete();
150         delete fPostProcess;
151     }
152     fPostProcess = 0;
153     if(fDDLMapSDD) delete fDDLMapSDD;
154     if(fNDigits) delete [] fNDigits;
155     fNDigits = 0;
156     if (fLoader)fLoader->GetModulesFolder()->Remove(this);
157     fLoader = 0; // Not deleting it.
158     fSDigits.Delete();
159     if (fDigits) {
160       fDigits->Delete();
161       delete fDigits;
162     }
163     fDigits=0;
164 }
165 //----------------------------------------------------------------------
166 AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source),
167 fSimulation(source.fSimulation),   // [NDet]
168 fSegmentation(source.fSegmentation), // [NDet]
169 fCalibration(source.fCalibration),     // [NMod]
170 fPreProcess(source.fPreProcess),   // [] e.g. Fill fHitModule with hits
171 fPostProcess(source.fPostProcess),  // [] e.g. Wright Raw data
172 fNSDigits(source.fNSDigits),    //! number of SDigits
173 fSDigits(*((TClonesArray*)source.fSDigits.Clone())),
174 fNDigits(source.fNDigits),     //! number of Digits
175 fRunNumber(source.fRunNumber),   //! Run number (to access DB)
176 fDigits(source.fDigits),       //! [NMod][NDigits]
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* rsdd = ((AliITSCalibration*)fCalibration->At(
346                                 GetITSgeom()->GetStartSDD()))->GetResponse();
347         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
348                                 GetITSgeom()->GetStartSSD()))->GetResponse();
349         if(rspd) delete rspd;
350         if(rsdd) delete rsdd;
351         if(rssd) delete rssd;
352         fCalibration->Clear();
353     }else if (fCalibration && fRunNumber>=0){
354         fCalibration->Clear();
355     }
356 }
357 //______________________________________________________________________
358 void AliITSDetTypeSim::ResetSegmentation(){
359     //Resets segmentation array
360     if(fSegmentation) fSegmentation->Clear();
361 }
362 //_______________________________________________________________________
363 AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod){
364     //Get response model for module number iMod 
365  
366     if(fCalibration==0) {
367         AliError("fCalibration is 0!");
368         return 0; 
369     }
370   return (AliITSCalibration*)(fCalibration->At(iMod));
371 }
372 //_______________________________________________________________________
373 void AliITSDetTypeSim::SetDefaults(){
374     //Set defaults for segmentation and response
375
376     if(GetITSgeom()==0){
377         Warning("SetDefaults","GetITSgeom() is 0!");
378         return;
379     } // end if
380     if (fCalibration==0) {
381         CreateCalibrationArray();
382     } // end if
383
384     ResetSegmentation();
385     if(!GetCalibration()){AliFatal("Exit"); exit(0);}
386
387     SetDigitClassName(0,"AliITSdigitSPD");
388     SetDigitClassName(1,"AliITSdigitSDD");
389     SetDigitClassName(2,"AliITSdigitSSD");
390
391     for(Int_t idet=0;idet<fgkNdettypes;idet++){
392       if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
393     }
394 }
395 //______________________________________________________________________
396 Bool_t AliITSDetTypeSim::GetCalibration() {
397   // Get Default calibration if a storage is not defined.
398
399   if(!fFirstcall){
400     AliITSCalibration* cal = GetCalibrationModel(0);
401     if(cal)return kTRUE;
402   }
403   else {
404     fFirstcall = kFALSE;
405   }
406
407   SetRunNumber((Int_t)AliCDBManager::Instance()->GetRun());
408   Int_t run=GetRunNumber();
409
410   Bool_t origCacheStatus = AliCDBManager::Instance()->GetCacheFlag();
411   Bool_t isCacheActive = kTRUE;
412   if(GetRunNumber()<0){
413     isCacheActive=kFALSE;
414     fCalibration->SetOwner(kTRUE);
415   }
416   else{
417     fCalibration->SetOwner(kFALSE);
418   }
419
420   AliCDBManager::Instance()->SetCacheFlag(isCacheActive);
421
422   AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDDead", run);
423   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", run);
424   AliCDBEntry *drSpSDD = AliCDBManager::Instance()->Get("ITS/Calib/DriftSpeedSDD",run);
425   AliCDBEntry *ddlMapSDD = AliCDBManager::Instance()->Get("ITS/Calib/DDLMapSDD",run);
426   AliCDBEntry *mapASDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsAnodeSDD",run);
427   AliCDBEntry *mapTSDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsTimeSDD",run);
428   // AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", run);
429   AliCDBEntry *entryNoiseSSD = AliCDBManager::Instance()->Get("ITS/Calib/NoiseSSD");
430   AliCDBEntry *entryGainSSD = AliCDBManager::Instance()->Get("ITS/Calib/GainSSD");
431   AliCDBEntry *entryBadChannelsSSD = AliCDBManager::Instance()->Get("ITS/Calib/BadChannelsSSD");
432
433   AliCDBEntry *entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", run);
434   AliCDBEntry *entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD", run);
435   AliCDBEntry *entry2SSD = AliCDBManager::Instance()->Get("ITS/Calib/RespSSD", run);
436
437   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
438      !entry2SPD || !entry2SDD || !entry2SSD || !drSpSDD || !ddlMapSDD || !mapASDD ||!mapTSDD){
439     AliFatal("Calibration object retrieval failed! ");
440     return kFALSE;
441   }     
442
443 //   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
444 //      !entry2SPD || !entry2SDD || !entry2SSD){
445 //     AliFatal("Calibration object retrieval failed! ");
446 //     return kFALSE;
447 //   }          
448
449   TObjArray *calSPD = (TObjArray *)entrySPD->GetObject();
450   if(!isCacheActive)entrySPD->SetObject(NULL);
451   entrySPD->SetOwner(kTRUE);
452
453   AliITSresponseSPD *pSPD = (AliITSresponseSPD*)entry2SPD->GetObject();
454   if(!isCacheActive)entry2SPD->SetObject(NULL);
455   entry2SPD->SetOwner(kTRUE);
456    
457   TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
458   if(!isCacheActive)entrySDD->SetObject(NULL);
459   entrySDD->SetOwner(kTRUE);
460
461   AliITSresponseSDD *pSDD = (AliITSresponseSDD*)entry2SDD->GetObject();
462   if(!isCacheActive)entry2SDD->SetObject(NULL);
463   entry2SDD->SetOwner(kTRUE);
464
465   TObjArray *drSp = (TObjArray *)drSpSDD->GetObject();
466   if(!isCacheActive)drSpSDD->SetObject(NULL);
467   drSpSDD->SetOwner(kTRUE);
468
469   AliITSDDLModuleMapSDD *ddlsdd=(AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
470   if(!isCacheActive)ddlMapSDD->SetObject(NULL);
471   ddlMapSDD->SetOwner(kTRUE);
472
473   TObjArray *mapAn = (TObjArray *)mapASDD->GetObject();
474   if(!isCacheActive)mapASDD->SetObject(NULL);
475   mapASDD->SetOwner(kTRUE);
476
477   TObjArray *mapT = (TObjArray *)mapTSDD->GetObject();
478   if(!isCacheActive)mapTSDD->SetObject(NULL);
479   mapTSDD->SetOwner(kTRUE);
480
481   /*
482   TObjArray *calSSD = (TObjArray *)entrySSD->GetObject();
483   if(!isCacheActive)entrySSD->SetObject(NULL);
484   entrySSD->SetOwner(kTRUE);
485   */
486
487   TObjArray *noiseSSD = (TObjArray *)entryNoiseSSD->GetObject();
488   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
489   entryNoiseSSD->SetOwner(kTRUE);
490
491   TObjArray *gainSSD = (TObjArray *)entryGainSSD->GetObject();
492   if(!isCacheActive)entryGainSSD->SetObject(NULL);
493   entryGainSSD->SetOwner(kTRUE);
494
495   TObjArray *badchannelsSSD = (TObjArray *)entryBadChannelsSSD->GetObject();
496   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
497   entryBadChannelsSSD->SetOwner(kTRUE);
498
499   AliITSresponseSSD *pSSD = (AliITSresponseSSD*)entry2SSD->GetObject();
500   if(!isCacheActive)entry2SSD->SetObject(NULL);
501   entry2SSD->SetOwner(kTRUE);
502   
503   // DB entries are deleted. In this way metadeta objects are deleted as well
504   if(!isCacheActive){
505     delete entrySPD;
506     delete entrySDD;
507     delete entryNoiseSSD;
508     delete entryGainSSD;
509     delete entryBadChannelsSSD;
510     delete entry2SPD;
511     delete entry2SDD;
512     delete entry2SSD;
513     delete mapASDD;   
514     delete mapTSDD;
515     delete drSpSDD;
516     delete ddlMapSDD;
517   }
518   
519   AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
520
521   if ((!pSPD)||(!pSDD)||(!pSSD) || (!calSPD) || (!calSDD) || (!drSp) || (!ddlsdd)
522       || (!mapAn) || (!mapT) || (!noiseSSD)|| (!gainSSD)|| (!badchannelsSSD)) {
523     AliWarning("Can not get calibration from calibration database !");
524     return kFALSE;
525   }
526
527   fNMod[0] = calSPD->GetEntries();
528   fNMod[1] = calSDD->GetEntries();
529   fNMod[2] = noiseSSD->GetEntries();
530   AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
531                fNMod[0], fNMod[1], fNMod[2]));
532   AliITSCalibration* cal;
533   for (Int_t i=0; i<fNMod[0]; i++) {
534     cal = (AliITSCalibration*) calSPD->At(i);
535     cal->SetResponse(pSPD);
536     SetCalibrationModel(i, cal);
537   }
538
539   fDDLMapSDD->SetDDLMap(ddlsdd);
540
541   for (Int_t i=0; i<fgkDefaultNModulesSDD; i++) {
542     Int_t iddl,icarlos;
543     Int_t iMod = i + fgkDefaultNModulesSPD;
544     fDDLMapSDD->FindInDDLMap(iMod,iddl,icarlos);
545     if(iddl<0){ 
546       AliITSCalibrationSDD* calsdddead=new AliITSCalibrationSDD();
547       calsdddead->SetResponse(pSDD);
548       calsdddead->SetBad();      
549       AliITSDriftSpeedSDD* driftspdef = new AliITSDriftSpeedSDD();
550       AliITSDriftSpeedArraySDD* arrdrsp=new AliITSDriftSpeedArraySDD(1);
551       arrdrsp->AddDriftSpeed(driftspdef);
552       calsdddead->SetDriftSpeed(0,arrdrsp);
553       calsdddead->SetDriftSpeed(1,arrdrsp);
554       SetCalibrationModel(iMod, calsdddead);
555       AliWarning(Form("SDD module %d not present in DDL map: set it as dead",iMod));
556     }else{
557       cal = (AliITSCalibration*) calSDD->At(i);
558       cal->SetResponse(pSDD);
559       Int_t i0=2*i;
560       Int_t i1=1+2*i;
561       AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
562       AliITSMapSDD* ma0 = (AliITSMapSDD*)mapAn->At(i0);
563       AliITSMapSDD* mt0 = (AliITSMapSDD*)mapT->At(i0);
564       AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
565       AliITSMapSDD* ma1 = (AliITSMapSDD*)mapAn->At(i1);
566       AliITSMapSDD* mt1 = (AliITSMapSDD*)mapT->At(i1);
567       cal->SetDriftSpeed(0,arr0);
568       cal->SetDriftSpeed(1,arr1);
569       cal->SetMapA(0,ma0);
570       cal->SetMapA(1,ma1);
571       cal->SetMapT(0,mt0);
572       cal->SetMapT(1,mt1);
573       SetCalibrationModel(iMod, cal);
574     }
575   }
576
577   for (Int_t i=0; i<fNMod[2]; i++) {
578     AliITSCalibrationSSD *calibSSD = new AliITSCalibrationSSD();
579     calibSSD->SetResponse((AliITSresponse*)pSSD);
580     
581     AliITSNoiseSSD *noise = (AliITSNoiseSSD*) (noiseSSD->At(i));
582     calibSSD->SetNoise(noise);
583     AliITSGainSSD *gain = (AliITSGainSSD*) (gainSSD->At(i));
584     calibSSD->SetGain(gain);
585     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (badchannelsSSD->At(i));
586     calibSSD->SetBadChannels(bad);
587
588     Int_t iMod = i + fgkDefaultNModulesSPD + fgkDefaultNModulesSDD;
589     SetCalibrationModel(iMod, calibSSD);
590
591  }
592   return kTRUE;
593 }
594 //_______________________________________________________________________
595 void AliITSDetTypeSim::SetDefaultSimulation(){
596   //Set default simulation for detector type
597
598   if(GetITSgeom()==0){
599     Warning("SetDefaultSimulation","GetITSgeom() is 0!");
600     return;
601   }
602   if(fCalibration==0){
603     Warning("SetDefaultSimulation","fCalibration is 0!");
604     return;
605   }
606   if(fSegmentation==0){
607     Warning("SetDefaultSimulation","fSegmentation is 0!");
608     for(Int_t i=0;i<fgkNdettypes;i++) SetDefaultSegmentation(i);
609   }else for(Int_t i=0;i<fgkNdettypes;i++) if(!GetSegmentationModel(i)){
610       Warning("SetDefaultSimulation",
611               "Segmentation not defined for det %d - Default taken\n!",i);
612       SetDefaultSegmentation(i);
613   }
614   AliITSsimulation* sim;
615
616   for(Int_t idet=0;idet<fgkNdettypes;idet++){
617    //SPD
618     if(idet==0){
619       sim = GetSimulationModel(idet); 
620       if(!sim){
621         sim = new AliITSsimulationSPD(this);
622         SetSimulationModel(idet,sim);
623       }
624     }
625     //SDD
626     if(idet==1){
627       sim = GetSimulationModel(idet);
628       if(!sim){
629         sim = new AliITSsimulationSDD(this);
630         SetSimulationModel(idet,sim);
631       }      
632     }
633     //SSD
634     if(idet==2){
635       sim = GetSimulationModel(idet);
636       if(!sim){
637         sim = new AliITSsimulationSSD(this);
638         SetSimulationModel(idet,sim);
639       }
640     }
641
642   }
643 }
644 //___________________________________________________________________
645 void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, Char_t* name){
646   // Set branch address for the ITS summable digits Trees.  
647   char branchname[30];
648
649   if(!treeS){
650     return;
651   }
652   TBranch *branch;
653   sprintf(branchname,"%s",name);
654   branch = treeS->GetBranch(branchname);
655   TClonesArray *sdigi = &fSDigits;
656   if (branch) branch->SetAddress(&sdigi);
657
658 }
659 //___________________________________________________________________
660 void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
661   // Set branch address for the digit Trees.
662   
663   const char *det[3] = {"SPD","SDD","SSD"};
664   TBranch *branch;
665   
666   char branchname[30];
667   
668   if(!treeD){
669     return;
670   }
671   if(!fDigits){
672     fDigits = new TObjArray(fgkNdettypes); 
673   }
674   for(Int_t i=0;i<fgkNdettypes;i++){
675     Char_t* digclass = GetDigitClassName(i);
676     if(digclass==0x0){
677       if(i==0) SetDigitClassName(i,"AliITSdigitSPD");
678       if(i==1) SetDigitClassName(i,"AliITSdigitSDD");
679       if(i==2) SetDigitClassName(i,"AliITSdigitSSD");
680       digclass = GetDigitClassName(i);
681     }
682     TString classn = digclass;
683     if(!(fDigits->At(i))){
684       fDigits->AddAt(new TClonesArray(classn.Data(),1000),i);
685     }else{
686       ResetDigits(i);
687     }
688     
689     if(fgkNdettypes==3) sprintf(branchname,"%sDigits%s",name,det[i]);
690     else sprintf(branchname,"%sDigits%d",name,i+1);
691     if(fDigits){
692       branch = treeD->GetBranch(branchname);
693       if(branch) branch->SetAddress(&((*fDigits)[i]));
694     }
695   }
696
697 }
698 //___________________________________________________________________
699 void AliITSDetTypeSim::ResetDigits(){
700   // Reset number of digits and the digits array for the ITS detector.  
701
702   if(!fDigits){
703     Error("ResetDigits","fDigits is null!");
704     return;
705   }
706   for(Int_t i=0;i<fgkNdettypes;i++){
707     ResetDigits(i);
708   }
709 }
710 //___________________________________________________________________
711 void AliITSDetTypeSim::ResetDigits(Int_t branch){
712   // Reset number of digits and the digits array for this branch.
713
714   if(fDigits->At(branch)){
715     ((TClonesArray*)fDigits->At(branch))->Clear();
716   }
717   if(fNDigits) fNDigits[branch]=0;
718
719 }
720
721
722
723 //_______________________________________________________________________
724 void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
725   // Standard Summable digits to Digits function.
726   if(!GetITSgeom()){
727     Warning("SDigitsToDigits","GetITSgeom() is null!!");
728     return;
729   }
730   
731   const char *all = strstr(opt,"All");
732   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
733                         strstr(opt,"SSD")};
734   if( !det[0] && !det[1] && !det[2] ) all = "All";
735   else all = 0;
736   static Bool_t setDef = kTRUE;
737   if(setDef) SetDefaultSimulation();
738   setDef = kFALSE;
739   
740   AliITSsimulation *sim =0;
741   TTree* trees = fLoader->TreeS();
742   if( !(trees && GetSDigits()) ){
743     Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
744     return;
745   } 
746   sprintf(name,"%s",name);
747   TBranch* brchSDigits = trees->GetBranch(name);
748   
749   Int_t id;
750   for(Int_t module=0;module<GetITSgeom()->GetIndexMax();module++){
751      id = GetITSgeom()->GetModuleType(module);
752     if (!all && !det[id]) continue;
753     sim = (AliITSsimulation*)GetSimulationModel(id);
754     if(!sim){
755       Error("SDigit2Digits","The simulation class was not "
756             "instanciated for module %d type %s!",module,
757             GetITSgeom()->GetModuleTypeName(module));
758       exit(1);
759     }
760     sim->InitSimulationModule(module,gAlice->GetEvNumber());
761     
762     fSDigits.Clear();
763     brchSDigits->GetEvent(module);
764     sim->AddSDigitsToModule(&fSDigits,0);
765     sim->FinishSDigitiseModule();
766     fLoader->TreeD()->Fill();
767     ResetDigits();
768   }
769   fLoader->TreeD()->GetEntries();
770   fLoader->TreeD()->AutoSave();
771   fLoader->TreeD()->Reset();
772 }
773 //_________________________________________________________
774 void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){  
775   //Adds the module full of summable digits to the summable digits tree.
776
777   new(fSDigits[fNSDigits++]) AliITSpListItem(sdig);
778 }
779 //__________________________________________________________
780 void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){  
781   //    Add a simulated digit.
782
783   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
784   switch(branch){
785   case 0:
786     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
787     break;
788   case 1:
789     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
790     break;
791   case 2:
792     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
793     break;
794   }  
795 }
796 //______________________________________________________________________
797 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
798                                    Int_t *tracks,Int_t *hits,Float_t *charges, 
799                                    Int_t sigexpanded){
800   //   Add a simulated digit to the list.
801
802   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
803   switch(branch){
804   case 0:
805     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
806     break;
807   case 1:
808     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
809                                                    hits,charges,sigexpanded);
810     break;
811   case 2:
812     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(digits,tracks,hits);
813     break;
814   } 
815 }
816 //______________________________________________________________________
817 void AliITSDetTypeSim::StoreCalibration(Int_t firstRun, Int_t lastRun,
818                                         AliCDBMetaData &md) {
819   // Store calibration in the calibration database
820   // The database must be created in an external piece of code (i.e. 
821   // a configuration macro )
822
823   if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
824     AliWarning("No storage set! Will use dummy one");
825     AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
826   }
827
828   if (!fCalibration) {
829     AliError("AliITSCalibration classes are not defined - nothing done");
830     return;
831   }
832   AliCDBId idRespSPD("ITS/Calib/SPDDead",firstRun, lastRun);
833   AliCDBId idRespSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
834   AliCDBId idRespSSD("ITS/Calib/CalibSSD",firstRun, lastRun);
835
836   TObjArray respSPD(fNMod[0]);
837   TObjArray respSDD(fNMod[1]-fNMod[0]);
838   TObjArray respSSD(fNMod[2]-fNMod[1]);
839   respSPD.SetOwner(kFALSE);
840   respSSD.SetOwner(kFALSE);
841   respSSD.SetOwner(kFALSE);
842
843   Int_t index[fgkNdettypes];
844   for (Int_t i = 0; i<fgkNdettypes; i++ ) {
845     index[i] = 0;
846     for (Int_t j = 0; j<=i; j++ )
847       index[i]+=fNMod[j];
848   }
849
850   for (Int_t i = 0; i<index[0]; i++ )
851     respSPD.Add(fCalibration->At(i));
852
853   for (Int_t i = index[0]; i<index[1]; i++ )
854     respSDD.Add(fCalibration->At(i));
855
856   for (Int_t i = index[1]; i<index[2]; i++ )
857     respSSD.Add(fCalibration->At(i));
858
859   AliCDBManager::Instance()->Put(&respSPD, idRespSPD, &md);
860   AliCDBManager::Instance()->Put(&respSDD, idRespSDD, &md);
861   AliCDBManager::Instance()->Put(&respSSD, idRespSSD, &md);
862 }
863
864