]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDBaseDigitizer.cxx
Commit of new FMD3 geometry and other geometry related issues.
[u/mrichter/AliRoot.git] / FMD / AliFMDBaseDigitizer.cxx
1 /**************************************************************************
2  * Copyright(c) 2004, 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 /* $Id$ */
16 /** @file    AliFMDBaseDigitizer.cxx
17     @author  Christian Holm Christensen <cholm@nbi.dk>
18     @date    Mon Mar 27 12:38:26 2006
19     @brief   FMD Digitizers implementation
20     @ingroup FMD_sim
21 */
22 //////////////////////////////////////////////////////////////////////////////
23 //
24 //  This class contains the procedures simulation ADC  signal for the
25 //  Forward Multiplicity detector  : Hits->Digits and Hits->SDigits
26 // 
27 //  Digits consists of
28 //   - Detector #
29 //   - Ring ID                                             
30 //   - Sector #     
31 //   - Strip #
32 //   - ADC count in this channel                                  
33 //
34 //  Digits consists of
35 //   - Detector #
36 //   - Ring ID                                             
37 //   - Sector #     
38 //   - Strip #
39 //   - Total energy deposited in the strip
40 //   - ADC count in this channel                                  
41 //
42 // As the Digits and SDigits have so much in common, the classes
43 // AliFMDDigitizer and AliFMDSDigitizer are implemented via a base
44 // class AliFMDBaseDigitizer.
45 //
46 //                 +---------------------+
47 //                 | AliFMDBaseDigitizer |
48 //                 +---------------------+
49 //                           ^
50 //                           |
51 //                +----------+---------+
52 //                |                    |
53 //      +-----------------+     +------------------+
54 //      | AliFMDDigitizer |     | AliFMDSDigitizer |
55 //      +-----------------+     +------------------+
56 //
57 // These classes has several paramters: 
58 //
59 //     fPedestal
60 //     fPedestalWidth
61 //         (Only AliFMDDigitizer)
62 //         Mean and width of the pedestal.  The pedestal is simulated
63 //         by a Guassian, but derived classes my override MakePedestal
64 //         to simulate it differently (or pick it up from a database).
65 //
66 //     fVA1MipRange
67 //         The dymamic MIP range of the VA1_ALICE pre-amplifier chip 
68 //
69 //     fAltroChannelSize
70 //         The largest number plus one that can be stored in one
71 //         channel in one time step in the ALTRO ADC chip. 
72 //
73 //     fSampleRate
74 //         How many times the ALTRO ADC chip samples the VA1_ALICE
75 //         pre-amplifier signal.   The VA1_ALICE chip is read-out at
76 //         10MHz, while it's possible to drive the ALTRO chip at
77 //         25MHz.  That means, that the ALTRO chip can have time to
78 //         sample each VA1_ALICE signal up to 2 times.  Although it's
79 //         not certain this feature will be used in the production,
80 //         we'd like have the option, and so it should be reflected in
81 //         the code.
82 //
83 //
84 // The shaping function of the VA1_ALICE is generally given by 
85 //
86 //      f(x) = A(1 - exp(-Bx))
87 //
88 // where A is the total charge collected in the pre-amp., and B is a
89 // paramter that depends on the shaping time of the VA1_ALICE circut.
90 // 
91 // When simulating the shaping function of the VA1_ALICe
92 // pre-amp. chip, we have to take into account, that the shaping
93 // function depends on the previous value of read from the pre-amp. 
94 //
95 // That results in the following algorithm:
96 //
97 //    last = 0;
98 //    FOR charge IN pre-amp. charge train DO 
99 //      IF last < charge THEN 
100 //        f(t) = (charge - last) * (1 - exp(-B * t)) + last
101 //      ELSE
102 //        f(t) = (last - charge) * exp(-B * t) + charge)
103 //      ENDIF
104 //      FOR i IN # samples DO 
105 //        adc_i = f(i / (# samples))
106 //      DONE
107 //      last = charge
108 //   DONE
109 //
110 // Here, 
111 //
112 //   pre-amp. charge train 
113 //       is a series of 128 charges read from the VA1_ALICE chip
114 //
115 //   # samples
116 //       is the number of times the ALTRO ADC samples each of the 128
117 //       charges from the pre-amp. 
118 //
119 // Where Q is the total charge collected by the VA1_ALICE
120 // pre-amplifier.   Q is then given by 
121 //
122 //           E S 
123 //      Q =  - -
124 //           e R
125 //
126 // where E is the total energy deposited in a silicon strip, R is the
127 // dynamic range of the VA1_ALICE pre-amp (fVA1MipRange), e is the
128 // energy deposited by a single MIP, and S ALTRO channel size in each
129 // time step (fAltroChannelSize).  
130 //
131 // The energy deposited per MIP is given by 
132 //
133 //      e = M * rho * w 
134 //
135 // where M is the universal number 1.664, rho is the density of
136 // silicon, and w is the depth of the silicon sensor. 
137 //
138 // The final ADC count is given by 
139 //
140 //      C' = C + P
141 //
142 // where P is the (randomized) pedestal (see MakePedestal)
143 //
144 // This class uses the class template AliFMDMap<Type> to make an
145 // internal cache of the energy deposted of the hits.  The class
146 // template is instantasized as 
147 //
148 //  typedef AliFMDMap<std::pair<Float_t, UShort_t> > AliFMDEdepMap;
149 //
150 // The first member of the values is the summed energy deposition in a
151 // given strip, while the second member of the values is the number of
152 // hits in a given strip.  Using the second member, it's possible to
153 // do some checks on just how many times a strip got hit, and what
154 // kind of error we get in our reconstructed hits.  Note, that this
155 // information is currently not written to the digits tree.  I think a
156 // QA (Quality Assurance) digit tree is better suited for that task.
157 // However, the information is there to be used in the future. 
158 //
159 //
160 // Latest changes by Christian Holm Christensen
161 //
162 //////////////////////////////////////////////////////////////////////////////
163
164 //      /1
165 //      |           A(-1 + B + exp(-B))
166 //      | f(x) dx = ------------------- = 1
167 //      |                    B
168 //      / 0
169 //
170 // and B is the a parameter defined by the shaping time (fShapingTime).  
171 //
172 // Solving the above equation, for A gives
173 //
174 //                 B
175 //      A = ----------------
176 //          -1 + B + exp(-B)
177 //
178 // So, if we define the function g: [0,1] -> [0:1] by 
179 //
180 //               / v
181 //               |              Bu + exp(-Bu) - Bv - exp(-Bv) 
182 //      g(u,v) = | f(x) dx = -A -----------------------------
183 //               |                            B
184 //               / u
185 //
186 // we can evaluate the ALTRO sample of the VA1_ALICE pre-amp between
187 // any two times (u, v), by 
188 //       
189 //
190 //                                B         Bu + exp(-Bu) - Bv - exp(-Bv)
191 //      C = Q g(u,v) = - Q ---------------- -----------------------------
192 //                         -1 + B + exp(-B)              B                  
193 //
194 //               Bu + exp(-Bu) - Bv - exp(-Bv) 
195 //        = -  Q -----------------------------
196 //                    -1 + B + exp(-B)
197 //
198
199 #include <TMath.h>
200 #include <TTree.h>              // ROOT_TTree
201 //#include <TRandom.h>          // ROOT_TRandom
202 // #include <AliLog.h>          // ALILOG_H
203 #include "AliFMDDebug.h" // Better debug macros
204 #include "AliFMDBaseDigitizer.h" // ALIFMDDIGITIZER_H
205 #include "AliFMD.h"             // ALIFMD_H
206 #include "AliFMDGeometry.h"     // ALIFMDGEOMETRY_H
207 #include "AliFMDDetector.h"     // ALIFMDDETECTOR_H
208 #include "AliFMDRing.h"         // ALIFMDRING_H
209 #include "AliFMDHit.h"          // ALIFMDHIT_H
210 // #include "AliFMDDigit.h"     // ALIFMDDIGIT_H
211 #include "AliFMDParameters.h"   // ALIFMDPARAMETERS_H
212 // #include <AliRunDigitizer.h> // ALIRUNDIGITIZER_H
213 //#include <AliRun.h>           // ALIRUN_H
214 #include <AliLoader.h>          // ALILOADER_H
215 #include <AliRunLoader.h>       // ALIRUNLOADER_H
216     
217 //====================================================================
218 ClassImp(AliFMDBaseDigitizer)
219 #if 0
220   ; // This is here to keep Emacs for indenting the next line
221 #endif
222
223 //____________________________________________________________________
224 AliFMDBaseDigitizer::AliFMDBaseDigitizer()  
225   : fRunLoader(0),
226     fEdep(AliFMDMap::kMaxDetectors, 
227           AliFMDMap::kMaxRings, 
228           AliFMDMap::kMaxSectors, 
229           AliFMDMap::kMaxStrips),
230     fShapingTime(6)
231 {
232   // Default ctor - don't use it
233 }
234
235 //____________________________________________________________________
236 AliFMDBaseDigitizer::AliFMDBaseDigitizer(AliRunDigitizer* manager) 
237   : AliDigitizer(manager, "AliFMDBaseDigitizer", "FMD Digitizer base class"), 
238     fRunLoader(0),
239     fEdep(AliFMDMap::kMaxDetectors, 
240           AliFMDMap::kMaxRings, 
241           AliFMDMap::kMaxSectors, 
242           AliFMDMap::kMaxStrips), 
243     fShapingTime(6)
244 {
245   // Normal CTOR
246   AliFMDDebug(1, (" processed"));
247   SetShapingTime();
248 }
249
250 //____________________________________________________________________
251 AliFMDBaseDigitizer::AliFMDBaseDigitizer(const Char_t* name, 
252                                          const Char_t* title) 
253   : AliDigitizer(name, title),
254     fRunLoader(0),
255     fEdep(AliFMDMap::kMaxDetectors, 
256           AliFMDMap::kMaxRings, 
257           AliFMDMap::kMaxSectors, 
258           AliFMDMap::kMaxStrips),
259     fShapingTime(6)
260 {
261   // Normal CTOR
262   AliFMDDebug(1, (" processed"));
263   SetShapingTime();
264 }
265
266 //____________________________________________________________________
267 AliFMDBaseDigitizer::~AliFMDBaseDigitizer()
268 {
269   // Destructor
270 }
271
272 //____________________________________________________________________
273 Bool_t 
274 AliFMDBaseDigitizer::Init()
275 {
276   // Initialization
277   AliFMDParameters::Instance()->Init();
278   return kTRUE;
279 }
280  
281
282 //____________________________________________________________________
283 UShort_t
284 AliFMDBaseDigitizer::MakePedestal(UShort_t, 
285                                   Char_t, 
286                                   UShort_t, 
287                                   UShort_t) const 
288
289   // Make a pedestal
290   return 0; 
291 }
292
293 //____________________________________________________________________
294 void
295 AliFMDBaseDigitizer::SumContributions(AliFMD* fmd) 
296 {
297   // Sum energy deposited contributions from each hit in a cache
298   // (fEdep).  
299   if (!fRunLoader) 
300     Fatal("SumContributions", "no run loader");
301   
302   // Clear array of deposited energies 
303   fEdep.Reset();
304   
305   // Get the FMD loader 
306   AliLoader* inFMD = fRunLoader->GetLoader("FMDLoader");
307   // And load the hits 
308   inFMD->LoadHits("READ");
309   
310   // Get the tree of hits 
311   TTree* hitsTree = inFMD->TreeH();
312   if (!hitsTree)  {
313     // Try again 
314     inFMD->LoadHits("READ");
315     hitsTree = inFMD->TreeH();
316   }
317   
318   // Get the FMD branch 
319   TBranch* hitsBranch = hitsTree->GetBranch("FMD");
320   if (hitsBranch) fmd->SetHitsAddressBranch(hitsBranch);
321   else            AliFatal("Branch FMD hit not found");
322   
323   // Get a list of hits from the FMD manager 
324   TClonesArray *fmdHits = fmd->Hits();
325   
326   // Get number of entries in the tree 
327   Int_t ntracks  = Int_t(hitsTree->GetEntries());
328   
329   AliFMDParameters* param = AliFMDParameters::Instance();
330   Int_t read = 0;
331   // Loop over the tracks in the 
332   for (Int_t track = 0; track < ntracks; track++)  {
333     // Read in entry number `track' 
334     read += hitsBranch->GetEntry(track);
335     
336     // Get the number of hits 
337     Int_t nhits = fmdHits->GetEntries ();
338     for (Int_t hit = 0; hit < nhits; hit++) {
339       // Get the hit number `hit'
340       AliFMDHit* fmdHit = 
341         static_cast<AliFMDHit*>(fmdHits->UncheckedAt(hit));
342       
343       // Extract parameters 
344       UShort_t detector = fmdHit->Detector();
345       Char_t   ring     = fmdHit->Ring();
346       UShort_t sector   = fmdHit->Sector();
347       UShort_t strip    = fmdHit->Strip();
348       Float_t  edep     = fmdHit->Edep();
349       // UShort_t minstrip = param->GetMinStrip(detector, ring, sector, strip);
350       // UShort_t maxstrip = param->GetMaxStrip(detector, ring, sector, strip);
351       // Check if strip is `dead' 
352       AliFMDDebug(2, ("Hit in FMD%d%c[%2d,%3d]=%f",
353                       detector, ring, sector, strip, edep));
354       if (param->IsDead(detector, ring, sector, strip)) { 
355         AliFMDDebug(1, ("FMD%d%c[%2d,%3d] is marked as dead", 
356                          detector, ring, sector, strip));
357         continue;
358       }
359       // Check if strip is out-side read-out range 
360       // if (strip < minstrip || strip > maxstrip) {
361       //   AliFMDDebug(5, ("FMD%d%c[%2d,%3d] is outside range [%3d,%3d]", 
362       //                    detector,ring,sector,strip,minstrip,maxstrip));
363       //   continue;
364       // }
365         
366       // Give warning in case of double hit 
367       if (fEdep(detector, ring, sector, strip).fEdep != 0)
368         AliFMDDebug(5, ("Double hit in %d%c(%d,%d)", 
369                          detector, ring, sector, strip));
370       
371       // Sum energy deposition
372       fEdep(detector, ring, sector, strip).fEdep  += edep;
373       fEdep(detector, ring, sector, strip).fN     += 1;
374       // Add this to the energy deposited for this strip
375     }  // hit loop
376   } // track loop
377   AliFMDDebug(1, ("Size of cache: %d bytes, read %d bytes", 
378                    sizeof(fEdep), read));
379 }
380
381 //____________________________________________________________________
382 void
383 AliFMDBaseDigitizer::DigitizeHits(AliFMD* fmd) const
384 {
385   // For the stored energy contributions in the cache (fEdep), convert
386   // the energy signal to ADC counts, and store the created digit in
387   // the digits array (AliFMD::fDigits)
388   //
389   AliFMDGeometry* geometry = AliFMDGeometry::Instance();
390   
391   TArrayI counts(4);
392   for (UShort_t detector=1; detector <= 3; detector++) {
393     AliFMDDebug(5, ("Processing hits in FMD%d", detector));
394     // Get pointer to subdetector 
395     AliFMDDetector* det = geometry->GetDetector(detector);
396     if (!det) continue;
397     for (UShort_t ringi = 0; ringi <= 1; ringi++) {
398       Char_t ring = ringi == 0 ? 'I' : 'O';
399       AliFMDDebug(5, (" Processing hits in FMD%d%c", detector,ring));
400       // Get pointer to Ring
401       AliFMDRing* r = det->GetRing(ring);
402       if (!r) continue;
403       
404       // Get number of sectors 
405       UShort_t nSectors = UShort_t(360. / r->GetTheta());
406       // Loop over the number of sectors 
407       for (UShort_t sector = 0; sector < nSectors; sector++) {
408         AliFMDDebug(5, ("  Processing hits in FMD%d%c[%2d]", 
409                         detector,ring,sector));
410         // Get number of strips 
411         UShort_t nStrips = r->GetNStrips();
412         // Loop over the stips 
413         Float_t last = 0;
414         for (UShort_t strip = 0; strip < nStrips; strip++) {
415           // Reset the counter array to the invalid value -1 
416           counts.Reset(-1);
417           // Reset the last `ADC' value when we've get to the end of a
418           // VA1_ALICE channel. 
419           if (strip % 128 == 0) last = 0;
420           
421           Float_t edep = fEdep(detector, ring, sector, strip).fEdep;
422           ConvertToCount(edep, last, detector, ring, sector, strip, counts);
423           last = edep;
424           if (edep<=0) continue;
425           AddDigit(fmd, detector, ring, sector, strip, edep, 
426                    UShort_t(counts[0]), Short_t(counts[1]), 
427                    Short_t(counts[2]), Short_t(counts[3]));
428           AliFMDDebug(10, ("   Adding digit in FMD%d%c[%2d,%3d]=%d", 
429                           detector,ring,sector,strip,counts[0]));
430 #if 0
431           // This checks if the digit created will give the `right'
432           // number of particles when reconstructed, using a naiive
433           // approach.  It's here only as a quality check - nothing
434           // else. 
435           CheckDigit(digit, fEdep(detector, ring, sector, strip).fN,
436                      counts);
437 #endif
438         } // Strip
439       } // Sector 
440     } // Ring 
441   } // Detector 
442 }
443
444 //____________________________________________________________________
445 void
446 AliFMDBaseDigitizer::ConvertToCount(Float_t   edep, 
447                                     Float_t   last,
448                                     UShort_t  detector, 
449                                     Char_t    ring, 
450                                     UShort_t  sector, 
451                                     UShort_t  strip,
452                                     TArrayI&  counts) const
453 {
454   // Convert the total energy deposited to a (set of) ADC count(s). 
455   // 
456   // This is done by 
457   // 
458   //               Energy_Deposited      ALTRO_Channel_Size
459   //    ADC = -------------------------- ------------------- + pedestal
460   //          Energy_Deposition_Of_1_MIP VA1_ALICE_MIP_Range
461   //
462   //               Energy_Deposited             fAltroChannelSize
463   //        = --------------------------------- ----------------- + pedestal 
464   //          1.664 * Si_Thickness * Si_Density   fVA1MipRange   
465   //          
466   // 
467   //        = Energy_Deposited * ConversionFactor + pedestal
468   // 
469   // However, this is modified by the response function of the
470   // VA1_ALICE pre-amp. chip in case we are doing oversampling of the
471   // VA1_ALICE output. 
472   // 
473   // In that case, we get N=fSampleRate values of the ADC, and the
474   // `EnergyDeposited' is a function of which sample where are
475   // calculating the ADC for 
476   // 
477   //     ADC_i = f(EnergyDeposited, i/N, Last) * ConversionFactor + pedestal 
478   // 
479   // where Last is the Energy deposited in the previous strip. 
480   // 
481   // Here, f is the shaping function of the VA1_ALICE.   This is given
482   // by 
483   //                       
484   //                    |   (E - l) * (1 - exp(-B * t) + l   if E > l
485   //       f(E, t, l) = <
486   //                    |   (l - E) * exp(-B * t) + E        otherwise
487   //                       
488   // 
489   //                  = E + (l - E) * ext(-B * t)
490   // 
491   AliFMDParameters* param = AliFMDParameters::Instance();
492   Float_t  convF          = 1./param->GetPulseGain(detector,ring,sector,strip);
493   Int_t    ped            = MakePedestal(detector,ring,sector,strip);
494   Int_t    maxAdc         = param->GetAltroChannelSize()-1;
495   if (maxAdc < 0) {
496     AliWarning(Form("Maximum ADC is %d < 0, forcing it to 1023", maxAdc));
497     maxAdc = 1023;
498   }
499   UShort_t rate           = param->GetSampleRate(detector,ring,sector,strip);
500   if (rate < 1 || rate > 4) rate = 1;
501   
502   // In case we don't oversample, just return the end value. 
503   if (rate == 1) {
504     Float_t    a = edep * convF + ped;
505     if (a < 0) a = 0;
506     counts[0]    = UShort_t(TMath::Min(a, Float_t(maxAdc)));
507     AliFMDDebug(2, ("FMD%d%c[%2d,%3d]: converting ELoss %f to "
508                      "ADC %4d (%f,%d)",
509                      detector,ring,sector,strip,edep,counts[0],convF,ped));
510     return;
511   }
512   
513   // Create a pedestal 
514   Float_t b = fShapingTime;
515   for (Ssiz_t i = 0; i < rate;  i++) {
516     Float_t t  = Float_t(i) / rate + 1./rate;
517     Float_t s  = edep + (last - edep) * TMath::Exp(-b * t);
518     Float_t a  = Int_t(s * convF + ped);
519     if (a < 0) a = 0;
520     counts[i]  = UShort_t(TMath::Min(a, Float_t(maxAdc)));
521   }
522 }
523
524
525
526 //____________________________________________________________________
527 //
528 // EOF
529 // 
530
531
532
533