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