]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITS.cxx
Removing warnings for gcc 4.0 and using AliLog
[u/mrichter/AliRoot.git] / ITS / AliITS.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 /* $Id$ */
17
18
19 ///////////////////////////////////////////////////////////////////////////////
20 //                                                                           //
21 //      An overview of the basic philosophy of the ITS code development      //
22 // and analysis is show in the figure below.                                 //
23 //Begin_Html                                                                 //
24 /*                                               
25 <img src="picts/ITS/ITS_Analysis_schema.gif">
26 </pre>
27 <br clear=left>
28 <font size=+2 color=red>
29 <p>Roberto Barbera is in charge of the ITS Offline code (1999).
30 <a href="mailto:roberto.barbera@ct.infn.it">Roberto Barbera</a>.
31 </font>
32 <pre>
33 */
34 //End_Html
35 //
36 //  AliITS. Inner Traking System base class.
37 //  This class contains the base procedures for the Inner Tracking System
38 //
39 //Begin_Html
40 /*
41 <img src="picts/ITS/AliITS_Class_Diagram.gif">
42 </pre>
43 <br clear=left>
44 <font size=+2 color=red>
45 <p>This show the class diagram of the different elements that are part of
46 the AliITS class.
47 </font>
48 <pre>
49 */
50 //End_Html
51 //
52 // Version: 0
53 // Written by Rene Brun, Federico Carminati, and Roberto Barbera
54 //
55 // Version: 1
56 // Modified and documented by Bjorn S. Nilsen
57 // July 11 1999
58 //
59 // Version: 2
60 // Modified and documented by A. Bologna
61 // October 18 1999
62 //
63 // AliITS is the general base class for the ITS. Also see AliDetector for
64 // futher information.
65 //
66 ///////////////////////////////////////////////////////////////////////////////
67
68 #include <Riostream.h>
69 #include <stdlib.h>
70
71 #include <TBranch.h>
72 #include <TClonesArray.h>
73 #include <TFile.h>
74 #include <TMath.h>
75 #include <TObjectTable.h>
76 #include <TROOT.h>
77 #include <TRandom.h>
78 #include <TString.h>
79 #include <TTree.h>
80 #include <TVector.h>
81 #include <TVirtualMC.h>
82
83 #include "AliConfig.h"
84 #include "AliHeader.h"
85 #include "AliITS.h"
86 #include "AliITSClusterFinderSDD.h"
87 #include "AliITSClusterFinderSPD.h"
88 #include "AliITSClusterFinderSSD.h"
89 #include "AliITSClusterFinderV2SDD.h"
90 #include "AliITSClusterFinderV2SPD.h"
91 #include "AliITSClusterFinderV2SSD.h"
92 #include "AliITSDDLRawData.h"
93 #include "AliITSDetType.h"
94 #include "AliITSDigitizer.h"
95 #include "AliITSLoader.h"
96 #include "AliITSRawClusterSDD.h"
97 #include "AliITSRawClusterSPD.h"
98 #include "AliITSRawClusterSSD.h"
99 #include "AliITSRecPoint.h"
100 #include "AliITSclusterV2.h"
101 #include "AliITSdigitSDD.h"
102 #include "AliITSdigitSPD.h"
103 #include "AliITSdigitSSD.h"
104 #include "AliITSgeom.h"
105 #include "AliITShit.h"
106 #include "AliITSmodule.h"
107 #include "AliITSpList.h"
108 #include "AliITSresponseSDD.h"
109 #include "AliITSresponseSPD.h"
110 #include "AliITSresponseSSD.h"
111 #include "AliITSsegmentationSDD.h"
112 #include "AliITSsegmentationSPD.h"
113 #include "AliITSsegmentationSSD.h"
114 #include "AliITSsimulationSDD.h"
115 #include "AliITSsimulationSPD.h"
116 #include "AliITSsimulationSSD.h"
117 #include "AliLog.h"
118 #include "AliMC.h"
119 #include "AliRawReader.h"
120 #include "AliRun.h"
121
122 ClassImp(AliITS)
123
124 //______________________________________________________________________
125 AliITS::AliITS() : AliDetector(),
126     fITSgeom(0),
127     fEuclidOut(0),
128     fITSmodules(0),
129     fOpt("All"),
130     fIdN(0),
131     fIdSens(0),
132     fIdName(0),
133     fNDetTypes(kNTYPES),
134     fDetTypes(0),
135     fSDigits(0),
136     fNSDigits(0),
137     fDtype(0),
138     fNdtype(0),
139     fCtype(0),
140     fNctype(0),
141     fRecPoints(0),
142     fNRecPoints(0),
143     fClustersV2(0),
144     fNClustersV2(0),               
145     fSelectedVertexer(0){
146     // Default initializer for ITS
147     //      The default constructor of the AliITS class. In addition to
148     // creating the AliITS class it zeros the variables fIshunt (a member
149     // of AliDetector class), fEuclidOut, and fIdN, and zeros the pointers
150     // fITSpoints, fIdSens, and fIdName. The AliDetector default constructor
151     // is also called.
152     // Inputs:
153     //      none.
154     // Outputs:
155     //      none.
156     // Return:
157     //      Blank ITS class.
158
159     fIshunt     = 0;   // not zeroed in AliDetector.
160
161     // AliITS variables.
162 //    SetDetectors(); // default to fOpt="All". This variable not written out.
163     SetMarkerColor(kRed);
164     SelectVertexer(" ");
165 }
166 //______________________________________________________________________
167 AliITS::AliITS(const char *name, const char *title):AliDetector(name,title),
168     fITSgeom(0),
169     fEuclidOut(0),
170     fITSmodules(0),
171     fOpt("All"),
172     fIdN(0),
173     fIdSens(0),
174     fIdName(0),
175     fNDetTypes(kNTYPES),
176     fDetTypes(0),
177     fSDigits(0),
178     fNSDigits(0),
179     fDtype(0),
180     fNdtype(0),
181     fCtype(0),
182     fNctype(0),
183     fRecPoints(0),
184     fNRecPoints(0),
185     fClustersV2(0),
186     fNClustersV2(0),                                                
187     fSelectedVertexer(0){
188     //     The standard Constructor for the ITS class. In addition to 
189     // creating the AliITS class, it allocates memory for the TClonesArrays 
190     // fHits, fSDigits, fDigits, fITSpoints, and the TObjArray of fCtype 
191     // (clusters). It also zeros the variables
192     // fIshunt (a member of AliDetector class), fEuclidOut, and fIdN, and zeros
193     // the pointers fIdSens and fIdName. To help in displaying hits via the
194     // ROOT macro display.C AliITS also sets the marker color to red. The
195     // variables passes with this constructor, const char *name and *title,
196     // are used by the constructor of AliDetector class. See AliDetector
197     // class for a description of these parameters and its constructor
198     // functions.
199     // Inputs:
200     //      const char *name      Detector name. Should always be "ITS"
201     //      const char *title     Detector title.
202     // Outputs:
203     //      none.
204     // Return:
205     //      An ITS class.
206
207     fIshunt     = 0;  // not zeroed in AliDetector
208     fHits       = new TClonesArray("AliITShit", 1560);//not done in AliDetector
209     // Not done in AliDetector.
210     if(gAlice->GetMCApp()) gAlice->GetMCApp()->AddHitList(fHits);
211
212     fEuclidOut  = 0;
213     fITSgeom    = 0;
214     fITSmodules = 0;
215     SetDetectors(); // default to fOpt="All". This variable not written out.
216
217     fIdN        = 0;
218     fIdName     = 0;
219     fIdSens     = 0;
220
221     fNDetTypes  = kNTYPES;
222     fDetTypes   = new TObjArray(fNDetTypes);
223
224     fSDigits    = new TClonesArray("AliITSpListItem",1000);
225     fNSDigits   = 0;
226
227     fNdtype     = new Int_t[fNDetTypes];
228     fDtype      = new TObjArray(fNDetTypes);
229
230     fCtype      = new TObjArray(fNDetTypes);
231     fNctype     = new Int_t[fNDetTypes];
232
233     fRecPoints  = new TClonesArray("AliITSRecPoint",1000);
234     fNRecPoints = 0;
235     fClustersV2 = new TClonesArray("AliITSclusterV2",3000);
236     fNClustersV2=0;
237
238     Int_t i;
239     for(i=0;i<fNDetTypes;i++) {
240         fDetTypes->AddAt(new AliITSDetType(),i); 
241         fNdtype[i] = 0;
242         fNctype[i] = 0;
243     } // end for i
244
245     SetMarkerColor(kRed);
246     SelectVertexer(" ");
247 }
248 //______________________________________________________________________
249 AliITS::~AliITS(){
250     // Default destructor for ITS.
251     //     The default destructor of the AliITS class. In addition to deleting
252     // the AliITS class it deletes the memory pointed to by the fHits, fDigits,
253     // fSDigits, fCtype, fITSmodules, fITSgeom, fRecPoints, fIdSens, fIdName, 
254     // fITSpoints, fDetType and it's contents.
255     // Inputs:
256     //      none.
257     // Outputs:
258     //      none.
259     // Return:
260     //      none.
261
262     if (fHits) {
263       fHits->Delete();
264       delete fHits;
265       fHits=0;
266     }
267     if (fSDigits) {
268       fSDigits->Delete();
269       delete fSDigits;
270       fSDigits=0;
271     }
272     if (fDigits) {
273       fDigits->Delete();
274       delete fDigits;
275       fDigits=0;
276     }
277     if (fRecPoints) {
278       fRecPoints->Delete();
279       delete fRecPoints;
280       fRecPoints=0;
281     }
282     if(fClustersV2){
283       fClustersV2->Delete();
284       delete fClustersV2;
285       fClustersV2=0;
286     }
287     delete[] fIdName;  // Array of TStrings
288     delete[] fIdSens;
289     if(fITSmodules) {
290         this->ClearModules();
291         delete fITSmodules;
292         fITSmodules = 0;
293     }// end if fITSmodules!=0
294
295     if(fDtype) {
296         fDtype->Delete();
297         delete fDtype;
298     } // end if fDtype
299     delete [] fNdtype;
300     if (fCtype) {
301         fCtype->Delete();
302         delete fCtype;
303         fCtype = 0;
304     } // end if fCtype
305     delete [] fNctype;
306
307     if (fDetTypes) {
308         fDetTypes->Delete();
309         delete fDetTypes;
310         fDetTypes = 0;
311     } // end if fDetTypes
312
313
314     if (fITSgeom) delete fITSgeom;
315 }
316 //______________________________________________________________________
317 AliITS::AliITS(const AliITS &source) : AliDetector(source){
318     // Copy constructor. This is a function which is not allowed to be
319     // done to the ITS. It exits with an error.
320     // Inputs:
321     //      AliITS &source  An AliITS class.
322     // Outputs:
323     //      none.
324     // Return:
325     //      none.
326
327     if(this==&source) return;
328     Error("Copy constructor",
329           "You are not allowed to make a copy of the AliITS");
330     exit(1);
331 }
332 //______________________________________________________________________
333 AliITS& AliITS::operator=(AliITS &source){
334     // Assignment operator. This is a function which is not allowed to be
335     // done to the ITS. It exits with an error.
336     // Inputs:
337     //      AliITS &source  An AliITS class.
338     // Outputs:
339     //      none.
340     // Return:
341     //      none.
342
343     if(this==&source) return *this;
344     Error("operator=","You are not allowed to make a copy of the AliITS");
345     exit(1);
346     return *this; //fake return
347 }
348 //______________________________________________________________________
349 AliDigitizer* AliITS::CreateDigitizer(AliRunDigitizer* manager)const{
350     // Creates the AliITSDigitizer in a standard way for use via AliModule.
351     // This function can not be included in the .h file because of problems
352     // with the order of inclusion (recursive).
353     // Inputs:
354     //    AliRunDigitizer *manager  The Manger class for Digitization
355     // Output:
356     //    none.
357     // Return:
358     //    A new AliITSRunDigitizer (cast as a AliDigitizer).
359
360      return new AliITSDigitizer(manager);
361 }
362 //______________________________________________________________________
363 void AliITS::Init(){
364     // Initializer ITS after it has been built
365     //     This routine initializes the AliITS class. It is intended to be
366     // called from the Init function in AliITSv?. Besides displaying a banner
367     // indicating that it has been called it initializes the array fIdSens
368     // and sets the default segmentation, response, digit and raw cluster
369     // classes therefore it should be called after a call to CreateGeometry.
370     // Inputs:
371     //      none.
372     // Outputs:
373     //      none.
374     // Return:
375     //      none.
376     Int_t i;
377
378     SetDefaults();
379     // Array of TStrings
380     if(gMC) for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId(fIdName[i]);
381 }
382 //______________________________________________________________________
383 void AliITS::SetDefaults(){
384     // sets the default segmentation, response, digit and raw cluster classes.
385     // Inputs:
386     //      none.
387     // Outputs:
388     //      none.
389     // Return:
390     //      none.
391     AliITSsegmentation *seg;
392     AliITSresponse    *resp;
393     AliITSDetType *iDetType;
394
395     AliInfoClass("Seting Defaults");
396
397     //SPD
398     iDetType = DetType(kSPD);
399     if(iDetType){
400         if (!iDetType->GetSegmentationModel()) {
401             seg = new AliITSsegmentationSPD(fITSgeom);
402             SetSegmentationModel(kSPD,seg); 
403         } // end if
404         if (!iDetType->GetResponseModel()) {
405             SetResponseModel(kSPD,new AliITSresponseSPD()); 
406         } // end if
407         // set digit and raw cluster classes to be used
408
409         const char *kData0=(iDetType->GetResponseModel())->DataType();
410         if (strstr(kData0,"real")) {
411             iDetType->ClassNames("AliITSdigit","AliITSRawClusterSPD");
412         } else iDetType->ClassNames("AliITSdigitSPD","AliITSRawClusterSPD");
413     } // end if iDetType
414
415     // SDD
416     iDetType = DetType(kSDD); 
417     if(iDetType){
418         if (!iDetType->GetResponseModel()) {
419             SetResponseModel(kSDD,new AliITSresponseSDD("simulated")); 
420         } // end if
421         resp = iDetType->GetResponseModel();
422         if (!iDetType->GetSegmentationModel()) {
423             seg = new AliITSsegmentationSDD(fITSgeom,resp);
424             SetSegmentationModel(kSDD,seg); 
425         } // end if
426         const char *kData1 = (iDetType->GetResponseModel())->DataType();
427         const char *kopt = iDetType->GetResponseModel()->ZeroSuppOption();
428         if((!strstr(kopt,"2D"))&&(!strstr(kopt,"1D")) || 
429                                    strstr(kData1,"real") ){
430             iDetType->ClassNames("AliITSdigit","AliITSRawClusterSDD");
431         } else iDetType->ClassNames("AliITSdigitSDD","AliITSRawClusterSDD");
432     } // end if iDetType
433
434     // SSD
435     iDetType = DetType(kSSD); 
436     if(iDetType){
437         if (!iDetType->GetSegmentationModel()) {
438             seg = new AliITSsegmentationSSD(fITSgeom);
439             SetSegmentationModel(kSSD,seg); 
440         } // end if
441         if (!iDetType->GetResponseModel()) {
442             SetResponseModel(kSSD,new AliITSresponseSSD("simulated"));
443         } // end if
444         const char *kData2 = (iDetType->GetResponseModel())->DataType();
445         if (strstr(kData2,"real")) {
446             iDetType->ClassNames("AliITSdigit","AliITSRawClusterSSD");
447         } else iDetType->ClassNames("AliITSdigitSSD","AliITSRawClusterSSD");
448     } // end if iDetType
449
450     if (kNTYPES>3) {
451         Warning("SetDefaults",
452                 "Only the three basic detector types are initialized!");
453     }  // end if
454 }
455 //______________________________________________________________________
456 void AliITS::SetDefaultSimulation(){
457     // sets the default simulation.
458     // Inputs:
459     //      none.
460     // Outputs:
461     //      none.
462     // Return:
463     //      none.
464     AliITSDetType *iDetType;
465     AliITSsimulation *sim;
466     AliITSsegmentation *seg;
467     AliITSresponse *res;
468
469     iDetType = DetType(kSPD);
470     if(iDetType){
471         sim = iDetType->GetSimulationModel();
472         if (!sim) {
473             seg = (AliITSsegmentation*)iDetType->GetSegmentationModel();
474             res = (AliITSresponse*)iDetType->GetResponseModel();
475             sim = new AliITSsimulationSPD(seg,res);
476             SetSimulationModel(kSPD,sim);
477         }else{ // simulation exists, make sure it is set up properly.
478             if(sim->GetResponseModel()==0) sim->SetResponseModel(
479                 (AliITSresponse*)iDetType->GetResponseModel());
480             if(sim->GetSegmentationModel()==0) sim->SetSegmentationModel(
481                 (AliITSsegmentation*)iDetType->GetSegmentationModel());
482             sim->Init();
483         } // end if
484     }// end if iDetType
485     iDetType  = DetType(kSDD);
486     if(iDetType){
487         sim = iDetType->GetSimulationModel();
488         if (!sim) {
489             seg = (AliITSsegmentation*)iDetType->GetSegmentationModel();
490             res =  (AliITSresponse*)iDetType->GetResponseModel();
491             sim = new AliITSsimulationSDD(seg,res);
492             SetSimulationModel(kSDD,sim);
493         }else{ // simulation exists, make sure it is set up properly.
494             if(sim->GetResponseModel()==0) sim->SetResponseModel(
495                 (AliITSresponse*)iDetType->GetResponseModel());
496             if(sim->GetSegmentationModel()==0) sim->SetSegmentationModel(
497                 (AliITSsegmentation*)iDetType->GetSegmentationModel());
498             sim->Init();
499         } //end if
500     }// end if iDetType
501     iDetType = DetType(kSSD);
502     if(iDetType){
503         sim = iDetType->GetSimulationModel();
504         if (!sim) {
505             seg =(AliITSsegmentation*)iDetType->GetSegmentationModel();
506             res =  (AliITSresponse*)iDetType->GetResponseModel();
507             sim = new AliITSsimulationSSD(seg,res);
508             SetSimulationModel(kSSD,sim);
509         }else{ // simulation exists, make sure it is set up properly.
510             if(sim->GetResponseModel()==0) sim->SetResponseModel(
511                 (AliITSresponse*)iDetType->GetResponseModel());
512             if(sim->GetSegmentationModel()==0) sim->SetSegmentationModel(
513                 (AliITSsegmentation*)iDetType->GetSegmentationModel());
514             sim->Init();
515         } // end if
516     } // end if iDetType
517 }
518 //______________________________________________________________________
519 void AliITS::SetDefaultClusterFinders(){
520     // Sets the default cluster finders. Used in finding RecPoints.
521     // Inputs:
522     //      none.
523     // Outputs:
524     //      none.
525     // Return:
526     //      none.
527     AliITSDetType *iDetType;
528     AliITSsegmentation *seg;
529     AliITSClusterFinder *clf;
530     AliITSresponse *res;
531
532     MakeTreeC();
533
534     // SPD
535     iDetType=DetType(kSPD);
536     if(iDetType){
537         if (!iDetType->GetReconstructionModel()) {
538             seg =(AliITSsegmentation*)iDetType->GetSegmentationModel();
539             TClonesArray  *dig0 = DigitsAddress(0);
540             TClonesArray *recp0 = ClustersAddress(0);
541             clf = new AliITSClusterFinderSPD(seg,dig0,recp0);
542             SetReconstructionModel(kSPD,clf);
543         } // end if
544     } // end if iDetType
545
546     // SDD
547     iDetType=DetType(kSDD);
548     if(iDetType){
549         if (!iDetType->GetReconstructionModel()) {
550             seg = (AliITSsegmentation*)iDetType->GetSegmentationModel();
551             res = (AliITSresponse*)iDetType->GetResponseModel();
552             TClonesArray  *dig1 = DigitsAddress(1);
553             TClonesArray *recp1 = ClustersAddress(1);
554             clf = new AliITSClusterFinderSDD(seg,res,dig1,recp1);
555             SetReconstructionModel(kSDD,clf);
556         } // end if
557     } // end if iDetType
558
559     // SSD
560     iDetType=DetType(kSSD);
561     if(iDetType){
562         if (!iDetType->GetReconstructionModel()) {
563             seg = (AliITSsegmentation*)iDetType->GetSegmentationModel();
564             TClonesArray *dig2 = DigitsAddress(2);
565             clf = new AliITSClusterFinderSSD(seg,dig2);
566             SetReconstructionModel(kSSD,clf);
567         } // end if
568     } // end if iDetType
569 }
570
571
572 //______________________________________________________________________
573 void AliITS::SetDefaultClusterFindersV2(){
574     // Sets the default cluster finders. Used in finding RecPoints.
575     // Inputs:
576     //      none.
577     // Outputs:
578     //      none.
579     // Return:
580     //      none.
581     AliITSDetType *iDetType;
582     AliITSsegmentation *seg;
583     AliITSClusterFinder *clf;
584
585     MakeTreeC();
586
587     // SPD
588     iDetType=DetType(kSPD);
589     if(iDetType){
590         if (!iDetType->GetReconstructionModel()) {
591             seg =(AliITSsegmentation*)iDetType->GetSegmentationModel();
592             clf = new AliITSClusterFinderV2SPD();
593             clf->SetSegmentation(seg);
594             clf->SetDigits(DigitsAddress(0));
595             SetReconstructionModel(kSPD,clf);
596         } // end if
597     } // end if iDetType
598
599     // SDD
600     iDetType=DetType(kSDD);
601     if(iDetType){
602         if (!iDetType->GetReconstructionModel()) {
603             seg = (AliITSsegmentation*)iDetType->GetSegmentationModel();
604             clf = new AliITSClusterFinderV2SDD();
605             clf->SetSegmentation(seg);
606             clf->SetDigits(DigitsAddress(1));
607             SetReconstructionModel(kSDD,clf);
608         } // end if
609     } // end if iDetType
610
611     // SSD
612     iDetType=DetType(kSSD);
613     if(iDetType){
614         if (!iDetType->GetReconstructionModel()) {
615             seg = (AliITSsegmentation*)iDetType->GetSegmentationModel();
616             clf = new AliITSClusterFinderV2SSD();
617             clf->SetSegmentation(seg);
618             clf->SetDigits(DigitsAddress(2));
619             SetReconstructionModel(kSSD,clf);
620         } // end if
621     } // end if iDetType
622 }
623
624
625 //______________________________________________________________________
626 void AliITS::MakeBranch(Option_t* option){
627     // Creates Tree branches for the ITS.
628     // Inputs:
629     //      Option_t *option    String of Tree types S,D, and/or R.
630     //      const char *file    String of the file name where these branches
631     //                          are to be stored. If blank then these branches
632     //                          are written to the same tree as the Hits were
633     //                          read from.
634     // Outputs:
635     //      none.
636     // Return:
637     //      none.
638     Bool_t cH = (strstr(option,"H")!=0);
639     Bool_t cS = (strstr(option,"S")!=0);
640     Bool_t cD = (strstr(option,"D")!=0);
641     Bool_t cR = (strstr(option,"R")!=0);
642     Bool_t cRF = (strstr(option,"RF")!=0);
643     Bool_t v2 = (strstr(option,"v2")!=0);
644     
645
646     if(cRF)cR = kFALSE;
647     if(cH && (fHits == 0x0)) fHits  = new TClonesArray("AliITShit", 1560);
648     
649     AliDetector::MakeBranch(option);
650
651     if(cS) MakeBranchS(0);
652     if(cD) MakeBranchD(0);
653     if(cR) MakeBranchR(0);
654     if(cRF) MakeBranchRF(0);
655     if(v2) MakeBranchR(0,"v2");
656 }
657 //______________________________________________________________________
658 void AliITS::SetTreeAddress(){
659     // Set branch address for the Trees.
660     // Inputs:
661     //      none.
662     // Outputs:
663     //      none.
664     // Return:
665     //      none.
666     TTree *treeS = fLoader->TreeS();
667     TTree *treeD = fLoader->TreeD();
668     TTree *treeR = fLoader->TreeR();
669     if (fLoader->TreeH() && (fHits == 0x0)) 
670         fHits = new TClonesArray("AliITShit", 1560);
671
672     AliDetector::SetTreeAddress();
673
674     SetTreeAddressS(treeS);
675     SetTreeAddressD(treeD);
676     SetTreeAddressR(treeR);
677 }
678 //______________________________________________________________________
679 void AliITS::AddHit(Int_t track, Int_t *vol, Float_t *hits){
680     // Add an ITS hit
681     //     The function to add information to the AliITShit class. See the
682     // AliITShit class for a full description. This function allocates the
683     // necessary new space for the hit information and passes the variable
684     // track, and the pointers *vol and *hits to the AliITShit constructor
685     // function.
686     // Inputs:
687     //      Int_t   track   Track number which produced this hit.
688     //      Int_t   *vol    Array of Integer Hit information. See AliITShit.h
689     //      Float_t *hits   Array of Floating Hit information.  see AliITShit.h
690     // Outputs:
691     //      none.
692     // Return:
693     //      none.
694
695     TClonesArray &lhits = *fHits;
696     new(lhits[fNhits++]) AliITShit(fIshunt,track,vol,hits);
697 }
698 //______________________________________________________________________
699 void AliITS::InitModules(Int_t size,Int_t &nmodules){
700     // Initialize the modules array.
701     // Inputs:
702     //      Int_t size  Size of array of the number of modules to be
703     //                  created. If size <=0 then the number of modules
704     //                  is gotten from AliITSgeom class kept in fITSgeom.
705     // Outputs:
706     //      Int_t &nmodules The number of modules existing.
707     // Return:
708     //      none.
709
710     if(fITSmodules){ 
711         fITSmodules->Delete();
712         delete fITSmodules;
713     } // end fir fITSmoudles
714
715     Int_t nl,indexMAX,index;
716
717     if(size<=0){ // default to using data stored in AliITSgeom
718         if(fITSgeom==0) {
719             Error("InitModules","fITSgeom not defined");
720             return;
721         } // end if fITSgeom==0
722         nl = fITSgeom->GetNlayers();
723         indexMAX = fITSgeom->GetIndexMax();
724         nmodules = indexMAX;
725         fITSmodules = new TObjArray(indexMAX);
726         for(index=0;index<indexMAX;index++){
727             fITSmodules->AddAt( new AliITSmodule(index),index);
728         } // end for index
729     }else{
730         fITSmodules = new TObjArray(size);
731         for(index=0;index<size;index++) {
732             fITSmodules->AddAt( new AliITSmodule(index),index);
733         } // end for index
734
735         nmodules = size;
736     } // end i size<=0
737 }
738 //______________________________________________________________________
739 void AliITS::FillModules(Int_t evnt,Int_t bgrev,Int_t nmodules,
740                          Option_t *option, const char *filename){
741     // fill the modules with the sorted by module hits; add hits from
742     // background if option=Add.
743     // Inputs:
744     //      Int_t evnt       Event to be processed.
745     //      Int_t bgrev      Background Hit tree number.
746     //      Int_t nmodules   Not used.
747     //      Option_t *option String indicating if merging hits or not. To
748     //                       merge hits set equal to "Add". Otherwise no
749     //                       background hits are considered.
750     //      Test_t *filename File name containing the background hits..
751     // Outputs:
752     //      none.
753     // Return:
754     //      none.
755     static TTree *trH1;                 //Tree with background hits
756     static Bool_t first=kTRUE;
757     static TFile *file;
758     const char *addBgr = strstr(option,"Add");
759
760     evnt = nmodules; // Dummy use of variables to remove warnings
761     if (addBgr ) {
762         if(first) {
763             file=new TFile(filename);
764         } // end if first
765         first=kFALSE;
766         file->cd();
767         file->ls();
768         // Get Hits Tree header from file
769         if(trH1) delete trH1;
770         trH1=0;
771
772         char treeName[20];
773         sprintf(treeName,"TreeH%d",bgrev);
774         trH1 = (TTree*)gDirectory->Get(treeName);
775         if (!trH1) {
776             Error("FillModules","cannot find Hits Tree for event:%d",bgrev);
777         } // end if !trH1
778         // Set branch addresses
779     } // end if addBgr
780
781     FillModules(fLoader->TreeH(),0); // fill from this file's tree.
782     
783     if (addBgr ) {
784         FillModules(trH1,10000000); // Default mask 10M.
785         TTree *fAli=fLoader->GetRunLoader()->TreeK();
786         TFile *fileAli=0;
787         if (fAli) fileAli =fAli->GetCurrentFile();
788         fileAli->cd();
789     } // end if add
790 }
791 //______________________________________________________________________
792 void AliITS::FillModules(TTree *treeH, Int_t mask) {
793     // fill the modules with the sorted by module hits; 
794     // can be called many times to do a merging
795     // Inputs:
796     //      TTree *treeH  The tree containing the hits to be copied into
797     //                    the modules.
798     //      Int_t mask    The track number mask to indecate which file
799     //                    this hits came from.
800     // Outputs:
801     //      none.
802     // Return:
803     //      none.
804
805     if (treeH == 0x0)
806      {
807        Error("FillModules","Tree is NULL");
808      }
809     Int_t lay,lad,det,index;
810     AliITShit *itsHit=0;
811     AliITSmodule *mod=0;
812     char branchname[20];
813     sprintf(branchname,"%s",GetName());
814     TBranch *branch = treeH->GetBranch(branchname);
815     if (!branch) {
816         Error("FillModules","%s branch in TreeH not found",branchname);
817         return;
818     } // end if !branch
819     branch->SetAddress(&fHits);
820     Int_t nTracks =(Int_t) treeH->GetEntries();
821     Int_t iPrimTrack,h;
822     for(iPrimTrack=0; iPrimTrack<nTracks; iPrimTrack++){
823         ResetHits();
824         Int_t nBytes = treeH->GetEvent(iPrimTrack);
825         if (nBytes <= 0) continue;
826         Int_t nHits = fHits->GetEntriesFast();
827         for(h=0; h<nHits; h++){
828             itsHit = (AliITShit *)fHits->UncheckedAt(h);
829             itsHit->GetDetectorID(lay,lad,det);
830             if (fITSgeom) {
831                 index = fITSgeom->GetModuleIndex(lay,lad,det);
832             } else {
833                 index=det-1; // This should not be used.
834             } // end if [You must have fITSgeom for this to work!]
835             mod = GetModule(index);
836             itsHit->SetTrack(itsHit->GetTrack()+mask); // Set track mask.
837             mod->AddHit(itsHit,iPrimTrack,h);
838         } // end loop over hits 
839     } // end loop over tracks
840 }
841 //______________________________________________________________________
842 void AliITS::MakeBranchS(const char *fl){
843     // Creates Tree Branch for the ITS summable digits.
844     // Inputs:
845     //      cont char *fl  File name where SDigits branch is to be written
846     //                     to. If blank it write the SDigits to the same
847     //                     file in which the Hits were found.
848     // Outputs:
849     //      none.
850     // Return:
851     //      none.
852     Int_t buffersize = 4000;
853     char branchname[30];
854
855     // only one branch for SDigits.
856     sprintf(branchname,"%s",GetName());
857     
858
859     if(fLoader->TreeS()){
860         if(fSDigits==0x0) fSDigits = new TClonesArray("AliITSpListItem",1000);
861         MakeBranchInTree(fLoader->TreeS(),branchname,&fSDigits,buffersize,fl);
862     } // end if
863 }
864 //______________________________________________________________________
865 void AliITS::SetTreeAddressS(TTree *treeS){
866     // Set branch address for the ITS summable digits Trees.
867     // Inputs:
868     //      TTree *treeS   Tree containing the SDigits.
869     // Outputs:
870     //      none.
871     // Return:
872     //      none.
873     char branchname[30];
874
875     if(!treeS) return;
876     if (fSDigits == 0x0)  fSDigits = new TClonesArray("AliITSpListItem",1000);
877     TBranch *branch;
878     sprintf(branchname,"%s",GetName());
879     branch = treeS->GetBranch(branchname);
880     if (branch) branch->SetAddress(&fSDigits);
881 }
882 //______________________________________________________________________
883 void AliITS::MakeBranchInTreeD(TTree *treeD,const char *file){
884     // Creates Tree branches for the ITS.
885     // Inputs:
886     //      TTree     *treeD Pointer to the Digits Tree.
887     //      cont char *file  File name where Digits branch is to be written
888     //                       to. If blank it write the SDigits to the same
889     //                       file in which the Hits were found.
890     // Outputs:
891     //      none.
892     // Return:
893     //      none.
894     // one branch for digits per type of detector
895     const Char_t *det[3] = {"SPD","SDD","SSD"};
896     TString digclass;
897     Int_t i;
898     Int_t buffersize = 4000;
899     Char_t branchname[30];
900
901     if (fDtype == 0x0) fDtype = new TObjArray(fNDetTypes);
902     for (i=0; i<kNTYPES ;i++) {
903         digclass = DetType(i)->GetDigitClassName();
904         // digits
905         if(!(fDtype->At(i))){
906             fDtype->AddAt(new TClonesArray(digclass.Data(),1000),i);
907         }else{
908             ResetDigits(i);
909         } // end if
910         if (kNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
911         else  sprintf(branchname,"%sDigits%d",GetName(),i+1);      
912         if (fDtype && treeD) {
913             MakeBranchInTree(treeD,branchname,&((*fDtype)[i]),buffersize,file);
914         } // end if
915     } // end for i
916          /*
917     for (i=0; i<kNTYPES ;i++) {
918         if (kNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
919         else  sprintf(branchname,"%sDigits%d",GetName(),i+1);      
920         if (fDtype && treeD) {
921             MakeBranchInTree(treeD,branchname,&((*fDtype)[i]),buffersize,file);
922         } // end if
923     } // end for i
924          */
925 }
926 //______________________________________________________________________
927 void AliITS::SetTreeAddressD(TTree *treeD){
928     // Set branch address for the Trees.
929     // Inputs:
930     //      TTree *treeD   Tree containing the Digits.
931     // Outputs:
932     //      none.
933     // Return:
934     //      none.
935     const char *det[3] = {"SPD","SDD","SSD"};
936     TBranch *branch;
937     TString digclass;
938     Int_t i;
939     char branchname[30];
940
941     if(!treeD) return;
942     if (fDtype == 0x0) fDtype = new TObjArray(fNDetTypes);
943     for (i=0; i<kNTYPES; i++) {
944         digclass = DetType(i)->GetDigitClassName();
945         // digits
946         if(!(fDtype->At(i))) {
947             fDtype->AddAt(new TClonesArray(digclass.Data(),1000),i);
948         }else{
949             ResetDigits(i);
950         } // end if
951         if (kNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
952         else  sprintf(branchname,"%sDigits%d",GetName(),i+1);
953         if (fDtype) {
954             branch = treeD->GetBranch(branchname);
955             if (branch) branch->SetAddress(&((*fDtype)[i]));
956         } // end if fDtype
957     } // end for i
958 }
959 //______________________________________________________________________
960 void AliITS::Hits2SDigits(){
961     // Standard Hits to summable Digits function.
962     // Inputs:
963     //      none.
964     // Outputs:
965     //      none.
966
967     fLoader->LoadHits("read");
968     fLoader->LoadSDigits("recreate");
969     AliRunLoader* rl = fLoader->GetRunLoader(); 
970
971     for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
972         // Do the Hits to Digits operation. Use Standard input values.
973         // Event number from file, no background hit merging , use size from
974         // AliITSgeom class, option="All", input from this file only.
975         rl->GetEvent(iEvent);
976         if (!fLoader->TreeS()) fLoader->MakeTree("S");
977         MakeBranch("S");
978         SetTreeAddress();
979         HitsToPreDigits(iEvent,0,-1," ",fOpt," ");
980     } // end for iEvent
981     
982     fLoader->UnloadHits();
983     fLoader->UnloadSDigits();
984 }
985 //______________________________________________________________________
986 void AliITS::SDigitsToDigits(Option_t *opt){
987     // Standard Summable digits to Digits function.
988     // Inputs:
989     //      none.
990     // Outputs:
991     //      none.
992     char name[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
993
994     if(!GetITSgeom()) return; // need transformations to do digitization.
995     AliITSgeom *geom = GetITSgeom();
996
997     const char *all = strstr(opt,"All");
998     const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
999                           strstr(opt,"SSD")};
1000     if( !det[0] && !det[1] && !det[2] ) all = "All";
1001     else all = 0;
1002     static Bool_t setDef = kTRUE;
1003     if(setDef) SetDefaultSimulation();
1004     setDef = kFALSE;
1005
1006     AliITSsimulation *sim      = 0;
1007     AliITSDetType    *iDetType = 0;
1008     TTree            *trees    = fLoader->TreeS();
1009     if( !(trees && this->GetSDigits()) ){
1010         Error("SDigits2Digits","Error: No trees or SDigits. Returning.");
1011         return;
1012     } // end if
1013     sprintf( name, "%s", this->GetName() );
1014     TBranch *brchSDigits = trees->GetBranch( name );
1015     
1016     Int_t id,module;
1017     for(module=0;module<geom->GetIndexMax();module++){
1018         id       = geom->GetModuleType(module);
1019         if (!all && !det[id]) continue;
1020         iDetType = DetType(id);
1021         sim      = (AliITSsimulation*)iDetType->GetSimulationModel();
1022         if (!sim) {
1023             Error("SDigit2Digits","The simulation class was not "
1024                   "instanciated for module %d type %s!",module,
1025                   geom->GetModuleTypeName(module));
1026             exit(1);
1027         } // end if !sim
1028         sim->InitSimulationModule(module,gAlice->GetEvNumber());
1029         //
1030         // add summable digits to module
1031         this->GetSDigits()->Clear();
1032         brchSDigits->GetEvent(module);
1033         sim->AddSDigitsToModule(GetSDigits(),0);
1034         //
1035         // Digitise current module sum(SDigits)->Digits
1036         sim->FinishSDigitiseModule();
1037
1038         // fills all branches - wasted disk space
1039         fLoader->TreeD()->Fill();
1040         this->ResetDigits();
1041     } // end for module
1042
1043     fLoader->TreeD()->GetEntries();
1044
1045     fLoader->TreeD()->AutoSave();
1046     // reset tree
1047     fLoader->TreeD()->Reset();
1048     
1049 }
1050 //______________________________________________________________________
1051 void AliITS::Hits2Digits(){
1052     // Standard Hits to Digits function.
1053     // Inputs:
1054     //      none.
1055     // Outputs:
1056     //      none.
1057
1058     fLoader->LoadHits("read");
1059     fLoader->LoadDigits("recreate");
1060     AliRunLoader* rl = fLoader->GetRunLoader(); 
1061
1062     for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
1063         // Do the Hits to Digits operation. Use Standard input values.
1064         // Event number from file, no background hit merging , use size from
1065         // AliITSgeom class, option="All", input from this file only.
1066         rl->GetEvent(iEvent);
1067         if (!fLoader->TreeD()) fLoader->MakeTree("D");
1068         MakeBranch("D");
1069         SetTreeAddress();   
1070         HitsToDigits(iEvent,0,-1," ",fOpt," ");
1071     } // end for iEvent
1072
1073     fLoader->UnloadHits();
1074     fLoader->UnloadDigits();
1075 }
1076 //______________________________________________________________________
1077 void AliITS::HitsToPreDigits(Int_t evNumber,Int_t bgrev,Int_t size,
1078                              Option_t *option,Option_t *opt,
1079                              const char *filename){
1080     //   Keep galice.root for signal and name differently the file for 
1081     // background when add! otherwise the track info for signal will be lost !
1082     // the condition below will disappear when the geom class will be
1083     // initialized for all versions - for the moment it is only for v5 !
1084     // 7 is the SDD beam test version.
1085     // Inputs:
1086     //      Int_t evnt       Event to be processed.
1087     //      Int_t bgrev      Background Hit tree number.
1088     //      Int_t nmodules   Not used.
1089     //      Option_t *option String indicating if merging hits or not. To
1090     //                       merge hits set equal to "Add". Otherwise no
1091     //                       background hits are considered.
1092     //      Test_t *filename File name containing the background hits..
1093     // Outputs:
1094     //      none.
1095     // Return:
1096     //      none.
1097
1098     if(!GetITSgeom()) return; // need transformations to do digitization.
1099     AliITSgeom *geom = GetITSgeom();
1100
1101     const char *all = strstr(opt,"All");
1102     const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
1103                           strstr(opt,"SSD")};
1104     static Bool_t setDef=kTRUE;
1105     if (setDef) SetDefaultSimulation();
1106     setDef=kFALSE;
1107
1108     Int_t nmodules;
1109     InitModules(size,nmodules);
1110     FillModules(evNumber,bgrev,nmodules,option,filename);
1111
1112     AliITSsimulation *sim      = 0;
1113     AliITSDetType    *iDetType = 0;
1114     AliITSmodule     *mod      = 0;
1115     Int_t id,module;
1116     for(module=0;module<geom->GetIndexMax();module++){
1117         id       = geom->GetModuleType(module);
1118         if (!all && !det[id]) continue;
1119         iDetType = DetType(id);
1120         sim      = (AliITSsimulation*)iDetType->GetSimulationModel();
1121         if (!sim) {
1122             Error("HitsToSDigits","The simulation class was not "
1123                   "instanciated for module %d type %s!",module,
1124                   geom->GetModuleTypeName(module));
1125             exit(1);
1126         } // end if !sim
1127         mod      = (AliITSmodule *)fITSmodules->At(module);
1128         sim->SDigitiseModule(mod,module,evNumber);
1129         // fills all branches - wasted disk space
1130         fLoader->TreeS()->Fill(); 
1131         ResetSDigits();
1132     } // end for module
1133
1134     ClearModules();
1135
1136     fLoader->TreeS()->GetEntries();
1137     fLoader->TreeS()->AutoSave();
1138     fLoader->WriteSDigits("OVERWRITE");
1139     // reset tree
1140     fLoader->TreeS()->Reset();
1141 }
1142 //______________________________________________________________________
1143 void AliITS::HitsToDigits(Int_t evNumber,Int_t bgrev,Int_t size,
1144                           Option_t *option,Option_t *opt,
1145                           const char *filename){
1146     //   Keep galice.root for signal and name differently the file for 
1147     // background when add! otherwise the track info for signal will be lost !
1148     // the condition below will disappear when the geom class will be
1149     // initialized for all versions - for the moment it is only for v5 !
1150     // 7 is the SDD beam test version.
1151     // Inputs:
1152     //      Int_t evnt       Event to be processed.
1153     //      Int_t bgrev      Background Hit tree number.
1154     //      Int_t nmodules   Not used.
1155     //      Option_t *option String indicating if merging hits or not. To
1156     //                       merge hits set equal to "Add". Otherwise no
1157     //                       background hits are considered.
1158     //      Test_t *filename File name containing the background hits..
1159     // Outputs:
1160     //      none.
1161     // Return:
1162     //      none.
1163
1164
1165     if(!GetITSgeom()) return; // need transformations to do digitization.
1166     AliITSgeom *geom = GetITSgeom();
1167
1168     const char *all = strstr(opt,"All");
1169     const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
1170                           strstr(opt,"SSD")};
1171     static Bool_t setDef=kTRUE;
1172     if (setDef) SetDefaultSimulation();
1173     setDef=kFALSE;
1174
1175     Int_t nmodules;
1176     InitModules(size,nmodules);
1177     FillModules(evNumber,bgrev,nmodules,option,filename);
1178
1179     AliITSsimulation *sim      = 0;
1180     AliITSDetType    *iDetType = 0;
1181     AliITSmodule     *mod      = 0;
1182     Int_t id,module;
1183     for(module=0;module<geom->GetIndexMax();module++){
1184         id       = geom->GetModuleType(module);
1185         if (!all && !det[id]) continue;
1186         iDetType = DetType(id);
1187         sim      = (AliITSsimulation*)iDetType->GetSimulationModel();
1188         if (!sim) {
1189             Error("HitsToDigits","The simulation class was not "
1190                   "instanciated for module %d type %s!",module,
1191                   geom->GetModuleTypeName(module));
1192             exit(1);
1193         } // end if !sim
1194         mod      = (AliITSmodule *)fITSmodules->At(module);
1195         sim->DigitiseModule(mod,module,evNumber);
1196         // fills all branches - wasted disk space
1197         fLoader->TreeD()->Fill(); 
1198         ResetDigits();
1199     } // end for module
1200
1201     ClearModules();
1202
1203     fLoader->TreeD()->GetEntries();
1204     fLoader->TreeD()->AutoSave();
1205     // reset tree
1206     fLoader->TreeD()->Reset();
1207 }
1208 //______________________________________________________________________
1209 void AliITS::ResetDigits(){
1210     // Reset number of digits and the digits array for the ITS detector.
1211     // Inputs:
1212     //      none.
1213     // Outputs:
1214     //      none.
1215
1216     if (!fDtype) return;
1217
1218     Int_t i;
1219     for (i=0;i<kNTYPES;i++ ) {
1220         ResetDigits(i);
1221     } // end for i
1222 }
1223 //______________________________________________________________________
1224 void AliITS::ResetDigits(Int_t i){
1225     // Reset number of digits and the digits array for this branch.
1226     // Inputs:
1227     //      none.
1228     // Outputs:
1229     //      none.
1230
1231     if (fDtype->At(i)) ((TClonesArray*)fDtype->At(i))->Clear();
1232     if (fNdtype)       fNdtype[i]=0;
1233 }
1234 //______________________________________________________________________
1235 void AliITS::AddSumDigit(AliITSpListItem &sdig){
1236     // Adds the a module full of summable digits to the summable digits tree.
1237     // Inputs:
1238     //      AliITSpListItem &sdig   SDigit to be added to SDigits tree.
1239     // Outputs:
1240     //      none.
1241     // Return:
1242     //      none.
1243
1244     TClonesArray &lsdig = *fSDigits;
1245     new(lsdig[fNSDigits++]) AliITSpListItem(sdig);
1246 }
1247 //______________________________________________________________________
1248 void AliITS::AddRealDigit(Int_t id, Int_t *digits){
1249     //   Add a real digit - as coming from data.
1250     // Inputs:
1251     //      Int_t id        Detector type number.
1252     //      Int_t *digits   Integer array containing the digits info. See 
1253     //                      AliITSdigit.h
1254     // Outputs:
1255     //      none.
1256     // Return:
1257     //      none.
1258
1259     TClonesArray &ldigits = *((TClonesArray*)fDtype->At(id));
1260     new(ldigits[fNdtype[id]++]) AliITSdigit(digits);
1261 }
1262 //______________________________________________________________________
1263 void AliITS::AddSimDigit(Int_t id, AliITSdigit *d){
1264     //    Add a simulated digit.
1265     // Inputs:
1266     //      Int_t id        Detector type number.
1267     //      AliITSdigit *d  Digit to be added to the Digits Tree. See 
1268     //                      AliITSdigit.h
1269     // Outputs:
1270     //      none.
1271     // Return:
1272     //      none.
1273
1274     TClonesArray &ldigits = *((TClonesArray*)fDtype->At(id));
1275
1276     switch(id){
1277     case 0:
1278         new(ldigits[fNdtype[id]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
1279         break;
1280     case 1:
1281         new(ldigits[fNdtype[id]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
1282         break;
1283     case 2:
1284         new(ldigits[fNdtype[id]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
1285         break;
1286     } // end switch id
1287 }
1288 //______________________________________________________________________
1289 void AliITS::AddSimDigit(Int_t id,Float_t phys,Int_t *digits,Int_t *tracks,
1290                          Int_t *hits,Float_t *charges){
1291   //   Add a simulated digit to the list.
1292   // Inputs:
1293   //      Int_t id        Detector type number.
1294   //      Float_t phys    Physics indicator. See AliITSdigits.h
1295   //      Int_t *digits   Integer array containing the digits info. See 
1296   //                      AliITSdigit.h
1297   //      Int_t *tracks   Integer array [AliITSdigitS?D::GetNTracks()] 
1298   //                      containing the track numbers that contributed to
1299   //                      this digit.
1300   //      Int_t *hits     Integer array [AliITSdigitS?D::GetNTracks()]
1301   //                      containing the hit numbers, from AliITSmodule, that
1302   //                      contributed to this digit.
1303   //      Float_t *charge Floating point array of the signals contributed
1304   //                      to this digit by each track.
1305   // Outputs:
1306   //      none.
1307   // Return:
1308   //      none.
1309
1310   TClonesArray &ldigits = *((TClonesArray*)fDtype->At(id));
1311   AliITSresponseSDD *resp = 0;
1312   switch(id){
1313   case 0:
1314       new(ldigits[fNdtype[id]++]) AliITSdigitSPD(digits,tracks,hits);
1315       break;
1316   case 1:
1317       resp = (AliITSresponseSDD*)DetType(1)->GetResponseModel();
1318       new(ldigits[fNdtype[id]++]) AliITSdigitSDD(phys,digits,tracks,
1319                                                  hits,charges,resp);
1320       break;
1321   case 2:
1322       new(ldigits[fNdtype[id]++]) AliITSdigitSSD(digits,tracks,hits);
1323       break;
1324   } // end switch id
1325 }
1326 //______________________________________________________________________
1327 void AliITS::Digits2Raw(){
1328     // convert digits of the current event to raw data
1329
1330   fLoader->LoadDigits();
1331   TTree* digits = fLoader->TreeD();
1332   if (!digits) {
1333       Error("Digits2Raw", "no digits tree");
1334       return;
1335   }
1336   SetTreeAddressD(digits);
1337
1338   AliITSDDLRawData rawWriter;
1339   //Verbose level
1340   // 0: Silent
1341   // 1: cout messages
1342   // 2: txt files with digits 
1343   //BE CAREFUL, verbose level 2 MUST be used only for debugging and
1344   //it is highly suggested to use this mode only for debugging digits files
1345   //reasonably small, because otherwise the size of the txt files can reach
1346   //quickly several MB wasting time and disk space.
1347   rawWriter.SetVerbose(0);
1348     
1349   //SILICON PIXEL DETECTOR
1350   Info("Digits2Raw", "Formatting raw data for SPD");
1351   rawWriter.RawDataSPD(digits->GetBranch("ITSDigitsSPD"));
1352     
1353   //SILICON DRIFT DETECTOR
1354   Info("Digits2Raw", "Formatting raw data for SDD");
1355   rawWriter.RawDataSDD(digits->GetBranch("ITSDigitsSDD"));
1356     
1357   //SILICON STRIP DETECTOR
1358   Info("Digits2Raw", "Formatting raw data for SSD");
1359   rawWriter.RawDataSSD(digits->GetBranch("ITSDigitsSSD"));
1360
1361   fLoader->UnloadDigits();
1362 }
1363
1364 //______________________________________________________________________
1365 void AliITS::MakeTreeC(Option_t *option){
1366   //   Create a separate tree to store the clusters.
1367   // Inputs:
1368   //      Option_t *option  string which must contain "C" otherwise
1369   //                        no Cluster Tree is created.
1370   // Outputs:
1371   //      none.
1372   // Return:
1373   //      none.
1374
1375   AliITSLoader *pITSLoader = (AliITSLoader*)fLoader;    
1376     
1377   if (pITSLoader == 0x0) {
1378       Error("MakeTreeC","fLoader == 0x0 option=%s",option);
1379       return;
1380   }
1381   if (pITSLoader->TreeC() == 0x0) pITSLoader->MakeTree("C");
1382   MakeBranchC();
1383 }
1384 //----------------------------------------------------------------------
1385 void AliITS::MakeBranchC(){
1386     //Makes barnches in treeC
1387     AliITSLoader *pITSLoader = (AliITSLoader*)fLoader;    
1388     if (pITSLoader == 0x0) {
1389         Error("MakeTreeC","fLoader == 0x0");
1390         return;
1391     }
1392     TTree * lTC = pITSLoader->TreeC();
1393     if (lTC == 0x0){
1394         Error("MakeTreeC","Can not get TreeC from Loader");
1395         return;
1396     }
1397
1398     Int_t buffersize = 4000;
1399     char branchname[30];
1400     const char *det[3] = {"SPD","SDD","SSD"};
1401     char digclass[40];
1402     char clclass[40];
1403     
1404     // one branch for Clusters per type of detector
1405     Int_t i;   
1406     for (i=0; i<kNTYPES ;i++) {
1407         AliITSDetType *iDetType=DetType(i); 
1408         iDetType->GetClassNames(digclass,clclass);
1409         // clusters
1410         if (fCtype == 0x0) fCtype  = new TObjArray(fNDetTypes);
1411         if(!ClustersAddress(i)){
1412             fCtype->AddAt(new TClonesArray(clclass,1000),i);
1413         }
1414         if (kNTYPES==3) sprintf(branchname,"%sClusters%s",GetName(),det[i]);
1415         else  sprintf(branchname,"%sClusters%d",GetName(),i+1);
1416         if (fCtype  && lTC) {
1417             if (lTC->GetBranch(branchname)){
1418                 Warning("MakeBranchC","Branch %s alread exists in TreeC",
1419                         branchname);
1420             }else{
1421                 Info("MakeBranchC","Creating branch %s in TreeC",branchname);
1422                 lTC->Branch(branchname,&((*fCtype)[i]), buffersize);
1423             }
1424         } // end if fCtype && lTC
1425     } // end for i
1426 }
1427 //______________________________________________________________________
1428 void AliITS::GetTreeC(Int_t event){
1429     //    Get the clusters tree for this event and set the branch address.
1430     // Inputs:
1431     //      Int_t event    Event number for the cluster tree.
1432     // Outputs:
1433     //      none.
1434     // Return:
1435     //      none.
1436     char branchname[30];
1437     const char *det[3] = {"SPD","SDD","SSD"};
1438
1439     AliITSLoader *pITSLoader = (AliITSLoader*)fLoader;
1440     TTree * lTC = pITSLoader->TreeC();
1441
1442     ResetClusters();
1443     if (lTC) {
1444         pITSLoader->CleanRawClusters();
1445     } // end if TreeC()
1446
1447     TBranch *branch;
1448
1449     if (lTC) {
1450         Int_t i;
1451         char digclass[40];
1452         char clclass[40];
1453         for (i=0; i<kNTYPES; i++) {
1454             AliITSDetType *iDetType=DetType(i); 
1455             iDetType->GetClassNames(digclass,clclass);
1456             // clusters
1457             if (fCtype == 0x0) fCtype  = new TObjArray(fNDetTypes);
1458             if(!fCtype->At(i)) fCtype->AddAt(new TClonesArray(clclass,1000),i);
1459             if(kNTYPES==3) sprintf(branchname,"%sClusters%s",GetName(),det[i]);
1460             else  sprintf(branchname,"%sClusters%d",GetName(),i+1);
1461             if (fCtype) {
1462                 branch = lTC->GetBranch(branchname);
1463                 if (branch) branch->SetAddress(&((*fCtype)[i]));
1464             } // end if fCtype
1465         } // end for i
1466     } else {
1467         Error("GetTreeC","cannot find Clusters Tree for event:%d",event);
1468     } // end if lTC
1469 }
1470 //______________________________________________________________________
1471 void AliITS::AddCluster(Int_t id, AliITSRawCluster *c){
1472     //   Add a cluster to the list.
1473     // Inputs:
1474     //      Int_t id             Detector type number.
1475     //      AliITSRawCluster *c  Cluster class to be added to the tree of
1476     //                           clusters.
1477     // Outputs:
1478     //      none.
1479     // Return:
1480     //      none.
1481
1482     TClonesArray &lc = *((TClonesArray*)fCtype->At(id));
1483
1484     switch(id){
1485     case 0:
1486         new(lc[fNctype[id]++]) AliITSRawClusterSPD(*((AliITSRawClusterSPD*)c));
1487         break;
1488     case 1:
1489         new(lc[fNctype[id]++]) AliITSRawClusterSDD(*((AliITSRawClusterSDD*)c));
1490         break;
1491     case 2:
1492         new(lc[fNctype[id]++]) AliITSRawClusterSSD(*((AliITSRawClusterSSD*)c));
1493         break;
1494     } // end switch id
1495 }
1496 //______________________________________________________________________
1497 void AliITS::ResetClusters(Int_t i){
1498     //    Reset number of clusters and the clusters array for this branch.
1499     // Inputs:
1500     //      Int_t i        Detector type number.
1501     // Outputs:
1502     //      none.
1503     // Return:
1504     //      none.
1505
1506     if (fCtype->At(i))    ((TClonesArray*)fCtype->At(i))->Clear();
1507     if (fNctype)  fNctype[i]=0;
1508 }
1509 //______________________________________________________________________
1510 void AliITS::MakeBranchR(const char *file, Option_t *opt){
1511     // Creates Tree branches for the ITS Reconstructed points.
1512     // Inputs:
1513     //      cont char *file  File name where RecPoints branch is to be written
1514     //                       to. If blank it write the SDigits to the same
1515     //                       file in which the Hits were found.
1516     // Outputs:
1517     //      none.
1518     // Return:
1519     //      none.
1520     Int_t buffsz = 4000;
1521     char branchname[30];
1522
1523     // only one branch for rec points for all detector types
1524     Bool_t oFast= (strstr(opt,"Fast")!=0);
1525     Bool_t v2 = (strstr(opt,"v2")!=0);
1526     
1527
1528     if(oFast){
1529         sprintf(branchname,"%sRecPointsF",GetName());
1530     } else if(v2){
1531         sprintf(branchname,"Clusters");
1532     } else {
1533         sprintf(branchname,"%sRecPoints",GetName());
1534     }
1535
1536     if(v2){
1537       
1538       if(!fClustersV2)fClustersV2 = new TClonesArray("AliITSclusterV2",3000);
1539       if(fLoader->TreeR()){
1540         if(fClustersV2==0x0) fClustersV2 = new TClonesArray("AliITSclusterV2",3000);
1541         MakeBranchInTree(fLoader->TreeR(),branchname,&fClustersV2,buffsz,file);
1542
1543       }
1544     }else{
1545       if(!fRecPoints)fRecPoints = new TClonesArray("AliITSRecPoint",1000);
1546       if (fLoader->TreeR()) {
1547         if(fRecPoints==0x0) fRecPoints = new TClonesArray("AliITSRecPoint",
1548                                                           1000);
1549         MakeBranchInTree(fLoader->TreeR(),branchname,&fRecPoints,buffsz,file);
1550       } // end if
1551     }
1552 }
1553 //______________________________________________________________________
1554 void AliITS::SetTreeAddressR(TTree *treeR){
1555     // Set branch address for the Reconstructed points Trees.
1556     // Inputs:
1557     //      TTree *treeR   Tree containing the RecPoints.
1558     // Outputs:
1559     //      none.
1560     // Return:
1561     //      none.
1562     char branchname[30];
1563
1564     if(!treeR) return;
1565     if(fRecPoints==0x0) fRecPoints = new TClonesArray("AliITSRecPoint",1000);
1566     TBranch *branch1;
1567     sprintf(branchname,"Clusters");
1568     branch1 = treeR->GetBranch(branchname);
1569     if(branch1){
1570       if(fClustersV2==0x0) fClustersV2 = new TClonesArray("AliITSclusterV2",3000);
1571       branch1->SetAddress(&fClustersV2);
1572     }
1573     else{
1574       TBranch *branch;
1575       sprintf(branchname,"%sRecPoints",GetName());
1576       branch = treeR->GetBranch(branchname);
1577       if (branch) {
1578         branch->SetAddress(&fRecPoints);
1579       }else {
1580         sprintf(branchname,"%sRecPointsF",GetName());
1581         branch = treeR->GetBranch(branchname);
1582         if (branch) {
1583           branch->SetAddress(&fRecPoints);
1584         }
1585       }
1586     }
1587 }
1588 //______________________________________________________________________
1589 void AliITS::AddRecPoint(const AliITSRecPoint &r){
1590     // Add a reconstructed space point to the list
1591     // Inputs:
1592     //      const AliITSRecPoint &r RecPoint class to be added to the tree
1593     //                              of reconstructed points TreeR.
1594     // Outputs:
1595     //      none.
1596     // Return:
1597     //      none.
1598
1599     TClonesArray &lrecp = *fRecPoints;
1600     new(lrecp[fNRecPoints++]) AliITSRecPoint(r);
1601 }
1602 //______________________________________________________________________
1603 void AliITS::AddClusterV2(const AliITSclusterV2 &r){
1604     // Add a reconstructed space point to the list
1605     // Inputs:
1606     //      const AliITSClusterV2 &r class to be added to the tree
1607     //                              of reconstructed points TreeR.
1608     // Outputs:
1609     //      none.
1610     // Return:
1611     //      none.
1612
1613     TClonesArray &lrecp = *fClustersV2;
1614     new(lrecp[fNClustersV2++]) AliITSclusterV2(r);
1615 }
1616 //______________________________________________________________________
1617 void AliITS::HitsToFastRecPoints(Int_t evNumber,Int_t bgrev,Int_t size,
1618                                   Option_t *opt0,Option_t *opt1,
1619                                  const char *flnm){
1620     // keep galice.root for signal and name differently the file for 
1621     // background when add! otherwise the track info for signal will be lost !
1622     // the condition below will disappear when the geom class will be
1623     // initialized for all versions - for the moment it is only for v5 !
1624     // Inputs:
1625     //      Int_t evnt       Event to be processed.
1626     //      Int_t bgrev      Background Hit tree number.
1627     //      Int_t size       Size used by InitModules. See InitModules.
1628     //      Option_t *opt0   Option passed to FillModules. See FillModules.
1629     //      Option_t *opt1   String indicating if merging hits or not. To
1630     //                       merge hits set equal to "Add". Otherwise no
1631     //                       background hits are considered.
1632     //      Test_t *flnm     File name containing the background hits..
1633     // Outputs:
1634     //      none.
1635     // Return:
1636     //      none.
1637
1638     if(!GetITSgeom()) return;
1639     AliITSLoader *pITSloader = (AliITSLoader*)fLoader;
1640     AliITSgeom *geom = GetITSgeom();
1641
1642     const char *all = strstr(opt1,"All");
1643     const char *det[3] ={strstr(opt1,"SPD"),strstr(opt1,"SDD"),
1644                          strstr(opt1,"SSD")};
1645     Int_t nmodules;
1646     InitModules(size,nmodules);
1647     FillModules(evNumber,bgrev,nmodules,opt0,flnm);
1648
1649     AliITSsimulation *sim      = 0;
1650     AliITSDetType    *iDetType = 0;
1651     AliITSmodule     *mod      = 0;
1652     Int_t id,module;
1653
1654     //m.b. : this change is nothing but a nice way to make sure
1655     //the CPU goes up !
1656     
1657     AliDebug(1,Form("N mod = %d",geom->GetIndexMax()));
1658     for(module=0;module<geom->GetIndexMax();module++){
1659         id       = geom->GetModuleType(module);
1660         if (!all && !det[id]) continue;
1661         iDetType = DetType(id);
1662         sim      = (AliITSsimulation*)iDetType->GetSimulationModel();
1663         if (!sim) {
1664             Error("HitsToFastPoints","The simulation class was not "
1665                   "instanciated for module %d type %x!",module,
1666                   geom->GetModuleTypeName(module));
1667             exit(1);
1668         } // end if !sim
1669         mod      = (AliITSmodule *)fITSmodules->At(module);
1670         sim->CreateFastRecPoints(mod,module,gRandom);
1671         cout<<module<<"\r";fflush(0);
1672         //gAlice->TreeR()->Fill();
1673         TTree *lTR = pITSloader->TreeR();
1674         TBranch *br=lTR->GetBranch("ITSRecPointsF");
1675         br->Fill();
1676         ResetRecPoints();
1677     } // end for module
1678
1679     ClearModules();
1680     fLoader->WriteRecPoints("OVERWRITE");
1681 }
1682 //______________________________________________________________________
1683 void AliITS::DigitsToRecPoints(Int_t evNumber,Int_t lastentry,Option_t *opt){
1684   // cluster finding and reconstruction of space points
1685   // the condition below will disappear when the geom class will be
1686   // initialized for all versions - for the moment it is only for v5 !
1687   // 7 is the SDD beam test version
1688   // Inputs:
1689   //      Int_t evNumber   Event number to be processed.
1690   //      Int_t lastentry  Offset for module when not all of the modules
1691   //                       are processed.
1692   //      Option_t *opt    String indicating which ITS sub-detectors should
1693   //                       be processed. If ="All" then all of the ITS
1694   //                       sub detectors are processed.
1695   // Outputs:
1696   //      none.
1697   // Return:
1698   //      none.
1699
1700   if(!GetITSgeom()) return;
1701   AliITSgeom *geom = GetITSgeom();
1702     
1703   const char *all = strstr(opt,"All");
1704   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
1705                         strstr(opt,"SSD")};
1706   static Bool_t setRec=kTRUE;
1707   if (setRec) SetDefaultClusterFinders();
1708   setRec=kFALSE;
1709
1710   AliITSLoader *pITSloader = (AliITSLoader*)fLoader;
1711   TTree *treeC=pITSloader->TreeC();
1712   AliITSClusterFinder *rec     = 0;
1713   AliITSDetType      *iDetType = 0;
1714   Int_t id,module,first=0;
1715   for(module=0;module<geom->GetIndexMax();module++){
1716       id       = geom->GetModuleType(module);
1717       if (!all && !det[id]) continue;
1718       if(det[id]) first = geom->GetStartDet(id);
1719       iDetType = DetType(id);
1720       rec = (AliITSClusterFinder*)iDetType->GetReconstructionModel();
1721       TClonesArray *itsDigits  = this->DigitsAddress(id);
1722       if (!rec) {
1723           Error("DigitsToRecPoints",
1724                 "The reconstruction class was not instanciated! event=%d",
1725                 evNumber);
1726           exit(1);
1727       } // end if !rec
1728       this->ResetDigits();
1729       TTree *lTD = pITSloader->TreeD();
1730       if (all) {
1731           lTD->GetEvent(lastentry+module);
1732       }else {
1733           lTD->GetEvent(lastentry+(module-first));
1734       }
1735       Int_t ndigits = itsDigits->GetEntriesFast();
1736       if(ndigits>0){
1737           rec->SetDigits(DigitsAddress(id));
1738           rec->SetClusters(ClustersAddress(id));
1739           rec->FindRawClusters(module);
1740       } // end if
1741       pITSloader->TreeR()->Fill();
1742       ResetRecPoints();
1743       ResetClustersV2();
1744       treeC->Fill();
1745       ResetClusters();
1746   } // end for module
1747
1748   pITSloader->WriteRecPoints("OVERWRITE");
1749   pITSloader->WriteRawClusters("OVERWRITE");
1750 }
1751 //______________________________________________________________________
1752 void AliITS::DigitsToRecPoints(AliRawReader* rawReader){
1753   // cluster finding and reconstruction of space points
1754   // the condition below will disappear when the geom class will be
1755   // initialized for all versions - for the moment it is only for v5 !
1756   // 7 is the SDD beam test version
1757   // Inputs:
1758   //      Int_t evNumber   Event number to be processed.
1759   //      Int_t lastentry  Offset for module when not all of the modules
1760   //                       are processed.
1761   //      Option_t *opt    String indicating which ITS sub-detectors should
1762   //                       be processed. If ="All" then all of the ITS
1763   //                       sub detectors are processed.
1764   // Outputs:
1765   //      none.
1766   // Return:
1767   //      none.
1768   if(!GetITSgeom()) return;
1769   AliITSgeom *geom = GetITSgeom();
1770   
1771     
1772   SetDefaultClusterFindersV2();
1773   
1774   AliITSLoader *pITSloader = (AliITSLoader*)fLoader;
1775   AliITSClusterFinderV2 *rec     = 0;
1776   AliITSDetType      *iDetType = 0;
1777   Int_t id=0;
1778
1779   if(!pITSloader->TreeR()) pITSloader->MakeTree("R");
1780   TTree* cTree = pITSloader->TreeR();
1781   TClonesArray *array=new TClonesArray("AliITSclusterV2",1000);
1782   cTree->Branch("Clusters",&array);
1783   delete array;
1784  
1785   TClonesArray** clusters = new TClonesArray*[geom->GetIndexMax()]; 
1786   for (Int_t iModule = 0; iModule < geom->GetIndexMax(); iModule++) {
1787     clusters[iModule] = NULL;
1788   }
1789   for(id=0;id<3;id++){
1790     iDetType = DetType(id);
1791     rec = (AliITSClusterFinderV2*)iDetType->GetReconstructionModel();
1792     if (!rec) {
1793       Error("DigitsToRecPoints",
1794             "The reconstruction class was not instanciated");
1795       exit(1);
1796     } 
1797     rec->RawdataToClusters(rawReader,clusters);    
1798   } 
1799   Int_t nClusters =0;
1800   for(Int_t iModule=0;iModule<geom->GetIndexMax();iModule++){
1801     array = clusters[iModule];
1802     if(!array){
1803       Error("DigitsToRecPoints","data for module %d missing!",iModule);
1804       array = new TClonesArray("AliITSclusterV2");
1805     }
1806     cTree->SetBranchAddress("Clusters",&array);
1807     cTree->Fill();
1808     nClusters+=array->GetEntriesFast();
1809     delete array;
1810   }
1811   pITSloader->WriteRecPoints("OVERWRITE");
1812
1813   delete[] clusters;
1814   Info("DigitsToRecPoints", "total number of found clustersV2 in ITS: %d\n", 
1815        nClusters);
1816   
1817 }
1818
1819 //______________________________________________________________________
1820 AliLoader* AliITS::MakeLoader(const char* topfoldername){ 
1821     //builds ITSgetter (AliLoader type)
1822     //if detector wants to use castomized getter, it must overload this method
1823
1824     Info("MakeLoader","Creating AliITSLoader. Top folder is %s.",
1825          topfoldername);
1826     fLoader = new AliITSLoader(GetName(),topfoldername);
1827     return fLoader;
1828 }