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