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