]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDBaseDigitizer.cxx
A lot of changes after detector review:
[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 <AliRun.h>             // ALILOADER_H
216 #include <AliRunLoader.h>       // ALIRUNLOADER_H
217 #include <TRandom.h>
218     
219 //====================================================================
220 ClassImp(AliFMDBaseDigitizer)
221 #if 0
222   ; // This is here to keep Emacs for indenting the next line
223 #endif
224
225 //____________________________________________________________________
226 AliFMDBaseDigitizer::AliFMDBaseDigitizer()  
227   : fFMD(0),
228     fRunLoader(0),
229     fEdep(AliFMDMap::kMaxDetectors, 
230           AliFMDMap::kMaxRings, 
231           AliFMDMap::kMaxSectors, 
232           AliFMDMap::kMaxStrips),
233     fShapingTime(6),
234     fStoreTrackRefs(kTRUE)
235 {
236   AliFMDDebug(1, ("Constructed"));
237   // Default ctor - don't use it
238 }
239
240 //____________________________________________________________________
241 AliFMDBaseDigitizer::AliFMDBaseDigitizer(AliRunDigitizer* manager) 
242   : AliDigitizer(manager, "AliFMDBaseDigitizer", "FMD Digitizer base class"), 
243     fFMD(0),
244     fRunLoader(0),
245     fEdep(AliFMDMap::kMaxDetectors, 
246           AliFMDMap::kMaxRings, 
247           AliFMDMap::kMaxSectors, 
248           AliFMDMap::kMaxStrips), 
249     fShapingTime(6),
250     fStoreTrackRefs(kTRUE)
251 {
252   // Normal CTOR
253   AliFMDDebug(1, ("Constructed"));
254   SetShapingTime();
255 }
256
257 //____________________________________________________________________
258 AliFMDBaseDigitizer::AliFMDBaseDigitizer(const Char_t* name, 
259                                          const Char_t* title) 
260   : AliDigitizer(name, title),
261     fFMD(0),
262     fRunLoader(0),
263     fEdep(AliFMDMap::kMaxDetectors, 
264           AliFMDMap::kMaxRings, 
265           AliFMDMap::kMaxSectors, 
266           AliFMDMap::kMaxStrips),
267     fShapingTime(6),
268     fStoreTrackRefs(kTRUE)
269 {
270   // Normal CTOR
271   AliFMDDebug(1, (" Constructed"));
272   SetShapingTime();
273 }
274
275 //____________________________________________________________________
276 AliFMDBaseDigitizer::~AliFMDBaseDigitizer()
277 {
278   // Destructor
279 }
280
281 //____________________________________________________________________
282 Bool_t 
283 AliFMDBaseDigitizer::Init()
284 {
285   // Initialization.   Get a pointer to the parameter manager, and
286   // initialize it.  
287   AliFMDParameters::Instance()->Init();
288   if (AliLog::GetDebugLevel("FMD","") >= 15) 
289     AliFMDParameters::Instance()->Print("");
290   return kTRUE;
291 }
292
293 //____________________________________________________________________
294 UShort_t
295 AliFMDBaseDigitizer::MakePedestal(UShort_t detector, 
296                                   Char_t   ring, 
297                                   UShort_t sector, 
298                                   UShort_t strip) const 
299
300   // Make a pedestal.  The pedestal value is drawn from a Gaussian
301   // distribution.  The mean of the distribution is the measured
302   // pedestal, and the width is the measured noise. 
303   AliFMDParameters* param =AliFMDParameters::Instance();
304   Float_t           mean  =param->GetPedestal(detector,ring,sector,strip);
305   Float_t           width =param->GetPedestalWidth(detector,ring,sector,strip);
306   return UShort_t(TMath::Max(gRandom->Gaus(mean, width), 0.));
307 }
308
309 //____________________________________________________________________
310 void
311 AliFMDBaseDigitizer::AddContribution(UShort_t detector, 
312                                      Char_t   ring, 
313                                      UShort_t sector, 
314                                      UShort_t strip, 
315                                      Float_t  edep, 
316                                      Bool_t   isPrimary,
317                                      Int_t    nTrack,
318                                      Int_t*   tracknos)
319 {
320   // Add edep contribution from (detector,ring,sector,strip) to cache
321   AliFMDParameters* param = AliFMDParameters::Instance();
322
323   // Check if strip is `dead' 
324   if (param->IsDead(detector, ring, sector, strip)) { 
325     AliFMDDebug(5, ("FMD%d%c[%2d,%3d] is marked as dead", 
326                     detector, ring, sector, strip));
327     return;
328   }
329   // Check if strip is out-side read-out range 
330   // if (strip < minstrip || strip > maxstrip) {
331   //   AliFMDDebug(5, ("FMD%d%c[%2d,%3d] is outside range [%3d,%3d]", 
332   //                detector,ring,sector,strip,minstrip,maxstrip));
333   //   continue;
334   // }
335   
336   AliFMDEdepHitPair& entry = fEdep(detector, ring, sector, strip);
337
338   // Give warning in case of double sdigit 
339   if (entry.fEdep != 0)
340     AliFMDDebug(5, ("Double digit in FMD%d%c[%2d,%3d]", 
341                     detector, ring, sector, strip));
342       
343   // Sum energy deposition
344   entry.fEdep += edep;
345   entry.fN    += nTrack;
346   if (isPrimary) entry.fNPrim += nTrack;
347   if (fStoreTrackRefs) { 
348     Int_t oldN = entry.fLabels.fN;
349     entry.fLabels.Set(entry.fN);
350     for (Int_t i = 0; i < nTrack; i++) 
351       entry.fLabels[oldN + i] = tracknos[i];
352   }
353   AliFMDDebug(15, ("Adding contribution %f to FMD%d%c[%2d,%3d] (%f) track %d", 
354                    edep, detector, ring, sector, strip,
355                    entry.fEdep, (nTrack > 0 ? tracknos[0] : -1)));
356   
357 }
358
359 //____________________________________________________________________
360 void
361 AliFMDBaseDigitizer::DigitizeHits() const
362 {
363   // For the stored energy contributions in the cache (fEdep), convert
364   // the energy signal to ADC counts, and store the created digit in
365   // the digits array (AliFMD::fDigits)
366   //
367   AliFMDDebug(5, ("Will now digitize all the summed signals"));
368   AliFMDGeometry* geometry = AliFMDGeometry::Instance();
369   
370   TArrayI counts(4);
371   for (UShort_t detector=1; detector <= 3; detector++) {
372     AliFMDDebug(10, ("Processing hits in FMD%d", detector));
373     // Get pointer to subdetector 
374     AliFMDDetector* det = geometry->GetDetector(detector);
375     if (!det) continue;
376     for (UShort_t ringi = 0; ringi <= 1; ringi++) {
377       Char_t ring = ringi == 0 ? 'I' : 'O';
378       AliFMDDebug(10, (" Processing hits in FMD%d%c", detector,ring));
379       // Get pointer to Ring
380       AliFMDRing* r = det->GetRing(ring);
381       if (!r) continue;
382       
383       // Get number of sectors 
384       UShort_t nSectors = UShort_t(360. / r->GetTheta());
385       // Loop over the number of sectors 
386       for (UShort_t sector = 0; sector < nSectors; sector++) {
387         AliFMDDebug(10, ("  Processing hits in FMD%d%c[%2d]", 
388                         detector,ring,sector));
389         // Get number of strips 
390         UShort_t nStrips = r->GetNStrips();
391         // Loop over the stips 
392         Float_t last = 0;
393         for (UShort_t strip = 0; strip < nStrips; strip++) {
394           // Reset the counter array to the invalid value -1 
395           counts.Reset(-1);
396           // Reset the last `ADC' value when we've get to the end of a
397           // VA1_ALICE channel. 
398           if (strip % 128 == 0) last = 0;
399           
400           const AliFMDEdepHitPair& entry  = fEdep(detector,ring,sector,strip);
401           Float_t                  edep   = entry.fEdep;
402           UShort_t                 ntot   = entry.fN;
403           UShort_t                 nprim  = entry.fNPrim;
404           const TArrayI&           labels = entry.fLabels;
405           if (edep > 0)
406             AliFMDDebug(15, ("Edep = %f for FMD%d%c[%2d,%3d]", 
407                              edep, detector, ring, sector, strip));
408           ConvertToCount(edep, last, detector, ring, sector, strip, counts);
409           last = edep;
410           
411
412           // The following line was introduced - wrongly - by Peter
413           // Hristov.  It _will_ break the digitisation and the
414           // following reconstruction.  The behviour of the
415           // digitisation models exactly the front-end as it should
416           // (no matter what memory concuption it may entail).  The
417           // check should be on zero suppression, since that's what
418           // models the front-end - if zero suppression is turned on
419           // in the front-end, then we can suppress empty digits -
420           // otherwise we shoud never do that.  Note, that the line
421           // affects _both_ normal digitisation and digitisation for
422           // summable digits, since the condition is on the energy
423           // deposition and not on the actual number of counts.  If
424           // this line should go anywhere, it should be in the
425           // possible overloaded AliFMDSDigitizer::AddDigit - not
426           // here. 
427           // 
428           //   if (edep<=0) continue;
429           AddDigit(detector, ring, sector, strip, edep, 
430                    UShort_t(counts[0]), Short_t(counts[1]), 
431                    Short_t(counts[2]), Short_t(counts[3]), 
432                    ntot, nprim, labels);
433           AliFMDDebug(15, ("   Adding digit in FMD%d%c[%2d,%3d]=%d", 
434                            detector,ring,sector,strip,counts[0]));
435 #if 0
436           // This checks if the digit created will give the `right'
437           // number of particles when reconstructed, using a naiive
438           // approach.  It's here only as a quality check - nothing
439           // else. 
440           CheckDigit(digit, fEdep(detector, ring, sector, strip).fN,
441                      counts);
442 #endif
443         } // Strip
444       } // Sector 
445     } // Ring 
446   } // Detector 
447 }
448
449 //____________________________________________________________________
450 void
451 AliFMDBaseDigitizer::ConvertToCount(Float_t   edep, 
452                                     Float_t   last,
453                                     UShort_t  detector, 
454                                     Char_t    ring, 
455                                     UShort_t  sector, 
456                                     UShort_t  strip,
457                                     TArrayI&  counts) const
458 {
459   // Convert the total energy deposited to a (set of) ADC count(s). 
460   // 
461   // This is done by 
462   // 
463   //               Energy_Deposited      ALTRO_Channel_Size
464   //    ADC = -------------------------- ------------------- + pedestal
465   //          Energy_Deposition_Of_1_MIP VA1_ALICE_MIP_Range
466   //
467   //               Energy_Deposited             fAltroChannelSize
468   //        = --------------------------------- ----------------- + pedestal 
469   //          1.664 * Si_Thickness * Si_Density   fVA1MipRange   
470   //          
471   // 
472   //        = Energy_Deposited * ConversionFactor + pedestal
473   // 
474   // However, this is modified by the response function of the
475   // VA1_ALICE pre-amp. chip in case we are doing oversampling of the
476   // VA1_ALICE output. 
477   // 
478   // In that case, we get N=fSampleRate values of the ADC, and the
479   // `EnergyDeposited' is a function of which sample where are
480   // calculating the ADC for 
481   // 
482   //     ADC_i = f(EnergyDeposited, i/N, Last) * ConversionFactor + pedestal 
483   // 
484   // where Last is the Energy deposited in the previous strip. 
485   // 
486   // Here, f is the shaping function of the VA1_ALICE.   This is given
487   // by 
488   //                       
489   //                    |   (E - l) * (1 - exp(-B * t) + l   if E > l
490   //       f(E, t, l) = <
491   //                    |   (l - E) * exp(-B * t) + E        otherwise
492   //                       
493   // 
494   //                  = E + (l - E) * ext(-B * t)
495   // 
496   AliFMDParameters* param = AliFMDParameters::Instance();
497   Float_t  convF          = (param->GetDACPerMIP() / param->GetEdepMip() *
498                              param->GetPulseGain(detector,ring,sector,strip));
499   Int_t    ped            = MakePedestal(detector,ring,sector,strip);
500   Int_t    maxAdc         = param->GetAltroChannelSize()-1;
501   if (maxAdc < 0) {
502     AliWarning(Form("Maximum ADC is %d < 0, forcing it to 1023", maxAdc));
503     maxAdc = 1023;
504   }
505   UShort_t rate           = param->GetSampleRate(detector,ring,sector,strip);
506   AliFMDDebug(15, ("Sample rate for FMD%d%c[%2d,%3d] = %d", 
507                    detector, ring, sector, strip, rate));
508   if (rate < 1 || rate > 4) {
509     AliWarning(Form("Invalid sample rate for for FMD%d%c[%2d,%3d] = %d", 
510                     detector, ring, sector, strip, rate));
511     rate = 1;
512   }
513
514   // In case we don't oversample, just return the end value. 
515   if (rate == 1) {
516     Float_t    a = edep * convF + ped;
517     if (a < 0) a = 0;
518     counts[0]    = UShort_t(TMath::Min(a, Float_t(maxAdc)));
519     AliFMDDebug(15, ("FMD%d%c[%2d,%3d]: converting ELoss %f to "
520                      "ADC %4d (%f,%d)",
521                      detector,ring,sector,strip,edep,counts[0],convF,ped));
522     return;
523   }
524
525   
526   // Create a pedestal 
527   Float_t b = fShapingTime;
528   for (Ssiz_t i = 0; i < rate;  i++) {
529     Float_t t  = Float_t(i) / rate + 1./rate;
530     Float_t s  = edep + (last - edep) * TMath::Exp(-b * t);
531     Float_t a  = Int_t(s * convF + ped);
532     if (a < 0) a = 0;
533     counts[i]  = UShort_t(TMath::Min(a, Float_t(maxAdc)));
534   }
535   AliFMDDebug(15, ("Converted edep = %f to ADC (%x,%x,%x,%x) "
536                    "[gain: %f=(%f/%f*%f), pedestal: %d, rate: %d]", 
537                    edep, counts[0], counts[1], counts[2], counts[3], 
538                    convF, param->GetDACPerMIP(),param->GetEdepMip(),
539                    param->GetPulseGain(detector,ring,sector,strip), 
540                    ped, rate));
541 }
542
543 //____________________________________________________________________
544 void
545 AliFMDBaseDigitizer::AddDigit(UShort_t        detector, 
546                               Char_t          ring,
547                               UShort_t        sector, 
548                               UShort_t        strip, 
549                               Float_t         /* edep */, 
550                               UShort_t        count1, 
551                               Short_t         count2, 
552                               Short_t         count3,
553                               Short_t         count4,
554                               UShort_t        /* ntot */, 
555                               UShort_t        /* nprim */,
556                               const TArrayI&  refs) const
557 {
558   // Add a digit or summable digit
559   
560   fFMD->AddDigitByFields(detector, ring, sector, strip, 
561                          count1, count2, count3, count4, refs);
562 }
563
564 //____________________________________________________________________
565 TTree*
566 AliFMDBaseDigitizer::MakeOutputTree(AliLoader* loader)
567 {
568   // Create output tree using loader.   If the passed loader differs
569   // from the currently set loader in the FMD object, reset the FMD
570   // loader to be the passed loader.   This is for the cases wher the
571   // output is different from the output. 
572   AliFMDDebug(5, ("Making digits tree"));
573   loader->LoadDigits("UPDATE"); // "RECREATE");
574   TTree* out = loader->TreeD();
575   if (!out) loader->MakeTree("D");
576   out = loader->TreeD(); 
577   if (out) { 
578     out->Reset();
579     if (loader != fFMD->GetLoader()) 
580       fFMD->SetLoader(loader);
581     fFMD->MakeBranch("D");
582   }
583   return out;
584 }
585
586 //____________________________________________________________________
587 void
588 AliFMDBaseDigitizer::StoreDigits(AliLoader* loader)
589 {
590   // Write the digits to disk 
591   AliFMDDebug(5, ("Storing %d digits",   fFMD->Digits()->GetEntries()));
592   loader->WriteDigits("OVERWRITE");
593   loader->UnloadDigits();
594   // Reset the digits in the AliFMD object 
595   fFMD->ResetDigits();
596 }
597
598 //____________________________________________________________________
599 //
600 // EOF
601 // 
602
603
604
605