]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDParameters.cxx
Added possibility to process only charged particles (without smearing and TPC accepta...
[u/mrichter/AliRoot.git] / FMD / AliFMDParameters.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 /* $Id$ */
16 /** @file    AliFMDParameters.cxx
17     @author  Christian Holm Christensen <cholm@nbi.dk>
18     @date    Mon Mar 27 12:44:26 2006
19     @brief   Manager of FMD parameters     
20 */
21 //____________________________________________________________________
22 //                                                                          
23 // Forward Multiplicity Detector based on Silicon wafers. 
24 //
25 // This class is a singleton that handles various parameters of
26 // the FMD detectors.  
27 // The manager normally serves the parameters from the Conditions
28 // Database (CDB).  These are retrivied by the member function
29 // `Init'.  Optionally, the class can serve hard-coded constants, if
30 // no CDB is available. 
31 //                                                       
32 #include "AliFMDDebug.h"                   // ALILOG_H
33 #include "AliFMDParameters.h"      // ALIFMDPARAMETERS_H
34 #include "AliFMDGeometry.h"        // ALIFMDGEOMETRY_H
35 #include "AliFMDRing.h"            // ALIFMDRING_H
36 #include "AliFMDCalibGain.h"       // ALIFMDCALIBGAIN_H
37 #include "AliFMDCalibPedestal.h"   // ALIFMDCALIBPEDESTAL_H
38 #include "AliFMDCalibSampleRate.h" // ALIFMDCALIBSAMPLERATE_H
39 #include "AliFMDCalibStripRange.h" // ALIFMDCALIBSTRIPRANGE_H
40 #include "AliFMDAltroMapping.h"    // ALIFMDALTROMAPPING_H
41 #include <AliCDBManager.h>         // ALICDBMANAGER_H
42 #include <AliCDBEntry.h>           // ALICDBMANAGER_H
43 #include <AliFMDPreprocessor.h>
44 #include <AliLog.h>
45 #include <Riostream.h>
46 #include <sstream>
47 #include <TSystem.h>
48 #include <TArrayF.h>
49 #include <TH2D.h>
50
51 //====================================================================
52 ClassImp(AliFMDParameters)
53 #if 0
54   ; // This is here to keep Emacs for indenting the next line
55 #endif
56
57 //____________________________________________________________________
58 AliFMDParameters* AliFMDParameters::fgInstance = 0;
59
60 //____________________________________________________________________
61 const char* AliFMDParameters::fgkPulseGain          = "FMD/Calib/PulseGain";
62 const char* AliFMDParameters::fgkPedestal           = "FMD/Calib/Pedestal";
63 const char* AliFMDParameters::fgkDead               = "FMD/Calib/Dead";
64 const char* AliFMDParameters::fgkSampleRate         = "FMD/Calib/SampleRate";
65 const char* AliFMDParameters::fgkAltroMap           = "FMD/Calib/AltroMap";
66 const char* AliFMDParameters::fgkZeroSuppression    = "FMD/Calib/ZeroSuppression";
67 const char* AliFMDParameters::fgkStripRange         = "FMD/Calib/StripRange";
68 const char* AliFMDParameters::fkPedestalShuttleID   = "pedestals";
69 const char* AliFMDParameters::fkGainShuttleID       = "gains";
70 const char* AliFMDParameters::fkConditionsShuttleID = "conditions";
71
72 //____________________________________________________________________
73 AliFMDParameters* 
74 AliFMDParameters::Instance() 
75 {
76   // Get static instance 
77   if (!fgInstance) fgInstance = new AliFMDParameters;
78   return fgInstance;
79 }
80
81 //____________________________________________________________________
82 AliFMDParameters::AliFMDParameters() 
83   : fIsInit(kFALSE),
84     fkSiDeDxMip(1.664), 
85     fVA1MipRange(0),
86     fAltroChannelSize(0),
87     fChannelsPerAltro(0),
88     fPedestalFactor(0),
89     fZSPre(0),
90     fZSPost(0),
91     fZSPedSubtract(kFALSE),
92     fFixedPedestal(0),
93     fFixedPedestalWidth(0),
94     fFixedZeroSuppression(0),
95     fFixedSampleRate(0),
96     fFixedThreshold(0),
97     fFixedMinStrip(0),
98     fFixedMaxStrip(0),
99     fFixedPulseGain(0), 
100     fEdepMip(0),
101     fHasCompleteHeader(kTRUE),
102     fZeroSuppression(0), 
103     fSampleRate(0), 
104     fPedestal(0), 
105     fPulseGain(0), 
106     fDeadMap(0), 
107     fAltroMap(0), 
108     fStripRange(0),
109     fOfflineTriggerLowCut(0.1),
110     fOfflineTriggerHitCut(0.6)
111 {
112   // Default constructor 
113   SetVA1MipRange();
114   SetAltroChannelSize();
115   SetChannelsPerAltro();
116   SetZeroSuppression();
117   SetSampleRate();
118   SetPedestal();
119   SetPedestalWidth();
120   SetPedestalFactor();
121   SetThreshold();
122   SetStripRange();
123   fAltroMap = new AliFMDAltroMapping;
124 }
125
126 //__________________________________________________________________
127 void
128 AliFMDParameters::Init(Bool_t forceReInit, UInt_t what)
129 {
130   // Initialize the parameters manager.  We need to get stuff from the
131   // CDB here. 
132   if (forceReInit) fIsInit = kFALSE;
133   if (fIsInit) return;
134   if (what & kPulseGain)       InitPulseGain();
135   if (what & kPedestal)        InitPedestal();
136   if (what & kDeadMap)         InitDeadMap();
137   if (what & kSampleRate)      InitSampleRate();
138   if (what & kZeroSuppression) InitZeroSuppression();
139   if (what & kAltroMap)        InitAltroMap();
140   if (what & kStripRange)      InitStripRange();
141   fIsInit = kTRUE;
142 }
143 //__________________________________________________________________
144 void
145 AliFMDParameters::Init(AliFMDPreprocessor* pp, Bool_t forceReInit, UInt_t what)
146 {
147   // Initialize the parameters manager.  We need to get stuff from the
148   // CDB here. 
149   if (forceReInit) fIsInit = kFALSE;
150   if (fIsInit) return;
151   if (what & kPulseGain)       InitPulseGain(pp);
152   if (what & kPedestal)        InitPedestal(pp);
153   if (what & kDeadMap)         InitDeadMap(pp);
154   if (what & kSampleRate)      InitSampleRate(pp);
155   if (what & kZeroSuppression) InitZeroSuppression(pp);
156   if (what & kAltroMap)        InitAltroMap(pp);
157   if (what & kStripRange)      InitStripRange(pp);
158   fIsInit = kTRUE;
159 }
160
161 //__________________________________________________________________
162 Bool_t
163 AliFMDParameters::CheckFile(const char* prefix, 
164                             const char* path, 
165                             int         number, 
166                             TString&    f) const
167 {
168   f = (Form("%s%d.csv", prefix, number));
169   AliFMDDebug(5, ("Checking if %s exists in %s ...", f.Data(), path));
170   f = gSystem->Which(path, f.Data());
171   AliFMDDebug(5, ("Got back '%s'", f.Data()));
172   return !f.IsNull();
173 }
174
175 //__________________________________________________________________
176 void
177 AliFMDParameters::Init(const char* path, Bool_t forceReInit, UInt_t what)
178 {
179   // Initialize the parameters manager.  Pedestals, gains, strip
180   // range, and sample rate is read from local comma separated value
181   // files if available.  Otherwise, the calibrations are obtained
182   // from OCDB.
183   if (forceReInit) fIsInit = kFALSE;
184   if (fIsInit) return;
185
186   AliFMDCalibStripRange*  range = 0;
187   AliFMDCalibSampleRate*  rate  = 0;
188   AliFMDCalibPedestal*    peds  = 0;
189   AliFMDCalibGain*        gains = 0;
190
191   for (Int_t i = 1; i <= 3; i++) { 
192     TString f;
193     if (((what & kSampleRate) || (what & kStripRange)) && 
194         CheckFile("conditions", path, i, f)) {
195       if (!rate  && (what & kSampleRate)) rate  = new AliFMDCalibSampleRate;
196       if (!range && (what & kStripRange)) range = new AliFMDCalibStripRange;
197       std::ifstream in(f.Data());
198       if (range) range->ReadFromFile(in);
199       if (rate)  rate->ReadFromFile(in);
200       in.close();
201     }
202     if ((what & kPedestal) && CheckFile("peds", path, i, f)) {
203       if (!peds) peds  = new AliFMDCalibPedestal;
204       std::ifstream in(f.Data());
205       peds->ReadFromFile(in);
206       in.close();
207     }
208     if ((what & kPulseGain) && CheckFile("gains", path, i, f)) { 
209       if (!gains) gains = new AliFMDCalibGain;
210       std::ifstream in(f.Data());
211       gains->ReadFromFile(in);
212       in.close();
213     }
214   }
215
216   if (range) what &= ~kStripRange;
217   if (rate)  what &= ~kSampleRate;
218   if (peds)  what &= ~kPedestal;
219   if (gains) what &= ~kPulseGain;
220
221   Init(kFALSE, what);
222   
223   if (range) SetStripRange(range);
224   if (rate)  SetSampleRate(rate);
225   if (peds)  SetPedestal(peds);
226   if (gains) SetGain(gains);
227
228   fIsInit = kTRUE;
229 }
230
231 //__________________________________________________________________
232 void
233 AliFMDParameters::MakeDeadMap(Float_t maxNoise, 
234                               Float_t minGain, 
235                               Float_t maxGain)
236 {
237   if (fPedestal)  
238     fDeadMap = fPedestal->MakeDeadMap(maxNoise, fDeadMap);
239   if (fPulseGain) 
240     fDeadMap = fPulseGain->MakeDeadMap(minGain, maxGain, fDeadMap);
241 }
242 //__________________________________________________________________
243 #define DET2IDX(det,ring,sec,str) \
244   (det * 1000 + (ring == 'I' ? 0 : 512) + str)  
245   
246 //__________________________________________________________________
247 void
248 AliFMDParameters::Draw(Option_t* option)
249 {
250   TString opt(option);
251   enum {
252     kLocalPulseGain,       // Path to PulseGain calib object
253     kLocalThreshold,       // Path to PulseGain calib object
254     kLocalPedestal,        // Path to Pedestal calib object
255     kLocalPedestalWidth,   // Path to Pedestal calib object
256     kLocalDead,            // Path to Dead calib object
257     kLocalSampleRate,      // Path to SampleRate calib object
258     kLocalAltroMap,        // Path to AltroMap calib object
259     kLocalZeroSuppression, // Path to ZeroSuppression cal object
260     kLocalMinStripRange,   // Path to strip range cal object
261     kLocalMaxStripRange    // Path to strip range cal object
262   } what;
263     
264   if      (opt.Contains("dead", TString::kIgnoreCase)) 
265     what = kLocalDead;
266   else if (opt.Contains("threshold",TString::kIgnoreCase)) 
267     what = kLocalThreshold;
268   else if (opt.Contains("gain",TString::kIgnoreCase)) 
269     what = kLocalPulseGain;
270   else if (opt.Contains("pedestal",TString::kIgnoreCase)) 
271     what = kLocalPedestal;
272   else if (opt.Contains("noise",TString::kIgnoreCase)) 
273     what = kLocalPedestalWidth;
274   else if (opt.Contains("zero",TString::kIgnoreCase)) 
275     what = kLocalZeroSuppression;
276   else if (opt.Contains("rate",TString::kIgnoreCase)) 
277     what = kLocalSampleRate;
278   else if (opt.Contains("min",TString::kIgnoreCase)) 
279     what = kLocalMinStripRange;
280   else if (opt.Contains("max",TString::kIgnoreCase)) 
281     what = kLocalMaxStripRange;
282   else if (opt.Contains("map",TString::kIgnoreCase)) 
283     what = kLocalAltroMap;
284   else {
285     Warning("Draw", "unknown parameter: %s\n\tShould be one of\n\t"
286             "dead, threshold, gain, pedestal, noise, zero, rate, "
287             "min, max, map",  
288             option); 
289     return;
290   }
291
292   TArrayD xbins(3 * 512 + 2 * 256 + 5);
293   Int_t i = 1;
294   Bool_t skip = kTRUE;
295   for (UShort_t det = 1; det <= 3; det++) {
296     UShort_t nRings = (det == 1 ? 1 : 2);
297     for (UShort_t iring = 0; iring < nRings; iring++) {
298       UShort_t nStrip  = (iring == 0 ? 512 : 256);
299       Char_t   ring    = (iring == 0 ? 'I' : 'O');
300       for (UShort_t str = 0; str < nStrip; str++) {
301         // UShort_t nSec    = (iring == 0 ? 20  : 40);
302         // Char_t   ring    = (iring == 0 ? 'I' : 'O');
303         // for (UShort_t sec = 0; sec < nSec; sec++) {
304         Int_t idx = DET2IDX(det, ring, 0, str);
305         // Int_t idx = DET2IDX(det, ring, sec, 0);
306         if (skip) {
307           xbins[i-1] = idx - .5;
308           skip  = kFALSE;
309         }
310         xbins[i] = idx + .5;
311         i++;
312       }
313       skip = kTRUE;
314       i++;
315     }
316   }
317   TArrayD ybins(41);
318   for (/*Int_t*/ i = 0; i < ybins.fN; i++) ybins[i] = Float_t(i - .5);
319   TH2D* hist = new TH2D("calib", Form("Calibration %s", option), 
320                         xbins.fN-1, xbins.fArray,  
321                         ybins.fN-1, ybins.fArray);
322   hist->GetXaxis()->SetTitle("1000 #times detector + 512 #times ring + strip");
323   hist->GetYaxis()->SetTitle("sector");
324   
325   // hist->Draw("Lego");
326   // return;
327   
328   for (UShort_t det = 1; det <= 3; det++) {
329     UShort_t nRings = (det == 1 ? 1 : 2);
330     for (UShort_t iring = 0; iring < nRings; iring++) {
331       UShort_t nSector = (iring == 0 ?  20 : 40);
332       UShort_t nStrip  = (iring == 0 ? 512 : 256);
333       Char_t   ring    = (iring == 0 ? 'I' : 'O');
334       for (UShort_t sec = 0; sec < nSector; sec++) {
335         for (UShort_t str = 0; str < nStrip; str++) {
336           Int_t idx = DET2IDX(det, ring, sec, str);
337           UShort_t ddl, addr, time, sam=0;
338           Double_t val = 0;
339           switch (what) {
340           case kLocalPulseGain:       // Path to PulseGain calib object
341             val = GetPulseGain(det,ring,sec,str); break;
342           case kLocalThreshold:       // Path to PulseGain calib object
343             val = GetThreshold(); break;
344           case kLocalPedestal:        // Path to Pedestal calib object
345             val = GetPedestal(det,ring,sec,str); break;
346           case kLocalPedestalWidth:   // Path to Pedestal calib object
347             val = GetPedestalWidth(det,ring,sec,str); break;
348           case kLocalDead:            // Path to Dead calib object
349             val = IsDead(det,ring,sec,str); break;
350           case kLocalSampleRate:      // Path to SampleRate calib object
351             val = GetSampleRate(det,ring,sec,str); break;
352           case kLocalAltroMap:        // Path to AltroMap calib object
353             Detector2Hardware(det,ring,sec,str,sam,ddl,addr,time); 
354             val = addr; break;
355           case kLocalZeroSuppression: // Path to ZeroSuppression cal object
356             val = GetZeroSuppression(det,ring,sec,str); break;
357           case kLocalMinStripRange:   // Path to strip range cal object
358             val = GetMinStrip(det,ring,sec,str); break;
359           case kLocalMaxStripRange:    // Path to strip range cal object
360             val = GetMaxStrip(det,ring,sec,str); break;
361           }
362           hist->Fill(idx,sec,val);
363           // hist->Fill(idx,str,val);
364         }
365       }
366     }
367   }
368   hist->Draw("lego");
369 }
370
371 //__________________________________________________________________
372 void
373 AliFMDParameters::Print(Option_t* option) const
374 {
375   // Print information. 
376   // If option contains an 'A' then everything is printed. 
377   // If the option contains the string "FMD" the function will search 
378   // for detector, ring, sector, and strip numbers to print, in the
379   // format 
380   // 
381   //    FMD<detector><ring>[<sector>,<string>] 
382   // 
383   // The wild card '*' means all of <detector>, <ring>, <sector>, or 
384   // <strip>. 
385   TString opt(option);
386   Bool_t showStrips  = opt.Contains("a", TString::kIgnoreCase);
387   UShort_t ds[]      = { 1, 2, 3, 0 };
388   Char_t   rs[]      = { 'I', 'O', '\0' };
389   UShort_t minStrip  = 0;
390   UShort_t maxStrip  = 512;
391   UShort_t minSector = 0;
392   UShort_t maxSector = 40;
393   
394   
395   if (opt.Contains("fmd",TString::kIgnoreCase)) {
396     Int_t   i    = opt.Index("fmd",TString::kIgnoreCase);
397     Int_t   j    = opt.Index("]",TString::kIgnoreCase);
398     if (j != kNPOS)
399       showStrips    = kTRUE;
400     else 
401       j = opt.Length();
402     enum {
403       kReadDet, 
404       kReadRing, 
405       kReadLbrack,
406       kReadSector,
407       kReadComma,
408       kReadStrip,
409       kReadRbrack, 
410       kEnd
411     } state = kReadDet;
412     std::stringstream s(opt(i+4, j-i-3).Data());
413     while (state != kEnd) {
414       Char_t tmp = s.peek();
415       if (tmp == ' ' || tmp == '\t') {
416         s.get();
417         continue;
418       }
419       switch (state) {
420       case kReadDet: { // First, try to kRead the detector 
421         if (tmp == '*') s.get();
422         else { 
423           UShort_t det;
424           s >> det;
425           if (!s.bad()) {
426             ds[0] = det;
427             ds[1] = 0;
428           }
429         }
430         state = (s.bad() ? kEnd : kReadRing);
431       } break;
432       case kReadRing: { // Then try to read the ring;
433         Char_t ring;
434         s >> ring;
435         if (ring != '*' && !s.bad()) {
436           rs[0] = ring;
437           rs[1] = '\0';
438         }
439         state = (s.bad() ? kEnd : kReadLbrack);
440       } break;
441       case kReadLbrack: { // Try to read a left bracket 
442         Char_t lbrack;
443         s >> lbrack;
444         state = (s.bad() ? kEnd : kReadSector);
445       } break;
446       case kReadSector: { // Try to read a sector 
447         if (tmp == '*') s.get();
448         else {
449           UShort_t sec;
450           s >> sec;
451           if (!s.bad()) {
452             minSector = sec;
453             maxSector = sec + 1;
454           }
455         }
456         state = (s.bad() ? kEnd : kReadComma);
457       } break;
458       case kReadComma: { // Try to read a left bracket 
459         Char_t comma;
460         s >> comma;
461         state = (s.bad() ? kEnd : kReadStrip);
462       } break;
463       case kReadStrip: { // Try to read a strip 
464         if (tmp == '*') s.get();
465         else {
466           UShort_t str;
467           s >> str;
468           if (!s.bad()) {
469             minStrip = str;
470             maxStrip = str + 1;
471           }
472         }
473         state = (s.bad() ? kEnd : kReadRbrack);
474       } break;
475       case kReadRbrack: { // Try to read a left bracket 
476         Char_t rbrack;
477         s >> rbrack;
478         state = kEnd;
479       } break;
480       case kEnd: 
481         break;
482       }
483     }
484   }
485   UShort_t* dp = ds;
486   UShort_t  det;
487   while ((det = *(dp++))) {
488
489     Char_t* rp = rs;
490     Char_t  ring;
491     while ((ring = *(rp++))) {
492       if (det == 1 && ring == 'O') continue;
493       UShort_t min  = GetMinStrip(det, ring, 0, 0);
494       UShort_t max  = GetMaxStrip(det, ring, 0, 0);
495       std::cout << "FMD" << det << ring 
496                 << "  Strip range: " 
497                 << std::setw(3) << min << "," 
498                 << std::setw(3) << max << std::endl;
499
500       UShort_t nSec = ( ring == 'I' ? 20  :  40 );
501       UShort_t nStr = ( ring == 'I' ? 512 : 256 );
502       for (UShort_t sec = minSector; sec < maxSector && sec < nSec; sec++) {
503
504         UShort_t rate = GetSampleRate(det, ring, sec, 0);
505         std::cout << "FMD" << det << ring << "[" << std::setw(2) << sec 
506                   << "] sample rate: " << rate << std::endl;
507
508         if (!showStrips) continue;
509         std::cout 
510           << "  Strip |     Pedestal      |    Gain    | ZS thr. | Address\n" 
511           << "--------+-------------------+------------+---------+---------" 
512           << std::endl;
513         for (UShort_t str = minStrip; str < nStr && str < maxStrip; str++) {
514           if (str == minStrip) std::cout << std::setw(3) << sec << ",";
515           else std::cout << "    ";
516           std::cout << std::setw(3) << str << " | ";
517           if (IsDead(det, ring, sec, str)) {
518             std::cout << "dead" << std::endl;
519             continue;
520           }
521           UShort_t ddl, addr, time, sam=0;
522           Detector2Hardware(det, ring, sec, str, sam, ddl, addr, time);
523           std::cout << std::setw(7) << GetPedestal(det, ring, sec, str) 
524                     << "+/-" << std::setw(7) 
525                     << GetPedestalWidth(det, ring, sec, str) 
526                     << " | " << std::setw(10) 
527                     << GetPulseGain(det, ring, sec, str) 
528                     << " | " << std::setw(7) 
529                     << GetZeroSuppression(det, ring, sec, str) 
530                     << " | 0x" << std::hex << std::setw(4) 
531                     << std::setfill('0') << ddl << ",0x" << std::setw(3) 
532                     << addr << std::dec << std::setfill(' ') << std::endl;
533         } // for (strip)
534       } // for (sector)
535       std::cout
536         << "=============================================================" 
537         << std::endl;
538     } // while (ring)
539   } // while (det)
540   
541 }
542
543 //__________________________________________________________________
544 void
545 AliFMDParameters::SetStripRange(UShort_t min, UShort_t max) 
546 {
547   // Set fixed strip range 
548   fFixedMinStrip = min;
549   fFixedMaxStrip = max;
550 }
551
552 //__________________________________________________________________
553 AliCDBEntry*
554 AliFMDParameters::GetEntry(const char* path, AliFMDPreprocessor* pp, 
555                            Bool_t fatal) const
556 {
557   // Get an entry from the CDB or via preprocessor 
558   AliCDBEntry* entry = 0;
559   if (!pp) {
560     AliCDBManager* cdb = AliCDBManager::Instance();
561     entry              = cdb->Get(path);
562   }
563   else {
564     const char* third  = gSystem->BaseName(path);
565     const char* second = gSystem->BaseName(gSystem->DirName(path));
566     entry              = pp->GetFromCDB(second, third);
567   }
568   if (!entry) { 
569     TString msg(Form("No %s found in CDB, perhaps you need to "
570                      "use AliFMDCalibFaker?", path));
571     if (fatal) { AliFatal(msg.Data()); }
572     else       AliLog::Message(AliLog::kWarning, msg.Data(), "FMD", 
573                                "AliFMDParameters", "GetEntry", __FILE__, 
574                                __LINE__);
575     return 0;
576   }
577   return entry;
578 }
579
580     
581 //__________________________________________________________________
582 void
583 AliFMDParameters::InitPulseGain(AliFMDPreprocessor* pp)
584 {
585   // Get pulse gain from CDB or used fixed 
586   AliCDBEntry*   gain     = GetEntry(fgkPulseGain, pp);
587   if (!gain) return;
588   
589   AliFMDDebug(5, ("Got gain from CDB"));
590   fPulseGain = dynamic_cast<AliFMDCalibGain*>(gain->GetObject());
591   if (!fPulseGain) AliFatal("Invalid pulser gain object from CDB");
592 }
593 //__________________________________________________________________
594 void
595 AliFMDParameters::InitPedestal(AliFMDPreprocessor* pp)
596 {
597   // Initialize the pedestals from CDB 
598   AliCDBEntry*   pedestal = GetEntry(fgkPedestal, pp);
599   if (!pedestal) return;
600
601   AliFMDDebug(5, ("Got pedestal from CDB"));
602   fPedestal = dynamic_cast<AliFMDCalibPedestal*>(pedestal->GetObject());
603   if (!fPedestal) AliFatal("Invalid pedestal object from CDB");
604 }
605
606 //__________________________________________________________________
607 void
608 AliFMDParameters::InitDeadMap(AliFMDPreprocessor* pp)
609 {
610   // Get Dead-channel-map from CDB 
611   AliCDBEntry*   deadMap  = GetEntry(fgkDead, pp);
612   if (!deadMap) return;
613   
614   AliFMDDebug(5, ("Got dead map from CDB"));
615   fDeadMap = dynamic_cast<AliFMDCalibDeadMap*>(deadMap->GetObject());
616   if (!fDeadMap) AliFatal("Invalid dead map object from CDB");
617 }
618
619 //__________________________________________________________________
620 void
621 AliFMDParameters::InitZeroSuppression(AliFMDPreprocessor* pp)
622 {
623   // Get 0-suppression from CDB 
624   AliCDBEntry*   zeroSup  = GetEntry(fgkZeroSuppression, pp);
625   if (!zeroSup) return;
626   AliFMDDebug(5, ("Got zero suppression from CDB"));
627   fZeroSuppression = 
628     dynamic_cast<AliFMDCalibZeroSuppression*>(zeroSup->GetObject());
629   if (!fZeroSuppression)AliFatal("Invalid zero suppression object from CDB");
630 }
631
632 //__________________________________________________________________
633 void
634 AliFMDParameters::InitSampleRate(AliFMDPreprocessor* pp)
635 {
636   // get Sample rate from CDB
637   AliCDBEntry*   sampRat  = GetEntry(fgkSampleRate, pp);
638   if (!sampRat) return;
639   AliFMDDebug(5, ("Got zero suppression from CDB"));
640   fSampleRate = dynamic_cast<AliFMDCalibSampleRate*>(sampRat->GetObject());
641   if (!fSampleRate) AliFatal("Invalid zero suppression object from CDB");
642 }
643
644 //__________________________________________________________________
645 void
646 AliFMDParameters::InitAltroMap(AliFMDPreprocessor* pp)
647 {
648   // Get hardware mapping from CDB
649   if (fAltroMap) { 
650     delete fAltroMap;
651     fAltroMap = 0;
652   }
653   AliCDBEntry*   hwMap    = GetEntry(fgkAltroMap, pp, kFALSE);       
654   if (!hwMap) return;
655
656   AliFMDDebug(5, ("Got ALTRO map from CDB"));
657   fAltroMap = dynamic_cast<AliFMDAltroMapping*>(hwMap->GetObject());
658   if (!fAltroMap) {
659     AliFatal("Invalid ALTRO map object from CDB");
660     fAltroMap = new AliFMDAltroMapping;
661   }
662 }
663
664 //__________________________________________________________________
665 void
666 AliFMDParameters::InitStripRange(AliFMDPreprocessor* pp)
667 {
668   // Get strips read-out from CDB
669   AliCDBEntry*   range    = GetEntry(fgkStripRange, pp);
670   if (!range) return;
671   AliFMDDebug(5, ("Got strip range from CDB"));
672   fStripRange = dynamic_cast<AliFMDCalibStripRange*>(range->GetObject());
673   if (!fStripRange) AliFatal("Invalid strip range object from CDB");
674 }
675
676
677 //__________________________________________________________________
678 Float_t
679 AliFMDParameters::GetThreshold() const
680 {
681   // Get threshold from CDB
682   if (!fPulseGain) return fFixedThreshold;
683   return fPulseGain->Threshold();
684 }
685
686 //__________________________________________________________________
687 Float_t
688 AliFMDParameters::GetPulseGain(UShort_t detector, Char_t ring, 
689                                UShort_t sector, UShort_t strip) const
690 {
691   // Returns the pulser calibrated gain for strip # strip in sector #
692   // sector or ring id ring of detector # detector. 
693   // 
694   // For simulation, this is normally set to 
695   // 
696   //       VA1_MIP_Range 
697   //    ------------------ * MIP_Energy_Loss
698   //    ALTRO_channel_size
699   // 
700   if (!fPulseGain) { 
701     if (fFixedPulseGain <= 0)
702       fFixedPulseGain = fVA1MipRange * GetEdepMip() / fAltroChannelSize;
703     return fFixedPulseGain;
704   }  
705   AliFMDDebug(50, ("pulse gain for FMD%d%c[%2d,%3d]=%f",
706                     detector, ring, sector, strip,
707                     fPulseGain->Value(detector, ring, sector, strip)));
708   return fPulseGain->Value(detector, ring, sector, strip);
709 }
710
711 //__________________________________________________________________
712 Bool_t
713 AliFMDParameters::IsDead(UShort_t detector, Char_t ring, 
714                          UShort_t sector, UShort_t strip) const
715 {
716   // Check if the channel is dead 
717   if (!fDeadMap) return kFALSE;
718   AliFMDDebug(50, ("Dead for FMD%d%c[%2d,%3d]=%s",
719                     detector, ring, sector, strip,
720                     fDeadMap->operator()(detector, ring, sector, strip) ? 
721                     "no" : "yes"));
722   return fDeadMap->operator()(detector, ring, sector, strip);
723 }
724
725 //__________________________________________________________________
726 UShort_t
727 AliFMDParameters::GetZeroSuppression(UShort_t detector, Char_t ring, 
728                                      UShort_t sector, UShort_t strip) const
729 {
730   // Get zero suppression threshold 
731   if (!fZeroSuppression) return fFixedZeroSuppression;
732   // Need to map strip to ALTRO chip. 
733   AliFMDDebug(50, ("zero sup. for FMD%d%c[%2d,%3d]=%f",
734                     detector, ring, sector, strip,
735                     fZeroSuppression->operator()(detector, ring, 
736                                                  sector, strip)));
737   return fZeroSuppression->operator()(detector, ring, sector, strip/128);
738 }
739
740 //__________________________________________________________________
741 UShort_t
742 AliFMDParameters::GetSampleRate(UShort_t det, Char_t ring, UShort_t sector, 
743                                 UShort_t str) const
744 {
745   // Get sampl rate 
746   if (!fSampleRate) return fFixedSampleRate;
747   // Need to map sector to digitizier card. 
748   UInt_t ret = fSampleRate->Rate(det, ring, sector, str);
749   AliFMDDebug(50, ("Sample rate for FMD%d%c[%2d,%3d]=%d", 
750                     det, ring, sector, str, ret));
751   return ret;
752 }
753
754 //__________________________________________________________________
755 UShort_t
756 AliFMDParameters::GetMinStrip(UShort_t det, Char_t ring, UShort_t sector, 
757                               UShort_t str) const
758 {
759   // Get strip range read out 
760   if (!fStripRange) return fFixedMinStrip;
761   // Need to map sector to digitizier card. 
762   UInt_t ret = fStripRange->Min(det, ring, sector, str);
763   AliFMDDebug(50, ("Min strip # for FMD%d%c[%2d,%3d]=%d", 
764                     det, ring, sector, str, ret));
765   return ret;
766 }
767
768 //__________________________________________________________________
769 UShort_t
770 AliFMDParameters::GetMaxStrip(UShort_t det, Char_t ring, UShort_t sector, 
771                               UShort_t str) const
772 {
773   // Get strip range read out 
774   if (!fStripRange) return fFixedMaxStrip;
775   // Need to map sector to digitizier card. 
776   UInt_t ret = fStripRange->Max(det, ring, sector, str);
777   AliFMDDebug(50, ("Max strip # for FMD%d%c[%2d,%3d]=%d", 
778                     det, ring, sector, str, ret));
779   return ret;
780 }
781
782 //__________________________________________________________________
783 Float_t
784 AliFMDParameters::GetPedestal(UShort_t detector, Char_t ring, 
785                               UShort_t sector, UShort_t strip) const
786 {
787   // Get the pedesal 
788   if (!fPedestal) return fFixedPedestal;
789   AliFMDDebug(50, ("pedestal for FMD%d%c[%2d,%3d]=%f",
790                     detector, ring, sector, strip,
791                     fPedestal->Value(detector, ring, sector, strip)));
792   return fPedestal->Value(detector, ring, sector, strip);
793 }
794
795 //__________________________________________________________________
796 Float_t
797 AliFMDParameters::GetPedestalWidth(UShort_t detector, Char_t ring, 
798                                    UShort_t sector, UShort_t strip) const
799 {
800   // Get the pedesal 
801   if (!fPedestal) return fFixedPedestalWidth;
802   AliFMDDebug(50, ("pedetal width for FMD%d%c[%2d,%3d]=%f",
803                     detector, ring, sector, strip,
804                     fPedestal->Width(detector, ring, sector, strip)));
805   return fPedestal->Width(detector, ring, sector, strip);
806 }
807   
808 //__________________________________________________________________
809 AliFMDAltroMapping*
810 AliFMDParameters::GetAltroMap() const
811 {
812   // Get the hardware address to detector index map 
813   return fAltroMap;
814 }
815
816
817 //____________________________________________________________________
818 Bool_t 
819 AliFMDParameters::Hardware2Detector(UShort_t  ddl,       UShort_t addr,
820                                     UShort_t  timebin,   
821                                     UShort_t& det,       Char_t&  ring, 
822                                     UShort_t& sec,       Short_t& str,
823                                     UShort_t& sam) const
824 {
825   // Translate a hardware address to detector coordinates. 
826   // 
827   // See also Hardware2Detector that accepts 4 inputs 
828   if (!fAltroMap) return kFALSE;
829   UShort_t board, chip, chan;
830   fAltroMap->ChannelAddress(addr, board, chip, chan);
831   return Hardware2Detector(ddl,board,chip,chan,timebin,det,ring,sec,str,sam);
832 }
833 //____________________________________________________________________
834 Bool_t 
835 AliFMDParameters::Hardware2Detector(UShort_t    ddl,       UShort_t   board,
836                                     UShort_t    chip,      UShort_t   chan,
837                                     UShort_t  timebin,   
838                                     UShort_t& det,       Char_t&   ring, 
839                                     UShort_t& sec,       Short_t& str,
840                                     UShort_t& sam) const
841 {
842   // Translate a hardware address to detector coordinates. 
843   // 
844   // See also Hardware2Detector that accepts 4 inputs 
845   if (!fAltroMap) return kFALSE;
846   if (fAltroMap->DDL2Detector(ddl) < 0) return kFALSE;
847   Short_t stripBase = 0;
848   if (!fAltroMap->Channel2StripBase(board,chip,chan, ring, sec, stripBase)) 
849     return kFALSE;
850   UShort_t preSamples = GetPreSamples(det, ring, sec, stripBase);
851   UShort_t sampleRate = GetSampleRate(det, ring, sec, stripBase);
852   Short_t stripOff = 0;
853   fAltroMap->Timebin2Strip(sec, timebin, preSamples, sampleRate, stripOff, sam);
854   str = stripBase + stripOff;
855   AliFMDDebug(50, ("%d/0x%02x/0x%x/0x%x/%04d -> FMD%d%c[%02d,%03d]-%d"
856                   " (pre=%2d, rate=%d)", 
857                    ddl, board, chip, chan, timebin, 
858                    det, ring, sec, str, sam, preSamples, sampleRate));
859   return kTRUE;
860 }
861
862 #if 0
863 //__________________________________________________________________
864 Bool_t
865 AliFMDParameters::Hardware2Detector(UShort_t    ddl,  UShort_t    board, 
866                                     UShort_t    chip, UShort_t    chan,
867                                     UShort_t& det,  Char_t&   ring, 
868                                     UShort_t& sec,  Short_t& str) const
869 {
870   // Map hardware address to detector index
871   if (!fAltroMap) return kFALSE;
872   return fAltroMap->Hardware2Detector(ddl,board,chip,chan, det,ring,sec,str);
873 }
874 //__________________________________________________________________
875 Bool_t
876 AliFMDParameters::Hardware2Detector(UShort_t    ddl,  UShort_t    addr, 
877                                     UShort_t& det,  Char_t&   ring, 
878                                     UShort_t& sec,  Short_t& str) const
879 {
880   // Map hardware address to detector index
881   if (!fAltroMap) return kFALSE;
882   return fAltroMap->Hardware2Detector(ddl, addr, det, ring, sec, str);
883 }
884 #endif
885
886 //____________________________________________________________________
887 Bool_t 
888 AliFMDParameters::Detector2Hardware(UShort_t  det,        Char_t    ring, 
889                                     UShort_t  sec,        UShort_t  str,
890                                     UShort_t  sam, 
891                                     UShort_t& ddl,        UShort_t& board, 
892                                     UShort_t& altro,      UShort_t& channel, 
893                                     UShort_t& timebin) const
894 {
895   if (!fAltroMap) return kFALSE;
896   UShort_t preSamples = GetPreSamples(det, ring, sec, str);
897   UShort_t sampleRate = GetSampleRate(det, ring, sec, str);
898   UShort_t strip      = str - GetMinStrip(det,ring,sec,str);
899   return fAltroMap->Detector2Hardware(det, ring, sec, strip, sam,
900                                       preSamples, sampleRate,
901                                       ddl, board, altro, channel, timebin);
902 }
903
904   
905
906 //____________________________________________________________________
907 Bool_t 
908 AliFMDParameters::Detector2Hardware(UShort_t  det,        Char_t    ring, 
909                                     UShort_t  sec,        UShort_t  str,
910                                     UShort_t  sam, 
911                                     UShort_t&   ddl,        UShort_t&   addr,
912                                     UShort_t& timebin) const
913 {
914   if (!fAltroMap) return kFALSE;
915   UShort_t preSamples = GetPreSamples(det, ring, sec, str);
916   UShort_t sampleRate = GetSampleRate(det, ring, sec, str);
917   UShort_t strip      = str - GetMinStrip(det,ring,sec,str);
918   return fAltroMap->Detector2Hardware(det, ring, sec, strip, sam,
919                                       preSamples, sampleRate,
920                                       ddl, addr, timebin);
921 }
922
923 #if 0
924 //__________________________________________________________________
925 Bool_t
926 AliFMDParameters::Detector2Hardware(UShort_t det,  Char_t   ring, 
927                                     UShort_t sec,  UShort_t str, 
928                                     UShort_t&  ddl,  UShort_t&  board, 
929                                     UShort_t&  chip, UShort_t&  chan) const
930 {
931   // Map detector index to hardware address
932   if (!fAltroMap) return kFALSE;
933   return fAltroMap->Detector2Hardware(det,ring,sec,str, ddl,board,chip,chan);
934 }
935
936 //__________________________________________________________________
937 Bool_t
938 AliFMDParameters::Detector2Hardware(UShort_t det, Char_t   ring, 
939                                     UShort_t sec, UShort_t str, 
940                                     UShort_t&  ddl, UShort_t&  addr) const
941 {
942   // Map detector index to hardware address
943   if (!fAltroMap) return kFALSE;
944   return fAltroMap->Detector2Hardware(det, ring, sec, str, ddl, addr);
945 }
946 #endif
947
948 //__________________________________________________________________
949 Float_t
950 AliFMDParameters::GetEdepMip() const 
951
952   // Get energy deposited by a MIP in the silicon sensors
953   if (fEdepMip <= 0){
954     AliFMDGeometry* fmd = AliFMDGeometry::Instance();
955     fEdepMip = (fkSiDeDxMip 
956                 * fmd->GetRing('I')->GetSiThickness() 
957                 * fmd->GetSiDensity());
958   }
959   return fEdepMip;
960 }
961 //____________________________________________________________________
962 Float_t  
963 AliFMDParameters::GetDACPerMIP() const
964 {
965   //This is the conversion from the Digital-to-Analog-Converter setting
966   // to the number of MIPs. The number was measured in the NBI lab during
967   // August 2008.
968   
969   return 29.67;
970   
971 }
972  
973 //____________________________________________________________________
974 //
975 // EOF
976 //