]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDHitDigitizer.cxx
updating ChangeLog
[u/mrichter/AliRoot.git] / FMD / AliFMDHitDigitizer.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: AliFMDHitDigitizer.cxx 28055 2008-08-18 00:33:20Z cholm $ */
16 /** @file    AliFMDHitDigitizer.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
26 // 
27 //  Digits consists of
28 //   - Detector #
29 //   - Ring ID                                             
30 //   - Sector #     
31 //   - Strip #
32 //   - ADC count in this channel                                  
33 //
34 // As the Digits and SDigits have so much in common, the classes
35 // AliFMDHitDigitizer and AliFMDSDigitizer are implemented via a base
36 // class AliFMDBaseDigitizer.
37 //
38 //                 +---------------------+
39 //                 | AliFMDBaseDigitizer |
40 //                 +---------------------+
41 //                           ^
42 //                           |
43 //                +----------+---------+
44 //                |                    |
45 //      +-----------------+     +------------------+
46 //      | AliFMDHitDigitizer |  | AliFMDSDigitizer |
47 //      +-----------------+     +------------------+
48 //
49 // These classes has several paramters: 
50 //
51 //     fPedestal
52 //     fPedestalWidth
53 //         (Only AliFMDHitDigitizer)
54 //         Mean and width of the pedestal.  The pedestal is simulated
55 //         by a Guassian, but derived classes my override MakePedestal
56 //         to simulate it differently (or pick it up from a database).
57 //
58 //     fVA1MipRange
59 //         The dymamic MIP range of the VA1_ALICE pre-amplifier chip 
60 //
61 //     fAltroChannelSize
62 //         The largest number plus one that can be stored in one
63 //         channel in one time step in the ALTRO ADC chip. 
64 //
65 //     fSampleRate
66 //         How many times the ALTRO ADC chip samples the VA1_ALICE
67 //         pre-amplifier signal.   The VA1_ALICE chip is read-out at
68 //         10MHz, while it's possible to drive the ALTRO chip at
69 //         25MHz.  That means, that the ALTRO chip can have time to
70 //         sample each VA1_ALICE signal up to 2 times.  Although it's
71 //         not certain this feature will be used in the production,
72 //         we'd like have the option, and so it should be reflected in
73 //         the code.
74 //
75 // These parameters are fetched from OCDB via the mananger AliFMDParameters.
76 //
77 // The shaping function of the VA1_ALICE is generally given by 
78 //
79 //      f(x) = A(1 - exp(-Bx))
80 //
81 // where A is the total charge collected in the pre-amp., and B is a
82 // paramter that depends on the shaping time of the VA1_ALICE circut.
83 // 
84 // When simulating the shaping function of the VA1_ALICe
85 // pre-amp. chip, we have to take into account, that the shaping
86 // function depends on the previous value of read from the pre-amp. 
87 //
88 // That results in the following algorithm:
89 //
90 //    last = 0;
91 //    FOR charge IN pre-amp. charge train DO 
92 //      IF last < charge THEN 
93 //        f(t) = (charge - last) * (1 - exp(-B * t)) + last
94 //      ELSE
95 //        f(t) = (last - charge) * exp(-B * t) + charge)
96 //      ENDIF
97 //      FOR i IN # samples DO 
98 //        adc_i = f(i / (# samples))
99 //      DONE
100 //      last = charge
101 //   DONE
102 //
103 // Here, 
104 //
105 //   pre-amp. charge train 
106 //       is a series of 128 charges read from the VA1_ALICE chip
107 //
108 //   # samples
109 //       is the number of times the ALTRO ADC samples each of the 128
110 //       charges from the pre-amp. 
111 //
112 // Where Q is the total charge collected by the VA1_ALICE
113 // pre-amplifier.   Q is then given by 
114 //
115 //           E S 
116 //      Q =  - -
117 //           e R
118 //
119 // where E is the total energy deposited in a silicon strip, R is the
120 // dynamic range of the VA1_ALICE pre-amp (fVA1MipRange), e is the
121 // energy deposited by a single MIP, and S ALTRO channel size in each
122 // time step (fAltroChannelSize).  
123 //
124 // The energy deposited per MIP is given by 
125 //
126 //      e = M * rho * w 
127 //
128 // where M is the universal number 1.664, rho is the density of
129 // silicon, and w is the depth of the silicon sensor. 
130 //
131 // The final ADC count is given by 
132 //
133 //      C' = C + P
134 //
135 // where P is the (randomized) pedestal (see MakePedestal)
136 //
137 // This class uses the class template AliFMDMap<Type> to make an
138 // internal cache of the energy deposted of the hits.  The class
139 // template is instantasized as 
140 //
141 //  typedef AliFMDMap<std::pair<Float_t, UShort_t> > AliFMDEdepMap;
142 //
143 // The first member of the values is the summed energy deposition in a
144 // given strip, while the second member of the values is the number of
145 // hits in a given strip.  Using the second member, it's possible to
146 // do some checks on just how many times a strip got hit, and what
147 // kind of error we get in our reconstructed hits.  Note, that this
148 // information is currently not written to the digits tree.  I think a
149 // QA (Quality Assurance) digit tree is better suited for that task.
150 // However, the information is there to be used in the future. 
151 //
152 //
153 // Latest changes by Christian Holm Christensen
154 //
155 //////////////////////////////////////////////////////////////////////////////
156
157 //      /1
158 //      |           A(-1 + B + exp(-B))
159 //      | f(x) dx = ------------------- = 1
160 //      |                    B
161 //      / 0
162 //
163 // and B is the a parameter defined by the shaping time (fShapingTime).  
164 //
165 // Solving the above equation, for A gives
166 //
167 //                 B
168 //      A = ----------------
169 //          -1 + B + exp(-B)
170 //
171 // So, if we define the function g: [0,1] -> [0:1] by 
172 //
173 //               / v
174 //               |              Bu + exp(-Bu) - Bv - exp(-Bv) 
175 //      g(u,v) = | f(x) dx = -A -----------------------------
176 //               |                            B
177 //               / u
178 //
179 // we can evaluate the ALTRO sample of the VA1_ALICE pre-amp between
180 // any two times (u, v), by 
181 //       
182 //
183 //                                B         Bu + exp(-Bu) - Bv - exp(-Bv)
184 //      C = Q g(u,v) = - Q ---------------- -----------------------------
185 //                         -1 + B + exp(-B)              B                  
186 //
187 //               Bu + exp(-Bu) - Bv - exp(-Bv) 
188 //        = -  Q -----------------------------
189 //                    -1 + B + exp(-B)
190 //
191
192 #include <TTree.h>              // ROOT_TTree
193 #include "AliFMDDebug.h"        // Better debug macros
194 #include "AliFMDHitDigitizer.h" // ALIFMDDIGITIZER_H
195 #include "AliFMD.h"             // ALIFMD_H
196 #include "AliFMDDigit.h"        // ALIFMDDIGIT_H
197 #include "AliFMDParameters.h"   // ALIFMDPARAMETERS_H
198 #include <AliRun.h>             // ALIRUN_H
199 #include <AliLoader.h>          // ALILOADER_H
200 #include <AliRunLoader.h>       // ALIRUNLOADER_H
201 #include <AliFMDHit.h>
202 #include <AliStack.h>
203 #include <TFile.h>
204 #include <TParticle.h>
205
206 //====================================================================
207 ClassImp(AliFMDHitDigitizer)    
208 #if 0
209 ;
210 #endif
211
212 //____________________________________________________________________
213 AliFMDHitDigitizer::AliFMDHitDigitizer(AliFMD* fmd, Output_t  output)
214   : AliFMDBaseDigitizer("FMD", (output == kDigits ? 
215                                 "FMD Hit->Digit digitizer" :
216                                 "FMD Hit->SDigit digitizer")),
217     fOutput(output), 
218     fHoldTime(2e-6),
219     fStack(0)
220 {
221   fFMD = fmd;
222 }
223
224 //____________________________________________________________________
225 AliFMDHitDigitizer& 
226 AliFMDHitDigitizer::operator=(const AliFMDHitDigitizer& o) 
227 {
228   /** 
229    * Assignment operator
230    *
231    * @param o Object to assign from 
232    * @return Reference to this 
233    */
234   if (&o == this) return *this; 
235   AliFMDBaseDigitizer::operator=(o);
236   fHoldTime    = o.fHoldTime;
237   fOutput      = o.fOutput;
238   fStack       = o.fStack;
239   return *this;
240 }
241
242 //____________________________________________________________________
243 void
244 AliFMDHitDigitizer::Digitize(Option_t* /*option*/)
245 {
246   // Run this digitizer 
247   // Get an inititialize parameter manager
248   AliFMDParameters::Instance()->Init();
249   if (AliLog::GetDebugLevel("FMD","") >= 10) 
250     AliFMDParameters::Instance()->Print("ALL");
251
252   // Get loader, and ask it to read in the hits 
253   AliLoader* loader = fFMD->GetLoader();
254   if (!loader) { 
255     AliError("Failed to get loader from detector object");
256     return;
257   }
258   loader->LoadHits("READ");
259   
260   // Get the run loader 
261   AliRunLoader* runLoader = loader->GetRunLoader();
262   if (!runLoader) {
263     AliError("Failed to get run loader from loader");
264     return;
265   }
266   
267   // Now loop over events
268   Int_t nEvents = runLoader->GetNumberOfEvents();
269   for (Int_t event = 0; event < nEvents; event++) { 
270     // Get the current event folder. 
271     TFolder* folder = loader->GetEventFolder();
272     if (!folder) { 
273       AliError("Failed to get event folder from loader");
274       return;
275     }
276
277     // Get the run-loader of this event. 
278     const char* loaderName = AliRunLoader::GetRunLoaderName();
279     AliRunLoader* thisLoader = 
280       static_cast<AliRunLoader*>(folder->FindObject(loaderName));
281     if (!thisLoader) { 
282       AliError(Form("Failed to get loader '%s' from event folder", loaderName));
283       return;
284     }
285     
286     // Read in the event
287     AliFMDDebug(1, ("Now digitizing (Hits->%s) event # %d", 
288                     (fOutput == kDigits ? "digits" : "sdigits"), event));
289     thisLoader->GetEvent(event);
290     
291     // Load kinematics to get primary information for SDigits
292     fStack = 0;
293     if (fOutput == kSDigits) {
294       if (thisLoader->LoadKinematics("READ")) {
295         AliError("Failed to get kinematics from event loader");
296         return;
297       }
298       AliFMDDebug(5, ("Loading stack of kinematics"));
299       fStack = thisLoader->Stack();
300     }
301
302     // Check that we have the hits 
303     if (!loader->TreeH() && loader->LoadHits()) {
304       AliError("Failed to load hits");
305       return;
306     }
307     TTree*   hitsTree = loader->TreeH();
308     TBranch* hitsBranch = hitsTree->GetBranch(fFMD->GetName());
309     if (!hitsBranch) { 
310       AliError("Failed to get hits branch in tree");
311       return;
312     }
313     // Check that we can make the output digits - This must come
314     // before AliFMD::SetBranchAddress
315     TTree* outTree = MakeOutputTree(loader);
316     if (!outTree) { 
317       AliError("Failed to get output tree");
318       return;
319     }
320     AliFMDDebug(5, ("Output tree name for %s is '%s'", 
321                     (fOutput == kDigits ? "digits" : "sdigits"),
322                     outTree->GetName()));
323     if (AliLog::GetDebugLevel("FMD","") >= 5) {
324       TFile* file = outTree->GetCurrentFile();
325       if (!file) {
326         AliWarning("Output tree has no file!");
327       }
328       else { 
329         AliFMDDebug(5, ("Output tree file %s content:", file->GetName()));
330         file->ls();
331       }
332     }
333
334     // Set-up the branch addresses 
335     fFMD->SetTreeAddress();
336     
337     // Now sum all contributions in cache 
338     SumContributions(hitsBranch);
339     loader->UnloadHits();
340
341     // And now digitize the hits 
342     DigitizeHits();
343     
344     // Write digits to tree
345     Int_t write = outTree->Fill();
346     AliFMDDebug(5, ("Wrote %d bytes to digit tree", write));
347
348     // Store the digits
349     StoreDigits(loader);
350
351   }  
352 }
353
354 //____________________________________________________________________
355 TTree*
356 AliFMDHitDigitizer::MakeOutputTree(AliLoader* loader)
357 {
358   /** 
359    * Make the output tree using the passed loader 
360    *
361    * @param loader 
362    * @return The generated tree. 
363    */
364   if (fOutput == kDigits) 
365     return AliFMDBaseDigitizer::MakeOutputTree(loader);
366   
367   AliFMDDebug(5, ("Making sdigits tree"));
368   loader->LoadSDigits("UPDATE"); // RECREATE");
369   TTree* out = loader->TreeS();
370   if (!out) loader->MakeTree("S");
371   out = loader->TreeS(); 
372   if (out) { 
373     out->Reset();
374     fFMD->MakeBranch("S");
375   }
376   return out;
377 }
378
379
380 //____________________________________________________________________
381 void
382 AliFMDHitDigitizer::SumContributions(TBranch* hitsBranch) 
383 {
384   // Sum energy deposited contributions from each hit in a cache
385   // (fEdep).  
386   
387   // Clear array of deposited energies 
388   fEdep.Reset();
389   
390   // Get a list of hits from the FMD manager 
391   AliFMDDebug(5, ("Get array of FMD hits"));
392   TClonesArray *fmdHits = fFMD->Hits();
393   
394
395   // Get number of entries in the tree 
396   AliFMDDebug(5, ("Get # of tracks"));
397   Int_t ntracks  = Int_t(hitsBranch->GetEntries());
398   AliFMDDebug(5, ("We got %d tracks", ntracks));
399
400   Int_t read = 0;
401   // Loop over the tracks in the 
402   for (Int_t track = 0; track < ntracks; track++)  {
403     // Read in entry number `track' 
404     read += hitsBranch->GetEntry(track);
405
406     // Get the number of hits 
407     Int_t nhits = fmdHits->GetEntries ();
408     for (Int_t hit = 0; hit < nhits; hit++) {
409       // Get the hit number `hit'
410       AliFMDHit* fmdHit = 
411         static_cast<AliFMDHit*>(fmdHits->UncheckedAt(hit));
412
413       // Ignore hits that arrive too late
414       if (fmdHit->Time() > fHoldTime) continue;
415       
416
417       // Check if this is a primary particle
418       Bool_t isPrimary = kTRUE;
419       Int_t  trackno   = -1;
420       if (fStack) {
421         trackno = fmdHit->Track();
422         AliFMDDebug(10, ("Will get track # %d/%d from entry # %d", 
423                         trackno, fStack->GetNtrack(), track));
424         if (fStack->GetNtrack() < trackno) {
425           AliError(Form("Track number %d/%d out of bounds", 
426                         trackno, fStack->GetNtrack()));
427           continue;
428         }
429 #if 1
430         isPrimary = fStack->IsPhysicalPrimary(trackno);
431 #else // This is our hand-crafted code.  We use the ALICE definition
432         TParticle* part    = fStack->Particle(trackno);
433         isPrimary          = part->IsPrimary();
434         if (!isPrimary) { 
435           // Extended testing of mother status - this is for Pythia6.
436           Int_t      mother1   = part->GetFirstMother();
437           TParticle* mother    = fStack->Particle(mother1);
438           if (!mother || mother->GetStatusCode() > 1)
439             isPrimary = kTRUE;
440           AliFMDDebug(15,
441                       ("Track %d secondary, mother: %d - %s - status %d: %s", 
442                        trackno, mother1, 
443                        (mother ? "found"                 : "not found"), 
444                        (mother ? mother->GetStatusCode() : -1),
445                        (isPrimary ? "primary" : "secondary")));
446         }
447 #endif
448       }
449     
450       // Extract parameters 
451       AliFMDDebug(15,("Adding contribution %7.5f for FMD%d%c[%2d,%3d] "
452                       " for trackno %6d (%s)", 
453                       fmdHit->Edep(),
454                       fmdHit->Detector(), 
455                       fmdHit->Ring(),
456                       fmdHit->Sector(), 
457                       fmdHit->Strip(), 
458                       trackno, 
459                       (isPrimary ? "primary" : "secondary")));
460       AddContribution(fmdHit->Detector(),
461                       fmdHit->Ring(),
462                       fmdHit->Sector(),
463                       fmdHit->Strip(),
464                       fmdHit->Edep(), 
465                       isPrimary, 
466                       1, 
467                       &trackno);
468     }  // hit loop
469   } // track loop
470   AliFMDDebug(5, ("Size of cache: %d bytes, read %d bytes", 
471                   int(sizeof(fEdep)), read));
472 }
473
474
475 //____________________________________________________________________
476 UShort_t
477 AliFMDHitDigitizer::MakePedestal(UShort_t  detector, 
478                                  Char_t    ring, 
479                                  UShort_t  sector, 
480                                  UShort_t  strip) const 
481 {
482   // Make a pedestal 
483   if (fOutput == kSDigits) return 0;
484   return AliFMDBaseDigitizer::MakePedestal(detector, ring, sector, strip);
485 }
486
487
488
489 //____________________________________________________________________
490 void
491 AliFMDHitDigitizer::AddDigit(UShort_t        detector, 
492                              Char_t          ring,
493                              UShort_t        sector, 
494                              UShort_t        strip, 
495                              Float_t         edep, 
496                              UShort_t        count1, 
497                              Short_t         count2, 
498                              Short_t         count3,
499                              Short_t         count4, 
500                              UShort_t        ntotal,
501                              UShort_t        nprim, 
502                              const TArrayI&  refs) const
503 {
504   // Add a digit or summable digit
505   if (fOutput == kDigits) { 
506     AliFMDDebug(15,("Adding digit for FMD%d%c[%2d,%3d] = (%x,%x,%x,%x)",
507                     detector, ring, sector, strip, 
508                     count1, count2, count3, count4));
509     AliFMDBaseDigitizer::AddDigit(detector, ring, sector, strip, 0,
510                                   count1, count2, count3, count4, 
511                                   ntotal, nprim, refs);
512     return;
513   }
514   if (edep <= 0) { 
515     AliFMDDebug(15, ("Digit edep = %f <= 0 for FMD%d%c[%2d,%3d]", 
516                     edep, detector, ring, sector, strip));
517     return;
518   }
519   if (count1 == 0 && count2 <= 0 && count3 <= 0 && count4 <= 0) {
520     AliFMDDebug(15, ("Digit counts = (%x,%x,%x,%x) <= 0 for FMD%d%c[%2d,%3d]", 
521                     count1, count2, count3, count4, 
522                     detector, ring, sector, strip));
523     return;
524   }
525   AliFMDDebug(15, ("Adding sdigit for FMD%d%c[%2d,%3d] = "
526                    "(%x,%x,%x,%x) [%d/%d] %d",
527                    detector, ring, sector, strip, 
528                    count1, count2, count3, count4, nprim, ntotal, refs.fN));
529   fFMD->AddSDigitByFields(detector, ring, sector, strip, edep,
530                           count1, count2, count3, count4, 
531                           ntotal, nprim, fStoreTrackRefs ? refs.fArray : 0);
532   if (fStoreTrackRefs && nprim > 3) fIgnoredLabels += nprim - 3;
533 }
534
535 //____________________________________________________________________
536 void
537 AliFMDHitDigitizer::CheckDigit(AliFMDDigit*    digit,
538                                UShort_t        nhits,
539                                const TArrayI&  counts) 
540 {
541   // Check that digit is consistent
542   AliFMDParameters* param = AliFMDParameters::Instance();
543   UShort_t          det   = digit->Detector();
544   Char_t            ring  = digit->Ring();
545   UShort_t          sec   = digit->Sector();
546   UShort_t          str   = digit->Strip();
547   Float_t           mean  = param->GetPedestal(det,ring,sec,str);
548   Float_t           width = param->GetPedestalWidth(det,ring,sec,str);
549   UShort_t          range = param->GetVA1MipRange();
550   UShort_t          size  = param->GetAltroChannelSize();
551   Int_t             integral = counts[0];
552   if (counts[1] >= 0) integral += counts[1];
553   if (counts[2] >= 0) integral += counts[2];
554   if (counts[3] >= 0) integral += counts[3];
555   integral -= Int_t(mean + 2 * width);
556   if (integral < 0) integral = 0;
557   
558   Float_t convF = Float_t(range) / size;
559   Float_t mips  = integral * convF;
560   if (mips > Float_t(nhits) + .5 || mips < Float_t(nhits) - .5) 
561     Warning("CheckDigit", "Digit -> %4.2f MIPS != %d +/- .5 hits", 
562             mips, nhits);
563 }
564
565 //____________________________________________________________________
566 void
567 AliFMDHitDigitizer::StoreDigits(const AliLoader* loader)
568 {
569   /** 
570    * Store the data using the loader 
571    *
572    * @param loader The loader 
573    */
574   if (fOutput == kDigits) { 
575     AliFMDBaseDigitizer::StoreDigits(loader);
576     return;
577   }
578   AliFMDDebug(5, ("Storing %d sdigits",   fFMD->SDigits()->GetEntries()));
579   // Write the digits to disk 
580   loader->WriteSDigits("OVERWRITE");
581   loader->UnloadSDigits();
582   // Reset the digits in the AliFMD object 
583   fFMD->ResetSDigits();
584 }
585
586
587 //____________________________________________________________________
588 //
589 // EOF
590 // 
591
592
593
594