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