]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDetTypeRec.cxx
dead anodes + baselines treatment for SDD
[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  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17  $Id$
18 */
19
20 ////////////////////////////////////////////////////////////////////////
21 // 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 "AliITSClusterFinderV2.h"
32 #include "AliITSClusterFinderV2SPD.h"
33 #include "AliITSClusterFinderV2SDD.h"
34 #include "AliITSClusterFinderV2SSD.h"
35 #include "AliITSClusterFinderSPD.h"
36 #include "AliITSClusterFinderSDD.h"
37 #include "AliITSClusterFinderSSD.h"
38 #include "AliITSDetTypeRec.h"
39 #include "AliITSRawCluster.h"
40 #include "AliITSRawClusterSPD.h"
41 #include "AliITSRawClusterSDD.h"
42 #include "AliITSRawClusterSSD.h"
43 #include "AliITSRecPoint.h"
44 #include "AliITSCalibrationSDD.h"
45 #include "AliITSsegmentationSPD.h"
46 #include "AliITSsegmentationSDD.h"
47 #include "AliITSsegmentationSSD.h"
48 #include "AliLog.h"
49
50
51 const Int_t AliITSDetTypeRec::fgkNdettypes = 3;
52 const Int_t AliITSDetTypeRec::fgkDefaultNModulesSPD =  240;
53 const Int_t AliITSDetTypeRec::fgkDefaultNModulesSDD =  260;
54 const Int_t AliITSDetTypeRec::fgkDefaultNModulesSSD = 1698;
55
56 ClassImp(AliITSDetTypeRec)
57
58 //________________________________________________________________
59 AliITSDetTypeRec::AliITSDetTypeRec(): TObject(){
60     // Default Constructor
61     // Inputs:
62     //    none.
63     // Outputs:
64     //    none.
65     // Return:
66     //    A properly zero-ed AliITSDetTypeRec class.
67   fReconstruction = 0;
68   fSegmentation = 0;
69   fCalibration = 0;
70   fPreProcess = 0;
71   fPostProcess = 0;
72   fDigits = 0;;
73   for(Int_t i=0; i<3; i++){
74     fClusterClassName[i]=0;
75     fDigClassName[i]=0;
76     fRecPointClassName[i]=0;
77   }
78   fNdtype = 0;
79   fCtype = 0;
80   fNMod = 0;
81   fNctype = 0;
82   fRecPoints = 0;
83   fNRecPoints = 0;
84   SelectVertexer(" ");
85   fLoader = 0;
86   fRunNumber = 0;
87
88 }
89 //________________________________________________________________
90 AliITSDetTypeRec::AliITSDetTypeRec(AliITSLoader *loader): TObject(){
91     // Standard Constructor
92     // Inputs:
93     //    none.
94     // Outputs:
95     //    none.
96     // Return:
97     //   
98
99   fReconstruction = new TObjArray(fgkNdettypes);
100   fSegmentation = 0;
101   fCalibration = 0;
102   fPreProcess = 0;
103   fPostProcess = 0;
104   fDigits = new TObjArray(fgkNdettypes);
105   for(Int_t i=0; i<3; i++){
106     fClusterClassName[i]=0;
107     fDigClassName[i]=0;
108     fRecPointClassName[i]=0;
109   }
110   fNdtype = new Int_t[fgkNdettypes];
111   fCtype = new TObjArray(fgkNdettypes);
112   fNctype = new Int_t[fgkNdettypes];
113   fNMod = new Int_t [fgkNdettypes];
114   fNMod[0] = fgkDefaultNModulesSPD;
115   fNMod[1] = fgkDefaultNModulesSDD;
116   fNMod[2] = fgkDefaultNModulesSSD;
117   fRecPoints = new TClonesArray("AliITSRecPoint",3000);
118   fNRecPoints = 0;
119   
120   for(Int_t i=0;i<fgkNdettypes;i++){
121     fNdtype[i]=0;
122     fNctype[i]=0;
123   }
124   
125   SelectVertexer(" ");
126   fLoader = loader;
127  
128   SetRunNumber();
129 }
130 //______________________________________________________________________
131 AliITSDetTypeRec::AliITSDetTypeRec(const AliITSDetTypeRec &/*rec*/):TObject(/*rec*/){
132     // Copy constructor. 
133
134   Error("Copy constructor","Copy constructor not allowed");
135   
136 }
137 //______________________________________________________________________
138 AliITSDetTypeRec& AliITSDetTypeRec::operator=(const AliITSDetTypeRec& /*source*/){
139     // Assignment operator. This is a function which is not allowed to be
140     // done.
141     Error("operator=","Assignment operator not allowed\n");
142     return *this; 
143 }
144
145 //_____________________________________________________________________
146 AliITSDetTypeRec::~AliITSDetTypeRec(){
147  
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     AliITSresponse* rspd = ((AliITSCalibration*)fCalibration->At(GetITSgeom()->GetStartSPD()))->GetResponse();    
162     AliITSresponse* rsdd = ((AliITSCalibration*)fCalibration->At(GetITSgeom()->GetStartSDD()))->GetResponse();
163     AliITSresponse* rssd = ((AliITSCalibration*)fCalibration->At(GetITSgeom()->GetStartSSD()))->GetResponse();
164     if(rspd) delete rspd;
165     if(rsdd) delete rsdd;
166     if(rssd) delete rssd;
167     fCalibration->Delete();
168     delete fCalibration;
169     fCalibration = 0;
170   }
171   if(fPreProcess) delete fPreProcess;
172   if(fPostProcess) delete fPostProcess;
173
174   if(fDigits){
175     fDigits->Delete();
176     delete fDigits;
177     fDigits=0;
178   }
179   if(fRecPoints){
180     fRecPoints->Delete();
181     delete fRecPoints;
182     fRecPoints=0;
183   }
184   if(fCtype) {
185     fCtype->Delete();
186     delete fCtype;
187     fCtype = 0;
188   }
189   delete [] fNctype;
190   delete [] fNdtype;
191   delete [] fNMod;
192   if(fLoader) delete fLoader;
193   
194 }
195
196 //___________________________________________________________________
197 void AliITSDetTypeRec::SetReconstructionModel(Int_t dettype,AliITSClusterFinder *clf){
198
199   //Set reconstruction model for detector type
200
201   if(fReconstruction==0) fReconstruction = new TObjArray(fgkNdettypes);
202   if(fReconstruction->At(dettype)!=0) delete fReconstruction->At(dettype);
203   fReconstruction->AddAt(clf,dettype);
204 }
205 //______________________________________________________________________
206 AliITSClusterFinder* AliITSDetTypeRec::GetReconstructionModel(Int_t dettype){
207
208   //Get reconstruction model for detector type
209   if(fReconstruction==0)  {
210     Warning("GetReconstructionModel","fReconstruction is 0!");
211     return 0;     
212   }
213   return (AliITSClusterFinder*)fReconstruction->At(dettype);
214 }
215
216 //______________________________________________________________________
217 void AliITSDetTypeRec::SetSegmentationModel(Int_t dettype,AliITSsegmentation *seg){
218    
219   //Set segmentation model for detector type
220   
221   if(fSegmentation==0) fSegmentation = new TObjArray(fgkNdettypes);
222   if(fSegmentation->At(dettype)!=0) delete fSegmentation->At(dettype);
223   fSegmentation->AddAt(seg,dettype);
224
225 }
226 //______________________________________________________________________
227 AliITSsegmentation* AliITSDetTypeRec::GetSegmentationModel(Int_t dettype){
228
229   //Get segmentation model for detector type
230    
231    if(fSegmentation==0) {
232      Warning("GetSegmentationModel","fSegmentation is 0!");
233      return 0; 
234    } 
235    return (AliITSsegmentation*)fSegmentation->At(dettype);
236
237 }
238 //_______________________________________________________________________
239 void AliITSDetTypeRec::SetCalibrationModel(Int_t iMod, AliITSCalibration *cal){
240
241   //Set calibration (response) for the module iMod of type dettype
242   if (fCalibration==0) {
243     fCalibration = new TObjArray(GetITSgeom()->GetIndexMax());
244     fCalibration->SetOwner(kTRUE);
245     fCalibration->Clear();
246   }
247
248   if (fCalibration->At(iMod) != 0)
249     delete (AliITSCalibration*) fCalibration->At(iMod);
250   fCalibration->AddAt(cal,iMod);
251
252 }
253 //_______________________________________________________________________
254 AliITSCalibration* AliITSDetTypeRec::GetCalibrationModel(Int_t iMod){
255   
256   //Get calibration model for module type
257   
258   if(fCalibration==0) {
259     Warning("GetalibrationModel","fCalibration is 0!");
260     return 0; 
261   }  
262
263   return (AliITSCalibration*)fCalibration->At(iMod);
264 }
265
266 //______________________________________________________________________
267 void AliITSDetTypeRec::SetTreeAddress(){
268     // Set branch address for the Trees.
269  
270   TTree *treeR = fLoader->TreeR();
271   TTree *treeD = fLoader->TreeD();
272  
273   SetTreeAddressD(treeD);
274   SetTreeAddressR(treeR);
275 }
276 //______________________________________________________________________
277 void AliITSDetTypeRec::SetTreeAddressD(TTree *treeD){
278     // Set branch address for the tree of digits.
279
280     const char *det[4] = {"SPD","SDD","SSD","ITS"};
281     TBranch *branch;
282     Char_t* digclass;
283     Int_t i;
284     char branchname[30];
285
286     if(!treeD) return;
287     if (fDigits == 0x0) fDigits = new TObjArray(fgkNdettypes);
288     for (i=0; i<fgkNdettypes; i++) {
289         digclass = GetDigitClassName(i);
290         if(!(fDigits->At(i))) {
291             fDigits->AddAt(new TClonesArray(digclass,1000),i);
292         }else{
293             ResetDigits(i);
294         } 
295         if (fgkNdettypes==3) sprintf(branchname,"%sDigits%s",det[3],det[i]);
296         else  sprintf(branchname,"%sDigits%d",det[3],i+1);
297         if (fDigits) {
298             branch = treeD->GetBranch(branchname);
299             if (branch) branch->SetAddress(&((*fDigits)[i]));
300         } 
301     } 
302 }
303
304 //_______________________________________________________________________
305 TBranch* AliITSDetTypeRec::MakeBranchInTree(TTree *tree, const char* name, 
306                                        const char *classname, 
307                                        void* address,Int_t size, 
308                                        Int_t splitlevel, const char */*file*/)
309
310 //
311 // Makes branch in given tree and diverts them to a separate file
312 // 
313 //
314 //
315     
316   if (tree == 0x0) {
317     Error("MakeBranchInTree","Making Branch %s Tree is NULL",name);
318     return 0x0;
319   }
320   TBranch *branch = tree->GetBranch(name);
321   if (branch) {  
322     return branch;
323   }
324   if (classname){
325     branch = tree->Branch(name,classname,address,size,splitlevel);
326   }
327   else {
328     branch = tree->Bronch(name, "TClonesArray", address, size, splitlevel);
329   }
330   
331   return branch;
332 }
333
334 //____________________________________________________________________
335 void AliITSDetTypeRec::SetDefaults(){
336   
337   //Set defaults for segmentation and response
338
339   if(!GetITSgeom()){
340     Warning("SetDefaults","null pointer to AliITSgeomGeom !");
341     return;
342   }
343
344   AliITSsegmentation* seg;
345   if(!GetCalibration()) {AliFatal("Exit");exit(0);}  
346
347   for(Int_t dettype=0;dettype<fgkNdettypes;dettype++){
348     if(dettype==0){
349       seg = new AliITSsegmentationSPD(GetITSgeom());
350       SetSegmentationModel(dettype,seg);
351       SetDigitClassName(dettype,"AliITSdigitSPD");
352       SetClusterClassName(dettype,"AliITSRawClusterSPD");
353
354     }
355     if(dettype==1){
356       AliITSCalibrationSDD* res=(AliITSCalibrationSDD*) GetCalibrationModel(GetITSgeom()->GetStartSDD()); 
357       seg = new AliITSsegmentationSDD(GetITSgeom(),res);
358       SetSegmentationModel(dettype,seg);
359       const char *kopt = ((AliITSresponseSDD*)res->GetResponse())->ZeroSuppOption();
360       if((!strstr(kopt,"2D"))&&(!strstr(kopt,"1D"))) SetDigitClassName(dettype,"AliITSdigit");
361       else SetDigitClassName(dettype,"AliITSdigitSDD");
362       SetClusterClassName(dettype,"AliITSRawClusterSDD");
363
364     }
365     if(dettype==2){
366       AliITSsegmentationSSD* seg2 = new AliITSsegmentationSSD(GetITSgeom());
367       seg2->SetAngles(0.0075,0.0275); // strip angels rad P and N side.
368       seg2->SetAnglesLay5(0.0075,0.0275); // strip angels rad P and N side.
369       seg2->SetAnglesLay6(0.0275,0.0075); // strip angels rad P and N side.
370       SetSegmentationModel(dettype,seg2);
371       SetDigitClassName(dettype,"AliITSdigitSSD");
372       SetClusterClassName(dettype,"AliITSRawClusterSSD");
373     }
374   }
375   
376 }
377 //______________________________________________________________________
378 Bool_t AliITSDetTypeRec::GetCalibration() {
379   // Get Default calibration if a storage is not defined.
380
381
382   AliCDBEntry *entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSPD", fRunNumber);
383   AliCDBEntry *entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", fRunNumber);
384   AliCDBEntry *entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", fRunNumber);
385   AliCDBEntry *entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", fRunNumber);
386   AliCDBEntry *entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD", fRunNumber);
387   AliCDBEntry *entry2SSD = AliCDBManager::Instance()->Get("ITS/Calib/RespSSD", fRunNumber);
388
389   if(!entrySPD || !entrySDD || !entrySSD || !entry2SPD || !entry2SDD || !entry2SSD){
390         AliWarning("Calibration object retrieval failed! Dummy calibration will be used.");
391         AliCDBStorage *origStorage = AliCDBManager::Instance()->GetDefaultStorage();
392         AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
393         
394         entrySPD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSPD", fRunNumber);
395         entrySDD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSDD", fRunNumber);
396         entrySSD = AliCDBManager::Instance()->Get("ITS/Calib/CalibSSD", fRunNumber);
397         entry2SPD = AliCDBManager::Instance()->Get("ITS/Calib/RespSPD", fRunNumber);
398         entry2SDD = AliCDBManager::Instance()->Get("ITS/Calib/RespSDD", fRunNumber);
399         entry2SSD = AliCDBManager::Instance()->Get("ITS/Calib/RespSSD", fRunNumber);
400         
401         AliCDBManager::Instance()->SetDefaultStorage(origStorage);
402   }
403
404  
405   TObjArray *respSPD = (TObjArray *)entrySPD->GetObject();
406   entrySPD->SetObject(NULL);
407   entrySPD->SetOwner(kTRUE);
408  
409   AliITSresponseSPD *pSPD = (AliITSresponseSPD*)entry2SPD->GetObject();
410   entry2SPD->SetObject(NULL);
411   entry2SPD->SetOwner(kTRUE);
412     
413   TObjArray *respSDD = (TObjArray *)entrySDD->GetObject();
414   entrySDD->SetObject(NULL);
415   entrySDD->SetOwner(kTRUE);
416  
417   AliITSresponseSDD *pSDD = (AliITSresponseSDD*)entry2SDD->GetObject();
418   entry2SDD->SetObject(NULL);
419   entry2SDD->SetOwner(kTRUE);
420
421   TObjArray *respSSD = (TObjArray *)entrySSD->GetObject();
422   entrySSD->SetObject(NULL);
423   entrySSD->SetOwner(kTRUE);
424
425   AliITSresponseSSD *pSSD = (AliITSresponseSSD*)entry2SSD->GetObject();
426   entry2SSD->SetObject(NULL);
427   entry2SSD->SetOwner(kTRUE);
428
429   // DB entries are dleted. In this waymetadeta objects are deleted as well
430   delete entrySPD;
431   delete entrySDD;
432   delete entrySSD;
433   delete entry2SPD;
434   delete entry2SDD;
435   delete entry2SSD;
436
437   
438   if ((!pSPD)||(!pSDD)||(!pSSD) || (!respSPD) || (!respSDD) || (!respSSD)) {
439     AliWarning("Can not get calibration from calibration database !");
440     return kFALSE;
441   }
442
443   fNMod[0] = respSPD->GetEntries();
444   fNMod[1] = respSDD->GetEntries();
445   fNMod[2] = respSSD->GetEntries();
446   AliInfo(Form("%i SPD, %i SDD and %i SSD in calibration database",
447                fNMod[0], fNMod[1], fNMod[2]));
448   AliITSCalibration* res;
449   for (Int_t i=0; i<fNMod[0]; i++) {
450     res = (AliITSCalibration*) respSPD->At(i);
451     res->SetResponse((AliITSresponse*)pSPD);
452     SetCalibrationModel(i, res);
453  }
454   for (Int_t i=0; i<fNMod[1]; i++) {
455     res = (AliITSCalibration*) respSDD->At(i);
456     res->SetResponse((AliITSresponse*)pSDD);
457     Int_t iMod = i + fNMod[0];
458     SetCalibrationModel(iMod, res);
459  }
460   for (Int_t i=0; i<fNMod[2]; i++) {
461     res = (AliITSCalibration*) respSSD->At(i);
462     res->SetResponse((AliITSresponse*)pSSD);
463     Int_t iMod = i + fNMod[0] + fNMod[1];
464     SetCalibrationModel(iMod, res);
465  }
466
467   return kTRUE;
468 }
469
470
471 //________________________________________________________________
472 void AliITSDetTypeRec::SetDefaultClusterFinders(){
473   
474   //set defaults for standard cluster finder
475
476   if(!GetITSgeom()){
477     Warning("SetDefaults","null pointer to AliITSgeom!");
478     return;
479   }
480
481   AliITSClusterFinder *clf; 
482
483   MakeTreeC();
484  
485  for(Int_t dettype=0;dettype<fgkNdettypes;dettype++){
486     //SPD
487     if(dettype==0){
488       if(!GetReconstructionModel(dettype)){
489         TClonesArray *dig0 = DigitsAddress(0);
490         TClonesArray *rec0 = ClustersAddress(0);
491         clf = new AliITSClusterFinderSPD(this,dig0,rec0);
492         SetReconstructionModel(dettype,clf);
493
494       }
495     }
496    
497     //SDD
498     if(dettype==1){
499       if(!GetReconstructionModel(dettype)){
500         TClonesArray *dig1 = DigitsAddress(1);
501         TClonesArray *rec1 = ClustersAddress(1);
502         clf = new AliITSClusterFinderSDD(this,dig1,rec1);
503         SetReconstructionModel(dettype,clf);
504       }
505
506     }
507     //SSD
508     if(dettype==2){
509       if(!GetReconstructionModel(dettype)){
510         TClonesArray* dig2 = DigitsAddress(2);
511         clf = new AliITSClusterFinderSSD(this,dig2);
512         SetReconstructionModel(dettype,clf);
513       }
514     }
515
516  }
517  
518   
519 }
520
521 //________________________________________________________________
522 void AliITSDetTypeRec::SetDefaultClusterFindersV2(Bool_t rawdata){
523
524   //Set defaults for cluster finder V2
525
526   if(!GetITSgeom()){
527     Warning("SetDefaults","Null pointer to AliITSgeom !");
528     return;
529   }
530
531   AliITSClusterFinder *clf; 
532
533   MakeTreeC();
534   for(Int_t dettype=0;dettype<fgkNdettypes;dettype++){
535     //SPD
536     if(dettype==0){
537       if(!GetReconstructionModel(dettype)){
538         clf = new AliITSClusterFinderV2SPD(this);
539         clf->InitGeometry();
540         if(!rawdata) clf->SetDigits(DigitsAddress(0));
541         SetReconstructionModel(dettype,clf);
542
543       }
544     }
545     //SDD
546     if(dettype==1){
547       if(!GetReconstructionModel(dettype)){
548         clf = new AliITSClusterFinderV2SDD(this);
549         clf->InitGeometry();
550         if(!rawdata) clf->SetDigits(DigitsAddress(1));
551         SetReconstructionModel(dettype,clf);
552       }
553
554     }
555
556     //SSD
557     if(dettype==2){
558       if(!GetReconstructionModel(dettype)){
559         clf = new AliITSClusterFinderV2SSD(this);
560         clf->InitGeometry();
561         if(!rawdata) clf->SetDigits(DigitsAddress(2));
562         SetReconstructionModel(dettype,clf);
563       }
564     }
565
566  }
567    
568 }
569 //______________________________________________________________________
570 void AliITSDetTypeRec::MakeBranch(Option_t* option){
571
572   //Creates branches for clusters and recpoints
573   Bool_t cR = (strstr(option,"R")!=0);
574   Bool_t cRF = (strstr(option,"RF")!=0);
575   
576   if(cRF)cR = kFALSE;
577
578   if(cR) MakeBranchR(0);
579   if(cRF) MakeBranchRF(0);
580
581 }
582
583 //_____________________________________________________________
584 void AliITSDetTypeRec::MakeTreeC(){
585   
586   //Create a separate tree to store the clusters
587   if(!fLoader){
588     Warning("MakeTreeC","ITS loader is null!");
589     return;
590   }
591   if(fLoader->TreeC()== 0x0) fLoader->MakeTree("C");
592   MakeBranchC();
593 }
594
595 //______________________________________________________________
596 void AliITSDetTypeRec::MakeBranchC(){
597   
598   //Make branches in the tree of clusters
599
600   if(!fLoader){
601     Warning("MakeBranchC","ITS loader is null!");
602     return;
603   }
604   TTree* lTC = fLoader->TreeC();
605   if(lTC==0x0){
606     Error("MakeTreeC","Can not get TreeC from loader");
607     return;
608   }
609   
610   Int_t buffersize = 4000;
611   Char_t branchname[30];
612   Char_t* cluclass;
613   const char *det[4]={"SPD","SDD","SSD","ITS"};
614
615   for(Int_t i=0;i<fgkNdettypes;i++){
616     cluclass = GetClusterClassName(i);
617     if(fCtype==0x0)  fCtype = new TObjArray(fgkNdettypes);
618     if(!ClustersAddress(i)){
619       fCtype->AddAt(new TClonesArray(cluclass,1000),i);
620     }
621     if(fgkNdettypes==3) sprintf(branchname,"%sClusters%s",det[3],det[i]);
622     else sprintf(branchname,"%sClusters%d",det[3],i+1);
623     if(fCtype && lTC){
624       if(lTC->GetBranch(branchname)){
625         Warning("MakeBranchC","Branch %s already exists in TreeC",branchname);
626       } else{
627         Info("MakeBranchC","Creating branch %s in TreeC",branchname);
628         lTC->Branch(branchname,&((*fCtype)[i]),buffersize);
629       }
630     }
631
632   }
633   
634 }
635
636 //_______________________________________________________________
637 void AliITSDetTypeRec::GetTreeC(Int_t event){
638   
639   //Get the clusters tree for this event and
640   //sets the branch address
641
642
643   if(!fLoader){
644     Warning("GetTreeC","ITS loader is null!");
645     return;
646   }
647   
648   Char_t branchname[30];
649   const char *det[4] = {"SPD","SDD","SSD","ITS"};
650   TTree* lTC = fLoader->TreeC();
651   
652   ResetClusters();
653   if(lTC) fLoader->CleanRawClusters();
654
655   TBranch* branch;
656   if(lTC){
657     Char_t* cluclass;
658     for(Int_t i=0;i<fgkNdettypes;i++){
659       cluclass = GetClusterClassName(i);
660       if(fCtype==0x0) fCtype = new TObjArray(fgkNdettypes);
661       if(!fCtype->At(i)) fCtype->AddAt(new TClonesArray(cluclass,1000),i);
662       if(fgkNdettypes==3) sprintf(branchname,"%sClusters%s",det[3],det[i]);
663       else sprintf(branchname,"%sClusters%d",det[3],i+1);
664       if(fCtype){
665         branch = lTC->GetBranch(branchname);
666         if(branch) branch->SetAddress(&((*fCtype)[i]));
667       }
668     }
669   } else{
670     Error("GetTreeC","cannot find clusters Tree for vent %d",event);
671   }
672
673 }
674
675 //___________________________________________________________________
676 void AliITSDetTypeRec::AddCluster(Int_t id, AliITSRawCluster *c){
677
678   // Adds a raw cluster to the list
679   TClonesArray &lc = *((TClonesArray*)fCtype->At(id));  
680   switch(id){
681   case 0:
682     new(lc[fNctype[id]++]) AliITSRawClusterSPD(*((AliITSRawClusterSPD*)c));
683     break;
684   case 1:
685     new(lc[fNctype[id]++]) AliITSRawClusterSDD(*((AliITSRawClusterSDD*)c));
686     break;
687   case 2:
688     new(lc[fNctype[id]++]) AliITSRawClusterSSD(*((AliITSRawClusterSSD*)c));
689     break;
690   } 
691 }
692 //___________________________________________________________________
693 void AliITSDetTypeRec::ResetDigits(){
694   // Reset number of digits and the digits array for the ITS detector.
695   
696   if(!fDigits) return;
697   for(Int_t i=0;i<fgkNdettypes;i++){
698     ResetDigits(i);
699   }
700 }
701 //___________________________________________________________________
702 void AliITSDetTypeRec::ResetDigits(Int_t branch){
703   // Reset number of digits and the digits array for this branch.
704   
705   if(fDigits->At(branch)) ((TClonesArray*)fDigits->At(branch))->Clear();
706   if(fNdtype) fNdtype[branch]=0;
707
708 }
709
710 //__________________________________________________________________
711 void AliITSDetTypeRec::ResetClusters(){
712
713   //Resets number of clusters and the cluster array 
714   for(Int_t i=0;i<fgkNdettypes;i++){
715     ResetClusters(i);
716   }
717 }
718
719 //__________________________________________________________________
720 void AliITSDetTypeRec::ResetClusters(Int_t i){
721
722   //Resets number of clusters and the cluster array for this branch
723
724   if (fCtype->At(i))    ((TClonesArray*)fCtype->At(i))->Clear();
725   if (fNctype)  fNctype[i]=0;
726 }
727 //__________________________________________________________________
728 void AliITSDetTypeRec::MakeBranchR(const char *file, Option_t *opt){
729
730   //Creates tree branches for recpoints
731   // Inputs:
732   //      cont char *file  File name where RecPoints branch is to be written
733   //                       to. If blank it write the SDigits to the same
734   //                       file in which the Hits were found.
735
736   if(!fLoader){
737     Warning("MakeBranchR","ITS loader is null!");
738     return;
739   }
740
741   Int_t buffsz = 4000;
742   char branchname[30];
743
744   // only one branch for rec points for all detector types
745   Bool_t oFast= (strstr(opt,"Fast")!=0);
746   
747   Char_t detname[10] = "ITS";
748  
749   
750   if(oFast){
751     sprintf(branchname,"%sRecPointsF",detname);
752   } else {
753     sprintf(branchname,"%sRecPoints",detname);
754   }
755   
756   if(!fRecPoints)fRecPoints = new TClonesArray("AliITSRecPoint",1000);
757   if (fLoader->TreeR()) {
758     if(fRecPoints==0x0) fRecPoints = new TClonesArray("AliITSRecPoint",
759                                                       1000);
760     MakeBranchInTree(fLoader->TreeR(),branchname,0,&fRecPoints,buffsz,99,file);
761   } // end if
762
763   
764 }
765 //______________________________________________________________________
766 void AliITSDetTypeRec::SetTreeAddressR(TTree *treeR){
767     // Set branch address for the Reconstructed points Trees.
768     // Inputs:
769     //      TTree *treeR   Tree containing the RecPoints.
770     // Outputs:
771     //      none.
772     // Return:
773
774     char branchname[30];
775     Char_t namedet[10]="ITS";
776
777     if(!treeR) return;
778     if(fRecPoints==0x0) fRecPoints = new TClonesArray("AliITSRecPoint",1000);
779     TBranch *branch;
780     sprintf(branchname,"%sRecPoints",namedet);
781     branch = treeR->GetBranch(branchname);
782     if (branch) {
783       branch->SetAddress(&fRecPoints);
784     }else {
785       sprintf(branchname,"%sRecPointsF",namedet);
786       branch = treeR->GetBranch(branchname);
787       if (branch) {
788         branch->SetAddress(&fRecPoints);
789       }
790
791     }
792 }
793 //____________________________________________________________________
794 void AliITSDetTypeRec::AddRecPoint(const AliITSRecPoint &r){
795     // Add a reconstructed space point to the list
796     // Inputs:
797     //      const AliITSRecPoint &r RecPoint class to be added to the tree
798     //                              of reconstructed points TreeR.
799     // Outputs:
800     //      none.
801     // Return:
802     //      none.
803
804     TClonesArray &lrecp = *fRecPoints;
805     new(lrecp[fNRecPoints++]) AliITSRecPoint(r);
806 }
807
808 //______________________________________________________________________
809 void AliITSDetTypeRec::DigitsToRecPoints(Int_t evNumber,Int_t lastentry,Option_t *opt, Bool_t v2){
810   // cluster finding and reconstruction of space points
811   // the condition below will disappear when the geom class will be
812   // initialized for all versions - for the moment it is only for v5 !
813   // 7 is the SDD beam test version
814   // Inputs:
815   //      Int_t evNumber   Event number to be processed.
816   //      Int_t lastentry  Offset for module when not all of the modules
817   //                       are processed.
818   //      Option_t *opt    String indicating which ITS sub-detectors should
819   //                       be processed. If ="All" then all of the ITS
820   //                       sub detectors are processed.
821
822   if(!GetITSgeom()){
823     Warning("DigitsToRecPoints","Null pointer to AliITSgeom !");
824     return;
825   }
826   if(!fLoader){
827     Warning("DigitsToRecPoints","ITS loader is null!");
828     return;
829   }
830
831   const char *all = strstr(opt,"All");
832   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
833                         strstr(opt,"SSD")};
834   if(!v2) {
835     SetDefaultClusterFinders();
836     AliInfo("Original cluster finder has been selected\n");
837   }
838   else   { 
839     SetDefaultClusterFindersV2();
840     AliInfo("V2 cluster finder has been selected \n");
841   }
842
843   TTree *treeC=fLoader->TreeC();
844   if(!treeC){
845     MakeTreeC();
846     MakeBranchC();
847   }
848   AliITSClusterFinder *rec     = 0;
849   Int_t id,module,first=0;
850   for(module=0;module<GetITSgeom()->GetIndexMax();module++){
851       id       = GetITSgeom()->GetModuleType(module);
852       if (!all && !det[id]) continue;
853       if(det[id]) first = GetITSgeom()->GetStartDet(id);
854       rec = (AliITSClusterFinder*)GetReconstructionModel(id);
855       TClonesArray *itsDigits  = DigitsAddress(id);
856       if (!rec) {
857           Error("DigitsToRecPoints",
858                 "The reconstruction class was not instanciated! event=%d",
859                 evNumber);
860           exit(1);
861       } 
862       ResetDigits();
863       TTree *lTD = fLoader->TreeD();
864       if (all) {
865           lTD->GetEvent(lastentry+module);
866       }else {
867           lTD->GetEvent(lastentry+(module-first));
868       }
869       Int_t ndigits = itsDigits->GetEntriesFast();
870       if(ndigits>0){
871         rec->SetDetTypeRec(this);
872         rec->SetDigits(DigitsAddress(id));
873         rec->SetClusters(ClustersAddress(id));
874         rec->FindRawClusters(module);
875       } // end if
876       fLoader->TreeR()->Fill();
877       ResetRecPoints();
878       treeC->Fill();
879       ResetClusters();
880   } 
881       
882   fLoader->WriteRecPoints("OVERWRITE");
883   fLoader->WriteRawClusters("OVERWRITE");
884 }
885 //______________________________________________________________________
886 void AliITSDetTypeRec::DigitsToRecPoints(AliRawReader* rawReader){
887   // cluster finding and reconstruction of space points
888   // the condition below will disappear when the geom class will be
889   // initialized for all versions - for the moment it is only for v5 !
890   // 7 is the SDD beam test version
891   // Inputs:
892   //      Int_t evNumber   Event number to be processed.
893   //      Int_t lastentry  Offset for module when not all of the modules
894   //                       are processed.
895   //      Option_t *opt    String indicating which ITS sub-detectors should
896   //                       be processed. If ="All" then all of the ITS
897   //                       sub detectors are processed.
898   // Outputs:
899   //      none.
900   // Return:
901   //      none.
902   if(!GetITSgeom()){
903     Warning("DigitsToRecPoints","Null pointer to AliITSgeom !");
904     return;
905   }
906   if(!fLoader){
907     Warning("DigitsToRecPoints","ITS loader is null!");
908     return;
909   }
910
911   
912   AliITSClusterFinderV2 *rec     = 0;
913   Int_t id=0;
914
915   if(!fLoader->TreeR()) fLoader->MakeTree("R");
916   TTree* cTree = fLoader->TreeR();
917   TClonesArray *array=new TClonesArray("AliITSRecPoint",1000);
918   cTree->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     rec = (AliITSClusterFinderV2*)GetReconstructionModel(id);
927     rec->SetDetTypeRec(this);
928     if (!rec) {
929       Error("DigitsToRecPoints",
930             "The reconstruction class was not instanciated");
931       exit(1);
932     } 
933     rec->RawdataToClusters(rawReader,clusters);    
934   } 
935   Int_t nClusters =0;
936   for(Int_t iModule=0;iModule<GetITSgeom()->GetIndexMax();iModule++){
937     array = clusters[iModule];
938     if(!array){
939       Error("DigitsToRecPoints","data for module %d missing!",iModule);
940       array = new TClonesArray("AliITSRecPoint");
941     }
942     cTree->SetBranchAddress("ITSRecPoints",&array);
943     cTree->Fill();
944     nClusters+=array->GetEntriesFast();
945     delete array;
946   }
947   fLoader->WriteRecPoints("OVERWRITE");
948
949   delete[] clusters;
950   Info("DigitsToRecPoints", "total number of found recpoints in ITS: %d\n", 
951        nClusters);
952   
953 }
954
955