]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITS.cxx
f31ed6e3e60715ca7d510f9cde16a0ac680e390e
[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.51  2001/05/16 14:57:15  alibrary
19 New files for folders and Stack
20
21 Revision 1.50  2001/05/11 09:15:21  barbera
22 Corrected to make fast point creation working with PPR geometry
23
24 Revision 1.49  2001/05/11 07:37:49  hristov
25 Legacy lines commented
26
27 Revision 1.48  2001/05/10 18:14:25  barbera
28 A typo corrected
29
30 Revision 1.47  2001/05/10 17:55:59  barbera
31 Modified to create rec points also for PPR geometries
32
33 Revision 1.46  2001/05/10 00:05:28  nilsen
34 Allowed for HitsToDigits function to work with versions 5, 7, 8, and 9. This
35 should probably be cleaned up to only check to make sure that fITSgeom has
36 been properly defined.
37
38 Revision 1.45  2001/05/01 22:35:48  nilsen
39 Remove/commented a number of cout<< statements. and made change needed by
40 SSD code.
41
42 Revision 1.44  2001/04/26 22:44:01  nilsen
43 Removed dependence on layer 5/6 in AliITS::HitsToDigits. This will be
44 done properly in AliITSv???.cxx via SetDefaults.
45
46 Revision 1.43  2001/04/26 13:22:52  barbera
47 TMatrix and TVector elimininated to speed up the code
48
49 Revision 1.42  2001/04/25 21:55:12  barbera
50 Updated version to be compatible with actual verion of STEER and TPC
51
52 Revision 1.41  2001/04/21 15:16:51  barbera
53 Updated with the new SSD reconstruction code
54
55 Revision 1.40  2001/03/17 15:07:06  mariana
56 Update SDD response parameters
57
58 Revision 1.39  2001/03/12 17:45:32  hristov
59 Changes needed on Sun with CC 5.0
60
61 Revision 1.38  2001/03/07 14:04:51  barbera
62 Some vector dimensions increased to cope with full events
63
64 Revision 1.37  2001/03/07 12:36:35  barbera
65 A change added in the tracking part to manage delta rays
66
67 Revision 1.36  2001/03/02 19:44:11  barbera
68  modified to taking into account new version tracking v1
69
70 Revision 1.35  2001/02/28 18:16:46  mariana
71 Make the code compatible with the new AliRun
72
73 Revision 1.34  2001/02/11 15:51:39  mariana
74 Set protection in MakeBranch
75
76 Revision 1.33  2001/02/10 22:26:39  mariana
77 Move the initialization of the containers for raw clusters in MakeTreeC()
78
79 Revision 1.32  2001/02/08 23:55:31  nilsen
80 Removed fMajor/MinorVersion variables in favor of variables in derived classes.
81 Set arrays char *det[3] = {"SPD","SDD","SSD"} as const.
82
83 Revision 1.31  2001/02/02 23:57:28  nilsen
84 Added include file that are no londer included in AliITSgeom.h
85
86 Revision 1.30  2001/01/30 09:23:13  hristov
87 Streamers removed (R.Brun)
88
89 Revision 1.29  2001/01/26 20:01:09  hristov
90 Major upgrade of AliRoot code
91
92 Revision 1.28  2000/12/18 14:02:00  barbera
93 new version of the ITS tracking to take into account the new TPC track parametrization
94
95 Revision 1.27  2000/12/08 13:49:27  barbera
96 Hidden declaration in a for loop removed to be compliant with HP-UX compiler
97
98 Revision 1.26  2000/11/27 13:12:13  barbera
99 New version containing the files for tracking
100
101 Revision 1.25  2000/11/12 22:38:05  barbera
102 Added header file for the SPD Bari model
103
104 Revision 1.24  2000/10/09 22:18:12  barbera
105 Bug fixes from MAriana to le AliITStest.C run correctly
106
107 Revision 1.23  2000/10/05 20:47:42  nilsen
108 fixed dependencies of include files. Tryed but failed to get a root automaticly
109 generates streamer function to work. Modified SetDefaults.
110
111 Revision 1.9.2.15  2000/10/04 16:56:40  nilsen
112 Needed to include stdlib.h
113
114 =======
115 Revision 1.22  2000/10/04 19:45:52  barbera
116 Corrected by F. Carminati for v3.04
117
118 Revision 1.21  2000/10/02 21:28:08  fca
119 Removal of useless dependecies via forward declarations
120
121 Revision 1.20  2000/10/02 16:31:39  barbera
122 General code clean-up
123
124 Revision 1.9.2.14  2000/10/02 15:43:51  barbera
125 General code clean-up (e.g., printf -> cout)
126
127 Revision 1.19  2000/09/22 12:13:25  nilsen
128 Patches and updates for fixes to this and other routines.
129
130 Revision 1.18  2000/07/12 05:32:20  fca
131 Correcting several syntax problem with static members
132
133 Revision 1.17  2000/07/10 16:07:18  fca
134 Release version of ITS code
135
136 Revision 1.9.2.3  2000/02/02 13:42:09  barbera
137 fixed AliITS.cxx for new AliRun structure. Added ITS hits list to list of hits which will have their track numbers updated
138
139 Revision 1.9.2.2  2000/01/23 03:03:13  nilsen
140 //fixed FillModule. Removed fi(fabs(xl)<dx....
141
142 Revision 1.9.2.1  2000/01/12 19:03:32  nilsen
143 This is the version of the files after the merging done in December 1999.
144 See the ReadMe110100.txt file for details
145
146 Revision 1.9  1999/11/14 14:33:25  fca
147 Correct problems with distructors and pointers, thanks to I.Hrivnacova
148
149 Revision 1.8  1999/09/29 09:24:19  fca
150 Introduction of the Copyright and cvs Log
151
152 */
153
154 ///////////////////////////////////////////////////////////////////////////////
155 //
156 //      An overview of the basic philosophy of the ITS code development
157 // and analysis is show in the figure below.
158 //Begin_Html
159 /*
160 <img src="picts/ITS/ITS_Analysis_schema.gif">
161 </pre>
162 <br clear=left>
163 <font size=+2 color=red>
164 <p>Roberto Barbera is in charge of the ITS Offline code (1999).
165 <a href="mailto:roberto.barbera@ct.infn.it">Roberto Barbera</a>.
166 </font>
167 <pre>
168 */
169 //End_Html
170 //
171 //  AliITS. Inner Traking System base class.
172 //  This class contains the base procedures for the Inner Tracking System
173 //
174 //Begin_Html
175 /*
176 <img src="picts/ITS/AliITS_Class_Diagram.gif">
177 </pre>
178 <br clear=left>
179 <font size=+2 color=red>
180 <p>This show the class diagram of the different elements that are part of
181 the AliITS class.
182 </font>
183 <pre>
184 */
185 //End_Html
186 //
187 // Version: 0
188 // Written by Rene Brun, Federico Carminati, and Roberto Barbera
189 //
190 // Version: 1
191 // Modified and documented by Bjorn S. Nilsen
192 // July 11 1999
193 //
194 // Version: 2
195 // Modified and documented by A. Bologna
196 // October 18 1999
197 //
198 // AliITS is the general base class for the ITS. Also see AliDetector for
199 // futher information.
200 //
201 ///////////////////////////////////////////////////////////////////////////////
202 #include <iostream.h>
203 #include <iomanip.h>
204 #include <fstream.h>
205 #include <stdlib.h>
206 #include <TMath.h>
207 #include <TRandom.h>
208 #include <TBranch.h>
209 #include <TVector.h>
210 #include <TClonesArray.h>
211 #include <TROOT.h>
212 #include <TObjectTable.h>
213 #include <TFile.h>
214 #include <TTree.h>
215 #include <TString.h>
216 #include <TParticle.h>
217
218
219 #include "AliRun.h"
220 #include "AliITS.h"
221 #include "AliITSMap.h"
222 #include "AliITSDetType.h"
223 #include "AliITSClusterFinder.h"
224 //#include "AliITSsimulation.h"
225 #include "AliITSsimulationSPD.h"
226 #include "AliITSsimulationSDD.h"
227 #include "AliITSsimulationSSD.h"
228 #include "AliITSresponse.h"
229 #include "AliITSsegmentationSPD.h"
230 #include "AliITSresponseSPD.h"
231 #include "AliITSresponseSPDbari.h"
232 #include "AliITSsegmentationSDD.h"
233 #include "AliITSresponseSDD.h"
234 #include "AliITSsegmentationSSD.h"
235 #include "AliITSresponseSSD.h"
236 #include "AliITShit.h"
237 #include "AliITSgeom.h"
238 #include "AliITSdigit.h"
239 #include "AliITSmodule.h"
240 #include "AliITSRecPoint.h"
241 #include "AliITSRawCluster.h"
242 #include "AliMC.h"
243 #include "stdlib.h"
244 #include "AliKalmanTrack.h" 
245 #include "AliMagF.h"
246
247 #include "AliITStrack.h"
248 #include "AliITSiotrack.h"
249 #include "AliITStracking.h"
250 #include "AliITSRad.h"   
251 #include "../TPC/AliTPC.h"
252 #include "../TPC/AliTPCParam.h"
253 #include "../TPC/AliTPCtracker.h"
254
255 ClassImp(AliITS)
256  
257 //_____________________________________________________________________________
258 AliITS::AliITS() : AliDetector() {
259   //
260   // Default initialiser for ITS
261   //     The default constructor of the AliITS class. In addition to
262   // creating the AliITS class it zeros the variables fIshunt (a member
263   // of AliDetector class), fEuclidOut, and fIdN, and zeros the pointers
264   // fITSpoints, fIdSens, and fIdName. The AliDetector default constructor
265   // is also called.
266   //
267
268
269   fIshunt     = 0;
270   fEuclidOut  = 0;
271
272   fNDetTypes = kNTYPES;
273   fIdN        = 0;
274   fIdName     = 0;
275   fIdSens     = 0;
276   fITSmodules = 0;
277   //
278   fDetTypes   = 0;
279   //
280   fDtype  = 0;
281   fNdtype = 0;
282   fCtype  = 0;
283   fNctype = 0;
284   fRecPoints = 0;
285   fNRecPoints = 0;
286   fTreeC = 0;
287   //
288   fITSgeom=0;
289 }
290
291 //_____________________________________________________________________________
292 AliITS::AliITS(const char *name, const char *title):AliDetector(name,title){
293   //
294   // Default initialiser for ITS
295   //     The constructor of the AliITS class. In addition to creating the
296   // AliITS class, it allocates memory for the TClonesArrays fHits and
297   // fDigits, and for the TObjArray fITSpoints. It also zeros the variables
298   // fIshunt (a member of AliDetector class), fEuclidOut, and fIdN, and zeros
299   // the pointers fIdSens and fIdName. To help in displaying hits via the ROOT
300   // macro display.C AliITS also sets the marker color to red. The variables
301   // passes with this constructor, const char *name and *title, are used by
302   // the constructor of AliDetector class. See AliDetector class for a
303   // description of these parameters and its constructor functions.
304   //
305
306
307   fHits       = new TClonesArray("AliITShit", 1560);
308   gAlice->AddHitList(fHits);
309
310   fNDetTypes = kNTYPES;
311
312   fNdtype = new Int_t[kNTYPES];
313   fDtype = new TObjArray(kNTYPES);
314
315   fNctype = new Int_t[kNTYPES];
316   fCtype = new TObjArray(kNTYPES);
317
318
319   fRecPoints = 0;
320   fNRecPoints = 0;
321
322   fTreeC = 0;
323
324   fITSmodules = 0; 
325
326   fIshunt     = 0;
327   fEuclidOut  = 0;
328   fIdN        = 0;
329   fIdName     = 0;
330   fIdSens     = 0;
331  
332   fDetTypes = new TObjArray(kNTYPES);  
333
334   Int_t i;
335   for(i=0;i<kNTYPES;i++) {
336     fDetTypes->AddAt(new AliITSDetType(),i); 
337     fNdtype[i]=0;
338     fNctype[i]=0;
339    }
340   //
341
342   SetMarkerColor(kRed);
343
344   fITSgeom=0;
345 }
346 //___________________________________________________________________________
347 AliITS::AliITS(AliITS &source){
348   // copy constructor
349   if(this==&source) return;
350   Error("AliITS::Copy constructor",
351         "You are not allowed to make a copy of the AliITS");
352   exit(1);
353 }
354 //____________________________________________________________________________
355 AliITS& AliITS::operator=(AliITS &source){
356   // assignment operator
357   if(this==&source) return *this;
358   Error("AliITS::operator=",
359         "You are not allowed to make a copy of the AliITS");
360   exit(1);
361   return *this; //fake return
362 }
363 //____________________________________________________________________________
364 void AliITS::ClearModules(){
365   //clear the modules TObjArray
366
367   if(fITSmodules) fITSmodules->Delete();
368
369 }
370 //_____________________________________________________________________________
371 AliITS::~AliITS(){
372   //
373   // Default distructor for ITS
374   //     The default destructor of the AliITS class. In addition to deleting
375   // the AliITS class it deletes the memory pointed to by the fHits, fDigits,
376   // fIdSens, fIdName, and fITSpoints.
377   //
378
379
380   delete fHits;
381   delete fDigits;
382   delete fRecPoints;
383 //  delete fIdName;        // TObjArray of TObjStrings
384   if(fIdName!=0) delete[] fIdName;  // Array of TStrings
385   if(fIdSens!=0) delete[] fIdSens;
386   if(fITSmodules!=0) {
387       this->ClearModules();
388       delete fITSmodules;
389   }// end if fITSmodules!=0
390
391   //
392   if(fDtype) {
393     fDtype->Delete();
394     delete fDtype;
395   }
396   delete [] fNdtype;
397   if (fCtype) {
398     fCtype->Delete();
399     delete fCtype;
400   }
401   delete [] fNctype;
402   //
403
404   if (fDetTypes) {
405     fDetTypes->Delete();
406     delete fDetTypes;
407   }
408
409   if (fTreeC) delete fTreeC;
410
411   if (fITSgeom) delete fITSgeom;
412
413 }
414
415 //___________________________________________
416 AliITSDetType* AliITS::DetType(Int_t id)
417 {
418   //return pointer to id detector type
419     return ((AliITSDetType*) (*fDetTypes)[id]);
420
421 }
422 //___________________________________________
423 void AliITS::SetClasses(Int_t id, const char *digit, const char *cluster)
424 {
425   //set the digit and cluster classes to be used for the id detector type
426     ((AliITSDetType*) (*fDetTypes)[id])->ClassNames(digit,cluster);
427
428 }
429 //___________________________________________
430 void AliITS::SetResponseModel(Int_t id, AliITSresponse *response)
431 {
432   //set the response model for the id detector type
433
434     ((AliITSDetType*) (*fDetTypes)[id])->ResponseModel(response);
435
436 }
437
438 //___________________________________________
439 void AliITS::SetSegmentationModel(Int_t id, AliITSsegmentation *seg)
440 {
441   //set the segmentation model for the id detector type
442
443     ((AliITSDetType*) (*fDetTypes)[id])->SegmentationModel(seg);
444
445 }
446
447 //___________________________________________
448 void AliITS::SetSimulationModel(Int_t id, AliITSsimulation *sim)
449 {
450   //set the simulation model for the id detector type
451
452    ((AliITSDetType*) (*fDetTypes)[id])->SimulationModel(sim);
453
454 }
455 //___________________________________________
456 void AliITS::SetReconstructionModel(Int_t id, AliITSClusterFinder *reconst)
457 {
458   //set the cluster finder model for the id detector type
459
460    ((AliITSDetType*) (*fDetTypes)[id])->ReconstructionModel(reconst);
461
462 }
463
464 //_____________________________________________________________________________
465 void AliITS::AddHit(Int_t track, Int_t *vol, Float_t *hits){
466   //
467   // Add an ITS hit
468   //     The function to add information to the AliITShit class. See the
469   // AliITShit class for a full description. This function allocates the
470   // necessary new space for the hit information and passes the variable
471   // track, and the pointers *vol and *hits to the AliITShit constructor
472   // function.
473   //
474   TClonesArray &lhits = *fHits;
475   new(lhits[fNhits++]) AliITShit(fIshunt,track,vol,hits);
476 }
477 //_____________________________________________________________________________
478 void AliITS::AddRealDigit(Int_t id, Int_t *digits) 
479 {
480   // add a real digit - as coming from data
481
482   TClonesArray &ldigits = *((TClonesArray*)(*fDtype)[id]);
483   new(ldigits[fNdtype[id]++]) AliITSdigit(digits);
484
485 }
486 //_____________________________________________________________________________
487 void AliITS::AddSimDigit(Int_t id, AliITSdigit *d) 
488 {
489
490   // add a simulated digit
491
492   TClonesArray &ldigits = *((TClonesArray*)(*fDtype)[id]);
493
494   switch(id)
495   {
496   case 0:
497      new(ldigits[fNdtype[id]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
498      break;
499   case 1:
500      new(ldigits[fNdtype[id]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
501      break;
502   case 2:
503      new(ldigits[fNdtype[id]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
504      break;
505   }
506
507 }
508
509 //_____________________________________________________________________________
510 void AliITS::AddSimDigit(Int_t id,Float_t phys,Int_t *digits,Int_t *tracks,Int_t *hits,Float_t *charges){
511
512   // add a simulated digit to the list
513
514   TClonesArray &ldigits = *((TClonesArray*)(*fDtype)[id]);
515   switch(id)
516   {
517   case 0:
518      new(ldigits[fNdtype[id]++]) AliITSdigitSPD(digits,tracks,hits);
519      break;
520   case 1:
521      new(ldigits[fNdtype[id]++]) AliITSdigitSDD(phys,digits,tracks,hits,charges);
522      break;
523   case 2:
524      new(ldigits[fNdtype[id]++]) AliITSdigitSSD(digits,tracks,hits);
525      break;
526   }
527  
528 }
529
530 //_____________________________________________________________________________
531 void AliITS::AddCluster(Int_t id, AliITSRawCluster *c) 
532 {
533
534   // add a cluster to the list
535
536   TClonesArray &lcl = *((TClonesArray*)(*fCtype)[id]);
537
538   switch(id)
539   {
540   case 0:
541      new(lcl[fNctype[id]++]) AliITSRawClusterSPD(*((AliITSRawClusterSPD*)c));
542      break;
543   case 1:
544      new(lcl[fNctype[id]++]) AliITSRawClusterSDD(*((AliITSRawClusterSDD*)c));
545      break;
546   case 2:
547      new(lcl[fNctype[id]++]) AliITSRawClusterSSD(*((AliITSRawClusterSSD*)c));
548      break;
549   }
550
551 }
552
553
554 //_____________________________________________________________________________
555 void AliITS::AddRecPoint(const AliITSRecPoint &r)
556 {
557   //
558   // Add a reconstructed space point to the list
559   //
560   TClonesArray &lrecp = *fRecPoints;
561   new(lrecp[fNRecPoints++]) AliITSRecPoint(r);
562 }
563  
564
565 //____________________________________________
566 void AliITS::ResetDigits()
567 {
568     //
569     // Reset number of digits and the digits array for the ITS detector
570     //
571
572     if (!fDtype) return;
573
574     Int_t i;
575     for (i=0;i<kNTYPES;i++ ) {
576         if ((*fDtype)[i])    ((TClonesArray*)(*fDtype)[i])->Clear();
577         if (fNdtype)  fNdtype[i]=0;
578     }
579 }
580
581 //____________________________________________
582 void AliITS::ResetDigits(Int_t i)
583 {
584     //
585     // Reset number of digits and the digits array for this branch
586     //
587   if ((*fDtype)[i])    ((TClonesArray*)(*fDtype)[i])->Clear();
588   if (fNdtype)  fNdtype[i]=0;
589 }
590
591
592 //____________________________________________
593 void AliITS::ResetClusters()
594 {
595     //
596     // Reset number of clusters and the clusters array for ITS
597     //
598
599     Int_t i;
600     for (i=0;i<kNTYPES;i++ ) {
601         if ((*fCtype)[i])    ((TClonesArray*)(*fCtype)[i])->Clear();
602         if (fNctype)  fNctype[i]=0;
603     }
604
605 }
606
607 //____________________________________________
608 void AliITS::ResetClusters(Int_t i)
609 {
610     //
611     // Reset number of clusters and the clusters array for this branch
612     //
613         if ((*fCtype)[i])    ((TClonesArray*)(*fCtype)[i])->Clear();
614         if (fNctype)  fNctype[i]=0;
615
616 }
617
618
619 //____________________________________________
620 void AliITS::ResetRecPoints()
621 {
622     //
623     // Reset number of rec points and the rec points array 
624     //
625     if (fRecPoints) fRecPoints->Clear();
626     fNRecPoints = 0;
627
628 }
629
630 //_____________________________________________________________________________
631 Int_t AliITS::DistancetoPrimitive(Int_t , Int_t ){
632   //
633   // Distance from mouse to ITS on the screen. Dummy routine
634   //     A dummy routine used by the ROOT macro display.C to allow for the
635   // use of the mouse (pointing device) in the macro. In general this should
636   // never be called. If it is it returns the number 9999 for any value of
637   // x and y.
638   //
639   return 9999;
640 }
641
642 //_____________________________________________________________________________
643 void AliITS::Init(){
644   //
645   // Initialise ITS after it has been built
646   //     This routine initializes the AliITS class. It is intended to be called
647   // from the Init function in AliITSv?. Besides displaying a banner
648   // indicating that it has been called it initializes the array fIdSens
649   // and sets the default segmentation, response, digit and raw cluster classes
650   // Therefore it should be called after a call to CreateGeometry.
651   //
652   Int_t i;
653
654 //
655   SetDefaults();
656 // Array of TStrings
657   for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId(fIdName[i]);
658 //
659 }
660
661 //_____________________________________________________________________________
662 void AliITS::SetDefaults()
663 {
664   // sets the default segmentation, response, digit and raw cluster classes
665
666   if(fDebug) printf("%s: SetDefaults\n",ClassName());
667
668   AliITSDetType *iDetType;
669
670
671   //SPD 
672
673   iDetType=DetType(0); 
674   if (!iDetType->GetSegmentationModel()) {
675     AliITSsegmentationSPD *seg0=new AliITSsegmentationSPD(fITSgeom);
676     SetSegmentationModel(0,seg0); 
677   }
678   if (!iDetType->GetResponseModel()) {
679      SetResponseModel(0,new AliITSresponseSPD()); 
680   }
681   // set digit and raw cluster classes to be used
682   
683   const char *kData0=(iDetType->GetResponseModel())->DataType();
684   if (strstr(kData0,"real")) {
685       iDetType->ClassNames("AliITSdigit","AliITSRawClusterSPD");
686   } else iDetType->ClassNames("AliITSdigitSPD","AliITSRawClusterSPD");
687
688   // SDD                                          //
689   iDetType=DetType(1); 
690   if (!iDetType->GetResponseModel()) {
691     SetResponseModel(1,new AliITSresponseSDD()); 
692   }
693   AliITSresponse *resp1=iDetType->GetResponseModel();
694   if (!iDetType->GetSegmentationModel()) {
695     AliITSsegmentationSDD *seg1=new AliITSsegmentationSDD(fITSgeom,resp1);
696     SetSegmentationModel(1,seg1); 
697   }
698   const char *kData1=(iDetType->GetResponseModel())->DataType();
699   const char *kopt=iDetType->GetResponseModel()->ZeroSuppOption();
700   if ((!strstr(kopt,"2D")) && (!strstr(kopt,"1D")) || strstr(kData1,"real") ) {
701       iDetType->ClassNames("AliITSdigit","AliITSRawClusterSDD");
702   } else iDetType->ClassNames("AliITSdigitSDD","AliITSRawClusterSDD");
703
704   // SSD
705   iDetType=DetType(2); 
706   if (!iDetType->GetSegmentationModel()) {
707     AliITSsegmentationSSD *seg2=new AliITSsegmentationSSD(fITSgeom);
708     SetSegmentationModel(2,seg2); 
709   }
710   if (!iDetType->GetResponseModel()) {
711     SetResponseModel(2,new AliITSresponseSSD()); 
712   }
713   const char *kData2=(iDetType->GetResponseModel())->DataType();
714   if (strstr(kData2,"real")) {
715       iDetType->ClassNames("AliITSdigit","AliITSRawClusterSSD");
716   } else iDetType->ClassNames("AliITSdigitSSD","AliITSRawClusterSSD");
717
718   if (kNTYPES>3) {
719     Warning("SetDefaults","Only the three basic detector types are initialised!");
720   } 
721
722 }
723 //_____________________________________________________________________________
724 void AliITS::SetDefaultSimulation()
725 {
726   // to be written
727
728 }
729 //_____________________________________________________________________________
730 void AliITS::SetDefaultClusterFinders()
731 {
732   // to be written
733
734 }
735 //_____________________________________________________________________________
736
737 void AliITS::MakeTreeC(Option_t *option)
738 {
739   // create a separate tree to store the clusters
740
741 //  cout << "AliITS::MakeTreeC" << endl;
742
743      const char *optC = strstr(option,"C");
744      if (optC && !fTreeC) fTreeC = new TTree("TC","Clusters in ITS");
745      else return;
746
747      Int_t buffersize = 4000;
748      char branchname[30];
749
750      const char *det[3] = {"SPD","SDD","SSD"};
751
752      char digclass[40];
753      char clclass[40];
754
755      // one branch for Clusters per type of detector
756      Int_t i;
757      for (i=0; i<kNTYPES ;i++) {
758        AliITSDetType *iDetType=DetType(i); 
759        iDetType->GetClassNames(digclass,clclass);
760        // clusters
761        (*fCtype)[i] = new TClonesArray(clclass,10000); 
762         if (kNTYPES==3) sprintf(branchname,"%sClusters%s",GetName(),det[i]);
763         else  sprintf(branchname,"%sClusters%d",GetName(),i+1);
764         if (fCtype   && fTreeC) {
765            TreeC()->Branch(branchname,&((*fCtype)[i]), buffersize);
766 //         cout << "Making Branch " << branchname;
767 //         cout << " for Clusters of detector type " << i+1 << endl;
768         }       
769      }
770
771 }
772
773 //_____________________________________________________________________________
774 void AliITS::GetTreeC(Int_t event)
775 {
776
777 //  cout << "AliITS::GetTreeC" << endl;
778
779   // get the clusters tree for this event and set the branch address
780     char treeName[20];
781     char branchname[30];
782
783     const char *det[3] = {"SPD","SDD","SSD"};
784
785     ResetClusters();
786     if (fTreeC) {
787           delete fTreeC;
788     }
789
790     sprintf(treeName,"TreeC%d",event);
791     fTreeC = (TTree*)gDirectory->Get(treeName);
792
793     TBranch *branch;
794     if (fTreeC) {
795         Int_t i;
796         for (i=0; i<kNTYPES; i++) {
797            if (kNTYPES==3) sprintf(branchname,"%sClusters%s",GetName(),det[i]);
798            else  sprintf(branchname,"%sClusters%d",GetName(),i+1);
799            if (fCtype) {
800                 branch = fTreeC->GetBranch(branchname);
801                 if (branch) branch->SetAddress(&((*fCtype)[i]));
802             }
803         }
804     } else {
805         Error("AliITS::GetTreeC",
806                 "cannot find Clusters Tree for event:%d\n",event);
807     }
808
809 }
810 //_____________________________________________________________________________
811 void AliITS::MakeBranch(Option_t* option, const char *file)
812 {
813   //
814   // Creates Tree branches for the ITS.
815   //
816   //
817   Int_t buffersize = 4000;
818   char branchname[30];
819   sprintf(branchname,"%s",GetName());
820
821   AliDetector::MakeBranch(option,file);
822
823   const char *cD = strstr(option,"D");
824   const char *cR = strstr(option,"R");
825
826   if (cD) {
827   //
828   // one branch for digits per type of detector
829   //
830    const char *det[3] = {"SPD","SDD","SSD"};
831
832    char digclass[40];
833    char clclass[40];
834
835    Int_t i;
836    for (i=0; i<kNTYPES ;i++) {
837        AliITSDetType *iDetType=DetType(i); 
838        iDetType->GetClassNames(digclass,clclass);
839        // digits
840        if(!((*fDtype)[i])) (*fDtype)[i] = new TClonesArray(digclass,10000);
841        else ResetDigits(i);
842    }
843
844    for (i=0; i<kNTYPES ;i++) {
845       if (kNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
846       else  sprintf(branchname,"%sDigits%d",GetName(),i+1);      
847       if (fDtype && gAlice->TreeD()) {
848         MakeBranchInTree(gAlice->TreeD(), 
849                          branchname, &((*fDtype)[i]), buffersize, file);
850 //          cout << "Making Branch " << branchname;
851 //          cout << " for digits of type "<< i+1 << endl;
852       } 
853     }
854   }
855
856   if (cR) {
857   //
858   // only one branch for rec points for all detector types
859   //
860     sprintf(branchname,"%sRecPoints",GetName());
861  
862     if(!fRecPoints) fRecPoints=new TClonesArray("AliITSRecPoint",10000);
863
864     if (fRecPoints && gAlice->TreeR()) {
865       MakeBranchInTree(gAlice->TreeR(), 
866                                branchname, &fRecPoints, buffersize, file) ;
867 //      cout << "Making Branch " << branchname;
868 //      cout << " for reconstructed space points" << endl;
869     }
870   }     
871 }
872
873 //___________________________________________
874 void AliITS::SetTreeAddress()
875 {
876
877   // Set branch address for the Trees.
878
879   char branchname[30];
880   AliDetector::SetTreeAddress();
881
882   const char *det[3] = {"SPD","SDD","SSD"};
883
884   TBranch *branch;
885   TTree *treeD = gAlice->TreeD();
886   TTree *treeR = gAlice->TreeR();
887
888   Int_t i;
889   if (treeD) {
890       for (i=0; i<kNTYPES; i++) {
891           if (kNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
892           else  sprintf(branchname,"%sDigits%d",GetName(),i+1);
893           if (fDtype) {
894               branch = treeD->GetBranch(branchname);
895               if (branch) branch->SetAddress(&((*fDtype)[i]));
896           }
897       }
898   }
899
900  
901   if (treeR) {
902     sprintf(branchname,"%sRecPoints",GetName());
903       branch = treeR->GetBranch(branchname);
904       if (branch) branch->SetAddress(&fRecPoints);
905   }
906   
907
908 }
909
910 //____________________________________________________________________________
911 void AliITS::InitModules(Int_t size,Int_t &nmodules){
912
913   //initialize the modules array
914
915   if(fITSmodules){ 
916       fITSmodules->Delete();
917       delete fITSmodules;
918   }
919
920     Int_t nl,indexMAX,index;
921
922     if(size<=0){ // default to using data stored in AliITSgeom
923         if(fITSgeom==0) {
924             Error("AliITS::InitModules",
925                 "in AliITS::InitModule fITSgeom not defined\n");
926             return;
927         } // end if fITSgeom==0
928         nl = fITSgeom->GetNlayers();
929         indexMAX = fITSgeom->GetModuleIndex(nl,fITSgeom->GetNladders(nl),
930                                             fITSgeom->GetNdetectors(nl))+1;
931         nmodules = indexMAX;
932         fITSmodules = new TObjArray(indexMAX);
933         for(index=0;index<indexMAX;index++){
934                 fITSmodules->AddAt( new AliITSmodule(index),index);
935         } // end for index
936     }else{
937         fITSmodules = new TObjArray(size);
938         for(index=0;index<size;index++) {
939             fITSmodules->AddAt( new AliITSmodule(index),index);
940         }
941
942         nmodules = size;
943     } // end i size<=0
944 }
945
946 //____________________________________________________________________________
947 void AliITS::FillModules(Int_t evnt,Int_t bgrev,Int_t nmodules,Option_t *option,Text_t *filename){
948
949   // fill the modules with the sorted by module hits; add hits from background
950   // if option=Add
951
952
953     static TTree *trH1;                 //Tree with background hits
954     static TClonesArray *fHits2;        //List of hits for one track only
955
956     static Bool_t first=kTRUE;
957     static TFile *file;
958     const char *addBgr = strstr(option,"Add");
959
960
961     if (addBgr ) {
962         if(first) {
963 //          cout<<"filename "<<filename<<endl;
964             file=new TFile(filename);
965 //          cout<<"I have opened "<<filename<<" file "<<endl;
966             fHits2     = new TClonesArray("AliITShit",1000  );
967         }           
968         first=kFALSE;
969         file->cd();
970         file->ls();
971         // Get Hits Tree header from file
972         if(fHits2) fHits2->Clear();
973         if(trH1) delete trH1;
974         trH1=0;
975         
976         char treeName[20];
977         sprintf(treeName,"TreeH%d",bgrev);
978         trH1 = (TTree*)gDirectory->Get(treeName);
979         //printf("TrH1 %p of treename %s for event %d \n",trH1,treeName,bgrev);
980         
981         if (!trH1) {
982             Error("AliITS::FillModules",
983             "cannot find Hits Tree for event:%d\n",bgrev);
984         }
985         // Set branch addresses
986         TBranch *branch;
987         char branchname[20];
988         sprintf(branchname,"%s",GetName());
989         if (trH1 && fHits2) {
990             branch = trH1->GetBranch(branchname);
991             if (branch) branch->SetAddress(&fHits2);
992         }
993
994         // test
995         //Int_t ntracks1 =(Int_t)TrH1->GetEntries();
996         //printf("background - ntracks1 - %d\n",ntracks1);
997    }
998
999     //Int_t npart = gAlice->GetEvent(evnt);
1000     //if(npart<=0) return;
1001     TClonesArray *itsHits = this->Hits();
1002     Int_t lay,lad,det,index;
1003     AliITShit *itsHit=0;
1004     AliITSmodule *mod=0;
1005
1006     TTree *iTH = gAlice->TreeH();
1007     Int_t ntracks =(Int_t) iTH->GetEntries();
1008
1009     Int_t t,h;
1010     for(t=0; t<ntracks; t++){
1011         gAlice->ResetHits();
1012         iTH->GetEvent(t);
1013         Int_t nhits = itsHits->GetEntriesFast();
1014         //printf("nhits %d\n",nhits);
1015         if (!nhits) continue;
1016         for(h=0; h<nhits; h++){
1017             itsHit = (AliITShit *)itsHits->UncheckedAt(h);
1018             itsHit->GetDetectorID(lay,lad,det);
1019             // temporarily index=det-1 !!!
1020             if(fITSgeom) index = fITSgeom->GetModuleIndex(lay,lad,det);
1021             else index=det-1;
1022             //
1023             mod = this->GetModule(index);
1024             mod->AddHit(itsHit,t,h);
1025         } // end loop over hits 
1026     } // end loop over tracks
1027
1028     // open the file with background
1029     
1030     if (addBgr ) {
1031           Int_t track,i;
1032           ntracks =(Int_t)trH1->GetEntries();
1033             //printf("background - ntracks1 %d\n",ntracks);
1034             //printf("background - Start loop over tracks \n");     
1035             //   Loop over tracks
1036
1037             for (track=0; track<ntracks; track++) {
1038
1039                 if (fHits2)       fHits2->Clear();
1040                 trH1->GetEvent(track);
1041                 //   Loop over hits
1042                 for(i=0;i<fHits2->GetEntriesFast();++i) {
1043
1044                     itsHit=(AliITShit*) (*fHits2)[i];
1045                     itsHit->GetDetectorID(lay,lad,det);
1046                     // temporarily index=det-1 !!!
1047                     if(fITSgeom) index = fITSgeom->GetModuleIndex(lay,lad,det);
1048                     else index=det-1;
1049                     //
1050                     mod = this->GetModule(index);
1051                     mod->AddHit(itsHit,track,i);
1052                }  // end loop over hits
1053             } // end loop over tracks
1054
1055             TTree *fAli=gAlice->TreeK();
1056             TFile *fileAli=0;
1057             
1058             if (fAli) fileAli =fAli->GetCurrentFile();
1059             fileAli->cd();
1060
1061     } // end if add
1062
1063     //gObjectTable->Print();
1064
1065 }
1066
1067 //____________________________________________________________________________
1068
1069 void AliITS::SDigits2Digits()
1070 {
1071   
1072   AliITSgeom *geom = GetITSgeom();
1073   
1074   // SPD
1075   AliITSDetType *iDetType;
1076   iDetType=DetType(0);
1077   AliITSsegmentationSPD *seg0=(AliITSsegmentationSPD*)iDetType->GetSegmentationModel();
1078   AliITSresponseSPD *res0 = (AliITSresponseSPD*)iDetType->GetResponseModel();
1079   AliITSsimulationSPD *sim0=new AliITSsimulationSPD(seg0,res0);
1080   SetSimulationModel(0,sim0);
1081   // test
1082   // printf("SPD dimensions %f %f \n",seg0->Dx(),seg0->Dz());
1083   // printf("SPD npixels %d %d \n",seg0->Npz(),seg0->Npx());
1084   // printf("SPD pitches %d %d \n",seg0->Dpz(0),seg0->Dpx(0));
1085   // end test
1086   // 
1087   // SDD
1088   //Set response functions
1089   // SDD compression param: 2 fDecrease, 2fTmin, 2fTmax or disable, 2 fTolerance
1090
1091   iDetType=DetType(1);
1092   AliITSresponseSDD *res1 = (AliITSresponseSDD*)iDetType->GetResponseModel();
1093   if (!res1) {
1094     res1=new AliITSresponseSDD();
1095     SetResponseModel(1,res1);
1096   }
1097   Float_t noise, baseline;
1098   res1->GetNoiseParam(noise,baseline);
1099   Float_t noise_after_el =  res1->GetNoiseAfterElectronics();
1100   Float_t fCutAmp = baseline + 2.*noise_after_el;
1101   Int_t cp[8]={0,0,(int)fCutAmp,(int)fCutAmp,0,0,0,0}; //1D
1102   res1->SetCompressParam(cp);
1103   AliITSsegmentationSDD *seg1=(AliITSsegmentationSDD*)iDetType->GetSegmentationModel();
1104   if (!seg1) {
1105     seg1 = new AliITSsegmentationSDD(geom,res1);
1106     SetSegmentationModel(1,seg1);
1107   }
1108   AliITSsimulationSDD *sim1=new AliITSsimulationSDD(seg1,res1);
1109   SetSimulationModel(1,sim1);
1110
1111   // SSD
1112   iDetType=DetType(2);
1113   AliITSsegmentationSSD *seg2=(AliITSsegmentationSSD*)iDetType->GetSegmentationModel();
1114   AliITSresponseSSD *res2 = (AliITSresponseSSD*)iDetType->GetResponseModel();
1115   res2->SetSigmaSpread(3.,2.);
1116   AliITSsimulationSSD *sim2=new AliITSsimulationSSD(seg2,res2);
1117   SetSimulationModel(2,sim2);
1118  
1119   cerr<<"Digitizing ITS...\n";
1120  
1121   TStopwatch timer;
1122   timer.Start();
1123   HitsToDigits(gAlice->GetEvNumber(),0,-1," ","All"," ");
1124   timer.Stop(); timer.Print();
1125
1126   delete sim0;
1127   delete sim1;
1128   delete sim2;
1129 }
1130
1131
1132 //____________________________________________________________________________
1133 void AliITS::HitsToDigits(Int_t evNumber,Int_t bgrev,Int_t size, Option_t *option, Option_t *opt,Text_t *filename)
1134 {
1135     // keep galice.root for signal and name differently the file for 
1136     // background when add! otherwise the track info for signal will be lost !
1137   
1138    // the condition below will disappear when the geom class will be
1139    // initialised for all versions - for the moment it is only for v5 !
1140    // 7 is the SDD beam test version  
1141    Int_t ver = this->IsVersion(); 
1142    if(ver!=5 && ver!=7 && ver!=8 && ver!=9) return; 
1143
1144    const char *all = strstr(opt,"All");
1145    const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),strstr(opt,"SSD")};
1146 //   cout<<" 1 AliITS "<<endl;
1147    Int_t nmodules;
1148    InitModules(size,nmodules); 
1149 //   cout<<" 2 AliITS "<<endl;
1150    FillModules(evNumber,bgrev,nmodules,option,filename);
1151 //   cout<<" 3 AliITS "<<endl;
1152
1153    //TBranch *branch;
1154    AliITSsimulation* sim;
1155    //TObjArray *branches=gAlice->TreeD()->GetListOfBranches();
1156    AliITSgeom *geom = GetITSgeom();
1157
1158    Int_t id,module;
1159 //   Int_t lay, lad, detect;
1160    Int_t first,last;
1161    for (id=0;id<kNTYPES;id++) {
1162         if (!all && !det[id]) continue;
1163         //branch = (TBranch*)branches->UncheckedAt(id);
1164         AliITSDetType *iDetType=DetType(id); 
1165         sim = (AliITSsimulation*)iDetType->GetSimulationModel();
1166
1167         if (!sim) {
1168            Error("HitsToDigits","The simulation class was not instantiated!");
1169            exit(1);
1170            // or SetDefaultSimulation();
1171         }
1172
1173         if(geom) {
1174           first = geom->GetStartDet(id);
1175           last = geom->GetLastDet(id);
1176         } else first=last=0;
1177 //      cout << "det type " << id << " first, last "<< first << last << endl;
1178         for(module=first;module<=last;module++) {
1179             AliITSmodule *mod = (AliITSmodule *)fITSmodules->At(module);
1180 /*
1181             geom->GetModuleId(module,lay, lad, detect);
1182             if ( lay == 6 )
1183               ((AliITSsegmentationSSD*)(((AliITSsimulationSSD*)sim)->GetSegmentation()))->SetLayer(6);
1184             if ( lay == 5 )
1185               ((AliITSsegmentationSSD*)(((AliITSsimulationSSD*)sim)->GetSegmentation()))->SetLayer(5);
1186 */
1187             sim->DigitiseModule(mod,module,evNumber);
1188             // fills all branches - wasted disk space
1189             gAlice->TreeD()->Fill(); 
1190             ResetDigits();
1191             // try and fill only the branch 
1192             //branch->Fill();
1193             //ResetDigits(id);
1194         } // loop over modules
1195    } // loop over detector types
1196
1197    ClearModules();
1198
1199 //   Int_t nentries=(Int_t)
1200    gAlice->TreeD()->GetEntries();
1201 //   cout << "nentries in TreeD" << nentries << endl;
1202
1203    char hname[30];
1204    sprintf(hname,"TreeD%d",evNumber);
1205    gAlice->TreeD()->Write(hname,TObject::kOverwrite);
1206    // reset tree
1207    gAlice->TreeD()->Reset();
1208
1209 }
1210
1211
1212 //____________________________________________________________________________
1213 void AliITS::DigitsToRecPoints(Int_t evNumber,Int_t lastentry,Option_t *opt)
1214 {
1215   // cluster finding and reconstruction of space points
1216   
1217    // the condition below will disappear when the geom class will be
1218    // initialised for all versions - for the moment it is only for v5 !
1219    // 7 is the SDD beam test version  
1220    Int_t ver = this->IsVersion(); 
1221    if(ver!=5 && ver!=8 && ver!=9) return;
1222
1223    const char *all = strstr(opt,"All");
1224    const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),strstr(opt,"SSD")};
1225
1226    static Bool_t first=kTRUE;
1227    if (!TreeC() && first) {
1228        MakeTreeC("C");
1229        first=kFALSE;
1230    }
1231
1232    TTree *treeC=TreeC();
1233  
1234
1235    //TBranch *branch;
1236    AliITSClusterFinder* rec;
1237
1238    //TObjArray *branches=gAlice->TreeR()->GetListOfBranches();
1239    AliITSgeom *geom = GetITSgeom();
1240
1241    Int_t id,module;
1242    for (id=0;id<kNTYPES;id++) {
1243         if (!all && !det[id]) continue;
1244         //branch = (TBranch*)branches->UncheckedAt(id);
1245         AliITSDetType *iDetType=DetType(id); 
1246         rec = (AliITSClusterFinder*)iDetType->GetReconstructionModel();
1247         if (!rec) {
1248            Error("DigitsToRecPoints","The cluster finder class was not instantiated!");
1249            exit(1);
1250            // or SetDefaultClusterFinders();
1251         }
1252         TClonesArray *itsDigits  = this->DigitsAddress(id);
1253
1254         Int_t first,last;
1255         if(geom) {
1256           first = geom->GetStartDet(id);
1257           last = geom->GetLastDet(id);
1258         } else first=last=0;
1259         printf("first module - last module %d %d\n",first,last);
1260         for(module=first;module<=last;module++) {
1261               this->ResetDigits();
1262               if (all) gAlice->TreeD()->GetEvent(lastentry+module);
1263               else gAlice->TreeD()->GetEvent(lastentry+(module-first));
1264               Int_t ndigits = itsDigits->GetEntriesFast();
1265               if (ndigits) rec->FindRawClusters(module);
1266               gAlice->TreeR()->Fill(); 
1267               ResetRecPoints();
1268               treeC->Fill();
1269               ResetClusters();
1270               // try and fill only the branch 
1271               //branch->Fill();
1272               //ResetRecPoints(id);
1273         } // loop over modules
1274    } // loop over detector types
1275
1276
1277 //   Int_t nentries=(Int_t)
1278    gAlice->TreeR()->GetEntries();
1279 //   Int_t ncentries=(Int_t)
1280    treeC->GetEntries();
1281 //   cout << " nentries ncentries " << nentries << ncentries <<  endl;
1282
1283    char hname[30];
1284    sprintf(hname,"TreeR%d",evNumber);
1285    gAlice->TreeR()->Write(hname,TObject::kOverwrite);
1286    // reset tree
1287    gAlice->TreeR()->Reset();
1288
1289    sprintf(hname,"TreeC%d",evNumber);
1290    treeC->Write(hname,TObject::kOverwrite);
1291    treeC->Reset();
1292 }
1293 //____________________________________________________________________________
1294 void AliITS::HitsToFastRecPoints(Int_t evNumber,Int_t bgrev,Int_t size,
1295 Option_t *option,Option_t *opt,Text_t *filename)
1296 {
1297     // keep galice.root for signal and name differently the file for 
1298     // background when add! otherwise the track info for signal will be lost !
1299   
1300
1301    // the condition below will disappear when the geom class will be
1302    // initialised for all versions - for the moment it is only for v5 !  
1303    Int_t ver = this->IsVersion(); 
1304    if(ver!=5 && ver!=8 && ver!=9) return;
1305    //if(ver!=5) return; 
1306
1307    const char *all = strstr(opt,"All");
1308    const char *det[3] ={strstr(opt,"SPD"),strstr(opt,"SDD"),strstr(opt,"SSD")};
1309
1310    Int_t nmodules;
1311    InitModules(size,nmodules);
1312    FillModules(evNumber,bgrev,nmodules,option,filename);
1313
1314
1315    AliITSsimulation* sim;
1316    AliITSgeom *geom = GetITSgeom();
1317
1318    TRandom *random=new TRandom[9];
1319    random[0].SetSeed(111);
1320    random[1].SetSeed(222);
1321    random[2].SetSeed(333);              
1322    random[3].SetSeed(444);
1323    random[4].SetSeed(555);
1324    random[5].SetSeed(666);              
1325    random[6].SetSeed(777);
1326    random[7].SetSeed(888);
1327    random[8].SetSeed(999);              
1328
1329
1330    Int_t id,module;
1331    for (id=0;id<kNTYPES;id++) {
1332         if (!all && !det[id]) continue;
1333         AliITSDetType *iDetType=DetType(id); 
1334         sim = (AliITSsimulation*)iDetType->GetSimulationModel();
1335         if (!sim) {
1336            Error("HitsToFastPoints",
1337                  "The simulation class was not instantiated!");
1338            exit(1);
1339            // or SetDefaultSimulation();
1340         }
1341
1342         Int_t first,last;
1343         if(geom) {
1344           first = geom->GetStartDet(id);
1345           last = geom->GetLastDet(id);
1346         } else first=last=0;
1347         printf("first module - last module %d %d\n",first,last);
1348         for(module=first;module<=last;module++) {
1349             AliITSmodule *mod = (AliITSmodule *)fITSmodules->At(module);
1350             sim->CreateFastRecPoints(mod,module,random);
1351             gAlice->TreeR()->Fill(); 
1352             ResetRecPoints();
1353         } // loop over modules
1354    } // loop over detector types
1355
1356
1357    ClearModules();
1358
1359    //Int_t nentries=(Int_t)gAlice->TreeR()->GetEntries();
1360
1361    char hname[30];
1362    sprintf(hname,"TreeR%d",evNumber);
1363    gAlice->TreeR()->Write(hname,TObject::kOverwrite);
1364    // reset tree
1365    gAlice->TreeR()->Reset();
1366
1367    delete [] random;
1368
1369 }
1370 //________________________________________________________________
1371 AliITStrack  AliITS::Tracking(AliITStrack &track, AliITStrack *reference,
1372                               TObjArray *fastpoints, Int_t **vettid,
1373                               Bool_t flagvert,  AliITSRad *rl  ) {
1374 // Origin  A. Badala' and G.S. Pappalardo:  e-mail Angela.Badala@ct.infn.it,
1375 // Giuseppe.S.Pappalardo@ct.infn.it
1376
1377   TList *list= new TList();   
1378
1379   AliITStrack tr(track);
1380   
1381   list->AddLast(&tr);
1382   
1383   Double_t Pt=(tr).GetPt();
1384 //  cout << "\n Pt = " << Pt <<"\n";  //stampa
1385
1386   AliITStracking obj(list, reference, this, fastpoints,
1387                      TMath::Abs(Pt),vettid, flagvert, rl);
1388   list->Delete();
1389   delete list;
1390
1391   Int_t itot=-1;
1392   TVector VecTotLabref(18);
1393   Int_t lay, k;
1394   for(lay=5; lay>=0; lay--) {
1395     TVector VecLabref(3); 
1396     VecLabref=(*reference).GetLabTrack(lay);
1397     Float_t ClustZ=(*reference).GetZclusterTrack( lay); //aggiunta il 5-3-2001
1398     for(k=0; k<3; k++){
1399         //{itot++; VecTotLabref(itot)=VecLabref(k);} //cambiata il 5-3-2001 
1400         Int_t lpp=(Int_t)VecLabref(k);
1401         if(lpp>=0) {
1402             TParticle *p=(TParticle*) gAlice->Particle(lpp);
1403             Int_t pcode=p->GetPdgCode();
1404             if(pcode==11) VecLabref(k)=p->GetFirstMother();
1405         } // end if lpp>=0    
1406         itot++; VecTotLabref(itot)=VecLabref(k);
1407         if(VecLabref(k)==0. && ClustZ == 0.) VecTotLabref(itot) =-3.; }  
1408   } // end for lay
1409   Long_t labref;
1410   Int_t freq;  
1411   (*reference).Search(VecTotLabref, labref, freq);
1412     
1413   //if(freq < 4) labref=-labref;
1414   //if(freq < 6) labref=-labref;        // cinque - sei
1415   if(freq < 5) labref=-labref;        // cinque - sei   
1416   (*reference).SetLabel(labref);
1417
1418   return *reference; 
1419
1420 }
1421 //________________________________________________________________------
1422 void AliITS::DoTracking(Int_t evNumber, Int_t min_t, Int_t max_t,
1423                         TFile *file, Bool_t flagvert) {
1424     //   ex macro for tracking ITS
1425
1426     printf("begin DoTracking - file %p\n",file);
1427
1428     //const char *pname="75x40_100x60";
1429   
1430     Int_t imax=200,jmax=450;
1431     AliITSRad *rl = new AliITSRad(imax,jmax);
1432     //cout<<" dopo costruttore AliITSRad\n"; getchar();
1433
1434     struct GoodTrack {
1435         Int_t lab,code;
1436         Float_t px,py,pz,x,y,z,pxg,pyg,pzg,ptg;
1437         Bool_t flag;
1438     };
1439
1440     gAlice->GetEvent(0);
1441
1442     AliKalmanTrack *kkprov;
1443     kkprov->SetConvConst(100/0.299792458/0.2/gAlice->Field()->Factor());  
1444
1445     /*  //modificato il 26-4-2001
1446         AliTPC *TPC=(AliTPC*)gAlice->GetDetector("TPC");
1447         AliTPCParam *digp = (AliTPCParam*)file->Get(pname);
1448         if (digp!=0) TPC->SetParam(digp);
1449     */
1450     TFile *cf=TFile::Open("AliTPCclusters.root");  
1451     AliTPCParam *digp= (AliTPCParam*)cf->Get("75x40_100x60");
1452     if (!digp) { cerr<<"TPC parameters have not been found !\n"; getchar();}
1453
1454     AliTPCtracker *tracker = new AliTPCtracker(digp); //aggiunto il 23-5 
1455     //aggiunto il 23-5 
1456     // Load clusters
1457     tracker->LoadInnerSectors();
1458     tracker->LoadOuterSectors();
1459
1460     GoodTrack gt[15000];
1461     Int_t ngood=0;
1462     ifstream in("itsgood_tracks");
1463
1464     cerr<<"Reading itsgood tracks...\n";
1465     while (in>>gt[ngood].lab>>gt[ngood].code
1466            >>gt[ngood].px >>gt[ngood].py>>gt[ngood].pz
1467            >>gt[ngood].x  >>gt[ngood].y >>gt[ngood].z
1468            >>gt[ngood].pxg  >>gt[ngood].pyg >>gt[ngood].pzg
1469            >>gt[ngood].ptg >>gt[ngood].flag) {
1470         ngood++;
1471         cerr<<ngood<<'\r';
1472         if (ngood==15000) {
1473             cerr<<"Too many good tracks !\n";
1474             break;
1475         } // end if ngood==1500
1476     } // end while
1477     if (!in.eof()) cerr<<"Read error (itsgood_tracks) !\n";
1478
1479     // Load tracks
1480     // TFile *tf=TFile::Open("tpctracks.root"); //commentato 26-4-2001 
1481     TFile *tf=TFile::Open("AliTPCtracks.root"); 
1482     if (!tf->IsOpen()) {cerr<<"Can't open AliTPCtracks.root !\n"; return ;}
1483     TObjArray tracks(200000);
1484     //TTree *tracktree=(TTree*)tf->Get("TreeT");
1485     TTree *tracktree=(TTree*)tf->Get("TPCf"); //aggiunto il 23-5
1486     if (!tracktree) {cerr<<"Can't get a tree with TPC tracks !\n";} 
1487     TBranch *tbranch=tracktree->GetBranch("tracks");
1488     Int_t nentr=(Int_t)tracktree->GetEntries();
1489     Int_t kk;
1490     /*  commentato il 26-4-2001
1491         for (kk=0; kk<nentr; kk++) {
1492         AliTPCtrack *iotrack=new AliTPCtrack;
1493         tbranch->SetAddress(&iotrack);
1494         tracktree->GetEvent(kk);
1495         tracks.AddLast(iotrack);
1496         } 
1497     */
1498     AliTPCtrack *iotracktpc=0;    
1499     for (kk=0; kk<nentr; kk++) {
1500         iotracktpc=new AliTPCtrack; 
1501         tbranch->SetAddress(&iotracktpc);
1502         tracktree->GetEvent(kk);
1503         tracker->CookLabel(iotracktpc,0.1); //aggiunto 23-5
1504         tracks.AddLast(iotracktpc);    
1505     } // end for kk
1506     delete tracker;
1507     tf->Close();
1508
1509     Int_t nt = tracks.GetEntriesFast();
1510     cerr<<"Number of found tracks "<<nt<<endl;
1511
1512     TVector DataOut(9);
1513     Int_t kkk=0;
1514
1515     Double_t ptg=0.,pxg=0.,pyg=0.,pzg=0.;
1516
1517     //////////////  good tracks definition in TPC  ///////////////
1518
1519     ofstream out1 ("AliITSTrag.out");
1520     Int_t i;
1521     for (i=0; i<ngood; i++) out1 << gt[i].ptg << "\n";
1522     out1.close();
1523
1524
1525     TVector vec(5);
1526     TTree *TR=gAlice->TreeR();
1527     Int_t nent=(Int_t)TR->GetEntries();
1528     TClonesArray  *recPoints = RecPoints();
1529     Int_t numbpoints;
1530     Int_t totalpoints=0;
1531     Int_t *np = new Int_t[nent];
1532     Int_t **vettid = new Int_t* [nent];
1533     Int_t mod;
1534     for (mod=0; mod<nent; mod++) {
1535         vettid[mod]=0;
1536         this->ResetRecPoints();
1537         //gAlice->TreeR()->GetEvent(mod+1); //first entry in TreeR is empty
1538         gAlice->TreeR()->GetEvent(mod); //first entry in TreeR is empty
1539         numbpoints = recPoints->GetEntries();
1540         totalpoints+=numbpoints;
1541         np[mod] = numbpoints;
1542         //cout<<" mod = "<<mod<<"   numbpoints = "<<numbpoints<<"\n";getchar();
1543         vettid[mod] = new Int_t[numbpoints];
1544         Int_t ii;
1545         for (ii=0;ii<numbpoints; ii++) *(vettid[mod]+ii)=0;
1546     } // end for mod
1547
1548     AliTPCtrack *track=0;
1549  
1550     if(min_t < 0) {min_t = 0; max_t = nt-1;}   
1551
1552 /*
1553   //////////////////// Definition of vertex end its error ////////////////////
1554   ////////////// In the future it will be given by a method //////////////////
1555   Double_t Vx=0.;
1556   Double_t Vy=0.;
1557   Double_t Vz=0.;
1558
1559   Float_t sigmavx=0.0050;      // 50  microns
1560   Float_t sigmavy=0.0050;      // 50  microns
1561   Float_t sigmavz=0.010;       // 100 microns
1562
1563   //Vx+=gRandom->Gaus(0,sigmavx);  Vy+=gRandom->Gaus(0,sigmavy);  Vz+=gRandom->Gaus(0,sigmavz);
1564   TVector vertex(3), ervertex(3)
1565   vertex(0)=Vx; vertex(1)=Vy; vertex(2)=Vz;
1566   ervertex(0)=sigmavx;  ervertex(1)=sigmavy;  ervertex(2)=sigmavz;
1567   //////////////////////////////////////////////////////////////////////////
1568 */
1569
1570     TTree tracktree1("TreeT","Tree with ITS tracks");
1571     AliITSiotrack *iotrack=0;
1572     tracktree1.Branch("ITStracks","AliITSiotrack",&iotrack,32000,0);
1573
1574     ofstream out ("AliITSTra.out");
1575     //ofstream outprova ("AliITSprova.out"); //commentato il 26-4-2001
1576
1577     Int_t j;       
1578     for (j=min_t; j<=max_t; j++) {     
1579         track=(AliTPCtrack*)tracks.UncheckedAt(j);
1580         Int_t flaglab=0;
1581         if (!track) continue;
1582         ////// elimination of not good tracks ////////////       
1583         Int_t ilab=TMath::Abs(track->GetLabel());
1584         Int_t iii;
1585         for (iii=0;iii<ngood;iii++) {
1586             //cout<<" ilab, gt[iii].lab = "<<ilab<<" "<<gt[iii].lab<<"\n";getchar();
1587             if (ilab==gt[iii].lab) { 
1588                 flaglab=1;
1589                 ptg=gt[iii].ptg; 
1590                 pxg=gt[iii].pxg;
1591                 pyg=gt[iii].pyg;
1592                 pzg=gt[iii].pzg;        
1593                 break;
1594             } // end if ilab==
1595         } // end for iii
1596         //cout<<" j flaglab =  " <<j<<" "<<flaglab<<"\n";  getchar();
1597         if (!flaglab) continue;  
1598         //cout<<" j =  " <<j<<"\n";  getchar();
1599         /*
1600           ////// old propagation to the end of TPC //////////////       
1601           Double_t xk=76.;
1602           track->PropagateTo(xk);
1603           xk-=0.11;
1604           track->PropagateTo(xk,42.7,2.27); //C
1605           xk-=2.6;
1606           track->PropagateTo(xk,36.2,1.98e-3); //C02
1607           xk-=0.051;
1608           track->PropagateTo(xk,42.7,2.27); //C 
1609           /////////////////////////////////////////////////// 
1610         */ 
1611         ////// new propagation to the end of TPC //////////////
1612         Double_t xk=77.415;
1613         track->PropagateTo(xk, 28.94, 1.204e-3);         //Ne
1614         xk -=0.01;
1615         track->PropagateTo(xk, 44.77, 1.71);     //Tedlar
1616         xk -=0.04;
1617         track->PropagateTo(xk, 44.86, 1.45);     //Kevlar
1618         xk -=2.0;
1619         track->PropagateTo(xk, 41.28, 0.029);    //Nomex         
1620         xk-=16;
1621         track->PropagateTo(xk,36.2,1.98e-3); //C02
1622         xk -=0.01;
1623         track->PropagateTo(xk, 24.01, 2.7);      //Al    
1624         xk -=0.01;
1625         track->PropagateTo(xk, 44.77, 1.71);     //Tedlar
1626         xk -=0.04;
1627         track->PropagateTo(xk, 44.86, 1.45);     //Kevlar
1628         xk -=0.5;
1629         track->PropagateTo(xk, 41.28, 0.029);    //Nomex
1630         ///////////////////////////////////////////////////////////////
1631
1632         ///////////////////////////////////////////////////////////////
1633         AliITStrack trackITS(*track);
1634         AliITStrack result(*track);
1635         AliITStrack primarytrack(*track); 
1636     
1637         /////////////////////////////////////////////////////////////////
1638         TVector Vgeant(3);
1639         Vgeant=result.GetVertex();
1640
1641         // Definition of Dv and Zv for vertex constraint        
1642         Double_t sigmaDv=0.0050;  Double_t sigmaZv=0.010;       
1643         //Double_t sigmaDv=0.0015;  Double_t sigmaZv=0.0015;
1644         Double_t uniform= gRandom->Uniform();
1645         Double_t signdv;
1646         if(uniform<=0.5) signdv=-1.;
1647         else
1648             signdv=1.;
1649
1650         Double_t Vr=TMath::Sqrt(Vgeant(0)*Vgeant(0)+ Vgeant(1)*Vgeant(1));
1651         Double_t Dv=gRandom->Gaus(signdv*Vr,(Float_t)sigmaDv); 
1652         Double_t Zv=gRandom->Gaus(Vgeant(2),(Float_t)sigmaZv);
1653                 
1654         //cout<<" Dv e Zv = "<<Dv<<" "<<Zv<<"\n";
1655         trackITS.SetDv(Dv);  trackITS.SetZv(Zv);
1656         trackITS.SetsigmaDv(sigmaDv); trackITS.SetsigmaZv(sigmaZv); 
1657         result.SetDv(Dv);  result.SetZv(Zv);
1658         result.SetsigmaDv(sigmaDv); result.SetsigmaZv(sigmaZv);
1659         primarytrack.SetDv(Dv);  primarytrack.SetZv(Zv);
1660         primarytrack.SetsigmaDv(sigmaDv); primarytrack.SetsigmaZv(sigmaZv);
1661
1662         ////////////////////////////////////////////////////////////////
1663
1664         primarytrack.PrimaryTrack(rl);
1665         TVector  d2=primarytrack.Getd2();
1666         TVector  tgl2=primarytrack.Gettgl2();
1667         TVector  dtgl=primarytrack.Getdtgl();
1668         trackITS.Setd2(d2); trackITS.Settgl2(tgl2);  trackITS.Setdtgl(dtgl); 
1669         result.Setd2(d2); result.Settgl2(tgl2);  result.Setdtgl(dtgl);     
1670         /*                       
1671            trackITS.SetVertex(vertex); trackITS.SetErrorVertex(ervertex);
1672            result.SetVertex(vertex);   result.SetErrorVertex(ervertex);   
1673         */
1674
1675
1676         Tracking(trackITS,&result,recPoints,vettid, flagvert,rl);
1677         // cout<<" progressive track number = "<<j<<"\r";
1678         // cout<<j<<"\r";
1679         Int_t NumofCluster=result.GetNumClust();  
1680         // cout<<" progressive track number = "<<j<<"\n";    // stampa
1681         Long_t labITS=result.GetLabel();
1682         //  cout << " ITS track label = " << labITS << "\n";    // stampa
1683         int lab=track->GetLabel();                  
1684         // cout << " TPC track label = " << lab <<"\n";      // stampa
1685
1686         //propagation to vertex
1687
1688         Double_t rbeam=3.;
1689
1690         result.Propagation(rbeam);
1691
1692         Double_t C00,C10,C11,C20,C21,C22,C30,C31,C32,C33,C40,C41,C42,C43,C44;
1693         result.GetCElements(C00,C10,C11,C20,C21,C22,C30,C31,C32,C33,C40,C41,C42,C43,C44);
1694         Double_t pt=TMath::Abs(result.GetPt());
1695         Double_t Dr=result.GetD();
1696         Double_t Z=result.GetZ();
1697         Double_t tgl=result.GetTgl();
1698         Double_t C=result.GetC();
1699         Double_t Cy=C/2.;
1700         Double_t Dz=Z-(tgl/Cy)*TMath::ASin(result.arga(rbeam));
1701         Dz-=Vgeant(2);
1702
1703         // cout<<" Dr e dz alla fine = "<<Dr<<" "<<Dz<<"\n"; getchar();
1704         Double_t phi=result.Getphi();
1705         Double_t phivertex = phi - TMath::ASin(result.argA(rbeam));
1706         Double_t duepi=2.*TMath::Pi();   
1707         if(phivertex>duepi) phivertex-=duepi;
1708         if(phivertex<0.) phivertex+=duepi;
1709         Double_t Dtot=TMath::Sqrt(Dr*Dr+Dz*Dz);
1710
1711         /////////////////////////////////////////////////////////////////////
1712
1713         Int_t idmodule,idpoint;
1714         if(NumofCluster >=5)  {            // cinque - sei
1715             //if(NumofCluster ==6)  {            // cinque - sei
1716
1717             AliITSiotrack outtrack;
1718
1719             iotrack=&outtrack;
1720
1721             iotrack->SetStatePhi(phi);
1722             iotrack->SetStateZ(Z);
1723             iotrack->SetStateD(Dr);
1724             iotrack->SetStateTgl(tgl);
1725             iotrack->SetStateC(C);
1726             Double_t radius=result.Getrtrack();
1727             iotrack->SetRadius(radius);
1728             Int_t charge;
1729             if(C>0.) charge=-1;  else charge=1;
1730             iotrack->SetCharge(charge);
1731
1732
1733             iotrack->SetCovMatrix(C00,C10,C11,C20,C21,C22,C30,C31,C32,C33,C40,C41,C42,C43,C44);  
1734
1735             Double_t px=pt*TMath::Cos(phivertex);
1736             Double_t py=pt*TMath::Sin(phivertex);
1737             Double_t pz=pt*tgl;
1738
1739             Double_t xtrack=Dr*TMath::Sin(phivertex);
1740             Double_t ytrack=Dr*TMath::Cos(phivertex);
1741             Double_t ztrack=Dz+Vgeant(2);
1742
1743
1744             iotrack->SetPx(px);
1745             iotrack->SetPy(py);
1746             iotrack->SetPz(pz);
1747             iotrack->SetX(xtrack);
1748             iotrack->SetY(ytrack);
1749             iotrack->SetZ(ztrack);
1750             iotrack->SetLabel(labITS);
1751
1752             Int_t il;           
1753             for(il=0;il<6; il++){
1754                 iotrack->SetIdPoint(il,result.GetIdPoint(il));
1755                 iotrack->SetIdModule(il,result.GetIdModule(il));
1756             } // end for il
1757             tracktree1.Fill();
1758
1759             //cout<<" labITS = "<<labITS<<"\n";
1760             //cout<<" phi z Dr tgl C = "<<phi<<" "<<Z<<" "<<Dr<<" "<<tgl<<" "<<C<<"\n";  getchar();        
1761
1762             DataOut(kkk) = ptg; kkk++; DataOut(kkk)=labITS; kkk++; DataOut(kkk)=lab; kkk++;             
1763
1764             for (il=0;il<6;il++) {
1765                 idpoint=result.GetIdPoint(il);
1766                 idmodule=result.GetIdModule(il);
1767                 *(vettid[idmodule]+idpoint)=1; 
1768                 iotrack->SetIdPoint(il,idpoint);
1769                 iotrack->SetIdModule(il,idmodule);
1770             } // end for il
1771
1772             //  cout<<"  +++++++++++++  pt e ptg = "<<pt<<" "<<ptg<<"  ++++++++++\n";
1773             /* ///  provvisorio il 23-5-2001
1774                Double_t pg=TMath::Sqrt(pxg*pxg+pyg*pyg+pzg*pzg);
1775                Double_t cosl=TMath::Sqrt(1./(1.+tgl*tgl));
1776                Double_t ptot=pt/cosl;
1777                cout<<"ptot e pg = "<<ptot<<" "<<pg<<"\n";
1778                Double_t difpt= (ptot-pg)/pg*100.;
1779             */
1780             ///////////////////////////////
1781             Double_t difpt= (pt-ptg)/ptg*100.;  //coomentato prov il 23-5-2001
1782             DataOut(kkk)=difpt; kkk++;
1783             Double_t lambdag=TMath::ATan(pzg/ptg);
1784             Double_t   lam=TMath::ATan(tgl);      
1785             Double_t diflam = (lam - lambdag)*1000.;
1786             DataOut(kkk) = diflam; kkk++;
1787             Double_t phig=TMath::ATan2(pyg,pxg);
1788             if(phig<0) phig=2.*TMath::Pi()+phig;       
1789             Double_t phi=phivertex;
1790
1791             Double_t difphi = (phi - phig)*1000.;
1792             DataOut(kkk)=difphi; kkk++;
1793             DataOut(kkk)=Dtot*1.e4; kkk++;
1794             DataOut(kkk)=Dr*1.e4; kkk++;
1795             DataOut(kkk)=Dz*1.e4; kkk++; 
1796             Int_t r;
1797             for (r=0; r<9; r++) { out<<DataOut(r)<<" ";}
1798             out<<"\n";
1799             kkk=0;
1800
1801         } // end if on NumofCluster
1802         //gObjectTable->Print();    // stampa memoria     
1803     }  //  end for (int j=min_t; j<=max_t; j++)
1804
1805     out.close();  
1806     //outprova.close();   
1807  
1808     static Bool_t first=kTRUE;
1809     static TFile *tfile;
1810
1811     if(first) {
1812         tfile=new TFile("itstracks.root","RECREATE");
1813         //cout<<"I have opened itstracks.root file "<<endl;
1814     } // end if first       
1815     first=kFALSE;
1816     tfile->cd();
1817     tfile->ls();
1818
1819     char hname[30];
1820     sprintf(hname,"TreeT%d",evNumber);
1821
1822     tracktree1.Write(hname);
1823
1824
1825   
1826     TTree *fAli=gAlice->TreeK();
1827     TFile *fileAli=0;
1828             
1829     if (fAli) fileAli =fAli->GetCurrentFile();
1830     fileAli->cd();
1831      
1832     ///////////////////////////////////////////////////////////////////
1833
1834     printf("delete vectors\n");
1835     if(np) delete [] np;
1836     if(vettid) delete [] vettid;
1837 }