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