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