]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMD.cxx
b80c7ee9ee4d9d926209c4d992481597126218fc
[u/mrichter/AliRoot.git] / FMD / AliFMD.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 /* $Id$ */
17
18 //____________________________________________________________________
19 //                                                                          
20 // Forward Multiplicity Detector based on Silicon wafers. This class
21 // contains the base procedures for the Forward Multiplicity detector
22 // Detector consists of 3 sub-detectors FMD1, FMD2, and FMD3, each of
23 // which has 1 or 2 rings of silicon sensors. 
24 //                                                       
25 // This is the base class for all FMD manager classes. 
26 //                    
27 // The actual code is done by various separate classes.   Below is
28 // diagram showing the relationship between the various FMD classes
29 // that handles the simulation
30 //
31 //
32 //       +----------+   +----------+   
33 //       | AliFMDv1 |   | AliFMDv0 |   
34 //       +----------+   +----------+   
35 //            |              |                    +-----------------+
36 //       +----+--------------+                 +--| AliFMDDigitizer |
37 //       |                                     |  +-----------------+
38 //       |           +---------------------+   |
39 //       |        +--| AliFMDBaseDigitizer |<--+
40 //       V     1  |  +---------------------+   |
41 //  +--------+<>--+                            |  +------------------+
42 //  | AliFMD |                                 +--| AliFMDSDigitizer |    
43 //  +--------+<>--+                               +------------------+       
44 //             1  |  +-----------------+ 
45 //                +--| AliFMDSimulator |
46 //                   +-----------------+
47 //                            ^              
48 //                            |
49 //              +-------------+-------------+
50 //              |                           |         
51 //     +--------------------+   +-------------------+
52 //     | AliFMDGeoSimulator |   | AliFMDG3Simulator | 
53 //     +--------------------+   +-------------------+
54 //               ^                         ^
55 //               |                         |
56 //  +-----------------------+   +----------------------+
57 //  | AliFMDGeoOldSimulator |   | AliFMDG3OldSimulator |
58 //  +-----------------------+   +----------------------+
59 //      
60 //
61 // *  AliFMD 
62 //    This defines the interface for the various parts of AliROOT that
63 //    uses the FMD, like AliFMDSimulator, AliFMDDigitizer, 
64 //    AliFMDReconstructor, and so on. 
65 //
66 // *  AliFMDv0
67 //    This is a concrete implementation of the AliFMD interface. 
68 //    It is the responsibility of this class to create the FMD
69 //    geometry.
70 //
71 // *  AliFMDv1 
72 //    This is a concrete implementation of the AliFMD interface. 
73 //    It is the responsibility of this class to create the FMD
74 //    geometry, process hits in the FMD, and serve hits and digits to
75 //    the various clients. 
76 //  
77 // *  AliFMDSimulator
78 //    This is the base class for the FMD simulation tasks.   The
79 //    simulator tasks are responsible to implment the geoemtry, and
80 //    process hits. 
81 //                                                                          
82 // *  AliFMDGeoSimulator
83 //    This is a concrete implementation of the AliFMDSimulator that
84 //    uses the TGeo classes directly only. 
85 //
86 // *  AliFMDG3Simulator
87 //    This is a concrete implementation of the AliFMDSimulator that
88 //    uses the TVirtualMC interface with GEANT 3.21-like messages.
89 //
90
91 // These files are not in the same directory, so there's no reason to
92 // ask the preprocessor to search in the current directory for these
93 // files by including them with `#include "..."' 
94 #include <math.h>               // __CMATH__
95 #include <TClonesArray.h>       // ROOT_TClonesArray
96 #include <TGeometry.h>          // ROOT_TGeomtry
97 #include <TNode.h>              // ROOT_TNode
98 #include <TXTRU.h>              // ROOT_TXTRU
99 #include <TRotMatrix.h>         // ROOT_TRotMatrix
100 #include <TTUBE.h>              // ROOT_TTUBE
101 #include <TTree.h>              // ROOT_TTree
102 #include <TBrowser.h>           // ROOT_TBrowser
103 #include <TMath.h>              // ROOT_TMath
104 #include <TVirtualMC.h>         // ROOT_TVirtualMC
105
106 #include <AliRunDigitizer.h>    // ALIRUNDIGITIZER_H
107 #include <AliLoader.h>          // ALILOADER_H
108 #include <AliRun.h>             // ALIRUN_H
109 #include <AliMC.h>              // ALIMC_H
110 #include <AliLog.h>             // ALILOG_H
111 #include "AliFMD.h"             // ALIFMD_H
112 #include "AliFMDDigit.h"        // ALIFMDDIGIG_H
113 #include "AliFMDHit.h"          // ALIFMDHIT_H
114 #include "AliFMDGeometry.h"     // ALIFMDGEOMETRY_H
115 #include "AliFMDDetector.h"     // ALIFMDDETECTOR_H
116 #include "AliFMDRing.h"         // ALIFMDRING_H
117 #include "AliFMDDigitizer.h"    // ALIFMDDIGITIZER_H
118 #include "AliFMDSimulator.h"    // ALIFMDSIMULATOR_H
119 #include "AliFMDG3Simulator.h"  // ALIFMDG3SIMULATOR_H
120 #include "AliFMDGeoSimulator.h" // ALIFMDGEOSIMULATOR_H
121 #include "AliFMDG3OldSimulator.h"       // ALIFMDG3OLDSIMULATOR_H
122 #include "AliFMDGeoOldSimulator.h"      // ALIFMDGEOOLDSIMULATOR_H
123 #include "AliFMDRawWriter.h"    // ALIFMDRAWWRITER_H
124
125 //____________________________________________________________________
126 ClassImp(AliFMD)
127 #if 0
128   ; // This is to keep Emacs from indenting the next line 
129 #endif 
130
131 //____________________________________________________________________
132 AliFMD::AliFMD()
133   : AliDetector(),
134     fSDigits(0), 
135     fNsdigits(0),
136     fDetailed(kTRUE),
137     fSimulator(0)
138 {
139   //
140   // Default constructor for class AliFMD
141   //
142   AliDebug(10, "\tDefault CTOR");
143   fHits        = 0;
144   fDigits      = 0;
145   fIshunt      = 0;
146   fUseOld      = kFALSE;
147   fUseDivided  = kFALSE;
148   fUseAssembly = kFALSE;
149   fUseGeo      = kTRUE;
150 }
151
152 //____________________________________________________________________
153 AliFMD::AliFMD(const AliFMD& other)
154   : AliDetector(other),
155     fSDigits(other.fSDigits), 
156     fNsdigits(other.fNsdigits),
157     fDetailed(other.fDetailed),
158     fSimulator(other.fSimulator)
159 {
160   // Copy constructor 
161   fUseOld      = other.fUseOld;
162   fUseDivided  = other.fUseDivided;
163   fUseAssembly = other.fUseAssembly;
164   fUseGeo      = other.fUseGeo;
165 }
166
167 //____________________________________________________________________
168 AliFMD::AliFMD(const char *name, const char *title)
169   : AliDetector (name, title),
170     fSDigits(0),
171     fNsdigits(0),
172     fDetailed(kTRUE),
173     fSimulator(0)
174 {
175   //
176   // Standard constructor for Forward Multiplicity Detector
177   //
178   AliDebug(10, "\tStandard CTOR");
179   fUseOld      = kFALSE;
180   fUseDivided  = kFALSE;
181   fUseAssembly = kFALSE;
182   fUseGeo      = kTRUE;
183
184   // Initialise Hit array
185   HitsArray();
186   gAlice->GetMCApp()->AddHitList(fHits);
187
188   // (S)Digits for the detectors disk
189   DigitsArray();
190   SDigitsArray();
191   
192   // CHC: What is this?
193   fIshunt = 0;
194   SetMarkerColor(kRed);
195   SetLineColor(kYellow);
196 }
197
198 //____________________________________________________________________
199 AliFMD::~AliFMD ()
200 {
201   // Destructor for base class AliFMD
202   if (fHits) {
203     fHits->Delete();
204     delete fHits;
205     fHits = 0;
206   }
207   if (fDigits) {
208     fDigits->Delete();
209     delete fDigits;
210     fDigits = 0;
211   }
212   if (fSDigits) {
213     fSDigits->Delete();
214     delete fSDigits;
215     fSDigits = 0;
216   }
217 }
218
219 //____________________________________________________________________
220 AliFMD&
221 AliFMD::operator=(const AliFMD& other)
222 {
223   AliDetector::operator=(other);
224   fSDigits              = other.fSDigits; 
225   fNsdigits             = other.fNsdigits;
226   fDetailed             = other.fDetailed;
227   fSimulator            = other.fSimulator;
228   
229   return *this;
230 }
231
232 //====================================================================
233 //
234 // GEometry ANd Traking
235 //
236 //____________________________________________________________________
237 void 
238 AliFMD::CreateGeometry()
239 {
240   //
241   // Create the geometry of Forward Multiplicity Detector.  The actual
242   // construction of the geometry is delegated to the class AliFMDRing
243   // and AliFMDSubDetector and the relevant derived classes. 
244   //
245   // The flow of this member function is:
246   //
247   //   FOR rings fInner and fOuter DO  
248   //     AliFMDRing::Init();
249   //   END FOR
250   // 
251   //   Set up hybrud card support (leg) volume shapes  
252   // 
253   //   FOR rings fInner and fOuter DO  
254   //     AliFMDRing::SetupGeometry();
255   //   END FOR
256   // 
257   //   FOR subdetectors fFMD1, fFMD2, and fFMD3 DO 
258   //     AliFMDSubDetector::SetupGeomtry();
259   //   END FOR
260   // 
261   //   FOR subdetectors fFMD1, fFMD2, and fFMD3 DO 
262   //     AliFMDSubDetector::Geomtry();
263   //   END FOR
264   //
265   if (!fSimulator) {
266     AliFatal("Simulator object not made yet!");
267     return;
268   }
269   fSimulator->DefineGeometry();
270 }    
271
272 //____________________________________________________________________
273 void AliFMD::CreateMaterials() 
274 {
275   // Register various materials and tracking mediums with the
276   // backend.   
277   // 
278   AliDebug(10, "\tCreating materials");
279
280   if (fSimulator) {
281     AliFatal("Simulator object already instantised!");
282     return;
283   }
284   AliFMDGeometry* geometry = AliFMDGeometry::Instance();
285   geometry->Init();
286   TVirtualMC* mc = TVirtualMC::GetMC();
287   Bool_t geo = mc->IsRootGeometrySupported();
288   if (geo && fUseGeo) {
289     if (fUseOld) 
290       fSimulator = new AliFMDGeoOldSimulator(this, fDetailed);
291     else 
292       fSimulator = new AliFMDGeoSimulator(this, fDetailed);
293   }
294   else {
295     if (fUseOld) 
296       fSimulator = new AliFMDG3OldSimulator(this, fDetailed);
297     else    
298       fSimulator = new AliFMDG3Simulator(this, fDetailed);
299   }
300   AliDebug(1, Form("using a %s as simulation backend", 
301                    fSimulator->IsA()->GetName()));
302   fSimulator->UseDivided(fUseDivided);
303   fSimulator->UseAssembly(fUseAssembly);
304   fSimulator->DefineMaterials();
305 }
306
307 //____________________________________________________________________
308 void  
309 AliFMD::Init()
310 {
311   //
312   // Initialis the FMD after it has been built
313   Int_t i;
314   //
315   if (AliLog::GetGlobalDebugLevel()) {
316     cout << "\n" << ClassName() << ": " << flush;
317     for (i = 0; i < 35; i++) cout << "*";
318     cout << " FMD_INIT ";
319     for (i = 0; i < 35; i++) cout << "*";
320     cout << "\n" << ClassName() << ": " << flush;
321     //
322     // Here the FMD initialisation code (if any!)
323     for (i = 0; i < 80; i++) cout << "*";
324     cout << endl;
325   }
326   //
327   //
328 }
329
330 //====================================================================
331 //
332 // Graphics and event display
333 //
334 //____________________________________________________________________
335 void 
336 AliFMD::BuildGeometry()
337 {
338   //
339   // Build simple ROOT TNode geometry for event display
340   //
341   // Build a simplified geometry of the FMD used for event display  
342   // 
343   // The actual building of the TNodes is done by
344   // AliFMDSubDetector::SimpleGeometry. 
345   AliDebug(10, "\tCreating a simplified geometry");
346
347   AliFMDGeometry* fmd = AliFMDGeometry::Instance();
348   
349   static TXTRU*     innerShape = 0;
350   static TXTRU*     outerShape = 0;
351   static TObjArray* innerRot   = 0;
352   static TObjArray* outerRot   = 0;
353
354   if (!innerShape || !outerShape) {
355     // Make the shapes for the modules 
356     for (Int_t i = 0; i < 2; i++) {
357       AliFMDRing* r = 0;
358       switch (i) {
359       case 0: r = fmd->GetRing('I'); break;
360       case 1: r = fmd->GetRing('O'); break;
361       }
362       if (!r) {
363         AliError(Form("no ring found for i=%d", i));
364         return;
365       }
366       Double_t    siThick  = r->GetSiThickness();
367       const Int_t nv       = r->GetNVerticies();
368       Double_t    theta    = r->GetTheta();
369       Int_t       nmod     = r->GetNModules();
370       
371       TXTRU* shape = new TXTRU(r->GetName(), r->GetTitle(), "void", nv, 2);
372       for (Int_t j = 0; j < nv; j++) {
373         TVector2* vv = r->GetVertex(nv - 1 - j);
374         shape->DefineVertex(j, vv->X(), vv->Y());
375       }
376       shape->DefineSection(0, -siThick / 2, 1, 0, 0);
377       shape->DefineSection(1, +siThick / 2, 1, 0, 0);
378       shape->SetLineColor(GetLineColor());
379       
380       TObjArray* rots = new TObjArray(nmod);
381       for (Int_t j = 0; j < nmod; j++) {
382         Double_t th = (j + .5) * theta * 2;
383         TString name(Form("FMD_ring_%c_rot_%02d", r->GetId(), j));
384         TString title(Form("FMD Ring %c Rotation # %d", r->GetId(), j));
385         TRotMatrix* rot = new TRotMatrix(name.Data(), title.Data(),
386                                          90, th, 90, fmod(90+th,360), 0, 0);
387         rots->AddAt(rot, j);
388       }
389       
390       switch (r->GetId()) {
391       case 'i':
392       case 'I': innerShape = shape; innerRot = rots; break;
393       case 'o':
394       case 'O': outerShape = shape; outerRot = rots; break;
395       }
396     }
397   }
398   
399   TNode* top = gAlice->GetGeometry()->GetNode("alice");
400   
401   for (Int_t i = 1; i <= 3; i++) {
402     AliFMDDetector* det = fmd->GetDetector(i);
403     if (!det) {
404       Warning("BuildGeometry", "FMD%d seems to be disabled", i);
405       continue;
406     }
407     Double_t w  = 0;
408     Double_t rh = det->GetRing('I')->GetHighR();
409     Char_t   id = 'I';
410     if (det->GetRing('O')) {
411       w  = TMath::Abs(det->GetRingZ('O') - det->GetRingZ('I'));
412       id = (TMath::Abs(det->GetRingZ('O')) 
413             > TMath::Abs(det->GetRingZ('I')) ? 'O' : 'I');
414       rh = det->GetRing('O')->GetHighR();
415     }
416     w += (det->GetRing(id)->GetModuleSpacing() +
417           det->GetRing(id)->GetSiThickness());
418     TShape* shape = new TTUBE(det->GetName(), det->GetTitle(), "void",
419                               det->GetRing('I')->GetLowR(), rh, w / 2);
420     Double_t z = (det->GetRingZ('I') - w / 2);
421     if (z > 0) z += det->GetRing(id)->GetModuleSpacing();
422     top->cd();
423     TNode* node = new TNode(det->GetName(), det->GetTitle(), shape, 
424                             0, 0, z, 0);
425     fNodes->Add(node);
426     
427     for (Int_t j = 0; j < 2; j++) {
428       AliFMDRing* r      = 0;
429       TShape*     rshape = 0;
430       TObjArray*  rots   = 0;
431       switch (j) {
432       case 0: 
433         r = det->GetRing('I'); rshape = innerShape; rots = innerRot; break;
434       case 1: 
435         r = det->GetRing('O'); rshape = outerShape; rots = outerRot; break;
436       }
437       if (!r) continue;
438       
439       Double_t    siThick  = r->GetSiThickness();
440       Int_t       nmod     = r->GetNModules();
441       Double_t    modspace = r->GetModuleSpacing();
442       Double_t    rz       = - (z - det->GetRingZ(r->GetId()));
443       
444       for (Int_t k = 0; k < nmod; k++) {
445         node->cd();
446         Double_t    offz    = (k % 2 == 1 ? modspace : 0);
447         TRotMatrix* rot     = static_cast<TRotMatrix*>(rots->At(k));
448         TString name(Form("%s%c_module_%02d", det->GetName(), r->GetId(),k));
449         TString title(Form("%s%c Module %d", det->GetName(), r->GetId(),k));
450         TNode* mnod = new TNode(name.Data(), title.Data(), rshape, 
451                                 0, 0, rz - siThick / 2 
452                                 + TMath::Sign(offz,z), rot);
453         mnod->SetLineColor(GetLineColor());
454         fNodes->Add(mnod);
455       } // for (Int_t k = 0 ; ...)
456     } // for (Int_t j = 0 ; ...)
457   } // for (Int_t i = 1 ; ...)
458 }
459
460 //____________________________________________________________________
461 void 
462 AliFMD::DrawDetector()
463 {
464   //
465   // Draw a shaded view of the Forward multiplicity detector
466   //
467   // DebugGuard guard("AliFMD::DrawDetector");
468   AliDebug(10, "\tDraw detector");
469   
470 #if 0
471   //Set ALIC mother transparent
472   gMC->Gsatt("ALIC","SEEN",0);
473   //
474   gMC->Gdopt("hide", "on");
475   gMC->Gdopt("shad", "on");
476   gMC->Gsatt("*", "fill", 7);
477   gMC->SetClipBox(".");
478   gMC->SetClipBox("*", 0, 1000, -1000, 1000, -1000, 1000);
479   gMC->DefaultRange();
480   gMC->Gdraw("alic", 40, 30, 0, 12, 12, .055, .055);
481   gMC->Gdhead(1111, "Forward Multiplicity Detector");
482   gMC->Gdman(16, 10, "MAN");
483   gMC->Gdopt("hide", "off");
484 #endif
485 }
486
487 //____________________________________________________________________
488 Int_t 
489 AliFMD::DistanceToPrimitive(Int_t, Int_t)
490 {
491   //
492   // Calculate the distance from the mouse to the FMD on the screen
493   // Dummy routine
494   //
495   return 9999;
496 }
497
498 //====================================================================
499 //
500 // Hit and Digit managment 
501 //
502 //____________________________________________________________________
503 void 
504 AliFMD::MakeBranch(Option_t * option)
505 {
506   // Create Tree branches for the FMD.
507   //
508   // Options:
509   //
510   //    H          Make a branch of TClonesArray of AliFMDHit's
511   //    D          Make a branch of TClonesArray of AliFMDDigit's
512   //    S          Make a branch of TClonesArray of AliFMDSDigit's
513   // 
514   const Int_t kBufferSize = 16000;
515   TString branchname(GetName());
516   TString opt(option);
517   
518   if (opt.Contains("H", TString::kIgnoreCase)) {
519     HitsArray();
520     AliDetector::MakeBranch(option); 
521   }
522   if (opt.Contains("D", TString::kIgnoreCase)) { 
523     DigitsArray();
524     MakeBranchInTree(fLoader->TreeD(), branchname.Data(),
525                      &fDigits, kBufferSize, 0);
526   }
527   if (opt.Contains("S", TString::kIgnoreCase)) { 
528     SDigitsArray();
529     MakeBranchInTree(fLoader->TreeS(), branchname.Data(),
530                      &fSDigits, kBufferSize, 0);
531   }
532 }
533
534 //____________________________________________________________________
535 void 
536 AliFMD::SetTreeAddress()
537 {
538   // Set branch address for the Hits, Digits, and SDigits Tree.
539   if (fLoader->TreeH()) HitsArray();
540   AliDetector::SetTreeAddress();
541
542   TTree *treeD = fLoader->TreeD();
543   if (treeD) {
544     DigitsArray();
545     TBranch* branch = treeD->GetBranch ("FMD");
546     if (branch) branch->SetAddress(&fDigits);
547   }
548
549   TTree *treeS = fLoader->TreeS();
550   if (treeS) {
551     SDigitsArray();
552     TBranch* branch = treeS->GetBranch ("FMD");
553     if (branch) branch->SetAddress(&fSDigits);
554   }
555 }
556
557
558
559 //____________________________________________________________________
560 void 
561 AliFMD::SetHitsAddressBranch(TBranch *b)
562 {
563   // Set the TClonesArray to read hits into. 
564   b->SetAddress(&fHits);
565 }
566
567 //____________________________________________________________________
568 void 
569 AliFMD::AddHit(Int_t track, Int_t *vol, Float_t *hits) 
570 {
571   // Add a hit to the hits tree 
572   // 
573   // The information of the two arrays are decoded as 
574   // 
575   // Parameters
576   //    track                Track #
577   //    ivol[0]  [UShort_t ] Detector # 
578   //    ivol[1]  [Char_t   ] Ring ID 
579   //    ivol[2]  [UShort_t ] Sector #
580   //    ivol[3]  [UShort_t ] Strip # 
581   //    hits[0]  [Float_t  ] Track's X-coordinate at hit 
582   //    hits[1]  [Float_t  ] Track's Y-coordinate at hit
583   //    hits[3]  [Float_t  ] Track's Z-coordinate at hit
584   //    hits[4]  [Float_t  ] X-component of track's momentum             
585   //    hits[5]  [Float_t  ] Y-component of track's momentum             
586   //    hits[6]  [Float_t  ] Z-component of track's momentum            
587   //    hits[7]  [Float_t  ] Energy deposited by track                  
588   //    hits[8]  [Int_t    ] Track's particle Id # 
589   //    hits[9]  [Float_t  ] Time when the track hit
590   // 
591   // 
592   AddHitByFields(track, 
593                  UShort_t(vol[0]),  // Detector # 
594                  Char_t(vol[1]),    // Ring ID
595                  UShort_t(vol[2]),  // Sector # 
596                  UShort_t(vol[3]),  // Strip # 
597                  hits[0],           // X
598                  hits[1],           // Y
599                  hits[2],           // Z
600                  hits[3],           // Px
601                  hits[4],           // Py
602                  hits[5],           // Pz
603                  hits[6],           // Energy loss 
604                  Int_t(hits[7]),    // PDG 
605                  hits[8]);          // Time
606 }
607
608 //____________________________________________________________________
609 void 
610 AliFMD::AddHitByFields(Int_t    track, 
611                        UShort_t detector, 
612                        Char_t   ring, 
613                        UShort_t sector, 
614                        UShort_t strip, 
615                        Float_t  x, 
616                        Float_t  y, 
617                        Float_t  z,
618                        Float_t  px, 
619                        Float_t  py, 
620                        Float_t  pz,
621                        Float_t  edep,
622                        Int_t    pdg,
623                        Float_t  t)
624 {
625   //
626   // Add a hit to the list
627   //
628   // Parameters:
629   // 
630   //    track     Track #
631   //    detector  Detector # (1, 2, or 3)                      
632   //    ring      Ring ID ('I' or 'O')
633   //    sector    Sector # (For inner/outer rings: 0-19/0-39)
634   //    strip     Strip # (For inner/outer rings: 0-511/0-255)
635   //    x         Track's X-coordinate at hit
636   //    y         Track's Y-coordinate at hit
637   //    z         Track's Z-coordinate at hit
638   //    px        X-component of track's momentum 
639   //    py        Y-component of track's momentum
640   //    pz        Z-component of track's momentum
641   //    edep      Energy deposited by track
642   //    pdg       Track's particle Id #
643   //    t         Time when the track hit 
644   // 
645   TClonesArray& a = *(HitsArray());
646   // Search through the list of already registered hits, and see if we
647   // find a hit with the same parameters.  If we do, then don't create
648   // a new hit, but rather update the energy deposited in the hit.
649   // This is done, so that a FLUKA based simulation will get the
650   // number of hits right, not just the enerrgy deposition. 
651   AliFMDHit* hit = 0;
652   for (Int_t i = 0; i < fNhits; i++) {
653     if (!a.At(i)) continue;
654     hit = static_cast<AliFMDHit*>(a.At(i));
655     if (hit->Detector() == detector 
656         && hit->Ring() == ring
657         && hit->Sector() == sector 
658         && hit->Strip() == strip
659         && hit->Track() == track) {
660       Warning("AddHit", "already had a hit in FMD%d%c[%2d,%3d] for track # %d,"
661               " adding energy (%f) to that hit (%f) -> %f", 
662               detector, ring, sector, strip, track, edep, hit->Edep(),
663               hit->Edep() + edep);
664       hit->SetEdep(hit->Edep() + edep);
665       return;
666     }
667   }
668   // If hit wasn't already registered, do so know. 
669   hit = new (a[fNhits]) AliFMDHit(fIshunt, track, detector, ring, sector, 
670                                   strip, x, y, z, px, py, pz, edep, pdg, t);
671   fNhits++;
672 }
673
674 //____________________________________________________________________
675 void 
676 AliFMD::AddDigit(Int_t* digits, Int_t*)
677 {
678   // Add a digit to the Digit tree 
679   // 
680   // Paramters 
681   //
682   //    digits[0]  [UShort_t] Detector #
683   //    digits[1]  [Char_t]   Ring ID
684   //    digits[2]  [UShort_t] Sector #
685   //    digits[3]  [UShort_t] Strip #
686   //    digits[4]  [UShort_t] ADC Count 
687   //    digits[5]  [Short_t]  ADC Count, -1 if not used
688   //    digits[6]  [Short_t]  ADC Count, -1 if not used 
689   // 
690   AddDigitByFields(UShort_t(digits[0]),  // Detector #
691                    Char_t(digits[1]),    // Ring ID
692                    UShort_t(digits[2]),  // Sector #
693                    UShort_t(digits[3]),  // Strip #
694                    UShort_t(digits[4]),  // ADC Count1 
695                    Short_t(digits[5]),   // ADC Count2 
696                    Short_t(digits[6]));  // ADC Count3 
697 }
698
699 //____________________________________________________________________
700 void 
701 AliFMD::AddDigitByFields(UShort_t detector, 
702                          Char_t   ring, 
703                          UShort_t sector, 
704                          UShort_t strip, 
705                          UShort_t count1, 
706                          Short_t  count2,
707                          Short_t  count3)
708 {
709   // add a real digit - as coming from data
710   // 
711   // Parameters 
712   //
713   //    detector  Detector # (1, 2, or 3)                      
714   //    ring      Ring ID ('I' or 'O')
715   //    sector    Sector # (For inner/outer rings: 0-19/0-39)
716   //    strip     Strip # (For inner/outer rings: 0-511/0-255)
717   //    count1    ADC count (a 10-bit word)
718   //    count2    ADC count (a 10-bit word), or -1 if not used
719   //    count3    ADC count (a 10-bit word), or -1 if not used
720   TClonesArray& a = *(DigitsArray());
721   
722   new (a[fNdigits++]) 
723     AliFMDDigit(detector, ring, sector, strip, count1, count2, count3);
724 }
725
726 //____________________________________________________________________
727 void 
728 AliFMD::AddSDigit(Int_t* digits)
729 {
730   // Add a digit to the SDigit tree 
731   // 
732   // Paramters 
733   //
734   //    digits[0]  [UShort_t] Detector #
735   //    digits[1]  [Char_t]   Ring ID
736   //    digits[2]  [UShort_t] Sector #
737   //    digits[3]  [UShort_t] Strip #
738   //    digits[4]  [Float_t]  Total energy deposited 
739   //    digits[5]  [UShort_t] ADC Count 
740   //    digits[6]  [Short_t]  ADC Count, -1 if not used
741   //    digits[7]  [Short_t]  ADC Count, -1 if not used 
742   // 
743   AddSDigitByFields(UShort_t(digits[0]),  // Detector #
744                     Char_t(digits[1]),    // Ring ID
745                     UShort_t(digits[2]),  // Sector #
746                     UShort_t(digits[3]),  // Strip #
747                     Float_t(digits[4]),   // Edep
748                     UShort_t(digits[5]),  // ADC Count1 
749                     Short_t(digits[6]),   // ADC Count2 
750                     Short_t(digits[7]));  // ADC Count3 
751 }
752
753 //____________________________________________________________________
754 void 
755 AliFMD::AddSDigitByFields(UShort_t detector, 
756                           Char_t   ring, 
757                           UShort_t sector, 
758                           UShort_t strip, 
759                           Float_t  edep,
760                           UShort_t count1, 
761                           Short_t  count2,
762                           Short_t  count3)
763 {
764   // add a summable digit
765   // 
766   // Parameters 
767   //
768   //    detector  Detector # (1, 2, or 3)                      
769   //    ring      Ring ID ('I' or 'O')
770   //    sector    Sector # (For inner/outer rings: 0-19/0-39)
771   //    strip     Strip # (For inner/outer rings: 0-511/0-255)
772   //    edep      Total energy deposited
773   //    count1    ADC count (a 10-bit word)
774   //    count2    ADC count (a 10-bit word), or -1 if not used
775   //    count3    ADC count (a 10-bit word), or -1 if not used
776   //
777   TClonesArray& a = *(SDigitsArray());
778   
779   new (a[fNsdigits++]) 
780     AliFMDSDigit(detector, ring, sector, strip, edep, count1, count2, count3);
781 }
782
783 //____________________________________________________________________
784 void 
785 AliFMD::ResetSDigits()
786 {
787   //
788   // Reset number of digits and the digits array for this detector
789   //
790   fNsdigits   = 0;
791   if (fSDigits) fSDigits->Clear();
792 }
793
794
795 //____________________________________________________________________
796 TClonesArray*
797 AliFMD::HitsArray() 
798 {
799   // Initialize hit array if not already, and return pointer to it. 
800   if (!fHits) { 
801     fHits = new TClonesArray("AliFMDHit", 1000);
802     fNhits = 0;
803   }
804   return fHits;
805 }
806
807 //____________________________________________________________________
808 TClonesArray*
809 AliFMD::DigitsArray() 
810 {
811   // Initialize digit array if not already, and return pointer to it. 
812   if (!fDigits) { 
813     fDigits = new TClonesArray("AliFMDDigit", 1000);
814     fNdigits = 0;
815   }
816   return fDigits;
817 }
818
819 //____________________________________________________________________
820 TClonesArray*
821 AliFMD::SDigitsArray() 
822 {
823   // Initialize digit array if not already, and return pointer to it. 
824   if (!fSDigits) { 
825     fSDigits = new TClonesArray("AliFMDSDigit", 1000);
826     fNsdigits = 0;
827   }
828   return fSDigits;
829 }
830
831 //====================================================================
832 //
833 // Digitization 
834 //
835 //____________________________________________________________________
836 void 
837 AliFMD::Hits2Digits() 
838 {
839   // Create AliFMDDigit's from AliFMDHit's.  This is done by making a
840   // AliFMDDigitizer, and executing that code.
841   // 
842   Warning("Hits2Digits", "Try not to use this method.\n"
843           "Instead, use AliSimulator");
844   AliRunDigitizer* manager = new AliRunDigitizer(1, 1);
845   manager->SetInputStream(0, "galice.root");
846   manager->SetOutputFile("H2Dfile");
847   
848   /* AliDigitizer* dig =*/ CreateDigitizer(manager);
849   manager->Exec("");
850   delete manager;
851 }
852
853 //____________________________________________________________________
854 void 
855 AliFMD::Hits2SDigits() 
856 {
857   // Create AliFMDSDigit's from AliFMDHit's.  This is done by creating
858   // an AliFMDSDigitizer object, and executing it. 
859   // 
860   AliFMDSDigitizer* digitizer = new AliFMDSDigitizer("galice.root");
861   digitizer->Exec("");
862   delete digitizer;
863 }
864
865   
866 //____________________________________________________________________
867 AliDigitizer* 
868 AliFMD::CreateDigitizer(AliRunDigitizer* manager) const
869 {
870   // Create a digitizer object 
871   AliFMDDigitizer* digitizer = new AliFMDDigitizer(manager);
872   return digitizer;
873 }
874
875 //====================================================================
876 //
877 // Raw data simulation 
878 //
879 //__________________________________________________________________
880 void 
881 AliFMD::Digits2Raw() 
882 {
883   // Turn digits into raw data. 
884   // 
885   // This uses the class AliFMDRawWriter to do the job.   Please refer
886   // to that class for more information. 
887   AliFMDRawWriter writer(this);
888   writer.Exec();
889 }
890
891
892 //====================================================================
893 //
894 // Utility 
895 //
896 //__________________________________________________________________
897 void 
898 AliFMD::Browse(TBrowser* b) 
899 {
900   // Browse this object. 
901   //
902   AliDebug(30, "\tBrowsing the FMD");
903   AliDetector::Browse(b);
904   if (fSimulator) b->Add(fSimulator);
905   b->Add(AliFMDGeometry::Instance());
906 }
907
908 //___________________________________________________________________
909 //
910 // EOF
911 //