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