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