]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOF.cxx
Minor changes to AliTOF constructor
[u/mrichter/AliRoot.git] / TOF / AliTOF.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.31  2001/11/22 11:22:51  hristov
19 Updated version of TOF digitization, N^2 problem solved (J.Chudoba)
20
21 Revision 1.30  2001/10/21 18:30:39  hristov
22 Several pointers were set to zero in the default constructors to avoid memory management problems
23
24 Revision 1.29  2001/10/17 14:19:24  hristov
25 delete replaced by delete []
26
27 Revision 1.28  2001/10/05 12:02:01  vicinanz
28 Minor improvements on Merger and SDigitizer
29
30 Revision 1.27  2001/10/02 13:03:13  vicinanz
31 Minor improvements on the code
32
33 Revision 1.26  2001/09/27 10:39:20  vicinanz
34 SDigitizer and Merger added
35
36 Revision 1.25  2001/09/07 08:37:40  hristov
37 Pointers initialised to 0 in the default constructor
38
39 Revision 1.24  2001/09/05 16:31:00  hristov
40 The deletion of TOF folders temporarily commented out
41
42 Revision 1.23  2001/08/29 12:59:01  vicinanz
43 Minor changes to the Digitizer procedure
44
45 Revision 1.22  2001/08/28 08:45:58  vicinanz
46 TTask and TFolder structures implemented
47
48 Revision 1.21  2001/05/16 14:57:24  alibrary
49 New files for folders and Stack
50
51 Revision 1.20  2001/05/04 10:09:47  vicinanz
52 Major upgrades to the strip structure
53
54 Revision 1.19  2001/03/12 17:47:25  hristov
55 Changes needed on Sun with CC 5.0
56
57 Revision 1.18  2001/01/26 19:57:42  hristov
58 Major upgrade of AliRoot code
59
60 Revision 1.17  2000/10/19 09:58:14  vicinanz
61 Updated Hits2Digit procedure
62
63 Revision 1.16  2000/10/02 21:28:17  fca
64 Removal of useless dependecies via forward declarations
65
66 Revision 1.15  2000/05/18 14:33:01  vicinanz
67 Modified to be full HP compliant
68
69 Revision 1.14  2000/05/15 19:32:36  fca
70 Add AddHitList !!
71
72 Revision 1.13  2000/05/10 16:52:18  vicinanz
73 New TOF version with holes for PHOS/RICH
74
75 Revision 1.11.2.1  2000/05/10 09:37:15  vicinanz
76 New version with Holes for PHOS/RICH
77
78 Revision 1.11  1999/11/05 22:39:06  fca
79 New hits structure
80
81 Revision 1.10  1999/11/01 20:41:57  fca
82 Added protections against using the wrong version of FRAME
83
84 Revision 1.9  1999/10/15 15:35:19  fca
85 New version for frame1099 with and without holes
86
87 Revision 1.9  1999/09/29 09:24:33  fca
88 Introduction of the Copyright and cvs Log
89
90 */
91
92 ///////////////////////////////////////////////////////////////////////////////
93 //                                                                           //
94 //  Time Of Flight                                                           //
95 //  This class contains the basic functions for the Time Of Flight           //
96 //  detector. Functions specific to one particular geometry are              //
97 //  contained in the derived classes                                         //
98 //
99 //  VERSIONE WITH 5 SYMMETRIC MODULES ALONG Z AXIS
100 //  ==============================================
101 //  
102 //  VERSION WITH HOLES FOR PHOS AND TRD IN SPACEFRAME WITH HOLES
103 //
104 //  Volume sensibile : FPAD
105 //
106 //
107 //
108 // Begin_Html
109 /*
110 <img src="picts/AliTOFClass.gif">
111 */
112 //End_Html
113 //             
114 //
115 //                                                                           //
116 ///////////////////////////////////////////////////////////////////////////////
117
118 #include <iostream.h>
119 #include <strstream.h>
120 #include <fstream.h>
121 #include <stdlib.h>
122
123 #include "AliTOF.h"
124 #include "AliTOFhit.h"
125 #include "AliTOFdigit.h"
126 #include "AliTOFSDigit.h"
127 #include "AliTOFRawSector.h"
128 #include "AliTOFRoc.h"
129 #include "AliTOFRawDigit.h"
130
131 #include "TROOT.h"
132 #include "TBRIK.h"
133 #include "TNode.h"
134 #include "TObject.h"
135 #include "TSystem.h"
136 #include "TTree.h"
137 #include "TFile.h"
138 #include "TFolder.h"
139 #include "TTask.h"
140
141 #include "AliRun.h"
142 #include "AliMC.h"
143 #include "AliMagF.h"
144 #include "AliConst.h"
145
146  
147 ClassImp(AliTOF)
148  
149 //_____________________________________________________________________________
150 AliTOF::AliTOF()
151 {
152   //
153   // Default constructor
154   //
155   fFGeom = 0;
156   fDTask = 0;
157   fReTask = 0;
158   fIshunt   = 0;
159   fSDigits       = 0 ;
160   fDigits        = 0 ;
161   fReconParticles = 0;
162   fName="TOF";
163   fMerger = 0;
164 /* fp
165   CreateTOFFolders();
166 */
167 }
168  
169 //_____________________________________________________________________________
170 AliTOF::AliTOF(const char *name, const char *title)
171        : AliDetector(name,title)
172 {
173   //
174   // AliTOF standard constructor
175   // 
176   // Here are fixed some important parameters
177   //
178
179   // Initialization of hits, sdigits and digits array
180   //
181
182   fDTask=0;
183   fReTask=0;
184   fFGeom=0;
185   fReconParticles=0;
186   fMerger=0;
187
188   fHits   = new TClonesArray("AliTOFhit",  1000);
189   gAlice->AddHitList(fHits);
190   fIshunt  = 0;
191   fSDigits       = new TClonesArray("AliTOFSDigit",  1000);
192   fDigits        = new TClonesArray("AliTOFdigit",  1000);
193   //
194   // Digitization parameters
195   //
196   // (Transfer Functions to be inserted here)
197   //
198   SetMarkerColor(7);
199   SetMarkerStyle(2);
200   SetMarkerSize(0.4);
201
202 // General Geometrical Parameters
203   fNTof    =  18;  // number of sectors
204   fRmax    = 399.0;//cm 
205   fRmin    = 370.0;//cm
206   fZlenC   = 177.5;//cm length of module C
207   fZlenB   = 141.0;//cm length of module B
208   fZlenA   = 106.0;//cm length of module A
209   fZtof    = 371.5;//cm total semi-length of TOF detector
210
211 // Strip Parameters
212   fStripLn = 122.0;//cm  Strip Length
213   fSpace   =   5.5;//cm  Space Beetween the strip and the bottom of the plate 
214   fDeadBndZ=   1.5;//cm  Dead Boundaries of a Strip along Z direction (width)
215   fDeadBndX=   1.0;//cm  Dead Boundaries of a Strip along X direction (length)
216   fXpad    =   2.5;//cm  X size of a pad
217   fZpad    =   3.5;//cm  Z size of a pad
218   fGapA    =   4.; //cm  Gap beetween tilted strip in A-type plate
219   fGapB    =   6.; //cm  Gap beetween tilted strip in B-type plate
220   fOverSpc =  15.3;//cm Space available for sensitive layers in radial direction
221   fNpadX   =  48;  // Number of pads in a strip along the X direction
222   fNpadZ   =   2;  // Number of pads in a strip along the Z direction
223   fPadXStr = fNpadX*fNpadZ; //Number of pads per strip
224   fNStripA = 15; // number of strips in A type module 
225   fNStripB = 19; // number of strips in B type module
226   fNStripC = 20; // number of strips in C type module
227  
228 // Physical performances
229   fTimeRes = 100.;//ps
230   fChrgRes = 100.;//pC
231
232 // DAQ characteristics
233 // cfr. TOF-TDR pag. 105 for Glossary
234 // TARODA : TOF-ALICE Read Out and Data Acquisition system
235   fPadXSector = 8928; // number of pad per sector -with no holes-
236                       // ((15+2*19+2*20)*(48*2))
237   fNRoc       = 14;   // number of Roc (Read Out Controller) (TARODA)
238   fNFec       = 32;   // number of Fec (Front-End electronic Card)
239                       // (TARODA)
240   fNTdc       = 32;   // number of Tdc (Time to Digital Converter)
241   fNPadXRoc   = (Int_t)fPadXSector/fNRoc; // number of pads for each ROC
242   /* fp 25 Sept 2001
243   // Create TOF Folder Structure
244   CreateTOFFolders();
245   */ 
246 }
247
248 //_____________________________________________________________________________
249 void AliTOF::CreateTOFFolders()
250 {
251   // create the ALICE TFolder
252   // create the ALICE TTasks
253   // create the ALICE main TFolder
254   // to be done by AliRun
255
256   TFolder * alice = new TFolder();
257   alice->SetNameTitle("FPAlice", "Alice Folder") ;
258   gROOT->GetListOfBrowsables()->Add(alice) ;
259
260   TFolder * aliceF  = alice->AddFolder("folders", "Alice memory Folder") ;
261   //  make it the owner of the objects that it contains
262   aliceF->SetOwner() ;
263   // geometry folder
264   TFolder * geomF = aliceF->AddFolder("Geometry", "Geometry objects") ;
265   TFolder * aliceT  = alice->AddFolder("tasks", "Alice tasks Folder") ;   
266   //  make it the owner of the objects that it contains
267   aliceT->SetOwner() ;
268
269   TTask * aliceDi = new TTask("(S)Digitizer", "Alice SDigitizer & Digitizer") ;
270   aliceT->Add(aliceDi);
271
272   TTask * aliceRe = new TTask("Reconstructioner", "Alice Reconstructioner") ;
273   aliceT->Add(aliceRe);
274
275   char * tempo = new char[80] ;
276
277   // creates the TOF Digitizer and adds it to alice main (S)Digitizer task
278   sprintf(tempo, "%sDigitizers container",GetName() ) ;
279   fDTask = new TTask(GetName(), tempo);
280   aliceDi->Add(fDTask) ;
281
282   // creates the TOF reconstructioner and adds it to alice main Reconstructioner task
283   sprintf(tempo, "%sReconstructioner container",GetName() ) ;
284   fReTask = new TTask(GetName(), tempo);
285   aliceRe->Add(fReTask) ;
286
287   delete [] tempo ;
288  
289   // creates the TOF geometry  folder
290   geomF->AddFolder("TOF", "Geometry for TOF") ;
291 }
292
293 //_____________________________________________________________________________
294 AliTOF::~AliTOF()
295 {
296   // dtor:
297   // it remove also the alice folder 
298   // and task that TOF creates instead of AliRun
299   /* PH Temporarily commented because of problems
300   TFolder * alice = (TFolder*)gROOT->GetListOfBrowsables()->FindObject("FPAlice") ;
301   delete alice;
302   alice = 0;
303   */
304   if (fHits)
305     {
306       fHits->Delete ();
307       delete fHits;
308       fHits = 0;
309     }
310   if (fDigits)
311     {
312       fDigits->Delete ();
313       delete fDigits;
314       fDigits = 0;
315     }
316   if (fSDigits)
317     {
318       fSDigits->Delete ();
319       delete fSDigits;
320       fSDigits = 0;
321     }
322   if (fReconParticles)
323     {
324       fReconParticles->Delete ();
325       delete fReconParticles;
326       fReconParticles = 0;
327     }
328
329 }
330
331 //_____________________________________________________________________________
332 void AliTOF::AddHit(Int_t track, Int_t *vol, Float_t *hits)
333 {
334   //
335   // Add a TOF hit
336   // new with placement used
337   //
338   TClonesArray &lhits = *fHits;
339   new(lhits[fNhits++]) AliTOFhit(fIshunt, track, vol, hits);
340 }
341  
342 //_____________________________________________________________________________
343 void AliTOF::AddDigit(Int_t *tracks, Int_t *vol, Float_t *digits)
344 {
345   //
346   // Add a TOF digit
347   // new with placement used
348   //
349   TClonesArray &ldigits = *fDigits;
350   new (ldigits[fNdigits++]) AliTOFdigit(tracks, vol, digits);
351 }
352
353 //___________________________________________
354 void AliTOF::AddSDigit(Int_t tracknum, Int_t *vol, Float_t *digits)
355 {
356      
357 //
358 // Add a TOF sdigit
359 //
360         
361   TClonesArray &lSDigits = *fSDigits;   
362   new(lSDigits[fNSDigits++]) AliTOFSDigit(tracknum, vol, digits);
363 }
364
365 //_____________________________________________________________________________
366 void AliTOF::SetTreeAddress ()
367 {
368   // Set branch address for the Hits and Digits Tree.
369   char branchname[30];
370   AliDetector::SetTreeAddress ();
371
372   TBranch *branch;
373   TTree *treeD = gAlice->TreeD ();
374
375
376   if (treeD)
377     {
378       if (fDigits)
379         {
380           branch = treeD->GetBranch (branchname);
381           if (branch)
382             branch->SetAddress (&fDigits);
383         }
384
385     }
386 //  if (fSDigits)
387     //  fSDigits->Clear ();
388
389   if (gAlice->TreeS () && fSDigits)
390     {
391       branch = gAlice->TreeS ()->GetBranch ("TOF");
392       if (branch)
393         branch->SetAddress (&fSDigits);
394     }
395
396   if (gAlice->TreeR() && fReconParticles) 
397     {
398       branch = gAlice->TreeR()->GetBranch("TOF"); 
399       if (branch) branch->SetAddress(&fReconParticles) ;
400     }   
401 }
402
403 //_____________________________________________________________________________
404 void AliTOF::CreateGeometry()
405 {
406   //
407   // Common geometry code 
408   //
409   //Begin_Html
410   /*
411     <img src="picts/AliTOFv23.gif">
412   */
413   //End_Html
414   //
415   const Double_t kPi=TMath::Pi();
416   const Double_t kDegrad=kPi/180.;
417   //
418   Float_t xTof, yTof, wall;
419
420   // frame inbetween TOF modules
421   wall = 4.;//cm
422
423   // Sizes of TOF module with its support etc..
424   xTof = 2.*(fRmin*TMath::Tan(10*kDegrad)-wall/2-.5);
425   yTof = fRmax-fRmin;
426
427 //  TOF module internal definitions 
428   TOFpc(xTof, yTof, fZlenC, fZlenB, fZlenA, fZtof);
429 }
430
431 //_____________________________________________________________________________
432 void AliTOF::DrawModule() const
433 {
434   //
435   // Draw a shaded view of the common part of the TOF geometry
436   //
437
438    cout << " Drawing of AliTOF"<< endl; 
439   // Set everything unseen
440   gMC->Gsatt("*", "seen", -1);
441   // 
442   // Set ALIC mother transparent
443   gMC->Gsatt("ALIC","SEEN",0);
444   //
445   // Set the volumes visible
446   gMC->Gsatt("FTOA","SEEN",1);
447   gMC->Gsatt("FTOB","SEEN",1);
448   gMC->Gsatt("FTOC","SEEN",1);
449   gMC->Gsatt("FLTA","SEEN",1);
450   gMC->Gsatt("FLTB","SEEN",1);
451   gMC->Gsatt("FLTC","SEEN",1);
452   gMC->Gsatt("FSTR","SEEN",1);
453   //
454   gMC->Gdopt("hide", "on");
455   gMC->Gdopt("shad", "on");
456   gMC->Gsatt("*", "fill", 7);
457   gMC->SetClipBox(".");
458   gMC->SetClipBox("*", 0, 1000, -1000, 1000, -1000, 1000);
459   gMC->DefaultRange();
460   gMC->Gdraw("alic", 40, 30, 0, 12, 9.5, .02, .02);
461   gMC->Gdhead(1111, "Time Of Flight");
462   gMC->Gdman(18, 4, "MAN");
463   gMC->Gdopt("hide","off");
464 }
465
466 //_____________________________________________________________________________
467 void AliTOF::CreateMaterials()
468 {
469   //
470   // Defines TOF materials for all versions
471   // Authors :   Maxim Martemianov, Boris Zagreev (ITEP) 
472   //            18/09/98 
473   // Revision: F. Pierella 5-3-2001
474   // Bologna University
475   //
476   Int_t   isxfld = gAlice->Field()->Integ();
477   Float_t sxmgmx = gAlice->Field()->Max();
478   //
479   //--- Quartz (SiO2) 
480   Float_t   aq[2] = { 28.0855,15.9994 };
481   Float_t   zq[2] = { 14.,8. };
482   Float_t   wq[2] = { 1.,2. };
483   Float_t   dq = 2.20;
484   Int_t nq = -2;
485   // --- Freon C2F4H2 (TOF-TDR pagg.)
486   // Geant Manual CONS110-1, pag. 43 (Geant, Detector Description and Simulation Tool)
487   Float_t afre[3]  = {12.011,18.998,1.007};
488   Float_t zfre[3]  = { 6., 9., 1.}; 
489   Float_t wfre[3]  = { 2., 4., 2.};
490   Float_t densfre  = 0.00375;   
491 // http://www.fi.infn.it/sezione/prevprot/gas/freon.html
492   Int_t nfre = -3; 
493 /*
494   //-- Isobutane quencher C4H10 (5% in the sensitive mixture)
495   Float_t aiso[2]  = {12.011,1.007};
496   Float_t ziso[2]  = { 6.,  1.};
497   Float_t wiso[2]  = { 4., 10.};
498   Float_t densiso  = .......;  // (g/cm3) density
499   Int_t nfre = -2; // < 0 i.e. proportion by number of atoms of each kind
500   //-- SF6 (5% in the sensitive mixture)
501   Float_t asf[3]  = {32.066,18.998};
502   Float_t zsf[3]  = { 16., 9.};
503   Float_t wsf[3]  = {  1., 6.}; 
504   Float_t denssf  = .....;   // (g/cm3) density
505   Int_t nfre = -2; // < 0 i.e. proportion by number of atoms of each kind
506 */
507   // --- CO2 
508   Float_t ac[2]   = {12.,16.};
509   Float_t zc[2]   = { 6., 8.};
510   Float_t wc[2]   = { 1., 2.};
511   Float_t dc = .001977;
512   Int_t nc = -2;
513    // For mylar (C5H4O2) 
514   Float_t amy[3] = { 12., 1., 16. };
515   Float_t zmy[3] = {  6., 1.,  8. };
516   Float_t wmy[3] = {  5., 4.,  2. };
517   Float_t dmy    = 1.39;
518   Int_t nmy = -3;
519  // For polyethilene (CH2) - honeycomb -
520   Float_t ape[2] = { 12., 1. };
521   Float_t zpe[2] = {  6., 1. };
522   Float_t wpe[2] = {  1., 2. };
523   Float_t dpe    = 0.935*0.479; //To have 1%X0 for 1cm as for honeycomb
524   Int_t npe = -2;
525   // --- G10 
526   Float_t ag10[4] = { 12.,1.,16.,28. };
527   Float_t zg10[4] = {  6.,1., 8.,14. };
528   Float_t wmatg10[4] = { .259,.288,.248,.205 };
529   Float_t densg10  = 1.7;
530   Int_t nlmatg10 = -4;
531   // --- DME 
532   Float_t adme[5] = { 12.,1.,16.,19.,79. };
533   Float_t zdme[5] = {  6.,1., 8., 9.,35. };
534   Float_t wmatdme[5] = { .4056,.0961,.2562,.1014,.1407 };
535   Float_t densdme  = .00205;
536   Int_t nlmatdme = 5;
537   // ---- ALUMINA (AL203) 
538   Float_t aal[2] = { 27.,16.};
539   Float_t zal[2] = { 13., 8.};
540   Float_t wmatal[2] = { 2.,3. };
541   Float_t densal  = 2.3;
542   Int_t nlmatal = -2;
543   // -- Water
544   Float_t awa[2] = {  1., 16. };
545   Float_t zwa[2] = {  1.,  8. };
546   Float_t wwa[2] = {  2.,  1. };
547   Float_t dwa    = 1.0;
548   Int_t nwa = -2;
549   //
550   //AliMaterial(0, "Vacuum$", 1e-16, 1e-16, 1e-16, 1e16, 1e16);
551   AliMaterial( 1, "Air$",14.61,7.3,0.001205,30423.24,67500.);
552   AliMaterial( 2, "Cu $",  63.54, 29.0, 8.96, 1.43, 14.8);
553   AliMaterial( 3, "C  $",  12.01,  6.0, 2.265,18.8, 74.4);
554   AliMixture ( 4, "Polyethilene$", ape, zpe, dpe, npe, wpe);
555   AliMixture ( 5, "G10$", ag10, zg10, densg10, nlmatg10, wmatg10);
556   AliMixture ( 6, "DME ", adme, zdme, densdme, nlmatdme, wmatdme);
557   AliMixture ( 7, "CO2$", ac, zc, dc, nc, wc);
558   AliMixture ( 8, "ALUMINA$", aal, zal, densal, nlmatal, wmatal);
559   AliMaterial( 9, "Al $", 26.98, 13., 2.7, 8.9, 37.2);
560   AliMaterial(10, "C-TRD$", 12.01, 6., 2.265*18.8/69.282*15./100, 18.8, 74.4); // for 15%
561   AliMixture (11, "Mylar$",  amy, zmy, dmy, nmy, wmy);
562   AliMixture (12, "Freon$",  afre, zfre, densfre, nfre, wfre);
563   AliMixture (13, "Quartz$", aq, zq, dq, nq, wq);
564   AliMixture (14, "Water$",  awa, zwa, dwa, nwa, wwa);
565
566   Float_t epsil, stmin, deemax, stemax;
567  
568   //   Previous data
569   //       EPSIL  =  0.1   ! Tracking precision, 
570   //       STEMAX = 0.1      ! Maximum displacement for multiple scattering
571   //       DEEMAX = 0.1    ! Maximum fractional energy loss, DLS 
572   //       STMIN  = 0.1 
573   //
574   //   New data  
575   epsil  = .001;  // Tracking precision,
576   stemax = -1.;   // Maximum displacement for multiple scattering
577   deemax = -.3;   // Maximum fractional energy loss, DLS
578   stmin  = -.8;
579
580   AliMedium( 1, "Air$"  ,  1, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
581   AliMedium( 2, "Cu $"  ,  2, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
582   AliMedium( 3, "C  $"  ,  3, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
583   AliMedium( 4, "Pol$"  ,  4, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
584   AliMedium( 5, "G10$"  ,  5, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
585   AliMedium( 6, "DME$"  ,  6, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
586   AliMedium( 7, "CO2$"  ,  7, 0, isxfld, sxmgmx, 10., -.01, -.1, .01, -.01);
587   AliMedium( 8,"ALUMINA$", 8, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
588   AliMedium( 9,"Al Frame$",9, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
589   AliMedium(10, "DME-S$",  6, 1, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
590   AliMedium(11, "C-TRD$", 10, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
591   AliMedium(12, "Myl$"  , 11, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
592   AliMedium(13, "Fre$"  , 12, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
593   AliMedium(14, "Fre-S$", 12, 1, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
594   AliMedium(15, "Glass$", 13, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
595   AliMedium(16, "Water$", 14, 0, isxfld, sxmgmx, 10., stemax, deemax, epsil, stmin);
596 }
597
598 //_____________________________________________________________________________
599 Int_t AliTOF::DistancetoPrimitive(Int_t , Int_t ) const
600 {
601   //
602   // Returns distance from mouse pointer to detector, default version
603   //
604   return 9999;
605 }
606
607 //___________________________________________
608 void AliTOF::ResetHits ()
609 {
610   // Reset number of clusters and the cluster array for this detector
611   AliDetector::ResetHits ();
612 }
613
614 //____________________________________________
615 void AliTOF::ResetDigits ()
616 {
617   //
618   // Reset number of digits and the digits array for this detector
619   AliDetector::ResetDigits ();
620   //
621
622 //_____________________________________________________________________________
623 void AliTOF::Init()
624 {
625   //
626   // Initialise TOF detector after it has been built
627   //
628   // Set id of TOF sensitive volume
629   if (IsVersion() !=0) fIdSens=gMC->VolId("FPAD");
630   //
631 }
632
633 //____________________________________________________________________________
634 void AliTOF::MakeBranch(Option_t* option, const char *file)
635 {
636  //
637  // Initializes the Branches of the TOF inside the 
638  // trees written for each event. 
639  // AliDetector::MakeBranch initializes just the 
640  // Branch inside TreeH. Here we add the branches in 
641  // TreeD, TreeS and TreeR.
642  //
643   AliDetector::MakeBranch(option,file);
644
645   Int_t buffersize = 4000;
646   Char_t branchname[10];
647   sprintf(branchname,"%s",GetName());
648   
649   const char *oD = strstr(option,"D");
650   const char *oS = strstr(option,"S");
651   const char *oR = strstr(option,"R");
652
653   if (oD)
654   //
655   // one branch for TOF digits
656   // 
657
658
659   if (fDigits && gAlice->TreeD() && oD){
660              MakeBranchInTree(gAlice->TreeD(), 
661                               branchname, &fDigits,buffersize, file) ;
662   }
663
664   if (oS)
665   //
666   // one branch for TOF sdigits
667   //
668
669
670   if (fSDigits && gAlice->TreeS() && oS){
671              MakeBranchInTree(gAlice->TreeS(),
672                               branchname, &fSDigits,buffersize, file) ;
673   }
674
675   if (oR)
676   //
677   // one branch for TOF reconstructed particles
678   //
679
680
681   if (fReconParticles && gAlice->TreeR() && oR){
682              MakeBranchInTree(gAlice->TreeR(),
683                               branchname, &fReconParticles,buffersize, file) ;
684   }
685
686 }
687
688 //____________________________________________________________________________
689 void AliTOF::Makehits(Bool_t hits) 
690 {
691 // default argument used, see AliTOF.h
692 // Enable/Disable the writing of the TOF-hits branch 
693 // on TreeH
694 // by default : enabled for TOFv1, v2, v3, v4
695 //              disabled for TOFv0
696 // 
697    if (hits &&  (IsVersion()!=0))
698       fIdSens = gMC->VolId("FPAD");
699    else
700       cout << "Option for writing the TOF-hits branch on TreeH: disabled" << endl;
701 }
702
703 //____________________________________________________________________________
704 void AliTOF::FinishEvent()
705 {
706 // do nothing
707 }
708
709 //___________________________________________
710 void AliTOF::SDigits2Digits()
711 {
712 //
713 // Generate digits performing merging
714 //
715   /*
716     int nparticles = gAlice->GetNtrack();
717     cout << "Particles       :" <<nparticles<<endl;
718     if (nparticles > 0 ) {
719       
720       AliTOF::Hits2Digits();
721       
722     }
723   */
724   cout<<"AliTOF::SDigits2Digits"<<endl; 
725     if (fMerger) {
726       fMerger->Init();
727       cout<<"AliTOF::SDigits2Digits Init"<<endl; 
728       fMerger->Digitise();
729       cout<<"AliTOF::SDigits2Digits Digitise() "<<endl; 
730      }
731 }
732
733 //---------------------------------------------------------------------
734 void   AliTOF::SetMerger(AliTOFMerger* merger)
735 {
736 // Set pointer to merger
737     fMerger = merger;
738 }
739
740 //---------------------------------------------------------------------
741 AliTOFMerger*  AliTOF::Merger()
742 {
743 // Return pointer to merger
744     return fMerger;
745 }
746
747
748 //---------------------------------------------------------------------
749
750 void AliTOF::Hits2SDigits()
751 {
752 //
753 // Use the TOF SDigitizer to make TOF SDigits
754 //
755 //
756   //#ifdef DEBUG
757   cout<<"ALiTOF::Hits2SDigits> start...\n";
758   //#endif
759   
760   char * fileSDigits = 0 ;
761   char * fileHeader = 0;
762   AliTOFSDigitizer * sd = new AliTOFSDigitizer(fileHeader,fileSDigits) ;
763
764   sd->Exec("") ;
765   sd->Print("");
766
767   delete sd ;
768   
769 }
770 //___________________________________________________________________________
771 Bool_t AliTOF::CheckOverlap(Int_t* vol, Float_t* digit,Int_t Track)
772 {
773 //
774 // Checks if 2 or more hits belong to the same pad.
775 // In this case the data assigned to the digit object
776 // are the ones of the first hit in order of Time.
777 // 2 hits from the same track on the same pad are collected.
778 // Called only by Hits2SDigits.
779 // This procedure has to be optimized in the next TOF release.
780 //
781
782         Bool_t overlap = kFALSE;
783         Int_t  vol2[5];
784
785         for (Int_t ndig=0; ndig<fSDigits->GetEntries(); ndig++){
786            AliTOFdigit* currentDigit = (AliTOFdigit*)(fSDigits->UncheckedAt(ndig));
787            currentDigit->GetLocation(vol2);
788            Bool_t idem= kTRUE;
789            // check on digit volume
790            for (Int_t i=0;i<=4;i++){
791                if (!idem) break;
792                if (vol[i]!=vol2[i]) idem=kFALSE;}
793
794            if (idem){  // same pad fired
795               Float_t tdc2 = digit[0];
796               Float_t tdc1 = currentDigit->GetTdc();
797
798               // we separate two digits on the same pad if
799               // they are separated in time by at least 25 ns
800               // remember that tdc time is given in ps
801
802               if (TMath::Abs(tdc1-tdc2)<25000){
803                   // in case of overlap we take the earliest
804                   if (tdc1>tdc2){
805                    currentDigit->SetTdc(tdc2); 
806                    currentDigit->SetAdc(digit[1]);
807                   }
808                   else {
809                    currentDigit->SetTdc(tdc1);
810                    currentDigit->SetAdc(digit[1]);
811                   }
812                   currentDigit->AddTrack(Track); // add track number in the track array
813                   overlap = kTRUE;
814                   return overlap;
815               } else 
816                 overlap= kFALSE;
817
818            } // close if (idem) -> two digits on the same TOF pad
819
820         } // end loop on existing sdigits
821
822         return overlap;
823 }
824
825
826 //____________________________________________________________________________
827 void AliTOF::Digits2Raw(Int_t evNumber)
828 {
829 //
830 // Starting from digits, writes the 
831 // Raw Data objects, i.e. a 
832 // TClonesArray of 18 AliTOFRawSector objects
833 //
834
835   TTree* tD;
836
837   // do nothing if no particles
838   Int_t nparticles = gAlice->GetEvent(evNumber); 
839   if (nparticles <= 0) return;
840
841   tD = gAlice->TreeD();
842   
843   TClonesArray* tofdigits = this->Digits();
844   Int_t ndigits = tofdigits->GetEntriesFast();
845
846   TClonesArray* rawsectors = new TClonesArray("AliTOFRawSector",fNTof+2); 
847
848   for (Int_t isect=1;isect<=fNTof;isect++){
849      AliTOFRawSector* currentSector = (AliTOFRawSector*)rawsectors->UncheckedAt(isect);
850      TClonesArray* rocData = (TClonesArray*)currentSector->GetRocData();
851
852      for (Int_t digit=0; digit<ndigits; digit++){
853         AliTOFdigit* currentDigit = (AliTOFdigit*)tofdigits->UncheckedAt(digit);
854         Int_t sector = currentDigit->GetSector();
855         if (sector==isect){
856             Int_t   pad    = currentDigit -> GetTotPad();
857             Int_t   roc    = (Int_t)(pad/fNPadXRoc)-1;
858             if (roc>=fNRoc) printf("Wrong n. of ROC ! Roc = %i",roc);
859             Int_t   padRoc = (Int_t) pad%fNPadXRoc;
860             Int_t   fec    = (Int_t)(padRoc/fNFec)-1;
861             Int_t   tdc    = (Int_t)(padRoc%fNFec)-1;
862             Float_t time   = currentDigit->GetTdc();
863             Float_t charge = currentDigit->GetAdc();
864             AliTOFRoc* currentROC = (AliTOFRoc*)rocData->UncheckedAt(roc);
865             Int_t error    = 0;
866             currentROC->AddItem(fec, tdc, error, charge, time);
867         } // close if (sector==isect) i.e. end loop on digits for the current sector
868      } // end loop on TOF digits
869      
870      UInt_t totSize=16,rocSize=0;
871      UInt_t rocHead[14],rocChek[14];
872      UInt_t globalCheckSum=0;
873
874      for (UInt_t iRoc = 1; iRoc<(UInt_t)fNRoc; iRoc++){
875         AliTOFRoc* currentRoc = (AliTOFRoc*)rocData->UncheckedAt(iRoc); 
876         rocSize  = currentRoc->GetItems()*2+1;
877         totSize += rocSize*4;
878         if (rocSize>=TMath::Power(2,16)) rocSize=0;
879         rocHead[iRoc]   = iRoc<<28;
880         rocHead[iRoc]  += rocSize;
881         rocChek[iRoc]   = currentRoc->GetCheckSum();
882         Int_t headCheck = currentRoc->BitCount(rocHead[iRoc]);
883         globalCheckSum += headCheck;
884         globalCheckSum += rocChek[iRoc];
885      }
886      
887      AliTOFRoc* dummyRoc = new AliTOFRoc();
888      totSize *= 4;
889      if (totSize>=TMath::Power(2,24)) totSize=0;
890      UInt_t header = totSize;
891      UInt_t sectId = ((UInt_t)isect)<<24;
892      header += sectId;
893      globalCheckSum += dummyRoc->BitCount(header);
894      currentSector->SetGlobalCS(globalCheckSum);
895      currentSector->SetHeader(header);
896   }  
897 }
898  
899 //____________________________________________________________________________
900 void AliTOF::Raw2Digits(Int_t evNumber)
901 {
902 //
903 //  Converts Raw Data objects into digits objects.
904 //  We schematize the raw data with a 
905 //  TClonesArray of 18 AliTOFRawSector objects
906 //
907
908   TTree    *tD;
909   Int_t    vol[5];
910   Int_t    tracks[3];
911   Float_t  digit[2];
912  
913   tracks[0]=0;
914   tracks[1]=0;
915   tracks[2]=0;
916  
917   Int_t nparticles = gAlice->GetEvent(evNumber); 
918   if (nparticles <= 0) return;
919
920   tD = gAlice->TreeD();
921   
922   TClonesArray* rawsectors = new TClonesArray("AliTOFRawSector",fNTof+2);
923   
924   for(Int_t nSec=1; nSec<=fNTof; nSec++){
925      AliTOFRawSector* currentSector = (AliTOFRawSector*)rawsectors->UncheckedAt(nSec);
926      TClonesArray* rocData = (TClonesArray*)currentSector->GetRocData();
927      for(Int_t nRoc=1; nRoc<=14; nRoc++){
928         AliTOFRoc* currentRoc = (AliTOFRoc*)rocData->UncheckedAt(nRoc);
929         Int_t currentItems = currentRoc->GetItems();
930         for(Int_t item=1; item<currentItems; item++){ 
931            Int_t nPad = currentRoc->GetTotPad(item);        
932            vol[0] = nSec;
933            Int_t nStrip = (Int_t)(nPad/fPadXStr)+1;
934            Int_t nPlate = 5;
935            if (nStrip<=fNStripC+2*fNStripB+fNStripA) nPlate = 4;
936            if (nStrip<=fNStripC+fNStripB+fNStripA)   nPlate = 3;
937            if (nStrip<=fNStripC+fNStripB)            nPlate = 2;
938            if (nStrip<=fNStripC)                     nPlate=1;
939            vol[1] = nPlate;
940            switch (nPlate){
941            case 1: break;
942            case 2: nStrip -= (fNStripC);
943                    break;
944            case 3: nStrip -= (fNStripC+fNStripB);
945                    break;
946            case 4: nStrip -= (fNStripC+fNStripB+fNStripA);
947                    break;
948            case 5: nStrip -= (fNStripC+2*fNStripB+fNStripA);
949                    break;
950            }
951            vol[2] = nStrip;
952            Int_t pad = nPad%fPadXStr;
953            if (pad==0) pad=fPadXStr;
954            Int_t nPadX=0, nPadZ=0;
955            (pad>fNpadX)? nPadX -= fNpadX : nPadX = pad ;
956            vol[3] = nPadX;
957            (pad>fNpadX)? nPadZ = 2 : nPadZ = 1 ;
958            vol[4] = nPadZ;
959            UInt_t error=0;
960            Float_t tdc = currentRoc->GetTime(item,error);
961            if (!error) digit[0]=tdc;
962            digit[1] = currentRoc->GetCharge(item);
963            AddDigit(tracks,vol,digit);
964         }
965      }
966   }
967   tD->Fill();
968   tD->Write(0,TObject::kOverwrite);
969
970