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