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