]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeSim.cxx
SSD: updated segmentation class and cleanup of ClusterFinder (M. Van Leeuwen). Elimin...
[u/mrichter/AliRoot.git] / ITS / AliITSDetTypeSim.cxx
1 /***************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17  $Id$
18 */
19
20
21 /////////////////////////////////////////////////////////////////////
22 // Base simulation functions for ITS                               //
23 //                                                                 //
24 //                                                                 //
25 /////////////////////////////////////////////////////////////////////          
26 #include "TBranch.h"
27 #include "TClonesArray.h"
28 #include "TObjArray.h"
29 #include "TTree.h"
30
31 #include "AliRun.h"
32
33 #include "AliCDBManager.h"
34 #include "AliCDBId.h"
35 #include "AliCDBStorage.h"
36 #include "AliCDBEntry.h"
37 #include "AliCDBMetaData.h"
38
39 #include "AliITSdigit.h"
40 #include "AliITSdigitSPD.h"
41 #include "AliITSdigitSDD.h"
42 #include "AliITSdigitSSD.h"
43 #include "AliITSDetTypeSim.h"
44 #include "AliITSpListItem.h"
45 #include "AliITSCalibrationSDD.h"
46 #include "AliITSMapSDD.h"
47 #include "AliITSDriftSpeedArraySDD.h"
48 #include "AliITSDriftSpeedSDD.h"
49 #include "AliITSCalibrationSSD.h"
50 #include "AliITSNoiseSSDv2.h"
51 #include "AliITSGainSSDv2.h"
52 #include "AliITSBadChannelsSSDv2.h"
53 #include "AliITSNoiseSSD.h"
54 #include "AliITSGainSSD.h"
55 #include "AliITSBadChannelsSSD.h"
56 #include "AliITSCalibrationSSD.h"
57 #include "AliITSsegmentationSPD.h"
58 #include "AliITSsegmentationSDD.h"
59 #include "AliITSsegmentationSSD.h"
60 #include "AliITSsimulation.h"
61 #include "AliITSsimulationSPD.h"
62 #include "AliITSsimulationSDD.h"
63 #include "AliITSsimulationSSD.h"
64
65
66 const Int_t AliITSDetTypeSim::fgkNdettypes = 3;
67 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSPD =  240;
68 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSDD =  260;
69 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSSD = 1698;
70
71 ClassImp(AliITSDetTypeSim)
72
73 //----------------------------------------------------------------------
74 AliITSDetTypeSim::AliITSDetTypeSim():
75 TObject(),
76 fSimulation(),   // [NDet]
77 fSegmentation(), // [NDet]
78 fCalibration(),     // [NMod]
79 fSSDCalibration(0),
80 fPreProcess(),   // [] e.g. Fill fHitModule with hits
81 fPostProcess(),  // [] e.g. Wright Raw data
82 fNSDigits(0),    //! number of SDigits
83 fSDigits("AliITSpListItem",1000),   
84 fNDigits(0),     //! number of Digits
85 fRunNumber(0),   //! Run number (to access DB)
86 fDigits(),       //! [NMod][NDigits]
87 fSimuPar(0),
88 fDDLMapSDD(0),
89 fHitClassName(), // String with Hit class name.
90 fSDigClassName(),// String with SDigit class name.
91 fDigClassName(), // String with digit class name.
92 fLoader(0),      // local pointer to loader
93 fFirstcall(kTRUE){ // flag
94     // Default Constructor
95     // Inputs:
96     //    none.
97     // Outputs:
98     //    none.
99     // Return:
100     //    A properly zero-ed AliITSDetTypeSim class.
101
102   fSimulation = new TObjArray(fgkNdettypes);
103   fSegmentation = new TObjArray(fgkNdettypes);
104   fSegmentation->SetOwner(kTRUE);
105   fDigits = new TObjArray(fgkNdettypes);
106   fNDigits = new Int_t[fgkNdettypes];
107   fDDLMapSDD=new AliITSDDLModuleMapSDD();
108   fSimuPar= new AliITSSimuParam();
109   fSSDCalibration=new AliITSCalibrationSSD();
110   fNMod[0] = fgkDefaultNModulesSPD;
111   fNMod[1] = fgkDefaultNModulesSDD;
112   fNMod[2] = fgkDefaultNModulesSSD;
113   SetRunNumber();
114 }
115 //----------------------------------------------------------------------
116 AliITSDetTypeSim::~AliITSDetTypeSim(){
117     // Destructor
118     // Inputs:
119     //    none.
120     // Outputs:
121     //    none.
122     // Return:
123     //    Nothing.
124
125     if(fSimulation){
126         fSimulation->Delete();
127         delete fSimulation;
128     }
129     fSimulation = 0;
130     if(fSegmentation){
131         fSegmentation->Delete();
132         delete fSegmentation;
133     }
134     fSegmentation = 0;
135     if(fCalibration && fRunNumber<0){
136         AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
137                                GetITSgeom()->GetStartSPD()))->GetResponse();
138         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
139                                GetITSgeom()->GetStartSSD()))->GetResponse();
140         if(rspd) delete rspd;
141         if(rssd) delete rssd;
142         fCalibration->Delete();
143         delete fCalibration;
144     }
145     fCalibration = 0;
146     if(fSSDCalibration) delete fSSDCalibration;
147      if(fPreProcess){
148         fPreProcess->Delete();
149         delete fPreProcess;
150     }
151     fPreProcess = 0;
152     if(fPostProcess){
153         fPostProcess->Delete();
154         delete fPostProcess;
155     }
156     fPostProcess = 0;
157     if(fSimuPar) delete fSimuPar;
158     if(fDDLMapSDD) delete fDDLMapSDD;
159     if(fNDigits) delete [] fNDigits;
160     fNDigits = 0;
161     if (fLoader)fLoader->GetModulesFolder()->Remove(this);
162     fLoader = 0; // Not deleting it.
163     fSDigits.Delete();
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 fSSDCalibration(source.fSSDCalibration),
176 fPreProcess(source.fPreProcess),   // [] e.g. Fill fHitModule with hits
177 fPostProcess(source.fPostProcess),  // [] e.g. Wright Raw data
178 fNSDigits(source.fNSDigits),    //! number of SDigits
179 fSDigits(*((TClonesArray*)source.fSDigits.Clone())),
180 fNDigits(source.fNDigits),     //! number of Digits
181 fRunNumber(source.fRunNumber),   //! Run number (to access DB)
182 fDigits(source.fDigits),       //! [NMod][NDigits]
183 fSimuPar(source.fSimuPar),
184 fDDLMapSDD(source.fDDLMapSDD),
185 fHitClassName(source.fHitClassName), // String with Hit class name.
186 fSDigClassName(source.fSDigClassName),// String with SDigit class name.
187 fDigClassName(), // String with digit class name.
188 fLoader(source.fLoader),      // local pointer to loader
189 fFirstcall(source.fFirstcall)
190 {
191     // Copy Constructor for object AliITSDetTypeSim not allowed
192   for(Int_t i=0;i<fgkNdettypes;i++){
193     fDigClassName[i] = source.fDigClassName[i];
194   }
195 }
196 //----------------------------------------------------------------------
197 AliITSDetTypeSim& AliITSDetTypeSim::operator=(const AliITSDetTypeSim &source){
198     // The = operator for object AliITSDetTypeSim
199  
200   this->~AliITSDetTypeSim();
201   new(this) AliITSDetTypeSim(source);
202   return *this;
203 }
204
205 //______________________________________________________________________
206 void AliITSDetTypeSim::SetITSgeom(AliITSgeom *geom){
207     // Sets/replaces the existing AliITSgeom object kept in AliITSLoader
208     // 
209     // Inputs:
210     //   AliITSgoem   *geom  The AliITSgeom object to be used.
211     // Output:
212     //   none.
213     // Return:
214     //   none.
215   if(!fLoader){
216     Error("SetITSgeom","No pointer to loader - nothing done");
217     return;
218   }
219   else {
220     fLoader->SetITSgeom(geom);  // protections in AliITSLoader::SetITSgeom
221   }
222  
223 }
224 //______________________________________________________________________
225 void AliITSDetTypeSim::SetLoader(AliITSLoader *loader){
226     // Sets the local copy of the AliITSLoader, and passes on the
227     // AliITSgeom object as needed.
228     // Inputs
229     //   AliITSLoader  *loader pointer to AliITSLoader for local use
230     // Outputs:
231     //   none.
232     // Return:
233     //  none.
234
235     if(fLoader==loader) return; // Same do nothing
236     if(fLoader){ // alread have an existing loader
237         Error("SetLoader",
238                 "Already have an exisiting loader ptr=%p Nothing done",
239                 fLoader);
240     } // end if
241     fLoader = loader;
242 }
243 //______________________________________________________________________
244 void AliITSDetTypeSim::SetSimulationModel(Int_t dettype,AliITSsimulation *sim){
245
246   //Set simulation model for detector type
247
248   if(fSimulation==0) fSimulation = new TObjArray(fgkNdettypes);
249   fSimulation->AddAt(sim,dettype);
250 }
251 //______________________________________________________________________
252 AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype){
253
254   //Get simulation model for detector type
255   if(fSimulation==0)  {
256     Warning("GetSimulationModel","fSimulation is 0!");
257     return 0;     
258   }
259   return (AliITSsimulation*)(fSimulation->At(dettype));
260 }
261 //______________________________________________________________________
262 AliITSsimulation* AliITSDetTypeSim::GetSimulationModelByModule(Int_t module){
263
264   //Get simulation model by module number
265   if(GetITSgeom()==0) {
266     Warning("GetSimulationModelByModule","GetITSgeom() is 0!");
267     return 0;
268   }
269   
270   return GetSimulationModel(GetITSgeom()->GetModuleType(module));
271 }
272 //_______________________________________________________________________
273 void AliITSDetTypeSim::SetDefaultSegmentation(Int_t idet){
274     // Set default segmentation model objects
275     AliITSsegmentation *seg;
276
277     if(fSegmentation==0x0){
278         fSegmentation = new TObjArray(fgkNdettypes);
279         fSegmentation->SetOwner(kTRUE);
280     }
281     if(GetSegmentationModel(idet))
282         delete (AliITSsegmentation*)fSegmentation->At(idet);
283     if(idet==0){
284         seg = new AliITSsegmentationSPD();
285     }else if(idet==1){
286       seg = new AliITSsegmentationSDD();
287       AliITSCalibrationSDD* cal=(AliITSCalibrationSDD*)GetCalibrationModel(fgkDefaultNModulesSPD+1);
288       if(cal->IsAMAt20MHz()){ 
289         seg->SetPadSize(seg->Dpz(0),20.);
290         seg->SetNPads(seg->Npz()/2,128);
291       }
292     }else {
293         seg = new AliITSsegmentationSSD();
294     }
295     SetSegmentationModel(idet,seg);
296 }
297 //______________________________________________________________________
298 void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,
299                                             AliITSsegmentation *seg){
300    
301   //Set segmentation model for detector type
302   if(fSegmentation==0x0){
303     fSegmentation = new TObjArray(fgkNdettypes);
304     fSegmentation->SetOwner(kTRUE);
305   }
306   fSegmentation->AddAt(seg,dettype);
307 }
308 //______________________________________________________________________
309 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype){
310   //Get segmentation model for detector type
311    
312    if(fSegmentation==0) {
313        Warning("GetSegmentationModel","fSegmentation is 0!");
314        return 0; 
315    } 
316    return (AliITSsegmentation*)(fSegmentation->At(dettype));
317 }
318 //_______________________________________________________________________
319 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module){
320     //Get segmentation model by module number
321     if(GetITSgeom()==0){
322         Warning("GetSegmentationModelByModule","GetITSgeom() is 0!");
323         return 0;
324     }     
325     return GetSegmentationModel(GetITSgeom()->GetModuleType(module));
326 }
327 //_______________________________________________________________________
328 void AliITSDetTypeSim::CreateCalibrationArray() {
329     //Create the container of calibration functions with correct size
330     if (fCalibration) {
331         Warning("CreateCalibration","pointer to calibration object exists\n");
332         fCalibration->Delete();
333         delete fCalibration;
334     }
335
336     Int_t nModTot = GetITSgeom()->GetIndexMax();
337     fCalibration = new TObjArray(nModTot);
338     fCalibration->SetOwner(kTRUE);
339     fCalibration->Clear();
340 }
341 //_______________________________________________________________________
342 void AliITSDetTypeSim::SetCalibrationModel(Int_t iMod, AliITSCalibration *resp){
343     //Set response model for modules
344
345     if (fCalibration==0) CreateCalibrationArray();
346  
347     if (fCalibration->At(iMod)!=0)
348         delete (AliITSCalibration*) fCalibration->At(iMod);
349     fCalibration->AddAt(resp, iMod);
350 }
351 //______________________________________________________________________
352 void AliITSDetTypeSim::ResetCalibrationArray(){
353     //resets response array
354     if(fCalibration && fRunNumber<0){  // if fRunNumber<0 fCalibration is owner
355         AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(
356                                 GetITSgeom()->GetStartSPD()))->GetResponse();
357         AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(
358                                 GetITSgeom()->GetStartSSD()))->GetResponse();
359         if(rspd) delete rspd;
360         if(rssd) delete rssd;
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 *mapASDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsAnodeSDD",run);
443   AliCDBEntry *mapTSDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsTimeSDD",run);
444   // AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", run);
445   AliCDBEntry *entryNoiseSSD = AliCDBManager::Instance()->Get("ITS/Calib/NoiseSSD");
446   AliCDBEntry *entryGainSSD = AliCDBManager::Instance()->Get("ITS/Calib/GainSSD");
447   AliCDBEntry *entryBadChannelsSSD = AliCDBManager::Instance()->Get("ITS/Calib/BadChannelsSSD");
448
449   AliCDBEntry *entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", run);
450
451   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
452      !entry2SPD || !drSpSDD || !ddlMapSDD || !mapTSDD){
453     AliFatal("Calibration object retrieval failed! ");
454     return kFALSE;
455   }     
456
457 //   if(!entrySPD || !entrySDD || !entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD || 
458 //      !entry2SPD || !entry2SSD){
459 //     AliFatal("Calibration object retrieval failed! ");
460 //     return kFALSE;
461 //   }          
462
463   TObjArray *calSPD = (TObjArray *)entrySPD->GetObject();
464   if(!isCacheActive)entrySPD->SetObject(NULL);
465   entrySPD->SetOwner(kTRUE);
466
467   AliITSresponseSPD *pSPD = (AliITSresponseSPD*)entry2SPD->GetObject();
468   if(!isCacheActive)entry2SPD->SetObject(NULL);
469   entry2SPD->SetOwner(kTRUE);
470    
471   TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
472   if(!isCacheActive)entrySDD->SetObject(NULL);
473   entrySDD->SetOwner(kTRUE);
474
475   TObjArray *drSp = (TObjArray *)drSpSDD->GetObject();
476   if(!isCacheActive)drSpSDD->SetObject(NULL);
477   drSpSDD->SetOwner(kTRUE);
478
479   AliITSDDLModuleMapSDD *ddlsdd=(AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
480   if(!isCacheActive)ddlMapSDD->SetObject(NULL);
481   ddlMapSDD->SetOwner(kTRUE);
482
483 //   TObjArray *mapAn = (TObjArray *)mapASDD->GetObject();
484 //   if(!isCacheActive)mapASDD->SetObject(NULL);
485 //   mapASDD->SetOwner(kTRUE);
486
487   TObjArray *mapT = (TObjArray *)mapTSDD->GetObject();
488   if(!isCacheActive)mapTSDD->SetObject(NULL);
489   mapTSDD->SetOwner(kTRUE);
490
491   /*
492   TObjArray *calSSD = (TObjArray *)entrySSD->GetObject();
493   if(!isCacheActive)entrySSD->SetObject(NULL);
494   entrySSD->SetOwner(kTRUE);
495   */
496
497   TObject *emptyssd = 0; TString ssdobjectname = 0;
498   AliITSNoiseSSDv2 *noiseSSD = new AliITSNoiseSSDv2();  
499   emptyssd = (TObject *)entryNoiseSSD->GetObject();
500   ssdobjectname = emptyssd->GetName();
501   if(ssdobjectname=="TObjArray") {
502     TObjArray *noiseSSDOld = (TObjArray *)entryNoiseSSD->GetObject();
503     ReadOldSSDNoise(noiseSSDOld, noiseSSD);
504   }
505   else if(ssdobjectname=="AliITSNoiseSSDv2")
506     noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
507   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
508   entryNoiseSSD->SetOwner(kTRUE);
509
510   AliITSGainSSDv2 *gainSSD = new AliITSGainSSDv2();
511   emptyssd = (TObject *)entryGainSSD->GetObject();
512   ssdobjectname = emptyssd->GetName();
513   if(ssdobjectname=="Gain") {
514     TObjArray *gainSSDOld = (TObjArray *)entryGainSSD->GetObject();
515     ReadOldSSDGain(gainSSDOld, gainSSD);
516   }
517   else if(ssdobjectname=="AliITSGainSSDv2")
518     gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
519   if(!isCacheActive)entryGainSSD->SetObject(NULL);
520   entryGainSSD->SetOwner(kTRUE);
521
522   AliITSBadChannelsSSDv2 *badChannelsSSD = new AliITSBadChannelsSSDv2();
523   emptyssd = (TObject *)entryBadChannelsSSD->GetObject();
524   ssdobjectname = emptyssd->GetName();
525   if(ssdobjectname=="TObjArray") {
526     TObjArray *badChannelsSSDOld = (TObjArray *)entryBadChannelsSSD->GetObject();
527     ReadOldSSDBadChannels(badChannelsSSDOld, badChannelsSSD);
528   }
529   else if(ssdobjectname=="AliITSBadChannelsSSDv2")
530     badChannelsSSD = (AliITSBadChannelsSSDv2*)entryBadChannelsSSD->GetObject();
531   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
532   entryBadChannelsSSD->SetOwner(kTRUE);
533
534   /*AliITSNoiseSSDv2 *noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
535   if(!isCacheActive)entryNoiseSSD->SetObject(NULL);
536   entryNoiseSSD->SetOwner(kTRUE);
537
538   AliITSGainSSDv2 *gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
539   if(!isCacheActive)entryGainSSD->SetObject(NULL);
540   entryGainSSD->SetOwner(kTRUE);
541
542   AliITSBadChannelsSSDv2 *badchannelsSSD = 
543     (AliITSBadChannelsSSDv2 *)entryBadChannelsSSD->GetObject();
544   if(!isCacheActive)entryBadChannelsSSD->SetObject(NULL);
545   entryBadChannelsSSD->SetOwner(kTRUE);*/
546
547   // DB entries are deleted. In this way metadeta objects are deleted as well
548   if(!isCacheActive){
549     delete entrySPD;
550     delete entrySDD;
551     delete entryNoiseSSD;
552     delete entryGainSSD;
553     delete entryBadChannelsSSD;
554     delete entry2SPD;
555 //    delete mapASDD;   
556     delete mapTSDD;
557     delete drSpSDD;
558     delete ddlMapSDD;
559   }
560   
561   AliCDBManager::Instance()->SetCacheFlag(origCacheStatus);
562
563   if ((!pSPD)|| (!calSPD) || (!calSDD) || (!drSp) || (!ddlsdd)
564       || (!mapT) || (!noiseSSD)|| (!gainSSD)|| (!badChannelsSSD)) {
565     AliWarning("Can not get calibration from calibration database !");
566     return kFALSE;
567   }
568
569   fNMod[0] = calSPD->GetEntries();
570   fNMod[1] = calSDD->GetEntries();
571   //  fNMod[2] = noiseSSD->GetEntries();
572   AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
573                fNMod[0], fNMod[1], fNMod[2]));
574   AliITSCalibration* cal;
575   for (Int_t i=0; i<fNMod[0]; i++) {
576     cal = (AliITSCalibration*) calSPD->At(i);
577     cal->SetResponse(pSPD);
578     SetCalibrationModel(i, cal);
579   }
580
581   fDDLMapSDD->SetDDLMap(ddlsdd);
582
583   for (Int_t i=0; i<fgkDefaultNModulesSDD; i++) {
584     Int_t iddl,icarlos;
585     Int_t iMod = i + fgkDefaultNModulesSPD;
586     fDDLMapSDD->FindInDDLMap(iMod,iddl,icarlos);
587     if(iddl<0){ 
588       AliITSCalibrationSDD* calsdddead=new AliITSCalibrationSDD();
589       calsdddead->SetBad();      
590       AliITSDriftSpeedSDD* driftspdef = new AliITSDriftSpeedSDD();
591       AliITSDriftSpeedArraySDD* arrdrsp=new AliITSDriftSpeedArraySDD(1);
592       arrdrsp->AddDriftSpeed(driftspdef);
593       calsdddead->SetDriftSpeed(0,arrdrsp);
594       calsdddead->SetDriftSpeed(1,arrdrsp);
595       SetCalibrationModel(iMod, calsdddead);
596       AliWarning(Form("SDD module %d not present in DDL map: set it as dead",iMod));
597     }else{
598       cal = (AliITSCalibration*) calSDD->At(i);
599       Int_t i0=2*i;
600       Int_t i1=1+2*i;
601       AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
602 //       AliITSMapSDD* ma0 = (AliITSMapSDD*)mapAn->At(i0);
603       AliITSMapSDD* mt0 = (AliITSMapSDD*)mapT->At(i0);
604       AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
605  //      AliITSMapSDD* ma1 = (AliITSMapSDD*)mapAn->At(i1);
606       AliITSMapSDD* mt1 = (AliITSMapSDD*)mapT->At(i1);
607       cal->SetDriftSpeed(0,arr0);
608       cal->SetDriftSpeed(1,arr1);
609 //       cal->SetMapA(0,ma0);
610 //       cal->SetMapA(1,ma1);
611       cal->SetMapT(0,mt0);
612       cal->SetMapT(1,mt1);
613       SetCalibrationModel(iMod, cal);
614     }
615   }
616
617   fSSDCalibration->SetNoise(noiseSSD);
618   fSSDCalibration->SetGain(gainSSD);
619   fSSDCalibration->SetBadChannels(badChannelsSSD);
620   //fSSDCalibration->FillBadChipMap();
621
622
623   /*
624   for (Int_t i=0; i<fNMod[2]; i++) {
625     AliITSCalibrationSSD *calibSSD = new AliITSCalibrationSSD();
626     calibSSD->SetResponse((AliITSresponse*)pSSD);
627     
628     AliITSNoiseSSD *noise = (AliITSNoiseSSD*) (noiseSSD->At(i));
629     calibSSD->SetNoise(noise);
630     AliITSGainSSD *gain = (AliITSGainSSD*) (gainSSD->At(i));
631     calibSSD->SetGain(gain);
632     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (badchannelsSSD->At(i));
633     calibSSD->SetBadChannels(bad);
634
635     Int_t iMod = i + fgkDefaultNModulesSPD + fgkDefaultNModulesSDD;
636     SetCalibrationModel(iMod, calibSSD);
637
638     }
639   */
640
641
642   return kTRUE;
643 }
644 //_______________________________________________________________________
645 void AliITSDetTypeSim::SetDefaultSimulation(){
646   //Set default simulation for detector type
647
648   if(GetITSgeom()==0){
649     Warning("SetDefaultSimulation","GetITSgeom() is 0!");
650     return;
651   }
652   if(fCalibration==0){
653     Warning("SetDefaultSimulation","fCalibration is 0!");
654     return;
655   }
656   if(fSegmentation==0){
657     Warning("SetDefaultSimulation","fSegmentation is 0!");
658     for(Int_t i=0;i<fgkNdettypes;i++) SetDefaultSegmentation(i);
659   }else for(Int_t i=0;i<fgkNdettypes;i++) if(!GetSegmentationModel(i)){
660       Warning("SetDefaultSimulation",
661               "Segmentation not defined for det %d - Default taken\n!",i);
662       SetDefaultSegmentation(i);
663   }
664   AliITSsimulation* sim;
665
666   for(Int_t idet=0;idet<fgkNdettypes;idet++){
667    //SPD
668     if(idet==0){
669       sim = GetSimulationModel(idet); 
670       if(!sim){
671         sim = new AliITSsimulationSPD(this);
672         SetSimulationModel(idet,sim);
673       }
674     }
675     //SDD
676     if(idet==1){
677       sim = GetSimulationModel(idet);
678       if(!sim){
679         sim = new AliITSsimulationSDD(this);
680         SetSimulationModel(idet,sim);
681       }      
682     }
683     //SSD
684     if(idet==2){
685       sim = GetSimulationModel(idet);
686       if(!sim){
687         sim = new AliITSsimulationSSD(this);
688         SetSimulationModel(idet,sim);
689       }
690     }
691
692   }
693 }
694 //___________________________________________________________________
695 void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, Char_t* name){
696   // Set branch address for the ITS summable digits Trees.  
697   char branchname[30];
698
699   if(!treeS){
700     return;
701   }
702   TBranch *branch;
703   sprintf(branchname,"%s",name);
704   branch = treeS->GetBranch(branchname);
705   TClonesArray *sdigi = &fSDigits;
706   if (branch) branch->SetAddress(&sdigi);
707
708 }
709 //___________________________________________________________________
710 void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
711   // Set branch address for the digit Trees.
712   
713   const char *det[3] = {"SPD","SDD","SSD"};
714   TBranch *branch;
715   
716   char branchname[30];
717   
718   if(!treeD){
719     return;
720   }
721   if(!fDigits){
722     fDigits = new TObjArray(fgkNdettypes); 
723   }
724   for(Int_t i=0;i<fgkNdettypes;i++){
725     Char_t* digclass = GetDigitClassName(i);
726     if(digclass==0x0){
727       if(i==0) SetDigitClassName(i,"AliITSdigitSPD");
728       if(i==1) SetDigitClassName(i,"AliITSdigitSDD");
729       if(i==2) SetDigitClassName(i,"AliITSdigitSSD");
730       digclass = GetDigitClassName(i);
731     }
732     TString classn = digclass;
733     if(!(fDigits->At(i))){
734       fDigits->AddAt(new TClonesArray(classn.Data(),1000),i);
735     }else{
736       ResetDigits(i);
737     }
738     
739     if(fgkNdettypes==3) sprintf(branchname,"%sDigits%s",name,det[i]);
740     else sprintf(branchname,"%sDigits%d",name,i+1);
741     if(fDigits){
742       branch = treeD->GetBranch(branchname);
743       if(branch) branch->SetAddress(&((*fDigits)[i]));
744     }
745   }
746
747 }
748 //___________________________________________________________________
749 void AliITSDetTypeSim::ResetDigits(){
750   // Reset number of digits and the digits array for the ITS detector.  
751
752   if(!fDigits){
753     Error("ResetDigits","fDigits is null!");
754     return;
755   }
756   for(Int_t i=0;i<fgkNdettypes;i++){
757     ResetDigits(i);
758   }
759 }
760 //___________________________________________________________________
761 void AliITSDetTypeSim::ResetDigits(Int_t branch){
762   // Reset number of digits and the digits array for this branch.
763
764   if(fDigits->At(branch)){
765     ((TClonesArray*)fDigits->At(branch))->Clear();
766   }
767   if(fNDigits) fNDigits[branch]=0;
768
769 }
770
771
772
773 //_______________________________________________________________________
774 void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
775   // Standard Summable digits to Digits function.
776   if(!GetITSgeom()){
777     Warning("SDigitsToDigits","GetITSgeom() is null!!");
778     return;
779   }
780   
781   const char *all = strstr(opt,"All");
782   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
783                         strstr(opt,"SSD")};
784   if( !det[0] && !det[1] && !det[2] ) all = "All";
785   else all = 0;
786   static Bool_t setDef = kTRUE;
787   if(setDef) SetDefaultSimulation();
788   setDef = kFALSE;
789   
790   AliITSsimulation *sim =0;
791   TTree* trees = fLoader->TreeS();
792   if( !(trees && GetSDigits()) ){
793     Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
794     return;
795   } 
796   sprintf(name,"%s",name);
797   TBranch* brchSDigits = trees->GetBranch(name);
798   
799   Int_t id;
800   for(Int_t module=0;module<GetITSgeom()->GetIndexMax();module++){
801      id = GetITSgeom()->GetModuleType(module);
802     if (!all && !det[id]) continue;
803     sim = (AliITSsimulation*)GetSimulationModel(id);
804     if(!sim){
805       Error("SDigit2Digits","The simulation class was not "
806             "instanciated for module %d type %s!",module,
807             GetITSgeom()->GetModuleTypeName(module));
808       exit(1);
809     }
810     sim->InitSimulationModule(module,gAlice->GetEvNumber());
811     
812     fSDigits.Clear();
813     brchSDigits->GetEvent(module);
814     sim->AddSDigitsToModule(&fSDigits,0);
815     sim->FinishSDigitiseModule();
816     fLoader->TreeD()->Fill();
817     ResetDigits();
818   }
819   fLoader->TreeD()->GetEntries();
820   fLoader->TreeD()->AutoSave();
821   fLoader->TreeD()->Reset();
822 }
823 //_________________________________________________________
824 void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){  
825   //Adds the module full of summable digits to the summable digits tree.
826
827   new(fSDigits[fNSDigits++]) AliITSpListItem(sdig);
828 }
829 //__________________________________________________________
830 void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){  
831   //    Add a simulated digit.
832
833   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
834   switch(branch){
835   case 0:
836     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
837     break;
838   case 1:
839     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
840     break;
841   case 2:
842     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
843     break;
844   }  
845 }
846 //______________________________________________________________________
847 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
848                                    Int_t *tracks,Int_t *hits,Float_t *charges, 
849                                    Int_t sigexpanded){
850   //   Add a simulated digit to the list.
851
852   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
853   switch(branch){
854   case 0:
855     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
856     break;
857   case 1:
858     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
859                                                    hits,charges,sigexpanded);
860     break;
861   case 2:
862     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(digits,tracks,hits);
863     break;
864   } 
865 }
866 //______________________________________________________________________
867 void AliITSDetTypeSim::StoreCalibration(Int_t firstRun, Int_t lastRun,
868                                         AliCDBMetaData &md) {
869   // Store calibration in the calibration database
870   // The database must be created in an external piece of code (i.e. 
871   // a configuration macro )
872
873   if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
874     AliWarning("No storage set! Will use dummy one");
875     AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
876   }
877
878   if (!fCalibration) {
879     AliError("AliITSCalibration classes are not defined - nothing done");
880     return;
881   }
882   AliCDBId idRespSPD("ITS/Calib/SPDDead",firstRun, lastRun);
883   AliCDBId idRespSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
884   AliCDBId idRespSSD("ITS/Calib/CalibSSD",firstRun, lastRun);
885
886   TObjArray respSPD(fNMod[0]);
887   TObjArray respSDD(fNMod[1]-fNMod[0]);
888   TObjArray respSSD(fNMod[2]-fNMod[1]);
889   respSPD.SetOwner(kFALSE);
890   respSSD.SetOwner(kFALSE);
891   respSSD.SetOwner(kFALSE);
892
893   Int_t index[fgkNdettypes];
894   for (Int_t i = 0; i<fgkNdettypes; i++ ) {
895     index[i] = 0;
896     for (Int_t j = 0; j<=i; j++ )
897       index[i]+=fNMod[j];
898   }
899
900   for (Int_t i = 0; i<index[0]; i++ )
901     respSPD.Add(fCalibration->At(i));
902
903   for (Int_t i = index[0]; i<index[1]; i++ )
904     respSDD.Add(fCalibration->At(i));
905
906   for (Int_t i = index[1]; i<index[2]; i++ )
907     respSSD.Add(fCalibration->At(i));
908
909   AliCDBManager::Instance()->Put(&respSPD, idRespSPD, &md);
910   AliCDBManager::Instance()->Put(&respSDD, idRespSDD, &md);
911   AliCDBManager::Instance()->Put(&respSSD, idRespSSD, &md);
912 }
913
914 //______________________________________________________________________
915 void AliITSDetTypeSim::ReadOldSSDNoise(TObjArray *array, 
916                                        AliITSNoiseSSDv2 *noiseSSD) {
917   //Reads the old SSD calibration object and converts it to the new format
918   const Int_t fgkSSDSTRIPSPERMODULE = 1536;
919   const Int_t fgkSSDPSIDESTRIPSPERMODULE = 768;
920
921   Int_t fNMod = array->GetEntries();
922   cout<<"Converting old calibration object for noise..."<<endl;
923
924   //NOISE
925   Double_t noise = 0.0;
926   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
927     AliITSNoiseSSD *noiseModule = (AliITSNoiseSSD*) (array->At(iModule));
928     for(Int_t iStrip = 0; iStrip < fgkSSDSTRIPSPERMODULE; iStrip++) {
929       noise = (iStrip < fgkSSDPSIDESTRIPSPERMODULE) ? noiseModule->GetNoiseP(iStrip) : noiseModule->GetNoiseN(1535 - iStrip);
930       if(iStrip < fgkSSDPSIDESTRIPSPERMODULE)
931         noiseSSD->AddNoiseP(iModule,iStrip,noise);
932       if(iStrip >= fgkSSDPSIDESTRIPSPERMODULE)
933         noiseSSD->AddNoiseN(iModule,1535 - iStrip,noise);
934     }//loop over strips
935   }//loop over modules      
936 }
937
938 //______________________________________________________________________
939 void AliITSDetTypeSim::ReadOldSSDBadChannels(TObjArray *array, 
940                                              AliITSBadChannelsSSDv2 *badChannelsSSD) {
941   //Reads the old SSD calibration object and converts it to the new format
942   Int_t fNMod = array->GetEntries();
943   cout<<"Converting old calibration object for bad channels..."<<endl;
944   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
945     //for (Int_t iModule = 0; iModule < 1; iModule++) {
946     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (array->At(iModule));
947     TArrayI arrayPSide = bad->GetBadPChannelsList();
948     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++) 
949       badChannelsSSD->AddBadChannelP(iModule,
950                                      iPCounter,
951                                      (Char_t)arrayPSide.At(iPCounter));
952         
953     TArrayI arrayNSide = bad->GetBadNChannelsList();
954     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++) 
955       badChannelsSSD->AddBadChannelN(iModule,
956                                      iNCounter,
957                                      (Char_t)arrayNSide.At(iNCounter));
958     
959   }//loop over modules      
960 }
961
962 //______________________________________________________________________
963 void AliITSDetTypeSim::ReadOldSSDGain(TObjArray *array, 
964                                       AliITSGainSSDv2 *gainSSD) {
965   //Reads the old SSD calibration object and converts it to the new format
966
967   Int_t fNMod = array->GetEntries();
968   cout<<"Converting old calibration object for gain..."<<endl;
969
970   //GAIN
971   for (Int_t iModule = 0; iModule < fNMod; iModule++) {
972     AliITSGainSSD *gainModule = (AliITSGainSSD*) (array->At(iModule));
973     TArrayF arrayPSide = gainModule->GetGainP();
974     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++)
975       gainSSD->AddGainP(iModule,
976                         iPCounter,
977                         arrayPSide.At(iPCounter));
978     TArrayF arrayNSide = gainModule->GetGainN();
979     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++)
980       gainSSD->AddGainN(iModule,
981                         iNCounter,
982                         arrayNSide.At(iNCounter));
983   }//loop over modules 
984 }
985