]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeSim.cxx
adding arguments to AddTaskPhysicsSelection for MC and BG identification (Antonin)
[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 #include "AliITSdigit.h"
39 #include "AliITSdigitSPD.h"
40 #include "AliITSdigitSDD.h"
41 #include "AliITSdigitSSD.h"
42 #include "AliITSgeom.h"
43 #include "AliITSDetTypeSim.h"
44 #include "AliITSpListItem.h"
45 #include "AliITSCalibration.h"
46 #include "AliITSCalibrationSDD.h"
47 #include "AliITSMapSDD.h"
48 #include "AliITSCorrMapSDD.h"
49 #include "AliITSDriftSpeedArraySDD.h"
50 #include "AliITSDriftSpeedSDD.h"
51 #include "AliITSCalibrationSSD.h"
52 #include "AliITSNoiseSSDv2.h"
53 #include "AliITSGainSSDv2.h"
54 #include "AliITSBadChannelsSSDv2.h"
55 #include "AliITSNoiseSSD.h"
56 #include "AliITSGainSSD.h"
57 #include "AliITSBadChannelsSSD.h"
58 #include "AliITSCalibrationSSD.h"
59 #include "AliITSsegmentationSPD.h"
60 #include "AliITSsegmentationSDD.h"
61 #include "AliITSsegmentationSSD.h"
62 #include "AliITSsimulation.h"
63 #include "AliITSsimulationSPD.h"
64 #include "AliITSsimulationSDD.h"
65 #include "AliITSsimulationSSD.h"
66 #include "AliITSDDLModuleMapSDD.h"
67 #include "AliITSTriggerConditions.h"
68 #include "AliBaseLoader.h"
69
70 const Int_t AliITSDetTypeSim::fgkNdettypes = 3;
71 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSPD =  240;
72 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSDD =  260;
73 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSSD = 1698;
74
75 ClassImp(AliITSDetTypeSim)
76
77 //----------------------------------------------------------------------
78 AliITSDetTypeSim::AliITSDetTypeSim():
79 TObject(),
80 fSimulation(),   // [NDet]
81 fSegmentation(), // [NDet]
82 fCalibration(),     // [NMod]
83 fSSDCalibration(0),
84 fSPDNoisy(0),
85 fNSDigits(0),    //! number of SDigits
86 fSDigits("AliITSpListItem",1000),   
87 fNDigits(0),     //! number of Digits
88 fRunNumber(0),   //! Run number (to access DB)
89 fDigits(),       //! [NMod][NDigits]
90 fSimuPar(0),
91 fDDLMapSDD(0),
92 fkDigClassName(), // String with digit class name.
93 fLoader(0),      // local pointer to loader
94 fFirstcall(kTRUE),
95 fFOGenerator(),
96 fTriggerConditions(NULL)
97
98     // Default Constructor
99     // Inputs:
100     //    none.
101     // Outputs:
102     //    none.
103     // Return:
104     //    A properly zero-ed AliITSDetTypeSim class.
105
106   fSimulation = new TObjArray(fgkNdettypes);
107   fSegmentation = new TObjArray(fgkNdettypes);
108   fSegmentation->SetOwner(kTRUE);
109   fDigits = new TObjArray(fgkNdettypes);
110   fNDigits = new Int_t[fgkNdettypes];
111   fDDLMapSDD=new AliITSDDLModuleMapSDD();
112   fSimuPar= new AliITSSimuParam();
113   fSSDCalibration=new AliITSCalibrationSSD();
114   fNMod[0] = fgkDefaultNModulesSPD;
115   fNMod[1] = fgkDefaultNModulesSDD;
116   fNMod[2] = fgkDefaultNModulesSSD;
117   SetRunNumber();
118 }
119 //----------------------------------------------------------------------
120 AliITSDetTypeSim::~AliITSDetTypeSim(){
121     // Destructor
122     // Inputs:
123     //    none.
124     // Outputs:
125     //    none.
126     // Return:
127     //    Nothing.
128
129     if(fSimulation){
130         fSimulation->Delete();
131         delete fSimulation;
132     }
133     fSimulation = 0;
134     if(fSegmentation){
135         fSegmentation->Delete();
136         delete fSegmentation;
137     }
138     fSegmentation = 0;
139     if(fCalibration && fRunNumber<0){
140
141         fCalibration->Delete();
142         delete fCalibration;
143     }
144     fCalibration = 0;
145     if(fSSDCalibration) delete fSSDCalibration;
146     if(fSPDNoisy){
147     if(!(AliCDBManager::Instance()->GetCacheFlag())) {
148        fSPDNoisy->Delete();
149        delete fSPDNoisy;
150        fSPDNoisy = 0;
151       }
152     }
153     if(fSimuPar) delete fSimuPar;
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     fSDigits.Delete();
160     if (fDigits) {
161       fDigits->Delete();
162       delete fDigits;
163     }
164     fDigits=0;
165 }
166 //----------------------------------------------------------------------
167 AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source),
168 fSimulation(source.fSimulation),   // [NDet]
169 fSegmentation(source.fSegmentation), // [NDet]
170 fCalibration(source.fCalibration),     // [NMod]
171 fSSDCalibration(source.fSSDCalibration),
172 fSPDNoisy(source.fSPDNoisy),
173 fNSDigits(source.fNSDigits),    //! number of SDigits
174 fSDigits(*((TClonesArray*)source.fSDigits.Clone())),
175 fNDigits(source.fNDigits),     //! number of Digits
176 fRunNumber(source.fRunNumber),   //! Run number (to access DB)
177 fDigits(source.fDigits),       //! [NMod][NDigits]
178 fSimuPar(source.fSimuPar),
179 fDDLMapSDD(source.fDDLMapSDD),
180 fkDigClassName(), // String with digit class name.
181 fLoader(source.fLoader),      // local pointer to loader
182 fFirstcall(source.fFirstcall),
183 fFOGenerator(source.fFOGenerator),
184 fTriggerConditions(source.fTriggerConditions) 
185 {
186     // Copy Constructor for object AliITSDetTypeSim not allowed
187   for(Int_t i=0;i<fgkNdettypes;i++){
188     fkDigClassName[i] = source.fkDigClassName[i];
189   }
190 }
191 //----------------------------------------------------------------------
192 AliITSDetTypeSim& AliITSDetTypeSim::operator=(const AliITSDetTypeSim &source){
193     // The = operator for object AliITSDetTypeSim
194  
195   this->~AliITSDetTypeSim();
196   new(this) AliITSDetTypeSim(source);
197   return *this;
198 }
199
200 //______________________________________________________________________
201 void AliITSDetTypeSim::SetITSgeom(AliITSgeom *geom){
202     // Sets/replaces the existing AliITSgeom object kept in AliITSLoader
203     // 
204     // Inputs:
205     //   AliITSgoem   *geom  The AliITSgeom object to be used.
206     // Output:
207     //   none.
208     // Return:
209     //   none.
210   if(!fLoader){
211     Error("SetITSgeom","No pointer to loader - nothing done");
212     return;
213   }
214   else {
215     fLoader->SetITSgeom(geom);  // protections in AliITSLoader::SetITSgeom
216   }
217  
218 }
219 //______________________________________________________________________
220 void AliITSDetTypeSim::SetLoader(AliITSLoader *loader){
221     // Sets the local copy of the AliITSLoader, and passes on the
222     // AliITSgeom object as needed.
223     // Inputs
224     //   AliITSLoader  *loader pointer to AliITSLoader for local use
225     // Outputs:
226     //   none.
227     // Return:
228     //  none.
229
230     if(fLoader==loader) return; // Same do nothing
231     if(fLoader){ // alread have an existing loader
232         Error("SetLoader",
233                 "Already have an exisiting loader ptr=%p Nothing done",
234                 fLoader);
235     } // end if
236     fLoader = loader;
237 }
238 //______________________________________________________________________
239 void AliITSDetTypeSim::SetSimulationModel(Int_t dettype,AliITSsimulation *sim){
240
241   //Set simulation model for detector type
242
243   if(fSimulation==0) fSimulation = new TObjArray(fgkNdettypes);
244   fSimulation->AddAt(sim,dettype);
245 }
246 //______________________________________________________________________
247 AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype) const { 
248
249   //Get simulation model for detector type
250   if(fSimulation==0)  {
251     Warning("GetSimulationModel","fSimulation is 0!");
252     return 0;     
253   }
254   return (AliITSsimulation*)(fSimulation->At(dettype));
255 }
256 //______________________________________________________________________
257 AliITSsimulation* AliITSDetTypeSim::GetSimulationModelByModule(Int_t module) const {
258
259   //Get simulation model by module number
260   if(GetITSgeom()==0) {
261     Warning("GetSimulationModelByModule","GetITSgeom() is 0!");
262     return 0;
263   }
264   
265   return GetSimulationModel(GetITSgeom()->GetModuleType(module));
266 }
267 //_______________________________________________________________________
268 void AliITSDetTypeSim::SetDefaultSegmentation(Int_t idet){
269     // Set default segmentation model objects
270     AliITSsegmentation *seg;
271
272     if(fSegmentation==0x0){
273         fSegmentation = new TObjArray(fgkNdettypes);
274         fSegmentation->SetOwner(kTRUE);
275     }
276     if(GetSegmentationModel(idet))
277         delete (AliITSsegmentation*)fSegmentation->At(idet);
278     if(idet==0){
279         seg = new AliITSsegmentationSPD();
280     }else if(idet==1){
281       seg = new AliITSsegmentationSDD();
282       AliITSCalibrationSDD* cal=(AliITSCalibrationSDD*)GetCalibrationModel(fgkDefaultNModulesSPD+1);
283       if(cal->IsAMAt20MHz()){ 
284         seg->SetPadSize(seg->Dpz(0),20.);
285         seg->SetNPads(seg->Npz()/2,128);
286       }
287     }else {
288         seg = new AliITSsegmentationSSD();
289     }
290     SetSegmentationModel(idet,seg);
291 }
292 //______________________________________________________________________
293 void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,
294                                             AliITSsegmentation *seg){
295    
296   //Set segmentation model for detector type
297   if(fSegmentation==0x0){
298     fSegmentation = new TObjArray(fgkNdettypes);
299     fSegmentation->SetOwner(kTRUE);
300   }
301   fSegmentation->AddAt(seg,dettype);
302 }
303 //______________________________________________________________________
304 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype) const{
305   //Get segmentation model for detector type
306    
307    if(fSegmentation==0) {
308        Warning("GetSegmentationModel","fSegmentation is 0!");
309        return 0; 
310    } 
311    return (AliITSsegmentation*)(fSegmentation->At(dettype));
312 }
313 //_______________________________________________________________________
314 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module) const{
315     //Get segmentation model by module number
316     if(GetITSgeom()==0){
317         Warning("GetSegmentationModelByModule","GetITSgeom() is 0!");
318         return 0;
319     }     
320     return GetSegmentationModel(GetITSgeom()->GetModuleType(module));
321 }
322 //_______________________________________________________________________
323 void AliITSDetTypeSim::CreateCalibrationArray() {
324     //Create the container of calibration functions with correct size
325     if (fCalibration) {
326         Warning("CreateCalibration","pointer to calibration object exists\n");
327         fCalibration->Delete();
328         delete fCalibration;
329     }
330
331     Int_t nModTot = GetITSgeom()->GetIndexMax();
332     fCalibration = new TObjArray(nModTot);
333     fCalibration->SetOwner(kTRUE);
334     fCalibration->Clear();
335 }
336 //_______________________________________________________________________
337 void AliITSDetTypeSim::SetCalibrationModel(Int_t iMod, AliITSCalibration *resp){
338     //Set response model for modules
339
340     if (fCalibration==0) CreateCalibrationArray();
341  
342     if (fCalibration->At(iMod)!=0)
343         delete (AliITSCalibration*) fCalibration->At(iMod);
344     fCalibration->AddAt(resp, iMod);
345 }
346 //_______________________________________________________________________
347 void AliITSDetTypeSim::SetSPDNoisyModel(Int_t iMod, AliITSCalibration *cal){
348   //Set noisy pixel info for the SPD module iMod
349   if (fSPDNoisy==0) {
350     fSPDNoisy = new TObjArray(fgkDefaultNModulesSPD);
351     fSPDNoisy->SetOwner(kTRUE);
352     fSPDNoisy->Clear();
353   }
354
355   if (fSPDNoisy->At(iMod) != 0)
356     delete (AliITSCalibration*) fSPDNoisy->At(iMod);
357   fSPDNoisy->AddAt(cal,iMod);
358 }
359 //______________________________________________________________________
360 void AliITSDetTypeSim::ResetCalibrationArray(){
361     //resets response array
362     if(fCalibration && fRunNumber<0){  // if fRunNumber<0 fCalibration is owner
363       /*
364         AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
365                                 GetITSgeom()->GetStartSPD()))->GetResponse();
366         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
367                                 GetITSgeom()->GetStartSSD()))->GetResponse();
368         if(rspd) delete rspd;
369         if(rssd) delete rssd;
370       */
371         fCalibration->Clear();
372     }else if (fCalibration && fRunNumber>=0){
373         fCalibration->Clear();
374     }
375 }
376 //______________________________________________________________________
377 void AliITSDetTypeSim::ResetSegmentation(){
378     //Resets segmentation array
379     if(fSegmentation) fSegmentation->Clear();
380 }
381 //_______________________________________________________________________
382 AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod) const {
383     //Get response model for module number iMod 
384  
385     if(fCalibration==0) {
386         AliError("fCalibration is 0!");
387         return 0; 
388     }
389   if(iMod<fgkDefaultNModulesSPD+fgkDefaultNModulesSDD){
390     return (AliITSCalibration*)fCalibration->At(iMod);
391   }else{
392     Int_t i=iMod-(fgkDefaultNModulesSPD+fgkDefaultNModulesSDD);
393     fSSDCalibration->SetModule(i);
394     return (AliITSCalibration*)fSSDCalibration;
395   }
396
397 }
398 //_______________________________________________________________________
399 AliITSCalibration* AliITSDetTypeSim::GetSPDNoisyModel(Int_t iMod) const {
400   //Get SPD noisy calib for module iMod 
401   if(fSPDNoisy==0) {
402     AliWarning("fSPDNoisy is 0!");
403     return 0; 
404   }
405   return (AliITSCalibration*)fSPDNoisy->At(iMod);
406 }
407 //_______________________________________________________________________
408 void AliITSDetTypeSim::SetDefaults(){
409     //Set defaults for segmentation and response
410
411     if(GetITSgeom()==0){
412         Warning("SetDefaults","GetITSgeom() is 0!");
413         return;
414     } // end if
415     if (fCalibration==0) {
416         CreateCalibrationArray();
417     } // end if
418
419     ResetSegmentation();
420     if(!GetCalibration()){AliFatal("Exit"); exit(0);}
421
422     SetDigitClassName(0,"AliITSdigitSPD");
423     SetDigitClassName(1,"AliITSdigitSDD");
424     SetDigitClassName(2,"AliITSdigitSSD");
425
426     for(Int_t idet=0;idet<fgkNdettypes;idet++){
427       if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
428     }
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 *deadSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDDead", run);
458   AliCDBEntry *noisySPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDNoisy", run);
459   AliCDBEntry *foEffSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDFOEfficiency", run);
460   AliCDBEntry *foNoiSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDFONoise", run);
461   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", run);
462   AliCDBEntry *drSpSDD = AliCDBManager::Instance()->Get("ITS/Calib/DriftSpeedSDD",run);
463   AliCDBEntry *ddlMapSDD = AliCDBManager::Instance()->Get("ITS/Calib/DDLMapSDD",run);
464   //AliCDBEntry *mapASDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsAnodeSDD",run);
465   AliCDBEntry *mapTSDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsTimeSDD",run);
466   // AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", run);
467   AliCDBEntry *entryNoiseSSD = AliCDBManager::Instance()->Get("ITS/Calib/NoiseSSD");
468   AliCDBEntry *entryGainSSD = AliCDBManager::Instance()->Get("ITS/Calib/GainSSD");
469   AliCDBEntry *entryBadChannelsSSD = AliCDBManager::Instance()->Get("ITS/Calib/BadChannelsSSD");
470
471 if(!deadSPD || !noisySPD || !foEffSPD || !foNoiSPD 
472      || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD 
473      || !drSpSDD || !ddlMapSDD || !mapTSDD){
474     AliFatal("Calibration object retrieval failed! ");
475     return kFALSE;
476   }             
477         
478
479   TObjArray *calDeadSPD = (TObjArray*) deadSPD->GetObject();
480   if (!isCacheActive) deadSPD->SetObject(NULL);
481   deadSPD->SetOwner(kTRUE);
482
483   TObjArray *calNoisySPD = (TObjArray*) noisySPD->GetObject();
484   if (!isCacheActive) noisySPD->SetObject(NULL);
485   noisySPD->SetOwner(kTRUE);
486
487   AliITSFOEfficiencySPD *calFoEffSPD = (AliITSFOEfficiencySPD*) foEffSPD->GetObject();
488   if (!isCacheActive) foEffSPD->SetObject(NULL);
489   foEffSPD->SetOwner(kTRUE);
490
491   AliITSFONoiseSPD *calFoNoiSPD = (AliITSFONoiseSPD*) foNoiSPD->GetObject();
492   if (!isCacheActive) foNoiSPD->SetObject(NULL);
493   foNoiSPD->SetOwner(kTRUE);
494    
495   TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
496   if(!isCacheActive)entrySDD->SetObject(NULL);
497   entrySDD->SetOwner(kTRUE);
498
499   TObjArray *drSp = (TObjArray *)drSpSDD->GetObject();
500   if(!isCacheActive)drSpSDD->SetObject(NULL);
501   drSpSDD->SetOwner(kTRUE);
502
503   AliITSDDLModuleMapSDD *ddlsdd=(AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
504   if(!isCacheActive)ddlMapSDD->SetObject(NULL);
505   ddlMapSDD->SetOwner(kTRUE);
506
507 //   TObjArray *mapAn = (TObjArray *)mapASDD->GetObject();
508 //   if(!isCacheActive)mapASDD->SetObject(NULL);
509 //   mapASDD->SetOwner(kTRUE);
510
511   TObjArray *mapT = (TObjArray *)mapTSDD->GetObject();
512   if(!isCacheActive)mapTSDD->SetObject(NULL);
513   mapTSDD->SetOwner(kTRUE);
514
515   /*
516   TObjArray *calSSD = (TObjArray *)entrySSD->GetObject();
517   if(!isCacheActive)entrySSD->SetObject(NULL);
518   entrySSD->SetOwner(kTRUE);
519   */
520
521   TObject *emptyssd = 0; TString ssdobjectname;
522   AliITSNoiseSSDv2 *noiseSSD = new AliITSNoiseSSDv2();  
523   emptyssd = (TObject *)entryNoiseSSD->GetObject();
524   ssdobjectname = emptyssd->GetName();
525   if(ssdobjectname=="TObjArray") {
526     TObjArray *noiseSSDOld = (TObjArray *)entryNoiseSSD->GetObject();
527     ReadOldSSDNoise(noiseSSDOld, noiseSSD);
528   }
529   else if(ssdobjectname=="AliITSNoiseSSDv2")
530     noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
531   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
532   entryNoiseSSD->SetOwner(kTRUE);
533
534   AliITSGainSSDv2 *gainSSD = new AliITSGainSSDv2();
535   emptyssd = (TObject *)entryGainSSD->GetObject();
536   ssdobjectname = emptyssd->GetName();
537   if(ssdobjectname=="Gain") {
538     TObjArray *gainSSDOld = (TObjArray *)entryGainSSD->GetObject();
539     ReadOldSSDGain(gainSSDOld, gainSSD);
540   }
541   else if(ssdobjectname=="AliITSGainSSDv2")
542     gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
543   if(!isCacheActive)entryGainSSD->SetObject(NULL);
544   entryGainSSD->SetOwner(kTRUE);
545
546   AliITSBadChannelsSSDv2 *badChannelsSSD = new AliITSBadChannelsSSDv2();
547   emptyssd = (TObject *)entryBadChannelsSSD->GetObject();
548   ssdobjectname = emptyssd->GetName();
549   if(ssdobjectname=="TObjArray") {
550     TObjArray *badChannelsSSDOld = (TObjArray *)entryBadChannelsSSD->GetObject();
551     ReadOldSSDBadChannels(badChannelsSSDOld, badChannelsSSD);
552   }
553   else if(ssdobjectname=="AliITSBadChannelsSSDv2")
554     badChannelsSSD = (AliITSBadChannelsSSDv2*)entryBadChannelsSSD->GetObject();
555   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
556   entryBadChannelsSSD->SetOwner(kTRUE);
557
558   /*AliITSNoiseSSDv2 *noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
559   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
560   entryNoiseSSD->SetOwner(kTRUE);
561
562   AliITSGainSSDv2 *gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
563   if(!isCacheActive)entryGainSSD->SetObject(NULL);
564   entryGainSSD->SetOwner(kTRUE);
565
566   AliITSBadChannelsSSDv2 *badchannelsSSD = 
567     (AliITSBadChannelsSSDv2 *)entryBadChannelsSSD->GetObject();
568   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
569   entryBadChannelsSSD->SetOwner(kTRUE);*/
570
571   // DB entries are deleted. In this way metadeta objects are deleted as well
572   if(!isCacheActive){
573     delete deadSPD;
574     delete noisySPD;
575     delete foEffSPD;
576     delete foNoiSPD;
577     delete entrySDD;
578     delete entryNoiseSSD;
579     delete entryGainSSD;
580     delete entryBadChannelsSSD;
581 //    delete mapASDD;   
582     delete mapTSDD;
583     delete drSpSDD;
584     delete ddlMapSDD;
585   }
586   
587   AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
588
589  if ((!calDeadSPD) || (!calNoisySPD) || (!calFoEffSPD) || (!calFoNoiSPD) 
590       || (!calSDD) || (!drSp) || (!ddlsdd)  
591       || (!mapT) || (!noiseSSD)|| (!gainSSD)|| (!badChannelsSSD)) {
592     AliWarning("Can not get calibration from calibration database !");
593     return kFALSE;
594   }
595
596
597   fNMod[0] = calDeadSPD->GetEntries();
598   fNMod[1] = calSDD->GetEntries();
599   //  fNMod[2] = noiseSSD->GetEntries();
600   AliDebug(1,Form("%i SPD, %i SDD and %i SSD in calibration database",
601                fNMod[0], fNMod[1], fNMod[2]));
602   AliITSCalibration* cal;
603   for (Int_t i=0; i<fNMod[0]; i++) {
604     cal = (AliITSCalibration*) calDeadSPD->At(i);
605     SetCalibrationModel(i, cal);
606     cal = (AliITSCalibration*) calNoisySPD->At(i);
607     SetSPDNoisyModel(i, cal);
608   }
609
610   fFOGenerator.SetEfficiency(calFoEffSPD); // this cal object is used only by the generator
611   fFOGenerator.SetNoise(calFoNoiSPD); // this cal object is used only by the generator
612   
613   fDDLMapSDD->SetDDLMap(ddlsdd);
614   Bool_t oldMapFormat=kFALSE;
615   TObject* objmap=(TObject*)mapT->At(0);
616   TString cname(objmap->ClassName());
617   if(cname.CompareTo("AliITSMapSDD")==0){ 
618     oldMapFormat=kTRUE;
619     AliInfo("SDD Maps converted to new format");
620   }
621
622   for (Int_t i=0; i<fgkDefaultNModulesSDD; i++) {
623     Int_t iddl,icarlos;
624     Int_t iMod = i + fgkDefaultNModulesSPD;
625     fDDLMapSDD->FindInDDLMap(iMod,iddl,icarlos);
626     if(iddl<0){ 
627       AliITSCalibrationSDD* calsdddead=new AliITSCalibrationSDD();
628       calsdddead->SetBad();      
629       AliITSDriftSpeedSDD* driftspdef = new AliITSDriftSpeedSDD();
630       AliITSDriftSpeedArraySDD* arrdrsp=new AliITSDriftSpeedArraySDD(1);
631       arrdrsp->AddDriftSpeed(driftspdef);
632       calsdddead->SetDriftSpeed(0,arrdrsp);
633       calsdddead->SetDriftSpeed(1,arrdrsp);
634       SetCalibrationModel(iMod, calsdddead);
635       AliWarning(Form("SDD module %d not present in DDL map: set it as dead",iMod));
636     }else{
637       cal = (AliITSCalibration*) calSDD->At(i);
638       Int_t i0=2*i;
639       Int_t i1=1+2*i;
640       AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
641       AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
642
643       AliITSCorrMapSDD* mt0 = 0;
644       AliITSCorrMapSDD* mt1 = 0;
645       if(oldMapFormat){ 
646         AliITSMapSDD* oldmap0=(AliITSMapSDD*)mapT->At(i0);
647         AliITSMapSDD* oldmap1=(AliITSMapSDD*)mapT->At(i1);
648         mt0=oldmap0->ConvertToNewFormat();
649         mt1=oldmap1->ConvertToNewFormat();
650       }else{
651         mt0=(AliITSCorrMapSDD*)mapT->At(i0);
652         mt1=(AliITSCorrMapSDD*)mapT->At(i1);
653       }
654       cal->SetDriftSpeed(0,arr0);
655       cal->SetDriftSpeed(1,arr1);
656       cal->SetMapT(0,mt0);
657       cal->SetMapT(1,mt1);
658       SetCalibrationModel(iMod, cal);
659     }
660   }
661
662   fSSDCalibration->SetNoise(noiseSSD);
663   fSSDCalibration->SetGain(gainSSD);
664   fSSDCalibration->SetBadChannels(badChannelsSSD);
665   //fSSDCalibration->FillBadChipMap();
666
667
668
669   return kTRUE;
670 }
671 //_______________________________________________________________________
672 void AliITSDetTypeSim::SetDefaultSimulation(){
673   //Set default simulation for detector type
674
675   if(GetITSgeom()==0){
676     Warning("SetDefaultSimulation","GetITSgeom() is 0!");
677     return;
678   }
679   if(fCalibration==0){
680     Warning("SetDefaultSimulation","fCalibration is 0!");
681     return;
682   }
683   if(fSegmentation==0){
684     Warning("SetDefaultSimulation","fSegmentation is 0!");
685     for(Int_t i=0;i<fgkNdettypes;i++) SetDefaultSegmentation(i);
686   }else for(Int_t i=0;i<fgkNdettypes;i++) if(!GetSegmentationModel(i)){
687       Warning("SetDefaultSimulation",
688               "Segmentation not defined for det %d - Default taken\n!",i);
689       SetDefaultSegmentation(i);
690   }
691   AliITSsimulation* sim;
692
693   for(Int_t idet=0;idet<fgkNdettypes;idet++){
694    //SPD
695     if(idet==0){
696       sim = GetSimulationModel(idet); 
697       if(!sim){
698         sim = new AliITSsimulationSPD(this);
699         SetSimulationModel(idet,sim);
700       }
701     }
702     //SDD
703     if(idet==1){
704       sim = GetSimulationModel(idet);
705       if(!sim){
706         sim = new AliITSsimulationSDD(this);
707         SetSimulationModel(idet,sim);
708       }      
709     }
710     //SSD
711     if(idet==2){
712       sim = GetSimulationModel(idet);
713       if(!sim){
714         sim = new AliITSsimulationSSD(this);
715         SetSimulationModel(idet,sim);
716       }
717     }
718
719   }
720 }
721 //___________________________________________________________________
722 void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, const Char_t* name){
723   // Set branch address for the ITS summable digits Trees.  
724   char branchname[30];
725
726   if(!treeS){
727     return;
728   }
729   TBranch *branch;
730   sprintf(branchname,"%s",name);
731   branch = treeS->GetBranch(branchname);
732   TClonesArray *sdigi = &fSDigits;
733   if (branch) branch->SetAddress(&sdigi);
734
735 }
736 //___________________________________________________________________
737 void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, const Char_t* name){
738   // Set branch address for the digit Trees.
739   
740   const char *det[3] = {"SPD","SDD","SSD"};
741   TBranch *branch;
742   
743   char branchname[30];
744   
745   if(!treeD){
746     return;
747   }
748   if(!fDigits){
749     fDigits = new TObjArray(fgkNdettypes); 
750   }
751   for(Int_t i=0;i<fgkNdettypes;i++){
752     const Char_t* digclass = GetDigitClassName(i);
753     if(digclass==0x0){
754       if(i==0) SetDigitClassName(i,"AliITSdigitSPD");
755       if(i==1) SetDigitClassName(i,"AliITSdigitSDD");
756       if(i==2) SetDigitClassName(i,"AliITSdigitSSD");
757       digclass = GetDigitClassName(i);
758     }
759     TString classn = digclass;
760     if(!(fDigits->At(i))){
761       fDigits->AddAt(new TClonesArray(classn.Data(),1000),i);
762     }else{
763       ResetDigits(i);
764     }
765     
766     if(fgkNdettypes==3) sprintf(branchname,"%sDigits%s",name,det[i]);
767     else sprintf(branchname,"%sDigits%d",name,i+1);
768     if(fDigits){
769       branch = treeD->GetBranch(branchname);
770       if(branch) branch->SetAddress(&((*fDigits)[i]));
771     }
772   }
773
774 }
775 //___________________________________________________________________
776 void AliITSDetTypeSim::ResetDigits(){
777   // Reset number of digits and the digits array for the ITS detector.  
778
779   if(!fDigits){
780     Error("ResetDigits","fDigits is null!");
781     return;
782   }
783   for(Int_t i=0;i<fgkNdettypes;i++){
784     ResetDigits(i);
785   }
786 }
787 //___________________________________________________________________
788 void AliITSDetTypeSim::ResetDigits(Int_t branch){
789   // Reset number of digits and the digits array for this branch.
790
791   if(fDigits->At(branch)){
792     ((TClonesArray*)fDigits->At(branch))->Clear();
793   }
794   if(fNDigits) fNDigits[branch]=0;
795
796 }
797
798
799
800 //_______________________________________________________________________
801 void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
802   // Standard Summable digits to Digits function.
803   if(!GetITSgeom()){
804     Warning("SDigitsToDigits","GetITSgeom() is null!!");
805     return;
806   }
807   
808   const char *all = strstr(opt,"All");
809   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
810                         strstr(opt,"SSD")};
811   if( !det[0] && !det[1] && !det[2] ) all = "All";
812   else all = 0;
813   static Bool_t setDef = kTRUE;
814   if(setDef) SetDefaultSimulation();
815   setDef = kFALSE;
816   
817   AliITSsimulation *sim =0;
818   TTree* trees = fLoader->TreeS();
819   if( !(trees && GetSDigits()) ){
820     Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
821     return;
822   } 
823   sprintf(name,"%s",name);
824   TBranch* brchSDigits = trees->GetBranch(name);
825   
826   Int_t id;
827   for(Int_t module=0;module<GetITSgeom()->GetIndexMax();module++){
828      id = GetITSgeom()->GetModuleType(module);
829     if (!all && !det[id]) continue;
830     sim = (AliITSsimulation*)GetSimulationModel(id);
831     if(!sim){
832       Error("SDigit2Digits","The simulation class was not "
833             "instanciated for module %d type %s!",module,
834             GetITSgeom()->GetModuleTypeName(module));
835       exit(1);
836     }
837     sim->InitSimulationModule(module,gAlice->GetEvNumber());
838     
839     fSDigits.Clear();
840     brchSDigits->GetEvent(module);
841     sim->AddSDigitsToModule(&fSDigits,0);
842     sim->FinishSDigitiseModule();
843     fLoader->TreeD()->Fill();
844     ResetDigits();
845   }
846   fLoader->TreeD()->GetEntries();
847   fLoader->TreeD()->AutoSave();
848   fLoader->TreeD()->Reset();
849 }
850 //_________________________________________________________
851 void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){  
852   //Adds the module full of summable digits to the summable digits tree.
853
854   new(fSDigits[fNSDigits++]) AliITSpListItem(sdig);
855 }
856 //__________________________________________________________
857 void AliITSDetTypeSim::AddSimDigit(Int_t branch, const AliITSdigit* d){  
858   //    Add a simulated digit.
859
860   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
861   switch(branch){
862   case 0:
863     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
864     break;
865   case 1:
866     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
867     break;
868   case 2:
869     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
870     break;
871   }  
872 }
873 //______________________________________________________________________
874 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
875                                    Int_t *tracks,Int_t *hits,Float_t *charges, 
876                                    Int_t sigexpanded){
877   //   Add a simulated digit to the list.
878
879   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
880   switch(branch){
881   case 0:
882     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
883     break;
884   case 1:
885     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
886                                                    hits,charges,sigexpanded);
887     break;
888   case 2:
889     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(digits,tracks,hits);
890     break;
891   } 
892 }
893 //______________________________________________________________________
894 void AliITSDetTypeSim::ReadOldSSDNoise(const TObjArray *array, 
895                                        AliITSNoiseSSDv2 *noiseSSD) {
896   //Reads the old SSD calibration object and converts it to the new format
897   const Int_t fgkSSDSTRIPSPERMODULE = 1536;
898   const Int_t fgkSSDPSIDESTRIPSPERMODULE = 768;
899
900   Int_t gNMod = array->GetEntries();
901   cout<<"Converting old calibration object for noise..."<<endl;
902
903   //NOISE
904   Double_t noise = 0.0;
905   for (Int_t iModule = 0; iModule < gNMod; iModule++) {
906     AliITSNoiseSSD *noiseModule = (AliITSNoiseSSD*) (array->At(iModule));
907     for(Int_t iStrip = 0; iStrip < fgkSSDSTRIPSPERMODULE; iStrip++) {
908       noise = (iStrip < fgkSSDPSIDESTRIPSPERMODULE) ? noiseModule->GetNoiseP(iStrip) : noiseModule->GetNoiseN(1535 - iStrip);
909       if(iStrip < fgkSSDPSIDESTRIPSPERMODULE)
910         noiseSSD->AddNoiseP(iModule,iStrip,noise);
911       if(iStrip >= fgkSSDPSIDESTRIPSPERMODULE)
912         noiseSSD->AddNoiseN(iModule,1535 - iStrip,noise);
913     }//loop over strips
914   }//loop over modules      
915 }
916
917 //______________________________________________________________________
918 void AliITSDetTypeSim::ReadOldSSDBadChannels(const TObjArray *array, 
919                                              AliITSBadChannelsSSDv2 *badChannelsSSD) {
920   //Reads the old SSD calibration object and converts it to the new format
921   Int_t nMod = array->GetEntries();
922   cout<<"Converting old calibration object for bad channels..."<<endl;
923   for (Int_t iModule = 0; iModule < nMod; iModule++) {
924     //for (Int_t iModule = 0; iModule < 1; iModule++) {
925     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (array->At(iModule));
926     TArrayI arrayPSide = bad->GetBadPChannelsList();
927     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++) 
928       badChannelsSSD->AddBadChannelP(iModule,
929                                      iPCounter,
930                                      (Char_t)arrayPSide.At(iPCounter));
931         
932     TArrayI arrayNSide = bad->GetBadNChannelsList();
933     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++) 
934       badChannelsSSD->AddBadChannelN(iModule,
935                                      iNCounter,
936                                      (Char_t)arrayNSide.At(iNCounter));
937     
938   }//loop over modules      
939 }
940
941 //______________________________________________________________________
942 void AliITSDetTypeSim::ReadOldSSDGain(const TObjArray *array, 
943                                       AliITSGainSSDv2 *gainSSD) {
944   //Reads the old SSD calibration object and converts it to the new format
945
946   Int_t nMod = array->GetEntries();
947   cout<<"Converting old calibration object for gain..."<<endl;
948
949   //GAIN
950   for (Int_t iModule = 0; iModule < nMod; iModule++) {
951     AliITSGainSSD *gainModule = (AliITSGainSSD*) (array->At(iModule));
952     TArrayF arrayPSide = gainModule->GetGainP();
953     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++)
954       gainSSD->AddGainP(iModule,
955                         iPCounter,
956                         arrayPSide.At(iPCounter));
957     TArrayF arrayNSide = gainModule->GetGainN();
958     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++)
959       gainSSD->AddGainN(iModule,
960                         iNCounter,
961                         arrayNSide.At(iNCounter));
962   }//loop over modules 
963 }
964 //______________________________________________________________________
965 void AliITSDetTypeSim::ProcessSPDDigitForFastOr(UInt_t module, UInt_t colM, UInt_t rowM) {
966   // Processes wether a single fired pixel will give rise to a fast-or signal
967   fFOGenerator.ProcessPixelHitM(module,colM,rowM);
968 }
969 //_______________________________________________________________________
970 AliITSTriggerConditions* AliITSDetTypeSim::GetTriggerConditions() {
971   // Get Pixel Trigger Conditions (separate method since it is used only when simulating trigger)
972   if (fTriggerConditions==NULL) { // read from db
973     fRunNumber = ((Int_t)AliCDBManager::Instance()->GetRun());
974     Bool_t origCacheStatus = AliCDBManager::Instance()->GetCacheFlag();
975     Bool_t isCacheActive;
976     if (fRunNumber<0) isCacheActive=kFALSE;
977     else              isCacheActive=kTRUE;
978     AliCDBManager::Instance()->SetCacheFlag(isCacheActive);
979     AliCDBEntry *pitCond = AliCDBManager::Instance()->Get("TRIGGER/SPD/PITConditions", fRunNumber);
980     if (!pitCond) {
981       AliError("Trigger conditions retrieval failed! ");
982       return NULL;
983     }
984     fTriggerConditions = (AliITSTriggerConditions*) pitCond->GetObject();
985     if (!isCacheActive) pitCond->SetObject(NULL);
986     pitCond->SetOwner(kTRUE);
987     if (!isCacheActive) {
988       delete pitCond;
989     }
990     AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
991     if (fTriggerConditions==NULL) {
992       AliWarning("fTriggerConditions is NULL!");
993     }
994   }
995   return fTriggerConditions;
996 }
997 //_______________________________________________________________________
998 void AliITSDetTypeSim::WriteFOSignals() {
999   // write fo signals to event
1000
1001   if (!fLoader) {
1002     AliError("ITS loader is NULL.");
1003     return;
1004   }
1005
1006   if(!fLoader->TreeD()){
1007    AliError("No TreeD available");
1008    return;
1009   }
1010   TTree *tree = fLoader->TreeD();
1011   AliITSFOSignalsSPD *foSignals = new AliITSFOSignalsSPD(*GetFOSignals()); 
1012   tree->GetUserInfo()->Add(foSignals);
1013 }
1014