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