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