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