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