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