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