]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeSim.cxx
Digitizer: improved access to CDB for multievent jobs. Memory leak fixed in AliITSDet...
[u/mrichter/AliRoot.git] / ITS / AliITSDetTypeSim.cxx
1 /***************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17  $Id$
18 */
19
20
21 /////////////////////////////////////////////////////////////////////
22 // Base simulation functions for ITS                               //
23 //                                                                 //
24 //                                                                 //
25 /////////////////////////////////////////////////////////////////////          
26 #include "TBranch.h"
27 #include "TClonesArray.h"
28 #include "TObjArray.h"
29 #include "TTree.h"
30
31 #include "AliRun.h"
32
33 #include "AliCDBManager.h"
34 #include "AliCDBId.h"
35 #include "AliCDBStorage.h"
36 #include "AliCDBEntry.h"
37 #include "AliCDBMetaData.h"
38
39 #include "AliITSdigit.h"
40 #include "AliITSdigitSPD.h"
41 #include "AliITSdigitSDD.h"
42 #include "AliITSdigitSSD.h"
43 #include "AliITSDetTypeSim.h"
44 #include "AliITSpListItem.h"
45 #include "AliITSresponseSDD.h"
46 #include "AliITSCalibrationSDD.h"
47 #include "AliITSCalibrationSSD.h"
48 #include "AliITSsegmentationSPD.h"
49 #include "AliITSsegmentationSDD.h"
50 #include "AliITSsegmentationSSD.h"
51 #include "AliITSsimulation.h"
52 #include "AliITSsimulationSPD.h"
53 #include "AliITSsimulationSDD.h"
54 #include "AliITSsimulationSSD.h"
55
56
57 const Int_t AliITSDetTypeSim::fgkNdettypes = 3;
58 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSPD =  240;
59 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSDD =  260;
60 const Int_t AliITSDetTypeSim::fgkDefaultNModulesSSD = 1698;
61
62 ClassImp(AliITSDetTypeSim)
63
64 //----------------------------------------------------------------------
65 AliITSDetTypeSim::AliITSDetTypeSim():
66 TObject(),
67 fGeom(),         //
68 fSimulation(),   // [NDet]
69 fSegmentation(), // [NDet]
70 fCalibration(),     // [NMod]
71 fPreProcess(),   // [] e.g. Fill fHitModule with hits
72 fPostProcess(),  // [] e.g. Wright Raw data
73 fNSDigits(0),    //! number of SDigits
74 fSDigits(),      //! [NMod][NSDigits]
75 fNDigits(0),     //! number of Digits
76 fDigits(),       //! [NMod][NDigits]
77 fHitClassName(), // String with Hit class name.
78 fSDigClassName(),// String with SDigit class name.
79 fDigClassName(){ // String with digit class name.
80     // Default Constructor
81     // Inputs:
82     //    none.
83     // Outputs:
84     //    none.
85     // Return:
86     //    A properly zero-ed AliITSDetTypeSim class.
87   fGeom = 0;
88   fSimulation = new TObjArray(fgkNdettypes);
89   fSegmentation = new TObjArray(fgkNdettypes);
90   fSegmentation->SetOwner(kTRUE);
91   fCalibration = 0;
92   fPreProcess = 0;
93   fPostProcess = 0;
94   fNSDigits = 0;
95   fSDigits = new TClonesArray("AliITSpListItem",1000);
96   fDigits = new TObjArray(fgkNdettypes);
97   fNDigits = new Int_t[fgkNdettypes];
98   fLoader = 0;
99   fNMod[0] = fgkDefaultNModulesSPD;
100   fNMod[1] = fgkDefaultNModulesSDD;
101   fNMod[2] = fgkDefaultNModulesSSD;
102   SetRunNumber();
103
104 }
105 //----------------------------------------------------------------------
106 AliITSDetTypeSim::~AliITSDetTypeSim(){
107     // Destructor
108     // Inputs:
109     //    none.
110     // Outputs:
111     //    none.
112     // Return:
113     //    Nothing.
114   
115     if(fSimulation){
116       fSimulation->Delete();
117       delete fSimulation;
118       fSimulation = 0;
119     }
120     
121     if(fSegmentation){
122       fSegmentation->Delete();
123       delete fSegmentation;
124       fSegmentation = 0;
125     }
126     
127     if(fCalibration){
128       AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(fGeom->GetStartSPD()))->GetResponse();
129       AliITSresponse* rsdd = ((AliITSCalibration*)fCalibration->At(fGeom->GetStartSDD()))->GetResponse();
130       AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(fGeom->GetStartSSD()))->GetResponse();
131       if(rspd) delete rspd;
132       if(rsdd) delete rsdd;
133       if(rssd) delete rssd;
134       fCalibration->Delete();
135       delete fCalibration;
136       fCalibration = 0;
137     }
138
139     if(fGeom) delete fGeom;
140     
141     if(fPreProcess){
142       fPreProcess->Delete();
143       delete fPreProcess;
144       fPreProcess = 0;
145     }
146     
147     if(fPostProcess){
148       fPostProcess->Delete();
149       delete fPostProcess;
150       fPostProcess = 0;
151     }
152
153     if(fNDigits) delete [] fNDigits;
154
155     if (fLoader)
156       {
157         fLoader->GetModulesFolder()->Remove(this);
158       }
159     
160     if (fSDigits) {
161       fSDigits->Delete();
162       delete fSDigits;
163       fSDigits=0;
164     }
165     if (fDigits) {
166       fDigits->Delete();
167       delete fDigits;
168       fDigits=0;
169     }
170   
171 }
172 //----------------------------------------------------------------------
173 AliITSDetTypeSim::AliITSDetTypeSim(const AliITSDetTypeSim &source) : TObject(source){
174     // Copy Constructor for object AliITSDetTypeSim not allowed
175   if(this==&source) return;
176   Error("Copy constructor",
177         "You are not allowed to make a copy of the AliITSDetTypeSim");
178   exit(1);
179
180         
181 }
182 //----------------------------------------------------------------------
183 AliITSDetTypeSim& AliITSDetTypeSim::operator=(const AliITSDetTypeSim &source){
184     // The = operator for object AliITSDetTypeSim
185  
186     if(&source==this) return *this;
187     Error("operator=","You are not allowed to make a copy of the AliITSDetTypeSIm");
188     exit(1);
189     return *this;
190 }
191
192
193 //______________________________________________________________________
194 void AliITSDetTypeSim::SetSimulationModel(Int_t dettype,AliITSsimulation *sim){
195
196   //Set simulation model for detector type
197
198   if(fSimulation==0) fSimulation = new TObjArray(fgkNdettypes);
199   fSimulation->AddAt(sim,dettype);
200 }
201 //______________________________________________________________________
202 AliITSsimulation* AliITSDetTypeSim::GetSimulationModel(Int_t dettype){
203
204   //Get simulation model for detector type
205   if(fSimulation==0)  {
206     Warning("GetSimulationModel","fSimulation is 0!");
207     return 0;     
208   }
209   return (AliITSsimulation*)(fSimulation->At(dettype));
210 }
211 //______________________________________________________________________
212 AliITSsimulation* AliITSDetTypeSim::GetSimulationModelByModule(Int_t module){
213
214   //Get simulation model by module number
215   if(fGeom==0) {
216     Warning("GetSimulationModelByModule","fGeom is 0!");
217     return 0;
218   }
219   
220   return GetSimulationModel(fGeom->GetModuleType(module));
221 }
222 //_______________________________________________________________________
223 void AliITSDetTypeSim::SetDefaultSegmentation(Int_t idet){
224   // Set default segmentation model objects
225   AliITSsegmentation *seg;
226   if(fSegmentation==0x0){
227     fSegmentation = new TObjArray(fgkNdettypes);
228     fSegmentation->SetOwner(kTRUE);
229   }
230   if(GetSegmentationModel(idet))delete (AliITSsegmentation*)fSegmentation->At(idet);
231   if(idet==0){
232     seg = new AliITSsegmentationSPD(fGeom);
233   }
234   else if(idet==1){
235     AliITSCalibration* res = GetCalibrationModel(fGeom->GetStartSDD());
236     seg = new AliITSsegmentationSDD(fGeom,res);
237   }
238   else {
239     seg = new AliITSsegmentationSSD(fGeom);
240   }
241   SetSegmentationModel(idet,seg);
242 }
243
244 //______________________________________________________________________
245 void AliITSDetTypeSim::SetSegmentationModel(Int_t dettype,AliITSsegmentation *seg){
246    
247   //Set segmentation model for detector type
248   if(fSegmentation==0x0){
249     fSegmentation = new TObjArray(fgkNdettypes);
250     fSegmentation->SetOwner(kTRUE);
251   }
252   fSegmentation->AddAt(seg,dettype);
253
254 }
255 //______________________________________________________________________
256 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModel(Int_t dettype){
257
258   //Get segmentation model for detector type
259    
260    if(fSegmentation==0) {
261      Warning("GetSegmentationModel","fSegmentation is 0!");
262      return 0; 
263    } 
264    return (AliITSsegmentation*)(fSegmentation->At(dettype));
265
266 }
267 //_______________________________________________________________________
268 AliITSsegmentation* AliITSDetTypeSim::GetSegmentationModelByModule(Int_t module){
269   
270   //Get segmentation model by module number
271    if(fGeom==0){
272      Warning("GetSegmentationModelByModule","fGeom is 0!");
273      return 0;
274    }     
275    return GetSegmentationModel(fGeom->GetModuleType(module));
276
277 }
278 //_______________________________________________________________________
279 void AliITSDetTypeSim::CreateCalibrationArray() {
280
281   //Create the container of calibration functions with correct size
282   if (fCalibration) {
283     fCalibration->Delete();
284     delete fCalibration;
285   }
286
287   Int_t nModTot = 0;
288   for (Int_t i=0; i<fgkNdettypes; i++) nModTot += fNMod[i];
289   fCalibration = new TObjArray(nModTot);
290   fCalibration->SetOwner(kTRUE);
291   fCalibration->Clear();
292
293 }
294 //_______________________________________________________________________
295 void AliITSDetTypeSim::SetCalibrationModel(Int_t iMod, AliITSCalibration *resp){
296
297   //Set response model for modules
298
299   if (fCalibration==0) CreateCalibrationArray();
300  
301   if (fCalibration->At(iMod)!=0)
302     delete (AliITSCalibration*) fCalibration->At(iMod);
303   fCalibration->AddAt(resp, iMod);
304
305 }
306
307 //______________________________________________________________________
308 void AliITSDetTypeSim::ResetCalibrationArray(){
309
310   //resets response array
311   if(fCalibration)
312     fCalibration->Clear();
313
314 }
315 //______________________________________________________________________
316 void AliITSDetTypeSim::ResetSegmentation(){
317  
318  //Resets segmentation array
319   if(fSegmentation) fSegmentation->Clear();
320 }
321
322 //_______________________________________________________________________
323 AliITSCalibration* AliITSDetTypeSim::GetCalibrationModel(Int_t iMod){
324    //Get response model for module number iMod 
325  
326   if(fCalibration==0) {
327     AliError("fCalibration is 0!");
328     return 0; 
329   }
330
331   return (AliITSCalibration*)(fCalibration->At(iMod));
332
333 }
334 //_______________________________________________________________________
335 void AliITSDetTypeSim::SetDefaults(){
336
337   //Set defaults for segmentation and response
338
339   if(fGeom==0){
340     Warning("SetDefaults","fGeom is 0!");
341     return;
342   }
343   if (fCalibration==0) CreateCalibrationArray();
344
345   ResetCalibrationArray();
346   ResetSegmentation();
347   if(!GetCalibration()){AliFatal("Exit"); exit(0);}
348
349   for(Int_t idet=0;idet<fgkNdettypes;idet++){
350     //SPD
351     if(idet==0){
352       if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
353       const char *kData0=(GetCalibrationModel(fGeom->GetStartSPD()))->DataType();
354       if (strstr(kData0,"real")) {
355         SetDigitClassName(idet,"AliITSdigit");
356       }
357       else {
358         SetDigitClassName(idet,"AliITSdigitSPD");
359       }
360     }
361     //SDD
362     if(idet==1){
363       if(!GetSegmentationModel(idet)) SetDefaultSegmentation(idet);
364       AliITSCalibrationSDD* rsp = (AliITSCalibrationSDD*)GetCalibrationModel(fGeom->GetStartSDD());
365       const char *kopt = ((AliITSresponseSDD*)rsp->GetResponse())->ZeroSuppOption();
366       if((!strstr(kopt,"2D"))&&(!strstr(kopt,"1D"))) {
367         SetDigitClassName(idet,"AliITSdigit");
368       }
369       else {
370         SetDigitClassName(idet,"AliITSdigitSDD");
371       }
372     
373     }
374     //SSD
375     if(idet==2){
376       if(!GetSegmentationModel(idet))SetDefaultSegmentation(idet);
377
378       const char *kData2 = (GetCalibrationModel(fGeom->GetStartSSD())->DataType());
379       if (strstr(kData2,"real")) {
380         SetDigitClassName(idet,"AliITSdigit");
381       }
382       else {
383         SetDigitClassName(idet,"AliITSdigitSSD");
384       }
385     }
386   }
387 }
388
389 //______________________________________________________________________
390 Bool_t AliITSDetTypeSim::GetCalibration() {
391   // Get Default calibration if a storage is not defined.
392
393   AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSPD", fRunNumber);
394   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", fRunNumber);
395   AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", fRunNumber);
396   AliCDBEntry *entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", fRunNumber);
397   AliCDBEntry *entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD", fRunNumber);
398   AliCDBEntry *entry2SSD = AliCDBManager::Instance()->Get("ITS/Calib/RespSSD", fRunNumber);
399
400   if(!entrySPD || !entrySDD || !entrySSD || !entry2SPD || !entry2SDD || !entry2SSD){
401     AliWarning("Calibration object retrieval failed! Dummy calibration will be used.");
402     AliCDBStorage *origStorage = AliCDBManager::Instance()->GetDefaultStorage();
403     AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
404         
405     entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSPD", fRunNumber);
406     entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", fRunNumber);
407     entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", fRunNumber);
408     entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", fRunNumber);
409     entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD", fRunNumber);
410     entry2SSD = AliCDBManager::Instance()->Get("ITS/Calib/RespSSD", fRunNumber);
411         
412     AliCDBManager::Instance()->SetDefaultStorage(origStorage);
413   }
414
415
416   TObjArray *respSPD = (TObjArray *)entrySPD->GetObject();
417   entrySPD->SetObject(NULL);
418   entrySPD->SetOwner(kTRUE);
419
420   AliITSresponseSPD *pSPD = (AliITSresponseSPD*)entry2SPD->GetObject();
421   entry2SPD->SetObject(NULL);
422   entry2SPD->SetOwner(kTRUE);
423    
424   TObjArray *respSDD = (TObjArray *)entrySDD->GetObject();
425   entrySDD->SetObject(NULL);
426   entrySDD->SetOwner(kTRUE);
427
428   AliITSresponseSDD *pSDD = (AliITSresponseSDD*)entry2SDD->GetObject();
429   entry2SDD->SetObject(NULL);
430   entry2SDD->SetOwner(kTRUE);
431
432   TObjArray *respSSD = (TObjArray *)entrySSD->GetObject();
433   entrySSD->SetObject(NULL);
434   entrySSD->SetOwner(kTRUE);
435
436   AliITSresponseSSD *pSSD = (AliITSresponseSSD*)entry2SSD->GetObject();
437   entry2SSD->SetObject(NULL);
438   entry2SSD->SetOwner(kTRUE);
439   
440   // DB entries are dleted. In this waymetadeta objects are deleted as well
441   delete entrySPD;
442   delete entrySDD;
443   delete entrySSD;
444   delete entry2SPD;
445   delete entry2SDD;
446   delete entry2SSD;
447   
448   if ((!pSPD)||(!pSDD)||(!pSSD)) {
449     AliWarning("Can not get calibration from calibration database !");
450     return kFALSE;
451   }
452   if ((! respSPD)||(! respSDD)||(! respSSD)) {
453     AliWarning("Can not get calibration from calibration database !");
454     return kFALSE;
455   }
456   fNMod[0] = respSPD->GetEntries();
457   fNMod[1] = respSDD->GetEntries();
458   fNMod[2] = respSSD->GetEntries();
459   AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
460                fNMod[0], fNMod[1], fNMod[2]));
461   AliITSCalibration* res;
462   for (Int_t i=0; i<fNMod[0]; i++) {
463     res = (AliITSCalibration*) respSPD->At(i);
464     res->SetResponse((AliITSresponse*)pSPD);
465     SetCalibrationModel(i, res);
466  }
467   for (Int_t i=0; i<fNMod[1]; i++) {
468     res = (AliITSCalibration*) respSDD->At(i);
469     res->SetResponse(pSDD);
470     Int_t iMod = i + fNMod[0];
471     SetCalibrationModel(iMod, res);
472  }
473   for (Int_t i=0; i<fNMod[2]; i++) {
474     res = (AliITSCalibration*) respSSD->At(i);
475     res->SetResponse((AliITSresponse*)pSSD);
476     Int_t iMod = i + fNMod[0] + fNMod[1];
477     SetCalibrationModel(iMod, res);
478  }
479
480   return kTRUE;
481 }
482
483
484
485 //_______________________________________________________________________
486 void AliITSDetTypeSim::SetDefaultSimulation(){
487
488   //Set default simulation for detector type
489
490   if(fGeom==0){
491     Warning("SetDefaultSimulation","fGeom is 0!");
492     return;
493   }
494   if(fCalibration==0){
495     Warning("SetDefaultSimulation","fCalibration is 0!");
496     return;
497   }
498   if(fSegmentation==0){
499     Warning("SetDefaultSimulation","fSegmentation is 0!");
500     for(Int_t i=0;i<fgkNdettypes;i++) SetDefaultSegmentation(i);
501   }
502   else{
503     for(Int_t i=0;i<fgkNdettypes;i++){
504       if(!GetSegmentationModel(i)){
505         Warning("SetDefaultSimulation","Segmentation not defined for det %d - Default taken\n!",i);
506         SetDefaultSegmentation(i);
507       }
508     }
509   }
510   AliITSsimulation* sim;
511
512   for(Int_t idet=0;idet<fgkNdettypes;idet++){
513    //SPD
514     if(idet==0){
515       sim = GetSimulationModel(idet);
516  
517       if(!sim){
518         sim = new AliITSsimulationSPD(this);
519         SetSimulationModel(idet,sim);
520       }
521     }
522     //SDD
523     if(idet==1){
524       sim = GetSimulationModel(idet);
525       if(!sim){
526         sim = new AliITSsimulationSDD(this);
527         SetSimulationModel(idet,sim);
528       }
529       
530     }
531     //SSD
532     if(idet==2){
533       sim = GetSimulationModel(idet);
534       if(!sim){
535         sim = new AliITSsimulationSSD(this);
536         SetSimulationModel(idet,sim);
537       }
538
539     }
540
541   }
542 }
543
544
545
546
547 //___________________________________________________________________
548 void AliITSDetTypeSim::SetTreeAddressS(TTree* treeS, Char_t* name){
549   // Set branch address for the ITS summable digits Trees.
550   
551   char branchname[30];
552
553   if(!treeS){
554     return;
555   }
556   if (fSDigits == 0x0){
557     fSDigits = new TClonesArray("AliITSpListItem",1000);
558   }
559   TBranch *branch;
560   sprintf(branchname,"%s",name);
561   branch = treeS->GetBranch(branchname);
562   if (branch) branch->SetAddress(&fSDigits);
563
564 }
565 //___________________________________________________________________
566 void AliITSDetTypeSim::SetTreeAddressD(TTree* treeD, Char_t* name){
567   // Set branch address for the digit Trees.
568   
569   const char *det[3] = {"SPD","SDD","SSD"};
570   TBranch *branch;
571   
572   char branchname[30];
573   
574   if(!treeD){
575     return;
576   }
577   if(!fDigits){
578     fDigits = new TObjArray(fgkNdettypes); 
579   }
580   for(Int_t i=0;i<fgkNdettypes;i++){
581     Char_t* digclass = GetDigitClassName(i);
582     if(digclass==0x0){
583       if(i==0) SetDigitClassName(i,"AliITSdigitSPD");
584       if(i==1) SetDigitClassName(i,"AliITSdigitSDD");
585       if(i==2) SetDigitClassName(i,"AliITSdigitSSD");
586       digclass = GetDigitClassName(i);
587     }
588     TString classn = digclass;
589     if(!(fDigits->At(i))){
590       fDigits->AddAt(new TClonesArray(classn.Data(),1000),i);
591     }else{
592       ResetDigits(i);
593     }
594     
595     if(fgkNdettypes==3) sprintf(branchname,"%sDigits%s",name,det[i]);
596     else sprintf(branchname,"%sDigits%d",name,i+1);
597     if(fDigits){
598       branch = treeD->GetBranch(branchname);
599       if(branch) branch->SetAddress(&((*fDigits)[i]));
600     }
601   }
602
603 }
604 //___________________________________________________________________
605 void AliITSDetTypeSim::ResetDigits(){
606   // Reset number of digits and the digits array for the ITS detector.
607   
608
609   if(!fDigits){
610     Error("ResetDigits","fDigits is null!");
611     return;
612   }
613   for(Int_t i=0;i<fgkNdettypes;i++){
614     ResetDigits(i);
615   }
616 }
617 //___________________________________________________________________
618 void AliITSDetTypeSim::ResetDigits(Int_t branch){
619   // Reset number of digits and the digits array for this branch.
620
621   if(fDigits->At(branch)){
622     ((TClonesArray*)fDigits->At(branch))->Clear();
623   }
624   if(fNDigits) fNDigits[branch]=0;
625
626 }
627
628
629
630 //_______________________________________________________________________
631 void AliITSDetTypeSim::SDigitsToDigits(Option_t* opt, Char_t* name){
632   // Standard Summable digits to Digits function.
633   if(!fGeom){
634     Warning("SDigitsToDigits","fGeom is null!!");
635     return;
636   }
637   
638   const char *all = strstr(opt,"All");
639   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
640                         strstr(opt,"SSD")};
641   if( !det[0] && !det[1] && !det[2] ) all = "All";
642   else all = 0;
643   static Bool_t setDef = kTRUE;
644   if(setDef) SetDefaultSimulation();
645   setDef = kFALSE;
646   
647   AliITSsimulation *sim =0;
648   TTree* trees = fLoader->TreeS();
649   if( !(trees && GetSDigits()) ){
650     Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
651     return;
652   } 
653   sprintf(name,"%s",name);
654   TBranch* brchSDigits = trees->GetBranch(name);
655   
656   Int_t id;
657   for(Int_t module=0;module<fGeom->GetIndexMax();module++){
658      id = fGeom->GetModuleType(module);
659     if (!all && !det[id]) continue;
660     sim = (AliITSsimulation*)GetSimulationModel(id);
661     printf("module=%d name=%s\n",module,sim->ClassName());
662     if(!sim){
663       Error("SDigit2Digits","The simulation class was not "
664             "instanciated for module %d type %s!",module,
665             fGeom->GetModuleTypeName(module));
666       exit(1);
667     }
668     sim->InitSimulationModule(module,gAlice->GetEvNumber());
669     
670     fSDigits->Clear();
671     brchSDigits->GetEvent(module);
672     sim->AddSDigitsToModule(fSDigits,0);
673     sim->FinishSDigitiseModule();
674     fLoader->TreeD()->Fill();
675     ResetDigits();
676   }
677   fLoader->TreeD()->GetEntries();
678   fLoader->TreeD()->AutoSave();
679   fLoader->TreeD()->Reset();
680 }
681
682
683
684 //_________________________________________________________
685 void AliITSDetTypeSim::AddSumDigit(AliITSpListItem &sdig){
686   
687   //Adds the module full of summable digits to the summable digits tree.
688   TClonesArray &lsdig = *fSDigits;
689   new(lsdig[fNSDigits++]) AliITSpListItem(sdig);
690 }
691 //__________________________________________________________
692 void AliITSDetTypeSim::AddRealDigit(Int_t branch, Int_t *digits){
693   //   Add a real digit - as coming from data.
694   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
695   new(ldigits[fNDigits[branch]++]) AliITSdigit(digits); 
696 }
697 //__________________________________________________________
698 void AliITSDetTypeSim::AddSimDigit(Int_t branch, AliITSdigit* d){
699   
700   //    Add a simulated digit.
701   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
702   switch(branch){
703   case 0:
704     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
705     break;
706   case 1:
707     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
708     break;
709   case 2:
710     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
711     break;
712   } 
713   
714 }
715
716 //______________________________________________________________________
717 void AliITSDetTypeSim::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,
718                                    Int_t *tracks,Int_t *hits,Float_t *charges){
719   //   Add a simulated digit to the list.
720
721   TClonesArray &ldigits = *((TClonesArray*)fDigits->At(branch));
722   AliITSCalibrationSDD *resp = 0;
723   switch(branch){
724   case 0:
725     new(ldigits[fNDigits[branch]++]) AliITSdigitSPD(digits,tracks,hits);
726     break;
727   case 1:
728     resp = (AliITSCalibrationSDD*)GetCalibrationModel(fGeom->GetStartSDD());
729     new(ldigits[fNDigits[branch]++]) AliITSdigitSDD(phys,digits,tracks,
730                                                    hits,charges,resp);
731     break;
732   case 2:
733     new(ldigits[fNDigits[branch]++]) AliITSdigitSSD(digits,tracks,hits);
734     break;
735   } 
736 }
737
738
739
740 //______________________________________________________________________
741 void AliITSDetTypeSim::StoreCalibration(Int_t firstRun, Int_t lastRun, AliCDBMetaData &md) {
742
743   // Store calibration in the calibration database
744
745   // The database must be created in an external piece of code (i.e. 
746   // a configuration macro )
747
748   if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
749     AliWarning("No storage set! Will use dummy one");
750     AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
751   }
752
753   if (!fCalibration) {
754     AliError("AliITSCalibration classes are not defined - nothing done");
755     return;
756   }
757   AliCDBId idRespSPD("ITS/Calib/CalibSPD",firstRun, lastRun);
758   AliCDBId idRespSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
759   AliCDBId idRespSSD("ITS/Calib/CalibSSD",firstRun, lastRun);
760
761   TObjArray respSPD(fNMod[0]);
762   TObjArray respSDD(fNMod[1]-fNMod[0]);
763   TObjArray respSSD(fNMod[2]-fNMod[1]);
764   respSPD.SetOwner(kFALSE);
765   respSSD.SetOwner(kFALSE);
766   respSSD.SetOwner(kFALSE);
767
768   Int_t index[fgkNdettypes];
769   for (Int_t i = 0; i<fgkNdettypes; i++ ) {
770     index[i] = 0;
771     for (Int_t j = 0; j<=i; j++ )
772       index[i]+=fNMod[j];
773   }
774
775   for (Int_t i = 0; i<index[0]; i++ )
776     respSPD.Add(fCalibration->At(i));
777
778   for (Int_t i = index[0]; i<index[1]; i++ )
779     respSDD.Add(fCalibration->At(i));
780
781   for (Int_t i = index[1]; i<index[2]; i++ )
782     respSSD.Add(fCalibration->At(i));
783
784   AliCDBManager::Instance()->Put(&respSPD, idRespSPD, &md);
785   AliCDBManager::Instance()->Put(&respSDD, idRespSDD, &md);
786   AliCDBManager::Instance()->Put(&respSSD, idRespSSD, &md);
787
788
789
790 }
791
792