]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPC.cxx
Default Branch split level set to 99
[u/mrichter/AliRoot.git] / TPC / AliTPC.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.39  2001/07/26 09:09:34  kowal2
19 Hits2Reco method added
20
21 Revision 1.38  2001/07/20 14:32:43  kowal2
22 Processing of many events possible now
23
24 Revision 1.37  2001/06/12 07:17:18  kowal2
25 Hits2SDigits method implemented (summable digits)
26
27 Revision 1.36  2001/05/16 14:57:25  alibrary
28 New files for folders and Stack
29
30 Revision 1.35  2001/05/08 16:02:22  kowal2
31 Updated material specifications
32
33 Revision 1.34  2001/05/08 15:00:15  hristov
34 Corrections for tracking in arbitrary magnenetic field. Changes towards a concept of global Alice track. Back propagation of reconstructed tracks (Yu.Belikov)
35
36 Revision 1.33  2001/04/03 12:40:43  kowal2
37 Removed printouts
38
39 Revision 1.32  2001/03/12 17:47:36  hristov
40 Changes needed on Sun with CC 5.0
41
42 Revision 1.31  2001/03/12 08:21:50  kowal2
43 Corrected C++ bug in the material definitions
44
45 Revision 1.30  2001/03/01 17:34:47  kowal2
46 Correction due to the accuracy problem
47
48 Revision 1.29  2001/02/28 16:34:40  kowal2
49 Protection against nonphysical values of the avalanche size,
50 10**6 is the maximum
51
52 Revision 1.28  2001/01/26 19:57:19  hristov
53 Major upgrade of AliRoot code
54
55 Revision 1.27  2001/01/13 17:29:33  kowal2
56 Sun compiler correction
57
58 Revision 1.26  2001/01/10 07:59:43  kowal2
59 Corrections to load points from the noncompressed hits.
60
61 Revision 1.25  2000/11/02 07:25:31  kowal2
62 Changes due to the new hit structure.
63 Memory leak removed.
64
65 Revision 1.24  2000/10/05 16:06:09  kowal2
66 Forward declarations. Changes due to a new class AliComplexCluster.
67
68 Revision 1.23  2000/10/02 21:28:18  fca
69 Removal of useless dependecies via forward declarations
70
71 Revision 1.22  2000/07/10 20:57:39  hristov
72 Update of TPC code and macros by M.Kowalski
73
74 Revision 1.19.2.4  2000/06/26 07:39:42  kowal2
75 Changes to obey the coding rules
76
77 Revision 1.19.2.3  2000/06/25 08:38:41  kowal2
78 Splitted from AliTPCtracking
79
80 Revision 1.19.2.2  2000/06/16 12:59:28  kowal2
81 Changed parameter settings
82
83 Revision 1.19.2.1  2000/06/09 07:15:07  kowal2
84
85 Defaults loaded automatically (hard-wired)
86 Optional parameters can be set via macro called in the constructor
87
88 Revision 1.19  2000/04/18 19:00:59  fca
89 Small bug fixes to TPC files
90
91 Revision 1.18  2000/04/17 09:37:33  kowal2
92 removed obsolete AliTPCDigitsDisplay.C
93
94 Revision 1.17.2.2  2000/04/10 08:15:12  kowal2
95
96 New, experimental data structure from M. Ivanov
97 New tracking algorithm
98 Different pad geometry for different sectors
99 Digitization rewritten
100
101 Revision 1.17.2.1  2000/04/10 07:56:53  kowal2
102 Not used anymore - removed
103
104 Revision 1.17  2000/01/19 17:17:30  fca
105 Introducing a list of lists of hits -- more hits allowed for detector now
106
107 Revision 1.16  1999/11/05 09:29:23  fca
108 Accept only signals > 0
109
110 Revision 1.15  1999/10/08 06:26:53  fca
111 Removed ClustersIndex - not used anymore
112
113 Revision 1.14  1999/09/29 09:24:33  fca
114 Introduction of the Copyright and cvs Log
115
116 */
117
118 ///////////////////////////////////////////////////////////////////////////////
119 //                                                                           //
120 //  Time Projection Chamber                                                  //
121 //  This class contains the basic functions for the Time Projection Chamber  //
122 //  detector. Functions specific to one particular geometry are              //
123 //  contained in the derived classes                                         //
124 //                                                                           //
125 //Begin_Html
126 /*
127 <img src="picts/AliTPCClass.gif">
128 */
129 //End_Html
130 //                                                                           //
131 //                                                                          //
132 ///////////////////////////////////////////////////////////////////////////////
133
134 //
135
136 #include <TMath.h>
137 #include <TRandom.h>
138 #include <TVector.h>
139 #include <TMatrix.h>
140 #include <TGeometry.h>
141 #include <TNode.h>
142 #include <TTUBS.h>
143 #include <TObjectTable.h>
144 #include "TParticle.h"
145 #include "AliTPC.h"
146 #include <TFile.h>  
147 #include <TROOT.h>
148 #include <TSystem.h>     
149 #include "AliRun.h"
150 #include <iostream.h>
151 #include <stdlib.h>
152 #include <fstream.h>
153 #include "AliMC.h"
154 #include "AliMagF.h"
155
156
157 #include "AliTPCParamSR.h"
158 #include "AliTPCPRF2D.h"
159 #include "AliTPCRF1D.h"
160 #include "AliDigits.h"
161 #include "AliSimDigits.h"
162 #include "AliTPCTrackHits.h"
163 #include "AliPoints.h"
164 #include "AliArrayBranch.h"
165
166
167 #include "AliTPCDigitsArray.h"
168 #include "AliComplexCluster.h"
169 #include "AliClusters.h"
170 #include "AliTPCClustersRow.h"
171 #include "AliTPCClustersArray.h"
172
173 #include "AliTPCcluster.h"
174 #include "AliTPCclusterer.h"
175 #include "AliTPCtracker.h"
176
177 #include <TInterpreter.h>
178 #include <TTree.h>
179
180
181
182 ClassImp(AliTPC) 
183
184 //_____________________________________________________________________________
185 AliTPC::AliTPC()
186 {
187   //
188   // Default constructor
189   //
190   fIshunt   = 0;
191   fHits     = 0;
192   fDigits   = 0;
193   fNsectors = 0;
194   //MI changes
195   fDigitsArray = 0;
196   fClustersArray = 0;
197   fDefaults = 0;
198   fTrackHits = 0;  
199   fHitType = 2;  
200   fTPCParam = 0; 
201 }
202  
203 //_____________________________________________________________________________
204 AliTPC::AliTPC(const char *name, const char *title)
205       : AliDetector(name,title)
206 {
207   //
208   // Standard constructor
209   //
210
211   //
212   // Initialise arrays of hits and digits 
213   fHits     = new TClonesArray("AliTPChit",  176);
214   gAlice->AddHitList(fHits);
215   //MI change  
216   fDigitsArray = 0;
217   fClustersArray= 0;
218   fDefaults = 0;
219   //
220   fTrackHits = new AliTPCTrackHits;  //MI - 13.09.2000
221   fTrackHits->SetHitPrecision(0.002);
222   fTrackHits->SetStepPrecision(0.003);  
223   fTrackHits->SetMaxDistance(100); 
224
225   fHitType = 2;
226   //
227   // Initialise counters
228   fNsectors = 0;
229
230   //
231   fIshunt     =  0;
232   //
233   // Initialise color attributes
234   SetMarkerColor(kYellow);
235
236   //
237   //  Set TPC parameters
238   //
239
240
241   if (!strcmp(title,"Default")) {       
242     fTPCParam = new AliTPCParamSR;
243   } else {
244     cerr<<"AliTPC warning: in Config.C you must set non-default parameters\n";
245     fTPCParam=0;
246   }
247
248 }
249
250 //_____________________________________________________________________________
251 AliTPC::~AliTPC()
252 {
253   //
254   // TPC destructor
255   //
256
257   fIshunt   = 0;
258   delete fHits;
259   delete fDigits;
260   delete fTPCParam;
261   delete fTrackHits; //MI 15.09.2000
262 }
263
264 //_____________________________________________________________________________
265 void AliTPC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
266 {
267   //
268   // Add a hit to the list
269   //
270   //  TClonesArray &lhits = *fHits;
271   //  new(lhits[fNhits++]) AliTPChit(fIshunt,track,vol,hits);
272   if (fHitType&1){
273     TClonesArray &lhits = *fHits;
274     new(lhits[fNhits++]) AliTPChit(fIshunt,track,vol,hits);
275   }
276   if (fHitType&2)
277    AddHit2(track,vol,hits);
278 }
279  
280 //_____________________________________________________________________________
281 void AliTPC::BuildGeometry()
282 {
283
284   //
285   // Build TPC ROOT TNode geometry for the event display
286   //
287   TNode *nNode, *nTop;
288   TTUBS *tubs;
289   Int_t i;
290   const int kColorTPC=19;
291   char name[5], title[25];
292   const Double_t kDegrad=TMath::Pi()/180;
293   const Double_t kRaddeg=180./TMath::Pi();
294
295
296   Float_t innerOpenAngle = fTPCParam->GetInnerAngle();
297   Float_t outerOpenAngle = fTPCParam->GetOuterAngle();
298
299   Float_t innerAngleShift = fTPCParam->GetInnerAngleShift();
300   Float_t outerAngleShift = fTPCParam->GetOuterAngleShift();
301
302   Int_t nLo = fTPCParam->GetNInnerSector()/2;
303   Int_t nHi = fTPCParam->GetNOuterSector()/2;  
304
305   const Double_t kloAng = (Double_t)TMath::Nint(innerOpenAngle*kRaddeg);
306   const Double_t khiAng = (Double_t)TMath::Nint(outerOpenAngle*kRaddeg);
307   const Double_t kloAngSh = (Double_t)TMath::Nint(innerAngleShift*kRaddeg);
308   const Double_t khiAngSh = (Double_t)TMath::Nint(outerAngleShift*kRaddeg);  
309
310
311   const Double_t kloCorr = 1/TMath::Cos(0.5*kloAng*kDegrad);
312   const Double_t khiCorr = 1/TMath::Cos(0.5*khiAng*kDegrad);
313
314   Double_t rl,ru;
315   
316
317   //
318   // Get ALICE top node
319   //
320
321   nTop=gAlice->GetGeometry()->GetNode("alice");
322
323   //  inner sectors
324
325   rl = fTPCParam->GetInnerRadiusLow();
326   ru = fTPCParam->GetInnerRadiusUp();
327  
328
329   for(i=0;i<nLo;i++) {
330     sprintf(name,"LS%2.2d",i);
331     name[4]='\0';
332     sprintf(title,"TPC low sector %3d",i);
333     title[24]='\0';
334     
335     tubs = new TTUBS(name,title,"void",rl*kloCorr,ru*kloCorr,250.,
336                      kloAng*(i-0.5)+kloAngSh,kloAng*(i+0.5)+kloAngSh);
337     tubs->SetNumberOfDivisions(1);
338     nTop->cd();
339     nNode = new TNode(name,title,name,0,0,0,"");
340     nNode->SetLineColor(kColorTPC);
341     fNodes->Add(nNode);
342   }
343
344   // Outer sectors
345
346   rl = fTPCParam->GetOuterRadiusLow();
347   ru = fTPCParam->GetOuterRadiusUp();
348
349   for(i=0;i<nHi;i++) {
350     sprintf(name,"US%2.2d",i);
351     name[4]='\0';
352     sprintf(title,"TPC upper sector %d",i);
353     title[24]='\0';
354     tubs = new TTUBS(name,title,"void",rl*khiCorr,ru*khiCorr,250,
355                      khiAng*(i-0.5)+khiAngSh,khiAng*(i+0.5)+khiAngSh);
356     tubs->SetNumberOfDivisions(1);
357     nTop->cd();
358     nNode = new TNode(name,title,name,0,0,0,"");
359     nNode->SetLineColor(kColorTPC);
360     fNodes->Add(nNode);
361   }
362
363 }    
364
365 //_____________________________________________________________________________
366 Int_t AliTPC::DistancetoPrimitive(Int_t , Int_t )
367 {
368   //
369   // Calculate distance from TPC to mouse on the display
370   // Dummy procedure
371   //
372   return 9999;
373 }
374
375 void AliTPC::Clusters2Tracks(TFile *of) {
376   //-----------------------------------------------------------------
377   // This is a track finder.
378   //-----------------------------------------------------------------
379   AliTPCtracker tracker(fTPCParam);
380   tracker.Clusters2Tracks(gFile,of);
381 }
382
383 //_____________________________________________________________________________
384 void AliTPC::CreateMaterials()
385 {
386   //-----------------------------------------------
387   // Create Materials for for TPC simulations
388   //-----------------------------------------------
389
390   //-----------------------------------------------------------------
391   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
392   //-----------------------------------------------------------------
393
394   Int_t iSXFLD=gAlice->Field()->Integ();
395   Float_t sXMGMX=gAlice->Field()->Max();
396
397   Float_t amat[5]; // atomic numbers
398   Float_t zmat[5]; // z
399   Float_t wmat[5]; // proportions
400
401   Float_t density;
402   Float_t apure[2];
403
404
405   //***************** Gases *************************
406   
407   //-------------------------------------------------
408   // pure gases
409   //-------------------------------------------------
410
411   // Neon
412
413
414   amat[0]= 20.18;
415   zmat[0]= 10.;  
416   density = 0.0009;
417  
418   apure[0]=amat[0];
419
420   AliMaterial(20,"Ne",amat[0],zmat[0],density,999.,999.);
421
422   // Argon
423
424   amat[0]= 39.948;
425   zmat[0]= 18.;  
426   density = 0.001782;  
427
428   apure[1]=amat[0];
429
430   AliMaterial(21,"Ar",amat[0],zmat[0],density,999.,999.);
431  
432
433   //--------------------------------------------------------------
434   // gases - compounds
435   //--------------------------------------------------------------
436
437   Float_t amol[3];
438
439   // CO2
440
441   amat[0]=12.011;
442   amat[1]=15.9994;
443
444   zmat[0]=6.;
445   zmat[1]=8.;
446
447   wmat[0]=1.;
448   wmat[1]=2.;
449
450   density=0.001977;
451
452   amol[0] = amat[0]*wmat[0]+amat[1]*wmat[1];
453
454   AliMixture(10,"CO2",amat,zmat,density,-2,wmat);
455   
456   // CF4
457
458   amat[0]=12.011;
459   amat[1]=18.998;
460
461   zmat[0]=6.;
462   zmat[1]=9.;
463  
464   wmat[0]=1.;
465   wmat[1]=4.;
466  
467   density=0.003034;
468
469   amol[1] = amat[0]*wmat[0]+amat[1]*wmat[1];
470
471   AliMixture(11,"CF4",amat,zmat,density,-2,wmat); 
472
473
474   // CH4
475
476   amat[0]=12.011;
477   amat[1]=1.;
478
479   zmat[0]=6.;
480   zmat[1]=1.;
481
482   wmat[0]=1.;
483   wmat[1]=4.;
484
485   density=0.000717;
486
487   amol[2] = amat[0]*wmat[0]+amat[1]*wmat[1];
488
489   AliMixture(12,"CH4",amat,zmat,density,-2,wmat);
490
491   //----------------------------------------------------------------
492   // gases - mixtures, ID >= 20 pure gases, <= 10 ID < 20 -compounds
493   //----------------------------------------------------------------
494
495   char namate[21]; 
496   density = 0.;
497   Float_t am=0;
498   Int_t nc;
499   Float_t rho,absl,X0,buf[1];
500   Int_t nbuf;
501   Float_t a,z;
502
503   for(nc = 0;nc<fNoComp;nc++)
504     {
505     
506       // retrive material constants
507       
508       gMC->Gfmate((*fIdmate)[fMixtComp[nc]],namate,a,z,rho,X0,absl,buf,nbuf);
509
510       amat[nc] = a;
511       zmat[nc] = z;
512
513       Int_t nnc = (fMixtComp[nc]>=20) ? fMixtComp[nc]%20 : fMixtComp[nc]%10;
514  
515       am += fMixtProp[nc]*((fMixtComp[nc]>=20) ? apure[nnc] : amol[nnc]); 
516       density += fMixtProp[nc]*rho;  // density of the mixture
517       
518     }
519
520   // mixture proportions by weight!
521
522   for(nc = 0;nc<fNoComp;nc++)
523     {
524
525       Int_t nnc = (fMixtComp[nc]>=20) ? fMixtComp[nc]%20 : fMixtComp[nc]%10;
526
527       wmat[nc] = fMixtProp[nc]*((fMixtComp[nc]>=20) ? 
528                  apure[nnc] : amol[nnc])/am;
529
530     } 
531
532   // Drift gases 1 - nonsensitive, 2 - sensitive
533
534   AliMixture(31,"Drift gas 1",amat,zmat,density,fNoComp,wmat);
535   AliMixture(32,"Drift gas 2",amat,zmat,density,fNoComp,wmat);
536
537
538   // Air
539
540   amat[0] = 14.61;
541   zmat[0] = 7.3;
542   density = 0.001205;
543
544   AliMaterial(24,"Air",amat[0],zmat[0],density,999.,999.); 
545
546
547   //----------------------------------------------------------------------
548   //               solid materials
549   //----------------------------------------------------------------------
550
551
552   // Kevlar C14H22O2N2
553
554   amat[0] = 12.011;
555   amat[1] = 1.;
556   amat[2] = 15.999;
557   amat[3] = 14.006;
558
559   zmat[0] = 6.;
560   zmat[1] = 1.;
561   zmat[2] = 8.;
562   zmat[3] = 7.;
563
564   wmat[0] = 14.;
565   wmat[1] = 22.;
566   wmat[2] = 2.;
567   wmat[3] = 2.;
568
569   density = 1.45;
570
571   AliMixture(34,"Kevlar",amat,zmat,density,-4,wmat);  
572
573   // NOMEX
574
575   amat[0] = 12.011;
576   amat[1] = 1.;
577   amat[2] = 15.999;
578   amat[3] = 14.006;
579
580   zmat[0] = 6.;
581   zmat[1] = 1.;
582   zmat[2] = 8.;
583   zmat[3] = 7.;
584
585   wmat[0] = 14.;
586   wmat[1] = 22.;
587   wmat[2] = 2.;
588   wmat[3] = 2.;
589
590   density = 0.03;
591
592   
593   AliMixture(35,"NOMEX",amat,zmat,density,-4,wmat);
594
595   // Makrolon C16H18O3
596
597   amat[0] = 12.011;
598   amat[1] = 1.;
599   amat[2] = 15.999;
600
601   zmat[0] = 6.;
602   zmat[1] = 1.;
603   zmat[2] = 8.;
604
605   wmat[0] = 16.;
606   wmat[1] = 18.;
607   wmat[2] = 3.;
608   
609   density = 1.2;
610
611   AliMixture(36,"Makrolon",amat,zmat,density,-3,wmat);
612   
613   // Mylar C5H4O2
614
615   amat[0]=12.011;
616   amat[1]=1.;
617   amat[2]=15.9994;
618
619   zmat[0]=6.;
620   zmat[1]=1.;
621   zmat[2]=8.;
622
623   wmat[0]=5.;
624   wmat[1]=4.;
625   wmat[2]=2.; 
626
627   density = 1.39;
628   
629   AliMixture(37, "Mylar",amat,zmat,density,-3,wmat); 
630
631   // SiO2 - used later for the glass fiber
632
633   amat[0]=28.086;
634   amat[1]=15.9994;
635
636   zmat[0]=14.;
637   zmat[1]=8.;
638
639   wmat[0]=1.;
640   wmat[1]=2.;
641
642
643   AliMixture(38,"SiO2",amat,zmat,2.2,-2,wmat); //SiO2 - quartz (rho=2.2)
644
645   // Al
646
647   amat[0] = 26.98;
648   zmat[0] = 13.;
649
650   density = 2.7;
651
652   AliMaterial(40,"Al",amat[0],zmat[0],density,999.,999.);
653
654   // Si
655
656   amat[0] = 28.086;
657   zmat[0] = 14.;
658
659   density = 2.33;
660
661   AliMaterial(41,"Si",amat[0],zmat[0],density,999.,999.);
662
663   // Cu
664
665   amat[0] = 63.546;
666   zmat[0] = 29.;
667
668   density = 8.96;
669
670   AliMaterial(42,"Cu",amat[0],zmat[0],density,999.,999.);
671
672   // Tedlar C2H3F
673
674   amat[0] = 12.011;
675   amat[1] = 1.;
676   amat[2] = 18.998;
677
678   zmat[0] = 6.;
679   zmat[1] = 1.;
680   zmat[2] = 9.;
681
682   wmat[0] = 2.;
683   wmat[1] = 3.; 
684   wmat[2] = 1.;
685
686   density = 1.71;
687
688   AliMixture(43, "Tedlar",amat,zmat,density,-3,wmat);  
689
690
691   // Plexiglas  C5H8O2
692
693   amat[0]=12.011;
694   amat[1]=1.;
695   amat[2]=15.9994;
696
697   zmat[0]=6.;
698   zmat[1]=1.;
699   zmat[2]=8.;
700
701   wmat[0]=5.;
702   wmat[1]=8.;
703   wmat[2]=2.;
704
705   density=1.18;
706
707   AliMixture(44,"Plexiglas",amat,zmat,density,-3,wmat);
708
709   // Epoxy - C14 H20 O3
710
711   
712   amat[0]=12.011;
713   amat[1]=1.;
714   amat[2]=15.9994;
715
716   zmat[0]=6.;
717   zmat[1]=1.;
718   zmat[2]=8.;
719
720   wmat[0]=14.;
721   wmat[1]=20.;
722   wmat[2]=3.;
723
724   density=1.25;
725
726   AliMixture(45,"Epoxy",amat,zmat,density,-3,wmat);
727
728   // Carbon
729
730   amat[0]=12.011;
731   zmat[0]=6.;
732   density= 2.265;
733
734   AliMaterial(46,"C",amat[0],zmat[0],density,999.,999.);
735
736   // get epoxy
737
738   gMC->Gfmate((*fIdmate)[45],namate,amat[1],zmat[1],rho,X0,absl,buf,nbuf);
739
740   // Carbon fiber
741
742   wmat[0]=0.644; // by weight!
743   wmat[1]=0.356;
744
745   density=0.5*(1.25+2.265);
746
747   AliMixture(47,"Cfiber",amat,zmat,density,2,wmat);
748
749   // get SiO2
750
751   gMC->Gfmate((*fIdmate)[38],namate,amat[0],zmat[0],rho,X0,absl,buf,nbuf); 
752
753   wmat[0]=0.725; // by weight!
754   wmat[1]=0.275;
755
756   density=1.7;
757
758   AliMixture(39,"G10",amat,zmat,density,2,wmat);
759
760  
761
762
763   //----------------------------------------------------------
764   // tracking media for gases
765   //----------------------------------------------------------
766
767   AliMedium(0, "Air", 24, 0, iSXFLD, sXMGMX, 10., 999., .1, .01, .1);
768   AliMedium(1, "Drift gas 1", 31, 0, iSXFLD, sXMGMX, 10., 999.,.1,.001, .001);
769   AliMedium(2, "Drift gas 2", 32, 1, iSXFLD, sXMGMX, 10., 999.,.1,.001, .001);
770   AliMedium(3,"CO2",10,0, iSXFLD, sXMGMX, 10., 999.,.1, .001, .001); 
771
772   //-----------------------------------------------------------  
773   // tracking media for solids
774   //-----------------------------------------------------------
775   
776   AliMedium(4,"Al",40,0, iSXFLD, sXMGMX, 10., 999., .1, .0005, .001);
777   AliMedium(5,"Kevlar",34,0, iSXFLD, sXMGMX, 10., 999., .1, .0005, .001);
778   AliMedium(6,"Nomex",35,0, iSXFLD, sXMGMX, 10., 999., .1, .001, .001);
779   AliMedium(7,"Makrolon",36,0, iSXFLD, sXMGMX, 10., 999., .1, .001, .001);
780   AliMedium(8,"Mylar",37,0, iSXFLD, sXMGMX, 10., 999., .1, .0005, .001);
781   AliMedium(9,"Tedlar",43,0, iSXFLD, sXMGMX, 10., 999., .1, .0005, .001);
782   AliMedium(10,"Cu",42,0, iSXFLD, sXMGMX, 10., 999., .1, .001, .001);
783   AliMedium(11,"Si",41,0, iSXFLD, sXMGMX, 10., 999., .1, .001, .001);
784   AliMedium(12,"G10",39,0, iSXFLD, sXMGMX, 10., 999., .1, .001, .001);
785   AliMedium(13,"Plexiglas",44,0, iSXFLD, sXMGMX, 10., 999., .1, .001, .001);
786   AliMedium(14,"Epoxy",45,0, iSXFLD, sXMGMX, 10., 999., .1, .0005, .001);
787   AliMedium(15,"Cfiber",47,0, iSXFLD, sXMGMX, 10., 999., .1, .001, .001);
788     
789 }
790
791
792 void AliTPC::Digits2Clusters(TFile *of, Int_t eventnumber)
793 {
794   //-----------------------------------------------------------------
795   // This is a simple cluster finder.
796   //-----------------------------------------------------------------
797   AliTPCclusterer::Digits2Clusters(fTPCParam,of,eventnumber);
798 }
799
800 extern Double_t SigmaY2(Double_t, Double_t, Double_t);
801 extern Double_t SigmaZ2(Double_t, Double_t);
802 //_____________________________________________________________________________
803 void AliTPC::Hits2Clusters(TFile *of, Int_t eventn)
804 {
805   //--------------------------------------------------------
806   // TPC simple cluster generator from hits
807   // obtained from the TPC Fast Simulator
808   // The point errors are taken from the parametrization
809   //--------------------------------------------------------
810
811   //-----------------------------------------------------------------
812   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
813   //-----------------------------------------------------------------
814   // Adopted to Marian's cluster data structure by I.Belikov, CERN,
815   // Jouri.Belikov@cern.ch
816   //----------------------------------------------------------------
817   
818   /////////////////////////////////////////////////////////////////////////////
819   //
820   //---------------------------------------------------------------------
821   //   ALICE TPC Cluster Parameters
822   //--------------------------------------------------------------------
823        
824   
825
826   // Cluster width in rphi
827   const Float_t kACrphi=0.18322;
828   const Float_t kBCrphi=0.59551e-3;
829   const Float_t kCCrphi=0.60952e-1;
830   // Cluster width in z
831   const Float_t kACz=0.19081;
832   const Float_t kBCz=0.55938e-3;
833   const Float_t kCCz=0.30428;
834
835   TDirectory *savedir=gDirectory; 
836
837   if (!of->IsOpen()) {
838      cerr<<"AliTPC::Hits2Clusters(): output file not open !\n";
839      return;
840   }
841
842   //if(fDefaults == 0) SetDefaults();
843
844   Float_t sigmaRphi,sigmaZ,clRphi,clZ;
845   //
846   TParticle *particle; // pointer to a given particle
847   AliTPChit *tpcHit; // pointer to a sigle TPC hit
848   Int_t sector;
849   Int_t ipart;
850   Float_t xyz[5];
851   Float_t pl,pt,tanth,rpad,ratio;
852   Float_t cph,sph;
853   
854   //---------------------------------------------------------------
855   //  Get the access to the tracks 
856   //---------------------------------------------------------------
857   
858   TTree *tH = gAlice->TreeH();
859   Stat_t ntracks = tH->GetEntries();
860
861   //Switch to the output file
862   of->cd();
863
864   char   cname[100];
865
866   sprintf(cname,"TreeC_TPC_%d",eventn);
867
868   fTPCParam->Write(fTPCParam->GetTitle());
869   AliTPCClustersArray carray;
870   carray.Setup(fTPCParam);
871   carray.SetClusterType("AliTPCcluster");
872   carray.MakeTree();
873
874   Int_t nclusters=0; //cluster counter
875   
876   //------------------------------------------------------------
877   // Loop over all sectors (72 sectors for 20 deg
878   // segmentation for both lower and upper sectors)
879   // Sectors 0-35 are lower sectors, 0-17 z>0, 17-35 z<0
880   // Sectors 36-71 are upper sectors, 36-53 z>0, 54-71 z<0
881   //
882   // First cluster for sector 0 starts at "0"
883   //------------------------------------------------------------
884    
885   for(Int_t isec=0;isec<fTPCParam->GetNSector();isec++){
886     //MI change
887     fTPCParam->AdjustCosSin(isec,cph,sph);
888     
889     //------------------------------------------------------------
890     // Loop over tracks
891     //------------------------------------------------------------
892     
893     for(Int_t track=0;track<ntracks;track++){
894       ResetHits();
895       tH->GetEvent(track);
896       //
897       //  Get number of the TPC hits
898       //
899       // nhits=fHits->GetEntriesFast();
900       //
901      
902        tpcHit = (AliTPChit*)FirstHit(-1);
903
904       // Loop over hits
905       //
906        //   for(Int_t hit=0;hit<nhits;hit++){
907        //tpcHit=(AliTPChit*)fHits->UncheckedAt(hit);
908
909        while(tpcHit){
910  
911          if (tpcHit->fQ == 0.) {
912            tpcHit = (AliTPChit*) NextHit();
913            continue; //information about track (I.Belikov)
914          }
915         sector=tpcHit->fSector; // sector number
916
917
918         //      if(sector != isec) continue; //terminate iteration
919
920        if(sector != isec){
921          tpcHit = (AliTPChit*) NextHit();
922          continue; 
923        }
924         ipart=tpcHit->Track();
925         particle=gAlice->Particle(ipart);
926         pl=particle->Pz();
927         pt=particle->Pt();
928         if(pt < 1.e-9) pt=1.e-9;
929         tanth=pl/pt;
930         tanth = TMath::Abs(tanth);
931         rpad=TMath::Sqrt(tpcHit->X()*tpcHit->X() + tpcHit->Y()*tpcHit->Y());
932         ratio=0.001*rpad/pt; // pt must be in MeV/c - historical reason
933
934         //   space-point resolutions
935         
936         sigmaRphi=SigmaY2(rpad,tanth,pt);
937         sigmaZ   =SigmaZ2(rpad,tanth   );
938         
939         //   cluster widths
940         
941         clRphi=kACrphi-kBCrphi*rpad*tanth+kCCrphi*ratio*ratio;
942         clZ=kACz-kBCz*rpad*tanth+kCCz*tanth*tanth;
943         
944         // temporary protection
945         
946         if(sigmaRphi < 0.) sigmaRphi=0.4e-3;
947         if(sigmaZ < 0.) sigmaZ=0.4e-3;
948         if(clRphi < 0.) clRphi=2.5e-3;
949         if(clZ < 0.) clZ=2.5e-5;
950         
951         //
952         
953         //
954         // smearing --> rotate to the 1 (13) or to the 25 (49) sector,
955         // then the inaccuracy in a X-Y plane is only along Y (pad row)!
956         //
957         Float_t xprim= tpcHit->X()*cph + tpcHit->Y()*sph;
958         Float_t yprim=-tpcHit->X()*sph + tpcHit->Y()*cph;
959         xyz[0]=gRandom->Gaus(yprim,TMath::Sqrt(sigmaRphi));   // y
960           Float_t alpha=(isec < fTPCParam->GetNInnerSector()) ?
961           fTPCParam->GetInnerAngle() : fTPCParam->GetOuterAngle();
962           Float_t ymax=xprim*TMath::Tan(0.5*alpha);
963           if (TMath::Abs(xyz[0])>ymax) xyz[0]=yprim; 
964         xyz[1]=gRandom->Gaus(tpcHit->Z(),TMath::Sqrt(sigmaZ)); // z
965           if (TMath::Abs(xyz[1])>fTPCParam->GetZLength()) xyz[1]=tpcHit->Z(); 
966         xyz[2]=sigmaRphi;                                     // fSigmaY2
967         xyz[3]=sigmaZ;                                        // fSigmaZ2
968         xyz[4]=tpcHit->fQ;                                    // q
969
970         AliTPCClustersRow *clrow=carray.GetRow(sector,tpcHit->fPadRow);
971         if (!clrow) clrow=carray.CreateRow(sector,tpcHit->fPadRow);     
972
973         Int_t tracks[3]={tpcHit->Track(), -1, -1};
974         AliTPCcluster cluster(tracks,xyz);
975
976         clrow->InsertCluster(&cluster); nclusters++;
977
978         tpcHit = (AliTPChit*)NextHit();
979         
980
981       } // end of loop over hits
982
983     }   // end of loop over tracks
984
985     Int_t nrows=fTPCParam->GetNRow(isec);
986     for (Int_t irow=0; irow<nrows; irow++) {
987         AliTPCClustersRow *clrow=carray.GetRow(isec,irow);
988         if (!clrow) continue;
989         carray.StoreRow(isec,irow);
990         carray.ClearRow(isec,irow);
991     }
992
993   } // end of loop over sectors  
994
995   cerr<<"Number of made clusters : "<<nclusters<<"                        \n";
996
997   carray.GetTree()->Write(cname);
998
999   savedir->cd(); //switch back to the input file
1000   
1001 } // end of function
1002
1003 //_________________________________________________________________
1004 void AliTPC::Hits2ExactClustersSector(Int_t isec)
1005 {
1006   //--------------------------------------------------------
1007   //calculate exact cross point of track and given pad row
1008   //resulting values are expressed in "digit" coordinata
1009   //--------------------------------------------------------
1010
1011   //-----------------------------------------------------------------
1012   // Origin: Marian Ivanov  GSI Darmstadt, m.ivanov@gsi.de
1013   //-----------------------------------------------------------------
1014   //
1015   if (fClustersArray==0){    
1016     return;
1017   }
1018   //
1019   TParticle *particle; // pointer to a given particle
1020   AliTPChit *tpcHit; // pointer to a sigle TPC hit
1021   Int_t sector,nhits;
1022   Int_t ipart;
1023   const Int_t kcmaxhits=30000;
1024   TVector * xxxx = new TVector(kcmaxhits*4);
1025   TVector & xxx = *xxxx;
1026   Int_t maxhits = kcmaxhits;
1027   //construct array for each padrow
1028   for (Int_t i=0; i<fTPCParam->GetNRow(isec);i++) 
1029     fClustersArray->CreateRow(isec,i);
1030   
1031   //---------------------------------------------------------------
1032   //  Get the access to the tracks 
1033   //---------------------------------------------------------------
1034   
1035   TTree *tH = gAlice->TreeH();
1036   Stat_t ntracks = tH->GetEntries();
1037   Int_t npart = gAlice->GetNtrack();
1038     
1039   //------------------------------------------------------------
1040   // Loop over tracks
1041   //------------------------------------------------------------
1042   
1043   for(Int_t track=0;track<ntracks;track++){
1044     ResetHits();
1045     tH->GetEvent(track);
1046     //
1047     //  Get number of the TPC hits and a pointer
1048     //  to the particles
1049     //
1050     nhits=fHits->GetEntriesFast();
1051     //
1052     // Loop over hits
1053     //
1054     Int_t currentIndex=0;
1055     Int_t lastrow=-1;  //last writen row
1056     for(Int_t hit=0;hit<nhits;hit++){
1057       tpcHit=(AliTPChit*)fHits->UncheckedAt(hit);
1058       if (tpcHit==0) continue;
1059       sector=tpcHit->fSector; // sector number
1060       if(sector != isec) continue; 
1061       ipart=tpcHit->Track();
1062       if (ipart<npart) particle=gAlice->Particle(ipart);
1063       
1064       //find row number
1065
1066       Float_t  x[3]={tpcHit->X(),tpcHit->Y(),tpcHit->Z()};
1067       Int_t    index[3]={1,isec,0};
1068       Int_t    currentrow = fTPCParam->GetPadRow(x,index) ;     
1069       if (currentrow<0) continue;
1070       if (lastrow<0) lastrow=currentrow;
1071       if (currentrow==lastrow){
1072         if ( currentIndex>=maxhits){
1073           maxhits+=kcmaxhits;
1074           xxx.ResizeTo(4*maxhits);
1075         }     
1076         xxx(currentIndex*4)=x[0];
1077         xxx(currentIndex*4+1)=x[1];
1078         xxx(currentIndex*4+2)=x[2];     
1079         xxx(currentIndex*4+3)=tpcHit->fQ;
1080         currentIndex++; 
1081       }
1082       else 
1083         if (currentIndex>2){
1084           Float_t sumx=0;
1085           Float_t sumx2=0;
1086           Float_t sumx3=0;
1087           Float_t sumx4=0;
1088           Float_t sumy=0;
1089           Float_t sumxy=0;
1090           Float_t sumx2y=0;
1091           Float_t sumz=0;
1092           Float_t sumxz=0;
1093           Float_t sumx2z=0;
1094           Float_t sumq=0;
1095           for (Int_t index=0;index<currentIndex;index++){
1096             Float_t x,x2,x3,x4;
1097             x=x2=x3=x4=xxx(index*4);
1098             x2*=x;
1099             x3*=x2;
1100             x4*=x3;
1101             sumx+=x;
1102             sumx2+=x2;
1103             sumx3+=x3;
1104             sumx4+=x4;
1105             sumy+=xxx(index*4+1);
1106             sumxy+=xxx(index*4+1)*x;
1107             sumx2y+=xxx(index*4+1)*x2;
1108             sumz+=xxx(index*4+2);
1109             sumxz+=xxx(index*4+2)*x;
1110             sumx2z+=xxx(index*4+2)*x2;   
1111             sumq+=xxx(index*4+3);
1112           }
1113           Float_t centralPad = (fTPCParam->GetNPads(isec,lastrow)-1)/2;
1114           Float_t det=currentIndex*(sumx2*sumx4-sumx3*sumx3)-sumx*(sumx*sumx4-sumx2*sumx3)+
1115             sumx2*(sumx*sumx3-sumx2*sumx2);
1116           
1117           Float_t detay=sumy*(sumx2*sumx4-sumx3*sumx3)-sumx*(sumxy*sumx4-sumx2y*sumx3)+
1118             sumx2*(sumxy*sumx3-sumx2y*sumx2);
1119           Float_t detaz=sumz*(sumx2*sumx4-sumx3*sumx3)-sumx*(sumxz*sumx4-sumx2z*sumx3)+
1120             sumx2*(sumxz*sumx3-sumx2z*sumx2);
1121           
1122           Float_t detby=currentIndex*(sumxy*sumx4-sumx2y*sumx3)-sumy*(sumx*sumx4-sumx2*sumx3)+
1123             sumx2*(sumx*sumx2y-sumx2*sumxy);
1124           Float_t detbz=currentIndex*(sumxz*sumx4-sumx2z*sumx3)-sumz*(sumx*sumx4-sumx2*sumx3)+
1125             sumx2*(sumx*sumx2z-sumx2*sumxz);
1126           
1127           Float_t y=detay/det+centralPad;
1128           Float_t z=detaz/det;  
1129           Float_t by=detby/det; //y angle
1130           Float_t bz=detbz/det; //z angle
1131           sumy/=Float_t(currentIndex);
1132           sumz/=Float_t(currentIndex);
1133           AliComplexCluster cl;
1134           cl.fX=z;
1135           cl.fY=y;
1136           cl.fQ=sumq;
1137           cl.fSigmaX2=bz;
1138           cl.fSigmaY2=by;
1139           cl.fTracks[0]=ipart;
1140           
1141           AliTPCClustersRow * row = (fClustersArray->GetRow(isec,lastrow));
1142           if (row!=0) row->InsertCluster(&cl);
1143           currentIndex=0;
1144           lastrow=currentrow;
1145         } //end of calculating cluster for given row
1146         
1147         
1148         
1149     } // end of loop over hits
1150   }   // end of loop over tracks 
1151   //write padrows to tree 
1152   for (Int_t ii=0; ii<fTPCParam->GetNRow(isec);ii++) {
1153     fClustersArray->StoreRow(isec,ii);    
1154     fClustersArray->ClearRow(isec,ii);        
1155   }
1156   xxxx->Delete();
1157  
1158 }
1159 //___________________________________________
1160 void AliTPC::SDigits2Digits(Int_t eventnumber)
1161 {
1162
1163
1164   cerr<<"Digitizing TPC...\n";
1165
1166   Hits2Digits(eventnumber);
1167    
1168     
1169   //write results
1170
1171   //  char treeName[100];
1172
1173   //  sprintf(treeName,"TreeD_%s_%d",fTPCParam->GetTitle(),eventnumber);
1174   
1175   //  GetDigitsArray()->GetTree()->Write(treeName);  
1176 }
1177 //__________________________________________________________________
1178 void AliTPC::SetDefaults(){
1179
1180    
1181    cerr<<"Setting default parameters...\n";
1182
1183   // Set response functions
1184
1185   AliTPCParamSR *param=(AliTPCParamSR*)gDirectory->Get("75x40_100x60");
1186   AliTPCPRF2D    * prfinner   = new AliTPCPRF2D;
1187   AliTPCPRF2D    * prfouter   = new AliTPCPRF2D;
1188   AliTPCRF1D     * rf    = new AliTPCRF1D(kTRUE);
1189   rf->SetGauss(param->GetZSigma(),param->GetZWidth(),1.);
1190   rf->SetOffset(3*param->GetZSigma());
1191   rf->Update();
1192   
1193   TDirectory *savedir=gDirectory;
1194   TFile *f=TFile::Open("$ALICE_ROOT/TPC/AliTPCprf2d.root");
1195   if (!f->IsOpen()) { 
1196     cerr<<"Can't open $ALICE_ROOT/TPC/AliTPCprf2d.root !\n" ;
1197      exit(3);
1198   }
1199   prfinner->Read("prf_07504_Gati_056068_d02");
1200   prfouter->Read("prf_10006_Gati_047051_d03");
1201   f->Close();
1202   savedir->cd();
1203
1204   param->SetInnerPRF(prfinner);
1205   param->SetOuterPRF(prfouter); 
1206   param->SetTimeRF(rf);
1207
1208   // set fTPCParam
1209
1210   SetParam(param);
1211
1212
1213   fDefaults = 1;
1214
1215 }
1216 //__________________________________________________________________  
1217 void AliTPC::Hits2Digits(Int_t eventnumber)  
1218
1219  //----------------------------------------------------
1220  // Loop over all sectors for a single event
1221  //----------------------------------------------------
1222
1223
1224   if(fDefaults == 0) SetDefaults();  // check if the parameters are set
1225
1226   //setup TPCDigitsArray 
1227
1228   if(GetDigitsArray()) delete GetDigitsArray();
1229
1230   AliTPCDigitsArray *arr = new AliTPCDigitsArray; 
1231   arr->SetClass("AliSimDigits");
1232   arr->Setup(fTPCParam);
1233   arr->MakeTree(fDigitsFile);
1234   SetDigitsArray(arr);
1235
1236   fDigitsSwitch=0; // standard digits
1237
1238   cerr<<"Digitizing TPC...\n";
1239
1240  for(Int_t isec=0;isec<fTPCParam->GetNSector();isec++) Hits2DigitsSector(isec);
1241
1242   // write results
1243
1244   char treeName[100];
1245
1246   sprintf(treeName,"TreeD_%s_%d",fTPCParam->GetTitle(),eventnumber);
1247   
1248   GetDigitsArray()->GetTree()->Write(treeName);  
1249
1250
1251 }
1252
1253
1254
1255 //__________________________________________________________________
1256 void AliTPC::Hits2SDigits(Int_t eventnumber)  
1257
1258
1259   //-----------------------------------------------------------
1260   //   summable digits - 16 bit "ADC", no noise, no saturation
1261   //-----------------------------------------------------------
1262
1263  //----------------------------------------------------
1264  // Loop over all sectors for a single event
1265  //----------------------------------------------------
1266
1267
1268   if(fDefaults == 0) SetDefaults();
1269
1270   //setup TPCDigitsArray 
1271
1272   if(GetDigitsArray()) delete GetDigitsArray();
1273
1274   AliTPCDigitsArray *arr = new AliTPCDigitsArray; 
1275   arr->SetClass("AliSimDigits");
1276   arr->Setup(fTPCParam);
1277   arr->MakeTree(fDigitsFile);
1278   SetDigitsArray(arr);
1279
1280   cerr<<"Digitizing TPC...\n"; 
1281
1282   fDigitsSwitch=1; // summable digits
1283
1284  for(Int_t isec=0;isec<fTPCParam->GetNSector();isec++) Hits2DigitsSector(isec);
1285
1286
1287   // write results
1288
1289   char treeName[100];
1290
1291   sprintf(treeName,"TreeS_%s_%d",fTPCParam->GetTitle(),eventnumber);
1292   
1293   GetDigitsArray()->GetTree()->Write(treeName); 
1294
1295 }
1296
1297
1298
1299 //_____________________________________________________________________________
1300 void AliTPC::Hits2DigitsSector(Int_t isec)
1301 {
1302   //-------------------------------------------------------------------
1303   // TPC conversion from hits to digits.
1304   //------------------------------------------------------------------- 
1305
1306   //-----------------------------------------------------------------
1307   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1308   //-----------------------------------------------------------------
1309
1310   //-------------------------------------------------------
1311   //  Get the access to the track hits
1312   //-------------------------------------------------------
1313
1314   // check if the parameters are set - important if one calls this method
1315   // directly, not from the Hits2Digits
1316
1317   if(fDefaults == 0) SetDefaults();
1318
1319   TTree *tH = gAlice->TreeH(); // pointer to the hits tree
1320   Stat_t ntracks = tH->GetEntries();
1321
1322   if( ntracks > 0){
1323
1324   //------------------------------------------- 
1325   //  Only if there are any tracks...
1326   //-------------------------------------------
1327
1328     TObjArray **row;
1329     
1330     //printf("*** Processing sector number %d ***\n",isec);
1331
1332       Int_t nrows =fTPCParam->GetNRow(isec);
1333
1334       row= new TObjArray* [nrows];
1335     
1336       MakeSector(isec,nrows,tH,ntracks,row);
1337
1338       //--------------------------------------------------------
1339       //   Digitize this sector, row by row
1340       //   row[i] is the pointer to the TObjArray of TVectors,
1341       //   each one containing electrons accepted on this
1342       //   row, assigned into tracks
1343       //--------------------------------------------------------
1344
1345       Int_t i;
1346
1347       if (fDigitsArray->GetTree()==0) fDigitsArray->MakeTree(fDigitsFile);
1348
1349       for (i=0;i<nrows;i++){
1350
1351         AliDigits * dig = fDigitsArray->CreateRow(isec,i); 
1352
1353         DigitizeRow(i,isec,row);
1354
1355         fDigitsArray->StoreRow(isec,i);
1356
1357                 Int_t ndig = dig->GetDigitSize(); 
1358         
1359  
1360        //printf("*** Sector, row, compressed digits %d %d %d ***\n",isec,i,ndig);
1361         
1362         fDigitsArray->ClearRow(isec,i);  
1363
1364    
1365        } // end of the sector digitization
1366
1367       for(i=0;i<nrows;i++){
1368         row[i]->Delete();  
1369         delete row[i];   
1370       }
1371       
1372        delete [] row; // delete the array of pointers to TObjArray-s
1373         
1374   } // ntracks >0
1375
1376 } // end of Hits2DigitsSector
1377
1378
1379 //_____________________________________________________________________________
1380 void AliTPC::DigitizeRow(Int_t irow,Int_t isec,TObjArray **rows)
1381 {
1382   //-----------------------------------------------------------
1383   // Single row digitization, coupling from the neighbouring
1384   // rows taken into account
1385   //-----------------------------------------------------------
1386
1387   //-----------------------------------------------------------------
1388   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1389   // Modified: Marian Ivanov GSI Darmstadt, m.ivanov@gsi.de
1390   //-----------------------------------------------------------------
1391  
1392
1393   Float_t zerosup = fTPCParam->GetZeroSup();
1394   Int_t nrows =fTPCParam->GetNRow(isec);
1395   fCurrentIndex[1]= isec;
1396   
1397
1398   Int_t nofPads = fTPCParam->GetNPads(isec,irow);
1399   Int_t nofTbins = fTPCParam->GetMaxTBin();
1400   Int_t indexRange[4];
1401   //
1402   //  Integrated signal for this row
1403   //  and a single track signal
1404   //    
1405   TMatrix *m1   = new TMatrix(0,nofPads,0,nofTbins); // integrated
1406   TMatrix *m2   = new TMatrix(0,nofPads,0,nofTbins); // single
1407   //
1408   TMatrix &total  = *m1;
1409
1410   //  Array of pointers to the label-signal list
1411
1412   Int_t nofDigits = nofPads*nofTbins; // number of digits for this row
1413   Float_t  **pList = new Float_t* [nofDigits]; 
1414
1415   Int_t lp;
1416   Int_t i1;   
1417   for(lp=0;lp<nofDigits;lp++)pList[lp]=0; // set all pointers to NULL
1418   //
1419   //calculate signal 
1420   //
1421   Int_t row1 = TMath::Max(irow-fTPCParam->GetNCrossRows(),0);
1422   Int_t row2 = TMath::Min(irow+fTPCParam->GetNCrossRows(),nrows-1);
1423   for (Int_t row= row1;row<=row2;row++){
1424     Int_t nTracks= rows[row]->GetEntries();
1425     for (i1=0;i1<nTracks;i1++){
1426       fCurrentIndex[2]= row;
1427       fCurrentIndex[3]=irow;
1428       if (row==irow){
1429         m2->Zero();  // clear single track signal matrix
1430         Float_t trackLabel = GetSignal(rows[row],i1,m2,m1,indexRange); 
1431         GetList(trackLabel,nofPads,m2,indexRange,pList);
1432       }
1433       else   GetSignal(rows[row],i1,0,m1,indexRange);
1434     }
1435   }
1436          
1437   Int_t tracks[3];
1438
1439   AliDigits *dig = fDigitsArray->GetRow(isec,irow);
1440   for(Int_t ip=0;ip<nofPads;ip++){
1441     for(Int_t it=0;it<nofTbins;it++){
1442
1443       Float_t q = total(ip,it);
1444
1445       Int_t gi =it*nofPads+ip; // global index
1446
1447       if(fDigitsSwitch == 0){
1448
1449         q = gRandom->Gaus(q,fTPCParam->GetNoise()*fTPCParam->GetNoiseNormFac()); 
1450
1451         q = (Int_t)q;
1452
1453         if(q <=zerosup) continue; // do not fill zeros
1454         if(q > fTPCParam->GetADCSat()) q = fTPCParam->GetADCSat();  // saturation
1455
1456       }
1457
1458       else {
1459        q *= 16.;
1460        q = (Int_t)q;
1461       }
1462
1463       //
1464       //  "real" signal or electronic noise (list = -1)?
1465       //    
1466
1467       for(Int_t j1=0;j1<3;j1++){
1468         tracks[j1] = (pList[gi]) ?(Int_t)(*(pList[gi]+j1)) : -1;
1469       }
1470
1471 //Begin_Html
1472 /*
1473   <A NAME="AliDigits"></A>
1474   using of AliDigits object
1475 */
1476 //End_Html
1477       dig->SetDigitFast((Short_t)q,it,ip);
1478       if (fDigitsArray->IsSimulated())
1479         {
1480          ((AliSimDigits*)dig)->SetTrackIDFast(tracks[0],it,ip,0);
1481          ((AliSimDigits*)dig)->SetTrackIDFast(tracks[1],it,ip,1);
1482          ((AliSimDigits*)dig)->SetTrackIDFast(tracks[2],it,ip,2);
1483         }
1484      
1485     
1486     } // end of loop over time buckets
1487   }  // end of lop over pads 
1488
1489   //
1490   //  This row has been digitized, delete nonused stuff
1491   //
1492
1493   for(lp=0;lp<nofDigits;lp++){
1494     if(pList[lp]) delete [] pList[lp];
1495   }
1496   
1497   delete [] pList;
1498
1499   delete m1;
1500   delete m2;
1501   //  delete m3;
1502
1503 } // end of DigitizeRow
1504
1505 //_____________________________________________________________________________
1506
1507 Float_t AliTPC::GetSignal(TObjArray *p1, Int_t ntr, TMatrix *m1, TMatrix *m2,
1508                           Int_t *indexRange)
1509 {
1510
1511   //---------------------------------------------------------------
1512   //  Calculates 2-D signal (pad,time) for a single track,
1513   //  returns a pointer to the signal matrix and the track label 
1514   //  No digitization is performed at this level!!!
1515   //---------------------------------------------------------------
1516
1517   //-----------------------------------------------------------------
1518   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1519   // Modified: Marian Ivanov 
1520   //-----------------------------------------------------------------
1521
1522   TVector *tv;
1523  
1524   tv = (TVector*)p1->At(ntr); // pointer to a track
1525   TVector &v = *tv;
1526   
1527   Float_t label = v(0);
1528   Int_t centralPad = (fTPCParam->GetNPads(fCurrentIndex[1],fCurrentIndex[3])-1)/2;
1529
1530   Int_t nElectrons = (tv->GetNrows()-1)/4;
1531   indexRange[0]=9999; // min pad
1532   indexRange[1]=-1; // max pad
1533   indexRange[2]=9999; //min time
1534   indexRange[3]=-1; // max time
1535
1536   //  Float_t IneffFactor = 0.5; // inefficiency in the gain close to the edge, as above
1537
1538   TMatrix &signal = *m1;
1539   TMatrix &total = *m2;
1540   //
1541   //  Loop over all electrons
1542   //
1543   for(Int_t nel=0; nel<nElectrons; nel++){
1544     Int_t idx=nel*4;
1545     Float_t aval =  v(idx+4);
1546     Float_t eltoadcfac=aval*fTPCParam->GetTotalNormFac(); 
1547     Float_t xyz[3]={v(idx+1),v(idx+2),v(idx+3)};
1548     Int_t n = fTPCParam->CalcResponse(xyz,fCurrentIndex,fCurrentIndex[3]);
1549     
1550     if (n>0) for (Int_t i =0; i<n; i++){
1551        Int_t *index = fTPCParam->GetResBin(i);        
1552        Int_t pad=index[1]+centralPad;  //in digit coordinates central pad has coordinate 0
1553        if ( ( pad<(fTPCParam->GetNPads(fCurrentIndex[1],fCurrentIndex[3]))) && (pad>0)) {
1554          Int_t time=index[2];    
1555          Float_t weight = fTPCParam->GetResWeight(i); //we normalise response to ADC channel
1556          weight *= eltoadcfac;
1557          
1558          if (m1!=0) signal(pad,time)+=weight; 
1559          total(pad,time)+=weight;
1560          indexRange[0]=TMath::Min(indexRange[0],pad);
1561          indexRange[1]=TMath::Max(indexRange[1],pad);
1562          indexRange[2]=TMath::Min(indexRange[2],time);
1563          indexRange[3]=TMath::Max(indexRange[3],time); 
1564        }         
1565     }
1566   } // end of loop over electrons
1567   
1568   return label; // returns track label when finished
1569 }
1570
1571 //_____________________________________________________________________________
1572 void AliTPC::GetList(Float_t label,Int_t np,TMatrix *m,Int_t *indexRange,
1573                      Float_t **pList)
1574 {
1575   //----------------------------------------------------------------------
1576   //  Updates the list of tracks contributing to digits for a given row
1577   //----------------------------------------------------------------------
1578
1579   //-----------------------------------------------------------------
1580   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1581   //-----------------------------------------------------------------
1582
1583   TMatrix &signal = *m;
1584
1585   // lop over nonzero digits
1586
1587   for(Int_t it=indexRange[2];it<indexRange[3]+1;it++){
1588     for(Int_t ip=indexRange[0];ip<indexRange[1]+1;ip++){
1589
1590
1591         // accept only the contribution larger than 500 electrons (1/2 s_noise)
1592
1593         if(signal(ip,it)<0.5) continue; 
1594
1595
1596         Int_t globalIndex = it*np+ip; // globalIndex starts from 0!
1597         
1598         if(!pList[globalIndex]){
1599         
1600           // 
1601           // Create new list (6 elements - 3 signals and 3 labels),
1602           //
1603
1604           pList[globalIndex] = new Float_t [6];
1605
1606           // set list to -1 
1607
1608           *pList[globalIndex] = -1.;
1609           *(pList[globalIndex]+1) = -1.;
1610           *(pList[globalIndex]+2) = -1.;
1611           *(pList[globalIndex]+3) = -1.;
1612           *(pList[globalIndex]+4) = -1.;
1613           *(pList[globalIndex]+5) = -1.;
1614
1615
1616           *pList[globalIndex] = label;
1617           *(pList[globalIndex]+3) = signal(ip,it);
1618         }
1619         else{
1620
1621           // check the signal magnitude
1622
1623           Float_t highest = *(pList[globalIndex]+3);
1624           Float_t middle = *(pList[globalIndex]+4);
1625           Float_t lowest = *(pList[globalIndex]+5);
1626
1627           //
1628           //  compare the new signal with already existing list
1629           //
1630
1631           if(signal(ip,it)<lowest) continue; // neglect this track
1632
1633           //
1634
1635           if (signal(ip,it)>highest){
1636             *(pList[globalIndex]+5) = middle;
1637             *(pList[globalIndex]+4) = highest;
1638             *(pList[globalIndex]+3) = signal(ip,it);
1639
1640             *(pList[globalIndex]+2) = *(pList[globalIndex]+1);
1641             *(pList[globalIndex]+1) = *pList[globalIndex];
1642             *pList[globalIndex] = label;
1643           }
1644           else if (signal(ip,it)>middle){
1645             *(pList[globalIndex]+5) = middle;
1646             *(pList[globalIndex]+4) = signal(ip,it);
1647
1648             *(pList[globalIndex]+2) = *(pList[globalIndex]+1);
1649             *(pList[globalIndex]+1) = label;
1650           }
1651           else{
1652             *(pList[globalIndex]+5) = signal(ip,it);
1653             *(pList[globalIndex]+2) = label;
1654           }
1655         }
1656
1657     } // end of loop over pads
1658   } // end of loop over time bins
1659
1660
1661
1662 }//end of GetList
1663 //___________________________________________________________________
1664 void AliTPC::MakeSector(Int_t isec,Int_t nrows,TTree *TH,
1665                         Stat_t ntracks,TObjArray **row)
1666 {
1667
1668   //-----------------------------------------------------------------
1669   // Prepares the sector digitization, creates the vectors of
1670   // tracks for each row of this sector. The track vector
1671   // contains the track label and the position of electrons.
1672   //-----------------------------------------------------------------
1673
1674   //-----------------------------------------------------------------
1675   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1676   //-----------------------------------------------------------------
1677
1678   Float_t gasgain = fTPCParam->GetGasGain();
1679   Int_t i;
1680   Float_t xyz[4]; 
1681
1682   AliTPChit *tpcHit; // pointer to a sigle TPC hit    
1683   //MI change
1684   TBranch * branch=0;
1685   if (fHitType&2) branch = TH->GetBranch("TPC2");
1686   else branch = TH->GetBranch("TPC");
1687
1688  
1689   //----------------------------------------------
1690   // Create TObjArray-s, one for each row,
1691   // each TObjArray will store the TVectors
1692   // of electrons, one TVector per each track.
1693   //---------------------------------------------- 
1694     
1695   Int_t *nofElectrons = new Int_t [nrows]; // electron counter for each row
1696   TVector **tracks = new TVector* [nrows]; //pointers to the track vectors
1697   for(i=0; i<nrows; i++){
1698     row[i] = new TObjArray;
1699     nofElectrons[i]=0;
1700     tracks[i]=0;
1701   }
1702
1703  
1704
1705   //--------------------------------------------------------------------
1706   //  Loop over tracks, the "track" contains the full history
1707   //--------------------------------------------------------------------
1708
1709   Int_t previousTrack,currentTrack;
1710   previousTrack = -1; // nothing to store so far!
1711
1712   for(Int_t track=0;track<ntracks;track++){
1713     Bool_t isInSector=kTRUE;
1714     ResetHits();
1715
1716     if (fHitType&2) {
1717       isInSector=kFALSE;
1718       TBranch * br = TH->GetBranch("fTrackHitsInfo");
1719       br->GetEvent(track);
1720       AliObjectArray * ar = fTrackHits->fTrackHitsInfo;
1721       for (UInt_t j=0;j<ar->GetSize();j++){
1722         if (  ((AliTrackHitsInfo*)ar->At(j))->fVolumeID==isec) isInSector=kTRUE;
1723       }
1724     }
1725     if (!isInSector) continue;
1726     //MI change
1727     branch->GetEntry(track); // get next track
1728
1729     //M.I. changes
1730
1731     tpcHit = (AliTPChit*)FirstHit(-1);
1732
1733     //--------------------------------------------------------------
1734     //  Loop over hits
1735     //--------------------------------------------------------------
1736
1737
1738     while(tpcHit){
1739       
1740       Int_t sector=tpcHit->fSector; // sector number
1741       //      if(sector != isec) continue; 
1742       if(sector != isec){
1743         tpcHit = (AliTPChit*) NextHit();
1744         continue; 
1745       }
1746
1747         currentTrack = tpcHit->Track(); // track number
1748
1749
1750         if(currentTrack != previousTrack){
1751                           
1752            // store already filled fTrack
1753               
1754            for(i=0;i<nrows;i++){
1755              if(previousTrack != -1){
1756                if(nofElectrons[i]>0){
1757                  TVector &v = *tracks[i];
1758                  v(0) = previousTrack;
1759                  tracks[i]->ResizeTo(4*nofElectrons[i]+1); // shrink if necessary
1760                  row[i]->Add(tracks[i]);                     
1761                }
1762                else{
1763                  delete tracks[i]; // delete empty TVector
1764                  tracks[i]=0;
1765                }
1766              }
1767
1768              nofElectrons[i]=0;
1769              tracks[i] = new TVector(481); // TVectors for the next fTrack
1770
1771            } // end of loop over rows
1772                
1773            previousTrack=currentTrack; // update track label 
1774         }
1775            
1776         Int_t qI = (Int_t) (tpcHit->fQ); // energy loss (number of electrons)
1777
1778        //---------------------------------------------------
1779        //  Calculate the electron attachment probability
1780        //---------------------------------------------------
1781
1782
1783         Float_t time = 1.e6*(fTPCParam->GetZLength()-TMath::Abs(tpcHit->Z()))
1784                                                         /fTPCParam->GetDriftV(); 
1785         // in microseconds!     
1786         Float_t attProb = fTPCParam->GetAttCoef()*
1787           fTPCParam->GetOxyCont()*time; //  fraction! 
1788    
1789         //-----------------------------------------------
1790         //  Loop over electrons
1791         //-----------------------------------------------
1792         Int_t index[3];
1793         index[1]=isec;
1794         for(Int_t nel=0;nel<qI;nel++){
1795           // skip if electron lost due to the attachment
1796           if((gRandom->Rndm(0)) < attProb) continue; // electron lost!
1797           xyz[0]=tpcHit->X();
1798           xyz[1]=tpcHit->Y();
1799           xyz[2]=tpcHit->Z();   
1800           //
1801           // protection for the nonphysical avalanche size (10**6 maximum)
1802           //  
1803           Double_t rn=TMath::Max(gRandom->Rndm(0),1.93e-22);
1804           xyz[3]= (Float_t) (-gasgain*TMath::Log(rn)); 
1805           index[0]=1;
1806           
1807           TransportElectron(xyz,index); //MI change -august       
1808           Int_t rowNumber;
1809           fTPCParam->GetPadRow(xyz,index); //MI change august
1810           rowNumber = index[2];
1811           //transform position to local digit coordinates
1812           //relative to nearest pad row 
1813           if ((rowNumber<0)||rowNumber>=fTPCParam->GetNRow(isec)) continue;       
1814           nofElectrons[rowNumber]++;      
1815           //----------------------------------
1816           // Expand vector if necessary
1817           //----------------------------------
1818           if(nofElectrons[rowNumber]>120){
1819             Int_t range = tracks[rowNumber]->GetNrows();
1820             if((nofElectrons[rowNumber])>(range-1)/4){
1821         
1822               tracks[rowNumber]->ResizeTo(range+400); // Add 100 electrons
1823             }
1824           }
1825           
1826           TVector &v = *tracks[rowNumber];
1827           Int_t idx = 4*nofElectrons[rowNumber]-3;
1828
1829           v(idx)=  xyz[0];   // X - pad row coordinate
1830           v(idx+1)=xyz[1];   // Y - pad coordinate (along the pad-row)
1831           v(idx+2)=xyz[2];   // Z - time bin coordinate
1832           v(idx+3)=xyz[3];   // avalanche size  
1833         } // end of loop over electrons
1834
1835         tpcHit = (AliTPChit*)NextHit();
1836         
1837       } // end of loop over hits
1838     } // end of loop over tracks
1839
1840     //
1841     //   store remaining track (the last one) if not empty
1842     //
1843
1844      for(i=0;i<nrows;i++){
1845        if(nofElectrons[i]>0){
1846           TVector &v = *tracks[i];
1847           v(0) = previousTrack;
1848           tracks[i]->ResizeTo(4*nofElectrons[i]+1); // shrink if necessary
1849           row[i]->Add(tracks[i]);  
1850         }
1851         else{
1852           delete tracks[i];
1853           tracks[i]=0;
1854         }  
1855       }  
1856
1857           delete [] tracks;
1858           delete [] nofElectrons;
1859  
1860
1861 } // end of MakeSector
1862
1863
1864 //_____________________________________________________________________________
1865 void AliTPC::Init()
1866 {
1867   //
1868   // Initialise TPC detector after definition of geometry
1869   //
1870   Int_t i;
1871   //
1872   if(fDebug) {
1873     printf("\n%s: ",ClassName());
1874     for(i=0;i<35;i++) printf("*");
1875     printf(" TPC_INIT ");
1876     for(i=0;i<35;i++) printf("*");
1877     printf("\n%s: ",ClassName());
1878     //
1879     for(i=0;i<80;i++) printf("*");
1880     printf("\n");
1881   }
1882 }
1883
1884 //_____________________________________________________________________________
1885 void AliTPC::MakeBranch(Option_t* option, const char *file)
1886 {
1887   //
1888   // Create Tree branches for the TPC.
1889   //
1890   Int_t buffersize = 4000;
1891   char branchname[10];
1892   sprintf(branchname,"%s",GetName());
1893
1894   AliDetector::MakeBranch(option,file);
1895
1896   const char *d = strstr(option,"D");
1897
1898   if (fDigits   && gAlice->TreeD() && d) {
1899       MakeBranchInTree(gAlice->TreeD(), 
1900                        branchname, &fDigits, buffersize, file);
1901   }     
1902
1903   if (fHitType&2) MakeBranch2(option,file); // MI change 14.09.2000
1904 }
1905  
1906 //_____________________________________________________________________________
1907 void AliTPC::ResetDigits()
1908 {
1909   //
1910   // Reset number of digits and the digits array for this detector
1911   //
1912   fNdigits   = 0;
1913   if (fDigits)   fDigits->Clear();
1914 }
1915
1916 //_____________________________________________________________________________
1917 void AliTPC::SetSecAL(Int_t sec)
1918 {
1919   //---------------------------------------------------
1920   // Activate/deactivate selection for lower sectors
1921   //---------------------------------------------------
1922
1923   //-----------------------------------------------------------------
1924   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1925   //-----------------------------------------------------------------
1926
1927   fSecAL = sec;
1928 }
1929
1930 //_____________________________________________________________________________
1931 void AliTPC::SetSecAU(Int_t sec)
1932 {
1933   //----------------------------------------------------
1934   // Activate/deactivate selection for upper sectors
1935   //---------------------------------------------------
1936
1937   //-----------------------------------------------------------------
1938   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1939   //-----------------------------------------------------------------
1940
1941   fSecAU = sec;
1942 }
1943
1944 //_____________________________________________________________________________
1945 void AliTPC::SetSecLows(Int_t s1,Int_t s2,Int_t s3,Int_t s4,Int_t s5, Int_t s6)
1946 {
1947   //----------------------------------------
1948   // Select active lower sectors
1949   //----------------------------------------
1950
1951   //-----------------------------------------------------------------
1952   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1953   //-----------------------------------------------------------------
1954
1955   fSecLows[0] = s1;
1956   fSecLows[1] = s2;
1957   fSecLows[2] = s3;
1958   fSecLows[3] = s4;
1959   fSecLows[4] = s5;
1960   fSecLows[5] = s6;
1961 }
1962
1963 //_____________________________________________________________________________
1964 void AliTPC::SetSecUps(Int_t s1,Int_t s2,Int_t s3,Int_t s4,Int_t s5, Int_t s6,
1965                        Int_t s7, Int_t s8 ,Int_t s9 ,Int_t s10, 
1966                        Int_t s11 , Int_t s12)
1967 {
1968   //--------------------------------
1969   // Select active upper sectors
1970   //--------------------------------
1971
1972   //-----------------------------------------------------------------
1973   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1974   //-----------------------------------------------------------------
1975
1976   fSecUps[0] = s1;
1977   fSecUps[1] = s2;
1978   fSecUps[2] = s3;
1979   fSecUps[3] = s4;
1980   fSecUps[4] = s5;
1981   fSecUps[5] = s6;
1982   fSecUps[6] = s7;
1983   fSecUps[7] = s8;
1984   fSecUps[8] = s9;
1985   fSecUps[9] = s10;
1986   fSecUps[10] = s11;
1987   fSecUps[11] = s12;
1988 }
1989
1990 //_____________________________________________________________________________
1991 void AliTPC::SetSens(Int_t sens)
1992 {
1993
1994   //-------------------------------------------------------------
1995   // Activates/deactivates the sensitive strips at the center of
1996   // the pad row -- this is for the space-point resolution calculations
1997   //-------------------------------------------------------------
1998
1999   //-----------------------------------------------------------------
2000   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
2001   //-----------------------------------------------------------------
2002
2003   fSens = sens;
2004 }
2005
2006  
2007 void AliTPC::SetSide(Float_t side=0.)
2008 {
2009   // choice of the TPC side
2010
2011   fSide = side;
2012  
2013 }
2014 //____________________________________________________________________________
2015 void AliTPC::SetGasMixt(Int_t nc,Int_t c1,Int_t c2,Int_t c3,Float_t p1,
2016                            Float_t p2,Float_t p3)
2017 {
2018
2019   // gax mixture definition
2020
2021  fNoComp = nc;
2022  
2023  fMixtComp[0]=c1;
2024  fMixtComp[1]=c2;
2025  fMixtComp[2]=c3;
2026
2027  fMixtProp[0]=p1;
2028  fMixtProp[1]=p2;
2029  fMixtProp[2]=p3; 
2030  
2031  
2032 }
2033 //_____________________________________________________________________________
2034
2035 void AliTPC::TransportElectron(Float_t *xyz, Int_t *index)
2036 {
2037   //
2038   // electron transport taking into account:
2039   // 1. diffusion, 
2040   // 2.ExB at the wires
2041   // 3. nonisochronity
2042   //
2043   // xyz and index must be already transformed to system 1
2044   //
2045
2046   fTPCParam->Transform1to2(xyz,index);
2047   
2048   //add diffusion
2049   Float_t driftl=xyz[2];
2050   if(driftl<0.01) driftl=0.01;
2051   driftl=TMath::Sqrt(driftl);
2052   Float_t sigT = driftl*(fTPCParam->GetDiffT());
2053   Float_t sigL = driftl*(fTPCParam->GetDiffL());
2054   xyz[0]=gRandom->Gaus(xyz[0],sigT);
2055   xyz[1]=gRandom->Gaus(xyz[1],sigT);
2056   xyz[2]=gRandom->Gaus(xyz[2],sigL);
2057
2058   // ExB
2059   
2060   if (fTPCParam->GetMWPCReadout()==kTRUE){
2061     Float_t x1=xyz[0];
2062     fTPCParam->Transform2to2NearestWire(xyz,index);
2063     Float_t dx=xyz[0]-x1;
2064     xyz[1]+=dx*(fTPCParam->GetOmegaTau());
2065   }
2066   //add nonisochronity (not implemented yet)
2067   
2068 }
2069   
2070 ClassImp(AliTPCdigit)
2071  
2072 //_____________________________________________________________________________
2073 AliTPCdigit::AliTPCdigit(Int_t *tracks, Int_t *digits):
2074   AliDigit(tracks)
2075 {
2076   //
2077   // Creates a TPC digit object
2078   //
2079   fSector     = digits[0];
2080   fPadRow     = digits[1];
2081   fPad        = digits[2];
2082   fTime       = digits[3];
2083   fSignal     = digits[4];
2084 }
2085
2086  
2087 ClassImp(AliTPChit)
2088  
2089 //_____________________________________________________________________________
2090 AliTPChit::AliTPChit(Int_t shunt, Int_t track, Int_t *vol, Float_t *hits):
2091 AliHit(shunt,track)
2092 {
2093   //
2094   // Creates a TPC hit object
2095   //
2096   fSector     = vol[0];
2097   fPadRow     = vol[1];
2098   fX          = hits[0];
2099   fY          = hits[1];
2100   fZ          = hits[2];
2101   fQ          = hits[3];
2102 }
2103  
2104
2105 //________________________________________________________________________
2106 // Additional code because of the AliTPCTrackHits
2107
2108 void AliTPC::MakeBranch2(Option_t *option,const char *file)
2109 {
2110   //
2111   // Create a new branch in the current Root Tree
2112   // The branch of fHits is automatically split
2113   // MI change 14.09.2000
2114   if (fHitType&2==0) return;
2115   char branchname[10];
2116   sprintf(branchname,"%s2",GetName());  
2117   //
2118   // Get the pointer to the header
2119   const char *cH = strstr(option,"H");
2120   //
2121   if (fTrackHits   && gAlice->TreeH() && cH) {    
2122     AliObjectBranch * branch = new AliObjectBranch(branchname,"AliTPCTrackHits",&fTrackHits, 
2123                                                    gAlice->TreeH(),fBufferSize);
2124     gAlice->TreeH()->GetListOfBranches()->Add(branch);
2125     if (GetDebug()>1) 
2126       printf("* AliDetector::MakeBranch * Making Branch %s for trackhits\n",branchname);
2127     const char folder [] = "RunMC/Event/Data";
2128     if (GetDebug())
2129       printf("%15s: Publishing %s to %s\n",ClassName(),branchname,folder);
2130     Publish(folder,&fTrackHits,branchname);
2131     if (file) {
2132         TBranch *b = gAlice->TreeH()->GetBranch(branchname);
2133         TDirectory *wd = gDirectory;
2134         b->SetFile(file);
2135         TIter next( b->GetListOfBranches());
2136         while ((b=(TBranch*)next())) {
2137           b->SetFile(file);
2138         }
2139         wd->cd(); 
2140         if (GetDebug()>1) 
2141               cout << "Diverting branch " << branchname << " to file " << file << endl;  
2142     }
2143   }     
2144 }
2145
2146 void AliTPC::SetTreeAddress()
2147 {
2148   if (fHitType&1) AliDetector::SetTreeAddress();
2149   if (fHitType&2) SetTreeAddress2();
2150 }
2151
2152 void AliTPC::SetTreeAddress2()
2153 {
2154   //
2155   // Set branch address for the TrackHits Tree
2156   // 
2157   TBranch *branch;
2158   char branchname[20];
2159   sprintf(branchname,"%s2",GetName());
2160   //
2161   // Branch address for hit tree
2162   TTree *treeH = gAlice->TreeH();
2163   if (treeH) {
2164     branch = treeH->GetBranch(branchname);
2165     if (branch) branch->SetAddress(&fTrackHits);
2166   }
2167 }
2168
2169 void AliTPC::FinishPrimary()
2170 {
2171   if (fTrackHits) fTrackHits->FlushHitStack();  
2172 }
2173
2174
2175 void AliTPC::AddHit2(Int_t track, Int_t *vol, Float_t *hits)
2176
2177   //
2178   // add hit to the list  
2179   Int_t rtrack;
2180   if (fIshunt) {
2181     int primary = gAlice->GetPrimary(track);
2182     gAlice->Particle(primary)->SetBit(kKeepBit);
2183     rtrack=primary;
2184   } else {
2185     rtrack=track;
2186     gAlice->FlagTrack(track);
2187   }  
2188   //AliTPChit *hit = (AliTPChit*)fHits->UncheckedAt(fNhits-1);
2189   //if (hit->fTrack!=rtrack)
2190   //  cout<<"bad track number\n";
2191   if (fTrackHits) 
2192     fTrackHits->AddHitKartez(vol[0],rtrack, hits[0],
2193                              hits[1],hits[2],(Int_t)hits[3]);
2194 }
2195
2196 void AliTPC::ResetHits()
2197 {
2198   if (fHitType&1) AliDetector::ResetHits();
2199   if (fHitType&2) ResetHits2();
2200 }
2201
2202 void AliTPC::ResetHits2()
2203 {
2204   //
2205   //reset hits
2206   if (fTrackHits) fTrackHits->Clear();
2207 }   
2208
2209 AliHit* AliTPC::FirstHit(Int_t track)
2210 {
2211   if (fHitType&2) return FirstHit2(track);
2212   return AliDetector::FirstHit(track);
2213 }
2214 AliHit* AliTPC::NextHit()
2215 {
2216   if (fHitType&2) return NextHit2();
2217   return AliDetector::NextHit();
2218 }
2219
2220 AliHit* AliTPC::FirstHit2(Int_t track)
2221 {
2222   //
2223   // Initialise the hit iterator
2224   // Return the address of the first hit for track
2225   // If track>=0 the track is read from disk
2226   // while if track<0 the first hit of the current
2227   // track is returned
2228   // 
2229   if(track>=0) {
2230     gAlice->ResetHits();
2231     gAlice->TreeH()->GetEvent(track);
2232   }
2233   //
2234   if (fTrackHits) {
2235     fTrackHits->First();
2236     return fTrackHits->GetHit();
2237   }
2238   else return 0;
2239 }
2240
2241 AliHit* AliTPC::NextHit2()
2242 {
2243   //
2244   //Return the next hit for the current track
2245
2246   if (fTrackHits) {
2247     fTrackHits->Next();
2248     return fTrackHits->GetHit();
2249   }
2250   else 
2251     return 0;
2252 }
2253
2254 void AliTPC::LoadPoints(Int_t)
2255 {
2256   //
2257   Int_t a = 0;
2258   /*  if(fHitType==1) return AliDetector::LoadPoints(a);
2259   LoadPoints2(a);
2260   */
2261   if(fHitType==1) AliDetector::LoadPoints(a);
2262   else LoadPoints2(a);
2263    
2264   // LoadPoints3(a);
2265
2266 }
2267
2268
2269 void AliTPC::RemapTrackHitIDs(Int_t *map)
2270 {
2271   if (!fTrackHits) return;
2272   AliObjectArray * arr = fTrackHits->fTrackHitsInfo;
2273   for (UInt_t i=0;i<arr->GetSize();i++){
2274     AliTrackHitsInfo * info = (AliTrackHitsInfo *)(arr->At(i));
2275     info->fTrackID = map[info->fTrackID];
2276   }
2277   
2278 }
2279
2280
2281 //_____________________________________________________________________________
2282 void AliTPC::LoadPoints2(Int_t)
2283 {
2284   //
2285   // Store x, y, z of all hits in memory
2286   //
2287   if (fTrackHits == 0) return;
2288   //
2289   Int_t nhits = fTrackHits->GetEntriesFast();
2290   if (nhits == 0) return;
2291   Int_t tracks = gAlice->GetNtrack();
2292   if (fPoints == 0) fPoints = new TObjArray(tracks);
2293   AliHit *ahit;
2294   //
2295   Int_t *ntrk=new Int_t[tracks];
2296   Int_t *limi=new Int_t[tracks];
2297   Float_t **coor=new Float_t*[tracks];
2298   for(Int_t i=0;i<tracks;i++) {
2299     ntrk[i]=0;
2300     coor[i]=0;
2301     limi[i]=0;
2302   }
2303   //
2304   AliPoints *points = 0;
2305   Float_t *fp=0;
2306   Int_t trk;
2307   Int_t chunk=nhits/4+1;
2308   //
2309   // Loop over all the hits and store their position
2310   //
2311   ahit = FirstHit2(-1);
2312   //for (Int_t hit=0;hit<nhits;hit++) {
2313   while (ahit){
2314     //    ahit = (AliHit*)fHits->UncheckedAt(hit);
2315     trk=ahit->GetTrack();
2316     if(ntrk[trk]==limi[trk]) {
2317       //
2318       // Initialise a new track
2319       fp=new Float_t[3*(limi[trk]+chunk)];
2320       if(coor[trk]) {
2321         memcpy(fp,coor[trk],sizeof(Float_t)*3*limi[trk]);
2322         delete [] coor[trk];
2323       }
2324       limi[trk]+=chunk;
2325       coor[trk] = fp;
2326     } else {
2327       fp = coor[trk];
2328     }
2329     fp[3*ntrk[trk]  ] = ahit->X();
2330     fp[3*ntrk[trk]+1] = ahit->Y();
2331     fp[3*ntrk[trk]+2] = ahit->Z();
2332     ntrk[trk]++;
2333     ahit = NextHit2();
2334   }
2335   //
2336   for(trk=0; trk<tracks; ++trk) {
2337     if(ntrk[trk]) {
2338       points = new AliPoints();
2339       points->SetMarkerColor(GetMarkerColor());
2340       points->SetMarkerSize(GetMarkerSize());
2341       points->SetDetector(this);
2342       points->SetParticle(trk);
2343       points->SetPolyMarker(ntrk[trk],coor[trk],GetMarkerStyle());
2344       fPoints->AddAt(points,trk);
2345       delete [] coor[trk];
2346       coor[trk]=0;
2347     }
2348   }
2349   delete [] coor;
2350   delete [] ntrk;
2351   delete [] limi;
2352 }
2353
2354
2355 //_____________________________________________________________________________
2356 void AliTPC::LoadPoints3(Int_t)
2357 {
2358   //
2359   // Store x, y, z of all hits in memory
2360   // - only intersection point with pad row
2361   if (fTrackHits == 0) return;
2362   //
2363   Int_t nhits = fTrackHits->GetEntriesFast();
2364   if (nhits == 0) return;
2365   Int_t tracks = gAlice->GetNtrack();
2366   if (fPoints == 0) fPoints = new TObjArray(2*tracks);
2367   fPoints->Expand(2*tracks);
2368   AliHit *ahit;
2369   //
2370   Int_t *ntrk=new Int_t[tracks];
2371   Int_t *limi=new Int_t[tracks];
2372   Float_t **coor=new Float_t*[tracks];
2373   for(Int_t i=0;i<tracks;i++) {
2374     ntrk[i]=0;
2375     coor[i]=0;
2376     limi[i]=0;
2377   }
2378   //
2379   AliPoints *points = 0;
2380   Float_t *fp=0;
2381   Int_t trk;
2382   Int_t chunk=nhits/4+1;
2383   //
2384   // Loop over all the hits and store their position
2385   //
2386   ahit = FirstHit2(-1);
2387   //for (Int_t hit=0;hit<nhits;hit++) {
2388
2389   Int_t lastrow = -1;
2390   while (ahit){
2391     //    ahit = (AliHit*)fHits->UncheckedAt(hit);
2392     trk=ahit->GetTrack(); 
2393     Float_t  x[3]={ahit->X(),ahit->Y(),ahit->Z()};
2394     Int_t    index[3]={1,((AliTPChit*)ahit)->fSector,0};
2395     Int_t    currentrow = fTPCParam->GetPadRow(x,index) ;
2396     if (currentrow!=lastrow){
2397       lastrow = currentrow;
2398       //later calculate intersection point           
2399       if(ntrk[trk]==limi[trk]) {
2400         //
2401         // Initialise a new track
2402         fp=new Float_t[3*(limi[trk]+chunk)];
2403         if(coor[trk]) {
2404           memcpy(fp,coor[trk],sizeof(Float_t)*3*limi[trk]);
2405           delete [] coor[trk];
2406         }
2407         limi[trk]+=chunk;
2408         coor[trk] = fp;
2409       } else {
2410         fp = coor[trk];
2411       }
2412       fp[3*ntrk[trk]  ] = ahit->X();
2413       fp[3*ntrk[trk]+1] = ahit->Y();
2414       fp[3*ntrk[trk]+2] = ahit->Z();
2415       ntrk[trk]++;
2416     }
2417     ahit = NextHit2();
2418   }
2419   
2420   //
2421   for(trk=0; trk<tracks; ++trk) {
2422     if(ntrk[trk]) {
2423       points = new AliPoints();
2424       points->SetMarkerColor(GetMarkerColor()+1);
2425       points->SetMarkerStyle(5);
2426       points->SetMarkerSize(0.2);
2427       points->SetDetector(this);
2428       points->SetParticle(trk);
2429       //      points->SetPolyMarker(ntrk[trk],coor[trk],GetMarkerStyle()20);
2430       points->SetPolyMarker(ntrk[trk],coor[trk],30);
2431       fPoints->AddAt(points,tracks+trk);
2432       delete [] coor[trk];
2433       coor[trk]=0;
2434     }
2435   }
2436   delete [] coor;
2437   delete [] ntrk;
2438   delete [] limi;
2439 }
2440
2441
2442
2443 void AliTPC::FindTrackHitsIntersection(TClonesArray * arr)
2444 {
2445
2446   //
2447   //fill clones array with intersection of current point with the
2448   //middle of the row
2449   Int_t sector;
2450   Int_t ipart;
2451   
2452   const Int_t kcmaxhits=30000;
2453   TVector * xxxx = new TVector(kcmaxhits*4);
2454   TVector & xxx = *xxxx;
2455   Int_t maxhits = kcmaxhits;
2456       
2457   //
2458   AliTPChit * tpcHit=0;
2459   tpcHit = (AliTPChit*)FirstHit2(-1);
2460   Int_t currentIndex=0;
2461   Int_t lastrow=-1;  //last writen row
2462
2463   while (tpcHit){
2464     if (tpcHit==0) continue;
2465     sector=tpcHit->fSector; // sector number
2466     ipart=tpcHit->Track();
2467     
2468     //find row number
2469     
2470     Float_t  x[3]={tpcHit->X(),tpcHit->Y(),tpcHit->Z()};
2471     Int_t    index[3]={1,sector,0};
2472     Int_t    currentrow = fTPCParam->GetPadRow(x,index) ;       
2473     if (currentrow<0) continue;
2474     if (lastrow<0) lastrow=currentrow;
2475     if (currentrow==lastrow){
2476       if ( currentIndex>=maxhits){
2477         maxhits+=kcmaxhits;
2478         xxx.ResizeTo(4*maxhits);
2479       }     
2480       xxx(currentIndex*4)=x[0];
2481       xxx(currentIndex*4+1)=x[1];
2482       xxx(currentIndex*4+2)=x[2];       
2483       xxx(currentIndex*4+3)=tpcHit->fQ;
2484       currentIndex++;   
2485     }
2486     else 
2487       if (currentIndex>2){
2488         Float_t sumx=0;
2489         Float_t sumx2=0;
2490         Float_t sumx3=0;
2491         Float_t sumx4=0;
2492         Float_t sumy=0;
2493         Float_t sumxy=0;
2494         Float_t sumx2y=0;
2495         Float_t sumz=0;
2496         Float_t sumxz=0;
2497         Float_t sumx2z=0;
2498         Float_t sumq=0;
2499         for (Int_t index=0;index<currentIndex;index++){
2500           Float_t x,x2,x3,x4;
2501           x=x2=x3=x4=xxx(index*4);
2502           x2*=x;
2503           x3*=x2;
2504           x4*=x3;
2505           sumx+=x;
2506           sumx2+=x2;
2507           sumx3+=x3;
2508           sumx4+=x4;
2509           sumy+=xxx(index*4+1);
2510           sumxy+=xxx(index*4+1)*x;
2511           sumx2y+=xxx(index*4+1)*x2;
2512           sumz+=xxx(index*4+2);
2513           sumxz+=xxx(index*4+2)*x;
2514           sumx2z+=xxx(index*4+2)*x2;     
2515           sumq+=xxx(index*4+3);
2516         }
2517         Float_t centralPad = (fTPCParam->GetNPads(sector,lastrow)-1)/2;
2518         Float_t det=currentIndex*(sumx2*sumx4-sumx3*sumx3)-sumx*(sumx*sumx4-sumx2*sumx3)+
2519           sumx2*(sumx*sumx3-sumx2*sumx2);
2520         
2521         Float_t detay=sumy*(sumx2*sumx4-sumx3*sumx3)-sumx*(sumxy*sumx4-sumx2y*sumx3)+
2522           sumx2*(sumxy*sumx3-sumx2y*sumx2);
2523         Float_t detaz=sumz*(sumx2*sumx4-sumx3*sumx3)-sumx*(sumxz*sumx4-sumx2z*sumx3)+
2524           sumx2*(sumxz*sumx3-sumx2z*sumx2);
2525         
2526         Float_t detby=currentIndex*(sumxy*sumx4-sumx2y*sumx3)-sumy*(sumx*sumx4-sumx2*sumx3)+
2527           sumx2*(sumx*sumx2y-sumx2*sumxy);
2528         Float_t detbz=currentIndex*(sumxz*sumx4-sumx2z*sumx3)-sumz*(sumx*sumx4-sumx2*sumx3)+
2529           sumx2*(sumx*sumx2z-sumx2*sumxz);
2530         
2531         Float_t y=detay/det+centralPad;
2532         Float_t z=detaz/det;    
2533         Float_t by=detby/det; //y angle
2534         Float_t bz=detbz/det; //z angle
2535         sumy/=Float_t(currentIndex);
2536         sumz/=Float_t(currentIndex);
2537         
2538         AliComplexCluster cl;
2539         cl.fX=z;
2540         cl.fY=y;
2541         cl.fQ=sumq;
2542         cl.fSigmaX2=bz;
2543         cl.fSigmaY2=by;
2544         cl.fTracks[0]=ipart;
2545         
2546         AliTPCClustersRow * row = (fClustersArray->GetRow(sector,lastrow));
2547         if (row!=0) row->InsertCluster(&cl);
2548         currentIndex=0;
2549         lastrow=currentrow;
2550       } //end of calculating cluster for given row
2551                 
2552   } // end of loop over hits
2553   xxxx->Delete();
2554
2555 }
2556 //_______________________________________________________________________________
2557 void AliTPC::Digits2Reco(Int_t firstevent,Int_t lastevent)
2558 {
2559   // produces rec points from digits and writes them on the root file
2560   // AliTPCclusters.root
2561
2562   TDirectory *cwd = gDirectory;
2563
2564
2565   AliTPCParam *dig=(AliTPCParam *)gDirectory->Get("75x40_100x60");
2566   SetParam(dig);
2567   cout<<"AliTPC::Digits2Reco: TPC parameteres have been set"<<endl; 
2568   TFile *out;
2569   if(!gSystem->Getenv("CONFIG_FILE")){
2570     out=TFile::Open("AliTPCclusters.root","recreate");
2571   }
2572   else {
2573     const char *tmp1;
2574     const char *tmp2;
2575     char tmp3[80];
2576     tmp1=gSystem->Getenv("CONFIG_FILE_PREFIX");
2577     tmp2=gSystem->Getenv("CONFIG_OUTDIR");
2578     sprintf(tmp3,"%s%s/AliTPCclusters.root",tmp1,tmp2);
2579     out=TFile::Open(tmp3,"recreate");
2580   }
2581
2582   TStopwatch timer;
2583   cout<<"AliTPC::Digits2Reco - determination of rec points begins"<<endl;
2584   timer.Start();
2585   cwd->cd();
2586   for(Int_t iev=firstevent;iev<lastevent+1;iev++){
2587
2588     printf("Processing event %d\n",iev);
2589     Digits2Clusters(out,iev);
2590   }
2591   cout<<"AliTPC::Digits2Reco - determination of rec points ended"<<endl;
2592   timer.Stop();
2593   timer.Print();
2594   out->Close();
2595   cwd->cd(); 
2596
2597
2598 }