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