]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeRec.cxx
Changes to obey to coding conventions
[u/mrichter/AliRoot.git] / ITS / AliITSDetTypeRec.cxx
1 /***************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Conributors 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 // This class defines the "Standard" reconstruction for the ITS       // 
22 // detector.                                                          //
23 //                                                                    //
24 ////////////////////////////////////////////////////////////////////////
25 #include "TObjArray.h"
26 #include "TTree.h"
27
28 #include "AliCDBManager.h"
29 #include "AliCDBEntry.h"
30 #include "AliITSClusterFinder.h"
31 #include "AliITSClusterFinderV2SPD.h"
32 #include "AliITSClusterFinderV2SDD.h"
33 #include "AliITSClusterFinderV2SSD.h"
34 #include "AliITSDetTypeRec.h"
35 #include "AliITSDDLModuleMapSDD.h"
36 #include "AliITSRecPoint.h"
37 #include "AliITSCalibrationSDD.h"
38 #include "AliITSHLTforSDD.h"
39 #include "AliITSCalibrationSSD.h"
40 #include "AliITSNoiseSSDv2.h"
41 #include "AliITSGainSSDv2.h"
42 #include "AliITSBadChannelsSSDv2.h"
43 #include "AliITSNoiseSSD.h"
44 #include "AliITSGainSSD.h"
45 #include "AliITSBadChannelsSSD.h"
46 #include "AliITSresponseSDD.h"
47 #include "AliITSsegmentationSPD.h"
48 #include "AliITSsegmentationSDD.h"
49 #include "AliITSsegmentationSSD.h"
50 #include "AliLog.h"
51
52 class AliITSDriftSpeedArraySDD;
53 class AliITSMapSDD;
54 class AliITSRecoParam;
55
56 const Int_t AliITSDetTypeRec::fgkNdettypes = 3;
57 const Int_t AliITSDetTypeRec::fgkDefaultNModulesSPD =  240;
58 const Int_t AliITSDetTypeRec::fgkDefaultNModulesSDD =  260;
59 const Int_t AliITSDetTypeRec::fgkDefaultNModulesSSD = 1698;
60
61 ClassImp(AliITSDetTypeRec)
62
63 //________________________________________________________________
64 AliITSDetTypeRec::AliITSDetTypeRec(): TObject(),
65 fNMod(0),
66 fITSgeom(0),
67 fReconstruction(0),
68 fSegmentation(0),
69 fCalibration(0),
70 fSSDCalibration(0),
71 fSPDDead(0),
72 fSPDFastOr(0),
73 fDigits(0),
74 fDDLMapSDD(0),
75 fRespSDD(0),
76 fAveGainSDD(0),
77 fIsHLTmodeC(0),
78 fRecPoints(0),
79 fNRecPoints(0),
80 fFirstcall(kTRUE),
81 fLoadOnlySPDCalib(0),
82 fFastOrFiredMap(1200){
83     // Standard Constructor
84     // Inputs:
85     //    none.
86     // Outputs:
87     //    none.
88     // Return:
89     //   
90
91   fReconstruction = new TObjArray(fgkNdettypes);
92   fDigits = new TObjArray(fgkNdettypes);
93   for(Int_t i=0; i<3; i++){
94     fkDigClassName[i]=0;
95   }
96   fSSDCalibration=new AliITSCalibrationSSD();
97   fNMod = new Int_t [fgkNdettypes];
98   fNMod[0] = fgkDefaultNModulesSPD;
99   fNMod[1] = fgkDefaultNModulesSDD;
100   fNMod[2] = fgkDefaultNModulesSSD;
101   fRecPoints = new TClonesArray("AliITSRecPoint",3000);
102   fNRecPoints = 0;
103   
104   
105 }
106
107 //______________________________________________________________________
108 AliITSDetTypeRec::AliITSDetTypeRec(const AliITSDetTypeRec & rec):TObject(rec),
109 fNMod(rec.fNMod),
110 fITSgeom(rec.fITSgeom),
111 fReconstruction(rec.fReconstruction),
112 fSegmentation(rec.fSegmentation),
113 fCalibration(rec.fCalibration),
114 fSSDCalibration(rec.fSSDCalibration),
115 fSPDDead(rec.fSPDDead),
116 fSPDFastOr(rec.fSPDFastOr),
117 fDigits(rec.fDigits),
118 fDDLMapSDD(rec.fDDLMapSDD),
119 fRespSDD(rec.fRespSDD),
120 fAveGainSDD(rec.fAveGainSDD),
121 fIsHLTmodeC(rec.fIsHLTmodeC),
122 fRecPoints(rec.fRecPoints),
123 fNRecPoints(rec.fNRecPoints),
124 fFirstcall(rec.fFirstcall),
125 fLoadOnlySPDCalib(rec.fLoadOnlySPDCalib),
126 fFastOrFiredMap(rec.fFastOrFiredMap){
127
128   // Copy constructor. 
129
130 }
131 //______________________________________________________________________
132 AliITSDetTypeRec& AliITSDetTypeRec::operator=(const AliITSDetTypeRec& source){
133     // Assignment operator. 
134     this->~AliITSDetTypeRec();
135     new(this) AliITSDetTypeRec(source);
136     return *this;
137
138 }
139
140 //_____________________________________________________________________
141 AliITSDetTypeRec::~AliITSDetTypeRec(){
142   //Destructor
143
144   if(fReconstruction){
145     fReconstruction->Delete();
146     delete fReconstruction;
147     fReconstruction = 0;
148   }
149   if(fSegmentation){
150     fSegmentation->Delete();
151     delete fSegmentation;
152     fSegmentation = 0;
153   }
154   if(fCalibration){
155     if(!(AliCDBManager::Instance()->GetCacheFlag())) {
156       fCalibration->Delete();
157       delete fCalibration;
158       fCalibration = 0;
159       if(fRespSDD) delete fRespSDD;
160       if(fDDLMapSDD) delete fDDLMapSDD;
161    }
162   }
163   if(fSSDCalibration) delete fSSDCalibration;
164    if(fSPDDead){
165     if(!(AliCDBManager::Instance()->GetCacheFlag())) {
166       fSPDDead->Delete();
167       delete fSPDDead;
168       fSPDDead = 0;
169     }
170   }  
171   if(fDigits){
172     fDigits->Delete();
173     delete fDigits;
174     fDigits=0;
175   }
176   if(fRecPoints){
177     fRecPoints->Delete();
178     delete fRecPoints;
179     fRecPoints=0;
180   }
181   delete [] fNMod;
182   
183   if (fITSgeom) delete fITSgeom;
184  
185 }
186
187 //___________________________________________________________________
188 void AliITSDetTypeRec::SetReconstructionModel(Int_t dettype,AliITSClusterFinder *clf){
189
190   //Set reconstruction model for detector type
191
192   if(fReconstruction==0) fReconstruction = new TObjArray(fgkNdettypes);
193   if(fReconstruction->At(dettype)!=0) delete fReconstruction->At(dettype);
194   fReconstruction->AddAt(clf,dettype);
195 }
196 //______________________________________________________________________
197 AliITSClusterFinder* AliITSDetTypeRec::GetReconstructionModel(Int_t dettype) const{
198
199   //Get reconstruction model for detector type
200   if(fReconstruction==0)  {
201     Warning("GetReconstructionModel","fReconstruction is 0!");
202     return 0;     
203   }
204   return (AliITSClusterFinder*)fReconstruction->At(dettype);
205 }
206
207 //______________________________________________________________________
208 void AliITSDetTypeRec::SetSegmentationModel(Int_t dettype,AliITSsegmentation *seg){
209    
210   //Set segmentation model for detector type
211   
212   if(fSegmentation==0) fSegmentation = new TObjArray(fgkNdettypes);
213   if(fSegmentation->At(dettype)!=0) delete fSegmentation->At(dettype);
214   fSegmentation->AddAt(seg,dettype);
215
216 }
217 //______________________________________________________________________
218 AliITSsegmentation* AliITSDetTypeRec::GetSegmentationModel(Int_t dettype) const {
219
220   //Get segmentation model for detector type
221    
222    if(fSegmentation==0) {
223      Warning("GetSegmentationModel","fSegmentation is 0!");
224      return 0; 
225    } 
226    return (AliITSsegmentation*)fSegmentation->At(dettype);
227
228 }
229 //_______________________________________________________________________
230 void AliITSDetTypeRec::SetCalibrationModel(Int_t iMod, AliITSCalibration *cal){
231
232   //Set calibration (response) for the module iMod of type dettype
233   if (fCalibration==0) {
234     fCalibration = new TObjArray(GetITSgeom()->GetIndexMax());
235     fCalibration->SetOwner(kTRUE);
236     fCalibration->Clear();
237   }
238
239   if (fCalibration->At(iMod) != 0)
240     delete (AliITSCalibration*) fCalibration->At(iMod);
241   fCalibration->AddAt(cal,iMod);
242
243 }
244 //_______________________________________________________________________
245 void AliITSDetTypeRec::SetSPDDeadModel(Int_t iMod, AliITSCalibration *cal){
246
247   //Set dead pixel info for the SPD module iMod
248   if (fSPDDead==0) {
249     fSPDDead = new TObjArray(fgkDefaultNModulesSPD);
250     fSPDDead->SetOwner(kTRUE);
251     fSPDDead->Clear();
252   }
253
254   if (fSPDDead->At(iMod) != 0)
255     delete (AliITSCalibration*) fSPDDead->At(iMod);
256   fSPDDead->AddAt(cal,iMod);
257 }
258 //_______________________________________________________________________
259 AliITSCalibration* AliITSDetTypeRec::GetCalibrationModel(Int_t iMod) const {
260   
261   //Get calibration model for module type
262   
263   if(fCalibration==0) {
264     Warning("GetalibrationModel","fCalibration is 0!");
265     return 0; 
266   }  
267
268   if(iMod<fgkDefaultNModulesSPD+fgkDefaultNModulesSDD){
269     return (AliITSCalibration*)fCalibration->At(iMod);
270   }else{
271     Int_t i=iMod-(fgkDefaultNModulesSPD+fgkDefaultNModulesSDD);
272     fSSDCalibration->SetModule(i);
273     return (AliITSCalibration*)fSSDCalibration;
274   }
275
276 }
277 //_______________________________________________________________________
278 AliITSCalibration* AliITSDetTypeRec::GetSPDDeadModel(Int_t iMod) const {
279   
280   //Get SPD dead for module iMod
281   
282   if(fSPDDead==0) {
283     AliWarning("fSPDDead is 0!");
284     return 0; 
285   }  
286
287   return (AliITSCalibration*)fSPDDead->At(iMod);
288 }
289
290 //______________________________________________________________________
291 void AliITSDetTypeRec::SetTreeAddressD(TTree* const treeD){
292     // Set branch address for the tree of digits.
293
294     const char *det[4] = {"SPD","SDD","SSD","ITS"};
295     TBranch *branch;
296     const Char_t* digclass;
297     Int_t i;
298     char branchname[30];
299
300     if(!treeD) return;
301     if (fDigits == 0x0) fDigits = new TObjArray(fgkNdettypes);
302     for (i=0; i<fgkNdettypes; i++) {
303         digclass = GetDigitClassName(i);
304         if(!(fDigits->At(i))) {
305             fDigits->AddAt(new TClonesArray(digclass,1000),i);
306         }else{
307             ResetDigits(i);
308         } 
309         if (fgkNdettypes==3) sprintf(branchname,"%sDigits%s",det[3],det[i]);
310         else  sprintf(branchname,"%sDigits%d",det[3],i+1);
311         if (fDigits) {
312             branch = treeD->GetBranch(branchname);
313             if (branch) branch->SetAddress(&((*fDigits)[i]));
314         } 
315     } 
316 }
317
318 //_______________________________________________________________________
319 TBranch* AliITSDetTypeRec::MakeBranchInTree(TTree* const tree, 
320                            const char* name, const char *classname, 
321                            void* address,Int_t size,Int_t splitlevel)
322
323 //
324 // Makes branch in given tree and diverts them to a separate file
325 // 
326 //
327 //
328     
329   if (tree == 0x0) {
330     Error("MakeBranchInTree","Making Branch %s Tree is NULL",name);
331     return 0x0;
332   }
333   TBranch *branch = tree->GetBranch(name);
334   if (branch) {  
335     return branch;
336   }
337   if (classname){
338     branch = tree->Branch(name,classname,address,size,splitlevel);
339   }
340   else {
341     branch = tree->Bronch(name, "TClonesArray", address, size, splitlevel);
342   }
343   
344   return branch;
345 }
346
347 //____________________________________________________________________
348 void AliITSDetTypeRec::SetDefaults(){
349   
350   //Set defaults for segmentation and response
351
352   if(!GetITSgeom()){
353     Warning("SetDefaults","null pointer to AliITSgeomGeom !");
354     return;
355   }
356
357   AliITSsegmentation* seg;
358   if(!GetCalibration()) {AliFatal("Exit");exit(0);}  
359
360   for(Int_t dettype=0;dettype<fgkNdettypes;dettype++){
361     if(dettype==0){
362       seg = new AliITSsegmentationSPD();
363       SetSegmentationModel(dettype,seg);
364       SetDigitClassName(dettype,"AliITSdigitSPD");
365     }
366     if(fLoadOnlySPDCalib==kFALSE){
367       if(dettype==1){
368         seg = new AliITSsegmentationSDD();
369         AliITSCalibrationSDD* cal=(AliITSCalibrationSDD*)GetCalibrationModel(fgkDefaultNModulesSPD+1);
370         if(cal->IsAMAt20MHz()){ 
371           seg->SetPadSize(seg->Dpz(0),20.);
372           seg->SetNPads(seg->Npz()/2,128);
373         }
374         SetSegmentationModel(dettype,seg);
375         SetDigitClassName(dettype,"AliITSdigitSDD");
376       }
377     }
378     if(dettype==2){
379       AliITSsegmentationSSD* seg2 = new AliITSsegmentationSSD();
380       SetSegmentationModel(dettype,seg2);
381       SetDigitClassName(dettype,"AliITSdigitSSD");
382     }
383   }
384 }
385 //______________________________________________________________________
386 Bool_t AliITSDetTypeRec::GetCalibration() {
387   // Get Default calibration if a storage is not defined.
388
389   if(!fFirstcall){
390     AliITSCalibration* cal = GetCalibrationModel(0);
391     if(cal)return kTRUE;
392   }else {
393     fFirstcall = kFALSE;
394   }
395
396   //  SetRunNumber((Int_t)AliCDBManager::Instance()->GetRun());
397   //  Int_t run=GetRunNumber();
398
399   Bool_t cacheStatus = AliCDBManager::Instance()->GetCacheFlag();
400   if (fCalibration==0) {
401     fCalibration = new TObjArray(GetITSgeom()->GetIndexMax());
402     fCalibration->SetOwner(!cacheStatus);
403     fCalibration->Clear();
404   }
405     
406   Bool_t retCode=GetCalibrationSPD(cacheStatus);
407   if(retCode==kFALSE) return kFALSE;
408
409   if(fLoadOnlySPDCalib==kFALSE){
410     retCode=GetCalibrationSDD(cacheStatus);
411     if(retCode==kFALSE) return kFALSE;
412     retCode=GetCalibrationSSD(cacheStatus);
413     if(retCode==kFALSE) return kFALSE;
414   }
415
416   AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
417                fNMod[0], fNMod[1], fNMod[2]));
418   return kTRUE;
419 }
420 //______________________________________________________________________
421 Bool_t AliITSDetTypeRec::GetCalibrationSPD(Bool_t cacheStatus) {
422   // Get SPD calibration objects from OCDB
423   // dead pixel are not used for local reconstruction
424
425   AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDNoisy");
426   AliCDBEntry *deadSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDDead");
427   AliCDBEntry *fastOrSPD = AliCDBManager::Instance()->Get("ITS/Calib/SPDFastOr");
428   if(!entrySPD || !deadSPD || !fastOrSPD ){
429     AliFatal("SPD Calibration object retrieval failed! ");
430     return kFALSE;
431   }     
432
433   TObjArray *calSPD = (TObjArray *)entrySPD->GetObject();
434   if(!cacheStatus)entrySPD->SetObject(NULL);
435   entrySPD->SetOwner(kTRUE);
436  
437   TObjArray *caldeadSPD = (TObjArray *)deadSPD->GetObject();
438   if(!cacheStatus)deadSPD->SetObject(NULL);
439   deadSPD->SetOwner(kTRUE);
440
441   AliITSFastOrCalibrationSPD *calfastOrSPD = (AliITSFastOrCalibrationSPD *)fastOrSPD->GetObject();
442   if(!cacheStatus)fastOrSPD->SetObject(NULL);
443   fastOrSPD->SetOwner(kTRUE);
444
445   if(!cacheStatus){
446     delete entrySPD;
447     delete deadSPD;
448     delete fastOrSPD;
449   }
450   if ((!calSPD) || (!caldeadSPD) || (!calfastOrSPD)){ 
451     AliWarning("Can not get SPD calibration from calibration database !");
452     return kFALSE;
453   }
454
455   fNMod[0] = calSPD->GetEntries();
456
457   AliITSCalibration* cal;
458   for (Int_t i=0; i<fNMod[0]; i++) {
459     cal = (AliITSCalibration*) calSPD->At(i);
460     SetCalibrationModel(i, cal);
461     cal = (AliITSCalibration*) caldeadSPD->At(i);
462     SetSPDDeadModel(i, cal);
463   }
464   fSPDFastOr = calfastOrSPD;
465
466   return kTRUE;
467 }
468
469 //______________________________________________________________________
470 Bool_t AliITSDetTypeRec::GetCalibrationSDD(Bool_t cacheStatus) {
471   // Get SDD calibration objects from OCDB
472
473   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD");
474   AliCDBEntry *entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD");
475   AliCDBEntry *drSpSDD = AliCDBManager::Instance()->Get("ITS/Calib/DriftSpeedSDD");
476   AliCDBEntry *ddlMapSDD = AliCDBManager::Instance()->Get("ITS/Calib/DDLMapSDD");
477   AliCDBEntry *hltforSDD = AliCDBManager::Instance()->Get("ITS/Calib/HLTforSDD");
478   //   AliCDBEntry *mapASDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsAnodeSDD");
479   AliCDBEntry *mapTSDD = AliCDBManager::Instance()->Get("ITS/Calib/MapsTimeSDD");
480
481   if(!entrySDD || !entry2SDD || !drSpSDD || !ddlMapSDD || !hltforSDD || !mapTSDD ){
482     AliFatal("SDD Calibration object retrieval failed! ");
483     return kFALSE;
484   }     
485
486
487     
488   TObjArray *calSDD = (TObjArray *)entrySDD->GetObject();
489   if(!cacheStatus)entrySDD->SetObject(NULL);
490   entrySDD->SetOwner(kTRUE);
491  
492   AliITSresponseSDD *pSDD = (AliITSresponseSDD*)entry2SDD->GetObject();
493   if(!cacheStatus)entry2SDD->SetObject(NULL);
494   entry2SDD->SetOwner(kTRUE);
495
496   TObjArray *drSp = (TObjArray *)drSpSDD->GetObject();
497   if(!cacheStatus)drSpSDD->SetObject(NULL);
498   drSpSDD->SetOwner(kTRUE);
499
500   AliITSDDLModuleMapSDD *ddlsdd=(AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
501   if(!cacheStatus)ddlMapSDD->SetObject(NULL);
502   ddlMapSDD->SetOwner(kTRUE);
503
504   AliITSHLTforSDD* hltsdd=(AliITSHLTforSDD*)hltforSDD->GetObject();
505   if(!cacheStatus)hltforSDD->SetObject(NULL);
506   hltforSDD->SetOwner(kTRUE);
507   
508 //   TObjArray *mapAn = (TObjArray *)mapASDD->GetObject();
509 //   if(!cacheStatus)mapASDD->SetObject(NULL);
510 //   mapASDD->SetOwner(kTRUE);
511
512   TObjArray *mapT = (TObjArray *)mapTSDD->GetObject();
513   if(!cacheStatus)mapTSDD->SetObject(NULL);
514   mapTSDD->SetOwner(kTRUE);
515
516
517   // DB entries are deleted. In this way metadeta objects are deleted as well
518   if(!cacheStatus){
519     delete entrySDD;
520     delete entry2SDD;
521     //delete mapASDD;
522     delete hltforSDD;
523     delete mapTSDD;
524     delete drSpSDD;
525     delete ddlMapSDD;
526   }
527
528   if ((!pSDD)||(!calSDD) || (!drSp) || (!ddlsdd) || (!hltsdd) || (!mapT) ){
529     AliWarning("Can not get SDD calibration from calibration database !");
530     return kFALSE;
531   }
532
533   fNMod[1] = calSDD->GetEntries();
534
535   fDDLMapSDD=ddlsdd;
536   fRespSDD=pSDD;
537   fIsHLTmodeC=hltsdd->IsHLTmodeC();
538   AliITSCalibration* cal;
539   Float_t avegain=0.;
540   Float_t nGdAnodes=0;
541   for(Int_t iddl=0; iddl<AliITSDDLModuleMapSDD::GetNDDLs(); iddl++){
542     for(Int_t icar=0; icar<AliITSDDLModuleMapSDD::GetNModPerDDL();icar++){
543       Int_t iMod=fDDLMapSDD->GetModuleNumber(iddl,icar);
544       if(iMod==-1) continue;
545       Int_t i=iMod - fgkDefaultNModulesSPD;
546       cal = (AliITSCalibration*) calSDD->At(i);
547       Int_t i0=2*i;
548       Int_t i1=1+2*i;
549       for(Int_t iAnode=0;iAnode< ((AliITSCalibrationSDD*)cal)->NOfAnodes(); iAnode++){
550         if(((AliITSCalibrationSDD*)cal)->IsBadChannel(iAnode)) continue;
551         avegain+= ((AliITSCalibrationSDD*)cal)->GetChannelGain(iAnode);
552         nGdAnodes++;
553       }
554       AliITSDriftSpeedArraySDD* arr0 = (AliITSDriftSpeedArraySDD*) drSp->At(i0);
555       //      AliITSMapSDD* ma0 = (AliITSMapSDD*)mapAn->At(i0);
556       AliITSMapSDD* mt0 = (AliITSMapSDD*)mapT->At(i0);
557       AliITSDriftSpeedArraySDD* arr1 = (AliITSDriftSpeedArraySDD*) drSp->At(i1);
558       //      AliITSMapSDD* ma1 = (AliITSMapSDD*)mapAn->At(i1);
559       AliITSMapSDD* mt1 = (AliITSMapSDD*)mapT->At(i1);
560       cal->SetDriftSpeed(0,arr0);
561       cal->SetDriftSpeed(1,arr1);
562 //       cal->SetMapA(0,ma0);
563 //       cal->SetMapA(1,ma1);
564       cal->SetMapT(0,mt0);
565       cal->SetMapT(1,mt1);
566       SetCalibrationModel(iMod, cal);
567     }
568   }
569   if(nGdAnodes) fAveGainSDD=avegain/nGdAnodes;
570   return kTRUE;
571 }
572
573
574 //______________________________________________________________________
575 Bool_t AliITSDetTypeRec::GetCalibrationSSD(Bool_t cacheStatus) {
576   // Get SSD calibration objects from OCDB
577   //  AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD");
578
579   AliCDBEntry *entryNoiseSSD = AliCDBManager::Instance()->Get("ITS/Calib/NoiseSSD");
580   AliCDBEntry *entryGainSSD = AliCDBManager::Instance()->Get("ITS/Calib/GainSSD");
581   AliCDBEntry *entryBadChannelsSSD = AliCDBManager::Instance()->Get("ITS/Calib/BadChannelsSSD");
582
583   if(!entryNoiseSSD || !entryGainSSD || !entryBadChannelsSSD){
584     AliFatal("SSD Calibration object retrieval failed! ");
585     return kFALSE;
586   }     
587
588   TObject *emptyssd = 0; TString ssdobjectname = 0;
589   AliITSNoiseSSDv2 *noiseSSD = new AliITSNoiseSSDv2();  
590   emptyssd = (TObject *)entryNoiseSSD->GetObject();
591   ssdobjectname = emptyssd->GetName();
592   if(ssdobjectname=="TObjArray") {
593     TObjArray *noiseSSDOld = (TObjArray *)entryNoiseSSD->GetObject();
594     ReadOldSSDNoise(noiseSSDOld, noiseSSD);
595   }
596   else if(ssdobjectname=="AliITSNoiseSSDv2")
597     noiseSSD = (AliITSNoiseSSDv2 *)entryNoiseSSD->GetObject();
598   if(!cacheStatus)entryNoiseSSD->SetObject(NULL);
599   entryNoiseSSD->SetOwner(kTRUE);
600
601   AliITSGainSSDv2 *gainSSD = new AliITSGainSSDv2();
602   emptyssd = (TObject *)entryGainSSD->GetObject();
603   ssdobjectname = emptyssd->GetName();
604   if(ssdobjectname=="Gain") {
605     TObjArray *gainSSDOld = (TObjArray *)entryGainSSD->GetObject();
606     ReadOldSSDGain(gainSSDOld, gainSSD);
607   }
608   else if(ssdobjectname=="AliITSGainSSDv2")
609     gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
610   if(!cacheStatus)entryGainSSD->SetObject(NULL);
611   entryGainSSD->SetOwner(kTRUE);
612
613   AliITSBadChannelsSSDv2 *badChannelsSSD = new AliITSBadChannelsSSDv2();
614   emptyssd = (TObject *)entryBadChannelsSSD->GetObject();
615   ssdobjectname = emptyssd->GetName();
616   if(ssdobjectname=="TObjArray") {
617     TObjArray *badChannelsSSDOld = (TObjArray *)entryBadChannelsSSD->GetObject();
618     ReadOldSSDBadChannels(badChannelsSSDOld, badChannelsSSD);
619   }
620   else if(ssdobjectname=="AliITSBadChannelsSSDv2")
621     badChannelsSSD = (AliITSBadChannelsSSDv2*)entryBadChannelsSSD->GetObject();
622   if(!cacheStatus)entryBadChannelsSSD->SetObject(NULL);
623   entryBadChannelsSSD->SetOwner(kTRUE);
624
625   // DB entries are deleted. In this way metadeta objects are deleted as well
626   if(!cacheStatus){
627     delete entryNoiseSSD;
628     delete entryGainSSD;
629     delete entryBadChannelsSSD;
630   }
631
632   if ((!noiseSSD)|| (!gainSSD)|| (!badChannelsSSD)) {
633     AliWarning("Can not get SSD calibration from calibration database !");
634     return kFALSE;
635   }
636
637   fSSDCalibration->SetNoise(noiseSSD);
638   fSSDCalibration->SetGain(gainSSD);
639   fSSDCalibration->SetBadChannels(badChannelsSSD);
640   //fSSDCalibration->FillBadChipMap();
641
642   return kTRUE;
643 }
644
645 //________________________________________________________________
646 void AliITSDetTypeRec::SetDefaultClusterFindersV2(Bool_t rawdata){
647
648   //Set defaults for cluster finder V2
649
650   if(!GetITSgeom()){
651     Warning("SetDefaults","Null pointer to AliITSgeom !");
652     return;
653   }
654
655   AliITSClusterFinder *clf; 
656
657   for(Int_t dettype=0;dettype<fgkNdettypes;dettype++){
658     //SPD
659     if(dettype==0){
660       if(!GetReconstructionModel(dettype)){
661         clf = new AliITSClusterFinderV2SPD(this);
662         clf->InitGeometry();
663         if(!rawdata) clf->SetDigits(DigitsAddress(0));
664         SetReconstructionModel(dettype,clf);
665
666       }
667     }
668     //SDD
669     if(dettype==1){
670       if(!GetReconstructionModel(dettype)){
671         clf = new AliITSClusterFinderV2SDD(this);
672         clf->InitGeometry();
673         if(!rawdata) clf->SetDigits(DigitsAddress(1));
674         SetReconstructionModel(dettype,clf);
675       }
676
677     }
678
679     //SSD
680     if(dettype==2){
681       if(!GetReconstructionModel(dettype)){
682         clf = new AliITSClusterFinderV2SSD(this);
683         clf->InitGeometry();
684         if(!rawdata) clf->SetDigits(DigitsAddress(2));
685         SetReconstructionModel(dettype,clf);
686       }
687     }
688
689  }
690    
691 }
692 //______________________________________________________________________
693 void AliITSDetTypeRec::MakeBranch(TTree* tree, Option_t* option){
694
695   //Creates branches for clusters and recpoints
696   Bool_t cR = (strstr(option,"R")!=0);
697   Bool_t cRF = (strstr(option,"RF")!=0);
698   
699   if(cRF)cR = kFALSE;
700
701   if(cR) MakeBranchR(tree);
702   if(cRF) MakeBranchRF(tree);
703
704 }
705
706 //___________________________________________________________________
707 void AliITSDetTypeRec::ResetDigits(){
708   // Reset number of digits and the digits array for the ITS detector.
709   
710   if(!fDigits) return;
711   for(Int_t i=0;i<fgkNdettypes;i++){
712     ResetDigits(i);
713   }
714 }
715 //___________________________________________________________________
716 void AliITSDetTypeRec::ResetDigits(Int_t branch){
717   // Reset number of digits and the digits array for this branch.
718   
719   if(fDigits->At(branch)) ((TClonesArray*)fDigits->At(branch))->Clear();
720
721 }
722 //__________________________________________________________________
723 void AliITSDetTypeRec::MakeBranchR(TTree *treeR, Option_t *opt){
724
725   //Creates tree branches for recpoints
726   // Inputs:
727   //      cont char *file  File name where RecPoints branch is to be written
728   //                       to. If blank it write the SDigits to the same
729   //                       file in which the Hits were found.
730
731   Int_t buffsz = 4000;
732   char branchname[30];
733
734   // only one branch for rec points for all detector types
735   Bool_t oFast= (strstr(opt,"Fast")!=0);
736   
737   Char_t detname[10] = "ITS";
738  
739   
740   if(oFast){
741     sprintf(branchname,"%sRecPointsF",detname);
742   } else {
743     sprintf(branchname,"%sRecPoints",detname);
744   }
745   
746   if(!fRecPoints)fRecPoints = new TClonesArray("AliITSRecPoint",1000);
747   if (treeR)
748     MakeBranchInTree(treeR,branchname,0,&fRecPoints,buffsz,99);
749 }
750 //______________________________________________________________________
751 void AliITSDetTypeRec::SetTreeAddressR(TTree* const treeR){
752     // Set branch address for the Reconstructed points Trees.
753     // Inputs:
754     //      TTree *treeR   Tree containing the RecPoints.
755     // Outputs:
756     //      none.
757     // Return:
758
759     char branchname[30];
760     Char_t namedet[10]="ITS";
761
762     if(!treeR) return;
763     if(fRecPoints==0x0) fRecPoints = new TClonesArray("AliITSRecPoint",1000);
764     TBranch *branch;
765     sprintf(branchname,"%sRecPoints",namedet);
766     branch = treeR->GetBranch(branchname);
767     if (branch) {
768       branch->SetAddress(&fRecPoints);
769     }else {
770       sprintf(branchname,"%sRecPointsF",namedet);
771       branch = treeR->GetBranch(branchname);
772       if (branch) {
773         branch->SetAddress(&fRecPoints);
774       }
775
776     }
777 }
778 //____________________________________________________________________
779 void AliITSDetTypeRec::AddRecPoint(const AliITSRecPoint &r){
780     // Add a reconstructed space point to the list
781     // Inputs:
782     //      const AliITSRecPoint &r RecPoint class to be added to the tree
783     //                              of reconstructed points TreeR.
784     // Outputs:
785     //      none.
786     // Return:
787     //      none.
788
789     TClonesArray &lrecp = *fRecPoints;
790     new(lrecp[fNRecPoints++]) AliITSRecPoint(r);
791 }
792
793 //______________________________________________________________________
794 void AliITSDetTypeRec::DigitsToRecPoints(TTree *treeD,TTree *treeR,Int_t lastentry,Option_t *opt, Int_t optCluFind){
795   // cluster finding and reconstruction of space points
796   // the condition below will disappear when the geom class will be
797   // initialized for all versions - for the moment it is only for v5 !
798   // 7 is the SDD beam test version
799   // Inputs:
800   //      TTree *treeD     Digits tree
801   //      TTree *treeR     Clusters tree
802   //      Int_t lastentry  Offset for module when not all of the modules
803   //                       are processed.
804   //      Option_t *opt    String indicating which ITS sub-detectors should
805   //                       be processed. If ="All" then all of the ITS
806   //                       sub detectors are processed.
807
808   const char *all = strstr(opt,"All");
809   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
810                         strstr(opt,"SSD")};
811   if(optCluFind==0){
812     SetDefaultClusterFindersV2();
813     AliInfo("V2 cluster finder has been selected \n");
814   }else{
815     SetDefaultClusterFindersV2();
816     AliInfo("Cluster Finder Option not implemented, V2 cluster finder will be used \n");    
817   }
818
819   AliITSClusterFinder *rec     = 0;
820   Int_t id,module,first=0;
821   for(module=0;module<GetITSgeom()->GetIndexMax();module++){
822       id       = GetITSgeom()->GetModuleType(module);
823       if (!all && !det[id]) continue;
824       if(det[id]) first = GetITSgeom()->GetStartDet(id);
825       rec = (AliITSClusterFinder*)GetReconstructionModel(id);
826       TClonesArray *itsDigits  = DigitsAddress(id);
827       if (!rec)
828           AliFatal("The reconstruction class was not instanciated!");
829       ResetDigits();  // MvL: Not sure we neeed this when rereading anyways
830       if (all) {
831           treeD->GetEvent(lastentry+module);
832       }else {
833           treeD->GetEvent(lastentry+(module-first));
834       }
835       Int_t ndigits = itsDigits->GetEntriesFast();
836       if(ndigits>0){
837         rec->SetDetTypeRec(this);
838         rec->SetDigits(DigitsAddress(id));
839         //      rec->SetClusters(ClustersAddress(id));
840         rec->FindRawClusters(module);
841       } // end if
842       treeR->Fill();
843       ResetRecPoints();
844   } 
845 }
846 //______________________________________________________________________
847 void AliITSDetTypeRec::DigitsToRecPoints(AliRawReader* rawReader,TTree *treeR,Option_t *opt){
848   // cluster finding and reconstruction of space points
849   // the condition below will disappear when the geom class will be
850   // initialized for all versions - for the moment it is only for v5 !
851   // 7 is the SDD beam test version
852   // Inputs:
853   //      AliRawReader *rawReader  Pointer to the raw-data reader
854   //      TTree *treeR             Clusters tree
855   // Outputs:
856   //      none.
857   // Return:
858   //      none.
859   const char *all = strstr(opt,"All");
860   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
861                         strstr(opt,"SSD")};
862   AliITSClusterFinder *rec     = 0;
863   Int_t id=0;
864
865   TClonesArray *array=new TClonesArray("AliITSRecPoint",1000);
866   TBranch *branch = treeR->Branch("ITSRecPoints",&array);
867   delete array;
868  
869   TClonesArray** clusters = new TClonesArray*[GetITSgeom()->GetIndexMax()]; 
870   for (Int_t iModule = 0; iModule < GetITSgeom()->GetIndexMax(); iModule++) {
871     clusters[iModule] = NULL;
872   }
873   for(id=0;id<3;id++){
874     if (!all && !det[id]) continue;
875     rec = (AliITSClusterFinder*)GetReconstructionModel(id);
876     if (!rec)
877       AliFatal("The reconstruction class was not instantiated");
878     rec->SetDetTypeRec(this);
879     rec->RawdataToClusters(rawReader,clusters);    
880   } 
881   Int_t nClusters =0;
882   TClonesArray *emptyArray=new TClonesArray("AliITSRecPoint");
883   for(Int_t iModule=0;iModule<GetITSgeom()->GetIndexMax();iModule++){
884     id = GetITSgeom()->GetModuleType(iModule);
885     if (!all && !det[id]) continue;
886     array = clusters[iModule];
887     if(!array){
888       AliDebug(1,Form("data for module %d missing!",iModule));
889       array = emptyArray;
890     }
891     branch->SetAddress(&array);
892     treeR->Fill();
893     nClusters+=array->GetEntriesFast();
894
895     if (array != emptyArray) {
896       array->Delete();
897       delete array;
898     }
899   }
900   delete emptyArray;
901
902   delete[] clusters;
903   Info("DigitsToRecPoints", "total number of found recpoints in ITS: %d\n", 
904        nClusters);
905   
906 }
907
908 //______________________________________________________________________
909 void AliITSDetTypeRec::ReadOldSSDNoise(const TObjArray *array, 
910                                        AliITSNoiseSSDv2 *noiseSSD) {
911   //Reads the old SSD calibration object and converts it to the new format
912   const Int_t fgkSSDSTRIPSPERMODULE = 1536;
913   const Int_t fgkSSDPSIDESTRIPSPERMODULE = 768;
914
915   Int_t gNMod = array->GetEntries();
916   cout<<"Converting old calibration object for noise..."<<endl;
917
918   //NOISE
919   Double_t noise = 0.0;
920   for (Int_t iModule = 0; iModule < gNMod; iModule++) {
921     AliITSNoiseSSD *noiseModule = (AliITSNoiseSSD*) (array->At(iModule));
922     for(Int_t iStrip = 0; iStrip < fgkSSDSTRIPSPERMODULE; iStrip++) {
923       noise = (iStrip < fgkSSDPSIDESTRIPSPERMODULE) ? noiseModule->GetNoiseP(iStrip) : noiseModule->GetNoiseN(1535 - iStrip);
924       if(iStrip < fgkSSDPSIDESTRIPSPERMODULE)
925         noiseSSD->AddNoiseP(iModule,iStrip,noise);
926       if(iStrip >= fgkSSDPSIDESTRIPSPERMODULE)
927         noiseSSD->AddNoiseN(iModule,1535 - iStrip,noise);
928     }//loop over strips
929   }//loop over modules      
930 }
931
932 //______________________________________________________________________
933 void AliITSDetTypeRec::ReadOldSSDBadChannels(const TObjArray *array, 
934                                              AliITSBadChannelsSSDv2 *badChannelsSSD) {
935   //Reads the old SSD calibration object and converts it to the new format
936   Int_t gNMod = array->GetEntries();
937   cout<<"Converting old calibration object for bad channels..."<<endl;
938   for (Int_t iModule = 0; iModule < gNMod; iModule++) {
939     //for (Int_t iModule = 0; iModule < 1; iModule++) {
940     AliITSBadChannelsSSD *bad = (AliITSBadChannelsSSD*) (array->At(iModule));
941     TArrayI arrayPSide = bad->GetBadPChannelsList();
942     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++) 
943       badChannelsSSD->AddBadChannelP(iModule,
944                                      iPCounter,
945                                      (Char_t)arrayPSide.At(iPCounter));
946         
947     TArrayI arrayNSide = bad->GetBadNChannelsList();
948     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++) 
949       badChannelsSSD->AddBadChannelN(iModule,
950                                      iNCounter,
951                                      (Char_t)arrayNSide.At(iNCounter));
952     
953   }//loop over modules      
954 }
955
956 //______________________________________________________________________
957 void AliITSDetTypeRec::ReadOldSSDGain(const TObjArray *array, 
958                                       AliITSGainSSDv2 *gainSSD) {
959   //Reads the old SSD calibration object and converts it to the new format
960
961   Int_t gNMod = array->GetEntries();
962   cout<<"Converting old calibration object for gain..."<<endl;
963
964   //GAIN
965   for (Int_t iModule = 0; iModule < gNMod; iModule++) {
966     AliITSGainSSD *gainModule = (AliITSGainSSD*) (array->At(iModule));
967     TArrayF arrayPSide = gainModule->GetGainP();
968     for(Int_t iPCounter = 0; iPCounter < arrayPSide.GetSize(); iPCounter++)
969       gainSSD->AddGainP(iModule,
970                         iPCounter,
971                         arrayPSide.At(iPCounter));
972     TArrayF arrayNSide = gainModule->GetGainN();
973     for(Int_t iNCounter = 0; iNCounter < arrayNSide.GetSize(); iNCounter++)
974       gainSSD->AddGainN(iModule,
975                         iNCounter,
976                         arrayNSide.At(iNCounter));
977   }//loop over modules 
978 }
979
980