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