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