]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeSim.cxx
HLT mode retrieved from the OCDB and used to set SDD raw data format in simulation...
[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 "AliITSHLTforSDD.h"
50 #include "AliITSCalibrationSSD.h"
51 #include "AliITSNoiseSSDv2.h"
52 #include "AliITSGainSSDv2.h"
53 #include "AliITSBadChannelsSSDv2.h"
54 #include "AliITSNoiseSSD.h"
55 #include "AliITSGainSSD.h"
56 #include "AliITSBadChannelsSSD.h"
57 #include "AliITSCalibrationSSD.h"
58 #include "AliITSsegmentationSPD.h"
59 #include "AliITSsegmentationSDD.h"
60 #include "AliITSsegmentationSSD.h"
61 #include "AliITSsimulation.h"
62 #include "AliITSsimulationSPD.h"
63 #include "AliITSsimulationSDD.h"
64 #include "AliITSsimulationSSD.h"
65
66
67 const Int_t AliITSDetTypeSim::fgkNdettypes = 3;
68 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSPD =  240;
69 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSDD =  260;
70 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSSD = 1698;
71
72 ClassImp(AliITSDetTypeSim)
73
74 //----------------------------------------------------------------------
75 AliITSDetTypeSim::AliITSDetTypeSim():
76 TObject(),
77 fSimulation(),   // [NDet]
78 fSegmentation(), // [NDet]
79 fCalibration(),     // [NMod]
80 fSSDCalibration(0),
81 fPreProcess(),   // [] e.g. Fill fHitModule with hits
82 fPostProcess(),  // [] e.g. Wright Raw data
83 fNSDigits(0),    //! number of SDigits
84 fSDigits("AliITSpListItem",1000),   
85 fNDigits(0),     //! number of Digits
86 fRunNumber(0),   //! Run number (to access DB)
87 fDigits(),       //! [NMod][NDigits]
88 fSimuPar(0),
89 fDDLMapSDD(0),
90 fHitClassName(), // String with Hit class name.
91 fSDigClassName(),// String with SDigit class name.
92 fDigClassName(), // String with digit class name.
93 fLoader(0),      // local pointer to loader
94 fFirstcall(kTRUE),
95 fIsHLTmodeC(0){ // flag
96     // Default Constructor
97     // Inputs:
98     //    none.
99     // Outputs:
100     //    none.
101     // Return:
102     //    A properly zero-ed AliITSDetTypeSim class.
103
104   fSimulation = new TObjArray(fgkNdettypes);
105   fSegmentation = new TObjArray(fgkNdettypes);
106   fSegmentation->SetOwner(kTRUE);
107   fDigits = new TObjArray(fgkNdettypes);
108   fNDigits = new Int_t[fgkNdettypes];
109   fDDLMapSDD=new AliITSDDLModuleMapSDD();
110   fSimuPar= new AliITSSimuParam();
111   fSSDCalibration=new AliITSCalibrationSSD();
112   fNMod[0] = fgkDefaultNModulesSPD;
113   fNMod[1] = fgkDefaultNModulesSDD;
114   fNMod[2] = fgkDefaultNModulesSSD;
115   SetRunNumber();
116 }
117 //----------------------------------------------------------------------
118 AliITSDetTypeSim::~AliITSDetTypeSim(){
119     // Destructor
120     // Inputs:
121     //    none.
122     // Outputs:
123     //    none.
124     // Return:
125     //    Nothing.
126
127     if(fSimulation){
128         fSimulation->Delete();
129         delete fSimulation;
130     }
131     fSimulation = 0;
132     if(fSegmentation){
133         fSegmentation->Delete();
134         delete fSegmentation;
135     }
136     fSegmentation = 0;
137     if(fCalibration && fRunNumber<0){
138
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 fIsHLTmodeC(source.fIsHLTmodeC)
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       AliITSCalibrationSDD* cal=(AliITSCalibrationSDD*)GetCalibrationModel(fgkDefaultNModulesSPD+1);
286       if(cal->IsAMAt20MHz()){ 
287         seg->SetPadSize(seg->Dpz(0),20.);
288         seg->SetNPads(seg->Npz()/2,128);
289       }
290     }else {
291         seg = new AliITSsegmentationSSD();
292     }
293     SetSegmentationModel(idet,seg);
294 }
295 //______________________________________________________________________
296 void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,
297                                             AliITSsegmentation *seg){
298    
299   //Set segmentation model for detector type
300   if(fSegmentation==0x0){
301     fSegmentation = new TObjArray(fgkNdettypes);
302     fSegmentation->SetOwner(kTRUE);
303   }
304   fSegmentation->AddAt(seg,dettype);
305 }
306 //______________________________________________________________________
307 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype){
308   //Get segmentation model for detector type
309    
310    if(fSegmentation==0) {
311        Warning("GetSegmentationModel","fSegmentation is 0!");
312        return 0; 
313    } 
314    return (AliITSsegmentation*)(fSegmentation->At(dettype));
315 }
316 //_______________________________________________________________________
317 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module){
318     //Get segmentation model by module number
319     if(GetITSgeom()==0){
320         Warning("GetSegmentationModelByModule","GetITSgeom() is 0!");
321         return 0;
322     }     
323     return GetSegmentationModel(GetITSgeom()->GetModuleType(module));
324 }
325 //_______________________________________________________________________
326 void AliITSDetTypeSim::CreateCalibrationArray() {
327     //Create the container of calibration functions with correct size
328     if (fCalibration) {
329         Warning("CreateCalibration","pointer to calibration object exists\n");
330         fCalibration->Delete();
331         delete fCalibration;
332     }
333
334     Int_t nModTot = GetITSgeom()->GetIndexMax();
335     fCalibration = new TObjArray(nModTot);
336     fCalibration->SetOwner(kTRUE);
337     fCalibration->Clear();
338 }
339 //_______________________________________________________________________
340 void AliITSDetTypeSim::SetCalibrationModel(Int_t iMod, AliITSCalibration *resp){
341     //Set response model for modules
342
343     if (fCalibration==0) CreateCalibrationArray();
344  
345     if (fCalibration->At(iMod)!=0)
346         delete (AliITSCalibration*) fCalibration->At(iMod);
347     fCalibration->AddAt(resp, iMod);
348 }
349 //______________________________________________________________________
350 void AliITSDetTypeSim::ResetCalibrationArray(){
351     //resets response array
352     if(fCalibration && fRunNumber<0){  // if fRunNumber<0 fCalibration is owner
353       /*
354         AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
355                                 GetITSgeom()->GetStartSPD()))->GetResponse();
356         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
357                                 GetITSgeom()->GetStartSSD()))->GetResponse();
358         if(rspd) delete rspd;
359         if(rssd) delete rssd;
360       */
361         fCalibration->Clear();
362     }else if (fCalibration && fRunNumber>=0){
363         fCalibration->Clear();
364     }
365 }
366 //______________________________________________________________________
367 void AliITSDetTypeSim::ResetSegmentation(){
368     //Resets segmentation array
369     if(fSegmentation) fSegmentation->Clear();
370 }
371 //_______________________________________________________________________
372 AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod){
373     //Get response model for module number iMod 
374  
375     if(fCalibration==0) {
376         AliError("fCalibration is 0!");
377         return 0; 
378     }
379   if(iMod<fgkDefaultNModulesSPD+fgkDefaultNModulesSDD){
380     return (AliITSCalibration*)fCalibration->At(iMod);
381   }else{
382     Int_t i=iMod-(fgkDefaultNModulesSPD+fgkDefaultNModulesSDD);
383     fSSDCalibration->SetModule(i);
384     return (AliITSCalibration*)fSSDCalibration;
385   }
386
387 }
388 //_______________________________________________________________________
389 void AliITSDetTypeSim::SetDefaults(){
390     //Set defaults for segmentation and response
391
392     if(GetITSgeom()==0){
393         Warning("SetDefaults","GetITSgeom() is 0!");
394         return;
395     } // end if
396     if (fCalibration==0) {
397         CreateCalibrationArray();
398     } // end if
399
400     ResetSegmentation();
401     if(!GetCalibration()){AliFatal("Exit"); exit(0);}
402
403     SetDigitClassName(0,"AliITSdigitSPD");
404     SetDigitClassName(1,"AliITSdigitSDD");
405     SetDigitClassName(2,"AliITSdigitSSD");
406
407     for(Int_t idet=0;idet<fgkNdettypes;idet++){
408       if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
409     }
410 }
411 //______________________________________________________________________
412 Bool_t AliITSDetTypeSim::GetCalibration() {
413   // Get Default calibration if a storage is not defined.
414
415   if(!fFirstcall){
416     AliITSCalibration* cal = GetCalibrationModel(0);
417     if(cal)return kTRUE;
418   }
419   else {
420     fFirstcall = kFALSE;
421   }
422
423   SetRunNumber((Int_t)AliCDBManager::Instance()->GetRun());
424   Int_t run=GetRunNumber();
425
426   Bool_t origCacheStatus = AliCDBManager::Instance()->GetCacheFlag();
427   Bool_t isCacheActive = kTRUE;
428   if(GetRunNumber()<0){
429     isCacheActive=kFALSE;
430     fCalibration->SetOwner(kTRUE);
431   }
432   else{
433     fCalibration->SetOwner(kFALSE);
434   }
435
436   AliCDBManager::Instance()->SetCacheFlag(isCacheActive);
437
438   AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDDead", run);
439   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", run);
440   AliCDBEntry *drSpSDD = AliCDBManager::Instance()->Get("ITS/Calib/DriftSpeedSDD",run);
441   AliCDBEntry *ddlMapSDD = AliCDBManager::Instance()->Get("ITS/Calib/DDLMapSDD",run);
442   AliCDBEntry *hltforSDD = AliCDBManager::Instance()->Get("ITS/Calib/HLTforSDD");
443   //AliCDBEntry *mapASDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsAnodeSDD",run);
444   AliCDBEntry *mapTSDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsTimeSDD",run);
445   // AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", run);
446   AliCDBEntry *entryNoiseSSD = AliCDBManager::Instance()->Get("ITS/Calib/NoiseSSD");
447   AliCDBEntry *entryGainSSD = AliCDBManager::Instance()->Get("ITS/Calib/GainSSD");
448   AliCDBEntry *entryBadChannelsSSD = AliCDBManager::Instance()->Get("ITS/Calib/BadChannelsSSD");
449
450   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
451       !drSpSDD || !ddlMapSDD || !hltforSDD || !mapTSDD){
452     AliFatal("Calibration object retrieval failed! ");
453     return kFALSE;
454   }     
455         
456
457   TObjArray *calSPD = (TObjArray *)entrySPD->GetObject();
458   if(!isCacheActive)entrySPD->SetObject(NULL);
459   entrySPD->SetOwner(kTRUE);
460
461    
462   TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
463   if(!isCacheActive)entrySDD->SetObject(NULL);
464   entrySDD->SetOwner(kTRUE);
465
466   TObjArray *drSp = (TObjArray *)drSpSDD->GetObject();
467   if(!isCacheActive)drSpSDD->SetObject(NULL);
468   drSpSDD->SetOwner(kTRUE);
469
470   AliITSDDLModuleMapSDD *ddlsdd=(AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
471   if(!isCacheActive)ddlMapSDD->SetObject(NULL);
472   ddlMapSDD->SetOwner(kTRUE);
473
474   AliITSHLTforSDD* hltsdd=(AliITSHLTforSDD*)hltforSDD->GetObject();
475   if(!isCacheActive)hltforSDD->SetObject(NULL);
476   hltforSDD->SetOwner(kTRUE);
477
478 //   TObjArray *mapAn = (TObjArray *)mapASDD->GetObject();
479 //   if(!isCacheActive)mapASDD->SetObject(NULL);
480 //   mapASDD->SetOwner(kTRUE);
481
482   TObjArray *mapT = (TObjArray *)mapTSDD->GetObject();
483   if(!isCacheActive)mapTSDD->SetObject(NULL);
484   mapTSDD->SetOwner(kTRUE);
485
486   /*
487   TObjArray *calSSD = (TObjArray *)entrySSD->GetObject();
488   if(!isCacheActive)entrySSD->SetObject(NULL);
489   entrySSD->SetOwner(kTRUE);
490   */
491
492   TObject *emptyssd = 0; TString ssdobjectname = 0;
493   AliITSNoiseSSDv2 *noiseSSD = new AliITSNoiseSSDv2();  
494   emptyssd = (TObject *)entryNoiseSSD->GetObject();
495   ssdobjectname = emptyssd->GetName();
496   if(ssdobjectname=="TObjArray") {
497     TObjArray *noiseSSDOld = (TObjArray *)entryNoiseSSD->GetObject();
498     ReadOldSSDNoise(noiseSSDOld, noiseSSD);
499   }
500   else if(ssdobjectname=="AliITSNoiseSSDv2")
501     noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
502   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
503   entryNoiseSSD->SetOwner(kTRUE);
504
505   AliITSGainSSDv2 *gainSSD = new AliITSGainSSDv2();
506   emptyssd = (TObject *)entryGainSSD->GetObject();
507   ssdobjectname = emptyssd->GetName();
508   if(ssdobjectname=="Gain") {
509     TObjArray *gainSSDOld = (TObjArray *)entryGainSSD->GetObject();
510     ReadOldSSDGain(gainSSDOld, gainSSD);
511   }
512   else if(ssdobjectname=="AliITSGainSSDv2")
513     gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
514   if(!isCacheActive)entryGainSSD->SetObject(NULL);
515   entryGainSSD->SetOwner(kTRUE);
516
517   AliITSBadChannelsSSDv2 *badChannelsSSD = new AliITSBadChannelsSSDv2();
518   emptyssd = (TObject *)entryBadChannelsSSD->GetObject();
519   ssdobjectname = emptyssd->GetName();
520   if(ssdobjectname=="TObjArray") {
521     TObjArray *badChannelsSSDOld = (TObjArray *)entryBadChannelsSSD->GetObject();
522     ReadOldSSDBadChannels(badChannelsSSDOld, badChannelsSSD);
523   }
524   else if(ssdobjectname=="AliITSBadChannelsSSDv2")
525     badChannelsSSD = (AliITSBadChannelsSSDv2*)entryBadChannelsSSD->GetObject();
526   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
527   entryBadChannelsSSD->SetOwner(kTRUE);
528
529   /*AliITSNoiseSSDv2 *noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
530   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
531   entryNoiseSSD->SetOwner(kTRUE);
532
533   AliITSGainSSDv2 *gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
534   if(!isCacheActive)entryGainSSD->SetObject(NULL);
535   entryGainSSD->SetOwner(kTRUE);
536
537   AliITSBadChannelsSSDv2 *badchannelsSSD = 
538     (AliITSBadChannelsSSDv2 *)entryBadChannelsSSD->GetObject();
539   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
540   entryBadChannelsSSD->SetOwner(kTRUE);*/
541
542   // DB entries are deleted. In this way metadeta objects are deleted as well
543   if(!isCacheActive){
544     delete entrySPD;
545     delete entrySDD;
546     delete entryNoiseSSD;
547     delete entryGainSSD;
548     delete entryBadChannelsSSD;
549 //    delete mapASDD;   
550     delete hltforSDD;
551     delete mapTSDD;
552     delete drSpSDD;
553     delete ddlMapSDD;
554   }
555   
556   AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
557
558   if ((!calSPD) || (!calSDD) || (!drSp) || (!ddlsdd)
559       || (!hltsdd) || (!mapT) || (!noiseSSD)|| (!gainSSD)|| (!badChannelsSSD)) {
560     AliWarning("Can not get calibration from calibration database !");
561     return kFALSE;
562   }
563
564   fNMod[0] = calSPD->GetEntries();
565   fNMod[1] = calSDD->GetEntries();
566   //  fNMod[2] = noiseSSD->GetEntries();
567   AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
568                fNMod[0], fNMod[1], fNMod[2]));
569   AliITSCalibration* cal;
570   for (Int_t i=0; i<fNMod[0]; i++) {
571     cal = (AliITSCalibration*) calSPD->At(i);
572     SetCalibrationModel(i, cal);
573   }
574
575   fDDLMapSDD->SetDDLMap(ddlsdd);
576   fIsHLTmodeC=hltsdd->IsHLTmodeC();
577
578   for (Int_t i=0; i<fgkDefaultNModulesSDD; i++) {
579     Int_t iddl,icarlos;
580     Int_t iMod = i + fgkDefaultNModulesSPD;
581     fDDLMapSDD->FindInDDLMap(iMod,iddl,icarlos);
582     if(iddl<0){ 
583       AliITSCalibrationSDD* calsdddead=new AliITSCalibrationSDD();
584       calsdddead->SetBad();      
585       AliITSDriftSpeedSDD* driftspdef = new AliITSDriftSpeedSDD();
586       AliITSDriftSpeedArraySDD* arrdrsp=new AliITSDriftSpeedArraySDD(1);
587       arrdrsp->AddDriftSpeed(driftspdef);
588       calsdddead->SetDriftSpeed(0,arrdrsp);
589       calsdddead->SetDriftSpeed(1,arrdrsp);
590       SetCalibrationModel(iMod, calsdddead);
591       AliWarning(Form("SDD module %d not present in DDL map: set it as dead",iMod));
592     }else{
593       cal = (AliITSCalibration*) calSDD->At(i);
594       Int_t i0=2*i;
595       Int_t i1=1+2*i;
596       AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
597 //       AliITSMapSDD* ma0 = (AliITSMapSDD*)mapAn->At(i0);
598       AliITSMapSDD* mt0 = (AliITSMapSDD*)mapT->At(i0);
599       AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
600  //      AliITSMapSDD* ma1 = (AliITSMapSDD*)mapAn->At(i1);
601       AliITSMapSDD* mt1 = (AliITSMapSDD*)mapT->At(i1);
602       cal->SetDriftSpeed(0,arr0);
603       cal->SetDriftSpeed(1,arr1);
604 //       cal->SetMapA(0,ma0);
605 //       cal->SetMapA(1,ma1);
606       cal->SetMapT(0,mt0);
607       cal->SetMapT(1,mt1);
608       SetCalibrationModel(iMod, cal);
609     }
610   }
611
612   fSSDCalibration->SetNoise(noiseSSD);
613   fSSDCalibration->SetGain(gainSSD);
614   fSSDCalibration->SetBadChannels(badChannelsSSD);
615   //fSSDCalibration->FillBadChipMap();
616
617
618
619   return kTRUE;
620 }
621 //_______________________________________________________________________
622 void AliITSDetTypeSim::SetDefaultSimulation(){
623   //Set default simulation for detector type
624
625   if(GetITSgeom()==0){
626     Warning("SetDefaultSimulation","GetITSgeom() is 0!");
627     return;
628   }
629   if(fCalibration==0){
630     Warning("SetDefaultSimulation","fCalibration is 0!");
631     return;
632   }
633   if(fSegmentation==0){
634     Warning("SetDefaultSimulation","fSegmentation is 0!");
635     for(Int_t i=0;i<fgkNdettypes;i++) SetDefaultSegmentation(i);
636   }else for(Int_t i=0;i<fgkNdettypes;i++) if(!GetSegmentationModel(i)){
637       Warning("SetDefaultSimulation",
638               "Segmentation not defined for det %d - Default taken\n!",i);
639       SetDefaultSegmentation(i);
640   }
641   AliITSsimulation* sim;
642
643   for(Int_t idet=0;idet<fgkNdettypes;idet++){
644    //SPD
645     if(idet==0){
646       sim = GetSimulationModel(idet); 
647       if(!sim){
648         sim = new AliITSsimulationSPD(this);
649         SetSimulationModel(idet,sim);
650       }
651     }
652     //SDD
653     if(idet==1){
654       sim = GetSimulationModel(idet);
655       if(!sim){
656         sim = new AliITSsimulationSDD(this);
657         SetSimulationModel(idet,sim);
658       }      
659     }
660     //SSD
661     if(idet==2){
662       sim = GetSimulationModel(idet);
663       if(!sim){
664         sim = new AliITSsimulationSSD(this);
665         SetSimulationModel(idet,sim);
666       }
667     }
668
669   }
670 }
671 //___________________________________________________________________
672 void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, Char_t* name){
673   // Set branch address for the ITS summable digits Trees.  
674   char branchname[30];
675
676   if(!treeS){
677     return;
678   }
679   TBranch *branch;
680   sprintf(branchname,"%s",name);
681   branch = treeS->GetBranch(branchname);
682   TClonesArray *sdigi = &fSDigits;
683   if (branch) branch->SetAddress(&sdigi);
684
685 }
686 //___________________________________________________________________
687 void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
688   // Set branch address for the digit Trees.
689   
690   const char *det[3] = {"SPD","SDD","SSD"};
691   TBranch *branch;
692   
693   char branchname[30];
694   
695   if(!treeD){
696     return;
697   }
698   if(!fDigits){
699     fDigits = new TObjArray(fgkNdettypes); 
700   }
701   for(Int_t i=0;i<fgkNdettypes;i++){
702     Char_t* digclass = GetDigitClassName(i);
703     if(digclass==0x0){
704       if(i==0) SetDigitClassName(i,"AliITSdigitSPD");
705       if(i==1) SetDigitClassName(i,"AliITSdigitSDD");
706       if(i==2) SetDigitClassName(i,"AliITSdigitSSD");
707       digclass = GetDigitClassName(i);
708     }
709     TString classn = digclass;
710     if(!(fDigits->At(i))){
711       fDigits->AddAt(new TClonesArray(classn.Data(),1000),i);
712     }else{
713       ResetDigits(i);
714     }
715     
716     if(fgkNdettypes==3) sprintf(branchname,"%sDigits%s",name,det[i]);
717     else sprintf(branchname,"%sDigits%d",name,i+1);
718     if(fDigits){
719       branch = treeD->GetBranch(branchname);
720       if(branch) branch->SetAddress(&((*fDigits)[i]));
721     }
722   }
723
724 }
725 //___________________________________________________________________
726 void AliITSDetTypeSim::ResetDigits(){
727   // Reset number of digits and the digits array for the ITS detector.  
728
729   if(!fDigits){
730     Error("ResetDigits","fDigits is null!");
731     return;
732   }
733   for(Int_t i=0;i<fgkNdettypes;i++){
734     ResetDigits(i);
735   }
736 }
737 //___________________________________________________________________
738 void AliITSDetTypeSim::ResetDigits(Int_t branch){
739   // Reset number of digits and the digits array for this branch.
740
741   if(fDigits->At(branch)){
742     ((TClonesArray*)fDigits->At(branch))->Clear();
743   }
744   if(fNDigits) fNDigits[branch]=0;
745
746 }
747
748
749
750 //_______________________________________________________________________
751 void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
752   // Standard Summable digits to Digits function.
753   if(!GetITSgeom()){
754     Warning("SDigitsToDigits","GetITSgeom() is null!!");
755     return;
756   }
757   
758   const char *all = strstr(opt,"All");
759   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
760                         strstr(opt,"SSD")};
761   if( !det[0] && !det[1] && !det[2] ) all = "All";
762   else all = 0;
763   static Bool_t setDef = kTRUE;
764   if(setDef) SetDefaultSimulation();
765   setDef = kFALSE;
766   
767   AliITSsimulation *sim =0;
768   TTree* trees = fLoader->TreeS();
769   if( !(trees && GetSDigits()) ){
770     Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
771     return;
772   } 
773   sprintf(name,"%s",name);
774   TBranch* brchSDigits = trees->GetBranch(name);
775   
776   Int_t id;
777   for(Int_t module=0;module<GetITSgeom()->GetIndexMax();module++){
778      id = GetITSgeom()->GetModuleType(module);
779     if (!all && !det[id]) continue;
780     sim = (AliITSsimulation*)GetSimulationModel(id);
781     if(!sim){
782       Error("SDigit2Digits","The simulation class was not "
783             "instanciated for module %d type %s!",module,
784             GetITSgeom()->GetModuleTypeName(module));
785       exit(1);
786     }
787     sim->InitSimulationModule(module,gAlice->GetEvNumber());
788     
789     fSDigits.Clear();
790     brchSDigits->GetEvent(module);
791     sim->AddSDigitsToModule(&fSDigits,0);
792     sim->FinishSDigitiseModule();
793     fLoader->TreeD()->Fill();
794     ResetDigits();
795   }
796   fLoader->TreeD()->GetEntries();
797   fLoader->TreeD()->AutoSave();
798   fLoader->TreeD()->Reset();
799 }
800 //_________________________________________________________
801 void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){  
802   //Adds the module full of summable digits to the summable digits tree.
803
804   new(fSDigits[fNSDigits++]) AliITSpListItem(sdig);
805 }
806 //__________________________________________________________
807 void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){  
808   //    Add a simulated digit.
809
810   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
811   switch(branch){
812   case 0:
813     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
814     break;
815   case 1:
816     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
817     break;
818   case 2:
819     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
820     break;
821   }  
822 }
823 //______________________________________________________________________
824 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
825                                    Int_t *tracks,Int_t *hits,Float_t *charges, 
826                                    Int_t sigexpanded){
827   //   Add a simulated digit to the list.
828
829   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
830   switch(branch){
831   case 0:
832     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
833     break;
834   case 1:
835     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
836                                                    hits,charges,sigexpanded);
837     break;
838   case 2:
839     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(digits,tracks,hits);
840     break;
841   } 
842 }
843 //______________________________________________________________________
844 void AliITSDetTypeSim::StoreCalibration(Int_t firstRun, Int_t lastRun,
845                                         AliCDBMetaData &md) {
846   // Store calibration in the calibration database
847   // The database must be created in an external piece of code (i.e. 
848   // a configuration macro )
849
850   if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
851     AliWarning("No storage set! Will use dummy one");
852     AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
853   }
854
855   if (!fCalibration) {
856     AliError("AliITSCalibration classes are not defined - nothing done");
857     return;
858   }
859   AliCDBId idRespSPD("ITS/Calib/SPDDead",firstRun, lastRun);
860   AliCDBId idRespSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
861   AliCDBId idRespSSD("ITS/Calib/CalibSSD",firstRun, lastRun);
862
863   TObjArray respSPD(fNMod[0]);
864   TObjArray respSDD(fNMod[1]-fNMod[0]);
865   TObjArray respSSD(fNMod[2]-fNMod[1]);
866   respSPD.SetOwner(kFALSE);
867   respSSD.SetOwner(kFALSE);
868   respSSD.SetOwner(kFALSE);
869
870   Int_t index[fgkNdettypes];
871   for (Int_t i = 0; i<fgkNdettypes; i++ ) {
872     index[i] = 0;
873     for (Int_t j = 0; j<=i; j++ )
874       index[i]+=fNMod[j];
875   }
876
877   for (Int_t i = 0; i<index[0]; i++ )
878     respSPD.Add(fCalibration->At(i));
879
880   for (Int_t i = index[0]; i<index[1]; i++ )
881     respSDD.Add(fCalibration->At(i));
882
883   for (Int_t i = index[1]; i<index[2]; i++ )
884     respSSD.Add(fCalibration->At(i));
885
886   AliCDBManager::Instance()->Put(&respSPD, idRespSPD, &md);
887   AliCDBManager::Instance()->Put(&respSDD, idRespSDD, &md);
888   AliCDBManager::Instance()->Put(&respSSD, idRespSSD, &md);
889 }
890
891 //______________________________________________________________________
892 void AliITSDetTypeSim::ReadOldSSDNoise(TObjArray *array, 
893                                        AliITSNoiseSSDv2 *noiseSSD) {
894   //Reads the old SSD calibration object and converts it to the new format
895   const Int_t fgkSSDSTRIPSPERMODULE = 1536;
896   const Int_t fgkSSDPSIDESTRIPSPERMODULE = 768;
897
898   Int_t fNMod = array->GetEntries();
899   cout<<"Converting old calibration object for noise..."<<endl;
900
901   //NOISE
902   Double_t noise = 0.0;
903   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
904     AliITSNoiseSSD *noiseModule = (AliITSNoiseSSD*) (array->At(iModule));
905     for(Int_t iStrip = 0; iStrip < fgkSSDSTRIPSPERMODULE; iStrip++) {
906       noise = (iStrip < fgkSSDPSIDESTRIPSPERMODULE) ? noiseModule->GetNoiseP(iStrip) : noiseModule->GetNoiseN(1535 - iStrip);
907       if(iStrip < fgkSSDPSIDESTRIPSPERMODULE)
908         noiseSSD->AddNoiseP(iModule,iStrip,noise);
909       if(iStrip >= fgkSSDPSIDESTRIPSPERMODULE)
910         noiseSSD->AddNoiseN(iModule,1535 - iStrip,noise);
911     }//loop over strips
912   }//loop over modules      
913 }
914
915 //______________________________________________________________________
916 void AliITSDetTypeSim::ReadOldSSDBadChannels(TObjArray *array, 
917                                              AliITSBadChannelsSSDv2 *badChannelsSSD) {
918   //Reads the old SSD calibration object and converts it to the new format
919   Int_t fNMod = array->GetEntries();
920   cout<<"Converting old calibration object for bad channels..."<<endl;
921   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
922     //for (Int_t iModule = 0; iModule < 1; iModule++) {
923     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (array->At(iModule));
924     TArrayI arrayPSide = bad->GetBadPChannelsList();
925     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++) 
926       badChannelsSSD->AddBadChannelP(iModule,
927                                      iPCounter,
928                                      (Char_t)arrayPSide.At(iPCounter));
929         
930     TArrayI arrayNSide = bad->GetBadNChannelsList();
931     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++) 
932       badChannelsSSD->AddBadChannelN(iModule,
933                                      iNCounter,
934                                      (Char_t)arrayNSide.At(iNCounter));
935     
936   }//loop over modules      
937 }
938
939 //______________________________________________________________________
940 void AliITSDetTypeSim::ReadOldSSDGain(TObjArray *array, 
941                                       AliITSGainSSDv2 *gainSSD) {
942   //Reads the old SSD calibration object and converts it to the new format
943
944   Int_t fNMod = array->GetEntries();
945   cout<<"Converting old calibration object for gain..."<<endl;
946
947   //GAIN
948   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
949     AliITSGainSSD *gainModule = (AliITSGainSSD*) (array->At(iModule));
950     TArrayF arrayPSide = gainModule->GetGainP();
951     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++)
952       gainSSD->AddGainP(iModule,
953                         iPCounter,
954                         arrayPSide.At(iPCounter));
955     TArrayF arrayNSide = gainModule->GetGainN();
956     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++)
957       gainSSD->AddGainN(iModule,
958                         iNCounter,
959                         arrayNSide.At(iNCounter));
960   }//loop over modules 
961 }
962