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