]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDPreprocessor.cxx
First V0 MC Analysis from H.Ricaud
[u/mrichter/AliRoot.git] / FMD / AliFMDPreprocessor.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    AliFMDPreprocessor.cxx
17     @author  Hans Hjersing Dalsgaard <canute@nbi.dk>
18     @date    Mon Mar 27 12:39:09 2006
19     @brief   Shuttle "preprocessor" for the FMD
20 */
21 //___________________________________________________________________
22 //
23 // The class processes data points from DCS (via Amanada), and DAQ DA
24 // files (via FXS) to make calibration data for the FMD. 
25 //
26 // Data points: 
27 //    *  Nothing yet. 
28 //
29 // DAQ FXS file:
30 //    * pedestals - a (ASCII) Comma Separated Values files with the
31 //                  fields 
32 //                       rcu     DDL number 
33 //                       board   FEC board number 
34 //                       chip    ALTRO chip number on FEC
35 //                       channel ALTRO channel number
36 //                       strip   VA1 strip number
37 //                       sample  Sample number
38 //                       ped     Mean of ADC spectra
39 //                       noise   Spread of ADC spectra
40 //                       mu      Mean of Gaussian fit to ADC spectra
41 //                       sigma   Variance of Gaussian fit to ADC spectra
42 //                       chi2    Chi^2 per degrees of freedom of fit
43 //    * Gains     - a (ASCII) Comma Separated Values files with the
44 //                  fields 
45 //                       rcu     DDL number 
46 //                       board   FEC board number 
47 //                       chip    ALTRO chip number on FEC
48 //                       channel ALTRO channel number
49 //                       strip   VA1 strip number
50 //                       gain    Slope of gain
51 //                       error   Error on gain
52 //                       chi2    Chi^2 per degrees of freedom of fit
53 //                  
54 // See also 
55 //
56 //   http://aliceinfo.cern.ch/Offline/Activities/Shuttle.html
57 //
58 // Latest changes by Christian Holm Christensen
59 //
60
61 // #include <iostream>
62
63 #include <fstream>
64 #include "AliFMDPreprocessor.h"
65 #include "AliFMDCalibPedestal.h"
66 #include "AliFMDCalibGain.h"
67 #include "AliFMDCalibStripRange.h"
68 #include "AliFMDCalibSampleRate.h"
69 #include "AliFMDParameters.h"
70 #include "AliCDBMetaData.h"
71 #include "AliCDBManager.h"
72 // #include "AliDCSValue.h"
73 #include "AliLog.h"
74 #include <TTimeStamp.h>
75 // #include <TFile.h>
76 #include <TObjString.h>
77 #include <TString.h>
78 #include <TNamed.h>
79
80
81 ClassImp(AliFMDPreprocessor)
82 #if 0 // Do not remove - here to make Emacs happy
83 ;
84 #endif 
85
86 //____________________________________________________
87 Bool_t AliFMDPreprocessor::GetAndCheckFileSources(TList*&     list,
88                                                   Int_t       system, 
89                                                   const char* id) 
90 {
91   // Convinience function 
92   // Parameters: 
93   //   list     On return, list of files. 
94   //   system   Alice system (DAQ, DCS, ...)
95   //   id       File id
96   // Return:
97   //   kTRUE on success. 
98   list = GetFileSources(system, id);
99   if (!list) { 
100     TString sys;
101     switch (system) { 
102     case kDAQ: sys = "DAQ";     break;
103     case kDCS: sys = "DCS";     break;
104     default:   sys = "unknown"; break;
105     }
106     Log(Form("Failed to get file sources for %s/%s", sys.Data(), system));
107     return kFALSE;
108   }
109   return kTRUE;
110 }
111
112 //____________________________________________________
113 AliCDBEntry* 
114 AliFMDPreprocessor::GetFromCDB(const char* second, const char* third)
115 {
116   return GetFromOCDB(second, third);
117 }
118
119
120 //____________________________________________________
121 UInt_t AliFMDPreprocessor::Process(TMap* /* dcsAliasMap */)
122 {
123   // Main member function. 
124   // Parameters: 
125   //    dcsAliassMap   Map of DCS data point aliases.
126   // Return 
127   //    0 on success, >0 otherwise 
128   Bool_t resultPed   = kTRUE;
129   Bool_t resultGain  = kTRUE;
130   Bool_t resultRange = kTRUE;
131   Bool_t resultRate  = kTRUE;
132   Bool_t resultZero  = kTRUE;
133
134   // Do we need this ?
135   // if(!dcsAliasMap) return 1;
136   // 
137   // Invoking the cdb manager and the FMD parameters class
138   // AliCDBManager* cdb   = AliCDBManager::Instance();
139   // cdb->SetDefaultStorage("local://$ALICE_ROOT");
140   // cdb->SetRun(0);
141   AliFMDParameters* pars = AliFMDParameters::Instance();
142   pars->Init(this, false, AliFMDParameters::kAltroMap);
143
144   // This is if the SOR contains Fee parameters, and we run a DA to
145   // extract these parameters.   The same code could work if we get
146   // the information from DCS via the FXS 
147   TList* files = 0;
148   // GetAndCheckFileSources(files, kDAQ,"info");
149   // if (!files) return 1;
150   AliFMDCalibSampleRate*      calibRate  = 0;
151   AliFMDCalibStripRange*      calibRange = 0;
152   AliFMDCalibZeroSuppression* calibZero  = 0;
153   GetInfoCalibration(files, calibRate, calibRange, calibZero);
154   // Disabled for now. 
155   // resultRate  = (!calibRate  ? kFALSE : kTRUE);
156   // resultRange = (!calibRange ? kFALSE : kTRUE);
157   // resultZero  = (!calibZero  ? kFALSE : kTRUE);
158
159   // Gt the run type 
160   TString runType(GetRunType()); 
161
162   //Creating calibration objects
163   AliFMDCalibPedestal* calibPed  = 0;
164   AliFMDCalibGain*     calibGain = 0;
165   if (runType.Contains("PEDESTAL", TString::kIgnoreCase) && 
166       GetAndCheckFileSources(files, kDAQ, "pedestal"))  
167     resultPed = (!(calibPed = GetPedestalCalibration(files)) ? kFALSE : kTRUE);
168   if (runType.Contains("PULSER", TString::kIgnoreCase) && 
169       GetAndCheckFileSources(files, kDAQ, "gain"))
170     resultGain = (!(calibGain = GetGainCalibration(files)) ? kFALSE : kTRUE);
171   
172   //Storing Calibration objects  
173   AliCDBMetaData metaData;
174   metaData.SetBeamPeriod(0);
175   metaData.SetResponsible("Hans H. Dalsgaard");
176   metaData.SetComment("Preprocessor stores pedestals and gains for the FMD.");
177   
178   if(calibPed)  { 
179     resultPed  = Store("Calib","Pedestal", calibPed, &metaData, 0, kTRUE);
180     delete calibPed;
181   }
182   if(calibGain) { 
183     resultGain = Store("Calib","PulseGain", calibGain, &metaData, 0, kTRUE);
184     delete calibGain;
185   }
186   if(calibRange) { 
187     resultRange = Store("Calib","StripRange", calibRange, &metaData, 0, kTRUE);
188     delete calibRange;
189   }
190   if(calibRate) { 
191     resultRate = Store("Calib","SampleRate", calibRate, &metaData, 0, kTRUE);
192     delete calibRate;
193   }
194   if(calibZero) { 
195     resultZero = Store("Calib","ZeroSuppression", calibZero, &metaData,0,kTRUE);
196     delete calibZero;
197   }
198
199 #if 0
200   // Disabled until we implement GetInfoCalibration properly
201   Bool_t success = (resultPed && resultGain  && resultRange && 
202                     resultRate  && resultZero);
203 #endif
204   Bool_t success = resultPed && resultGain;
205   Log(Form("FMD preprocessor was %s", (success ? "successful" : "failed")));
206   return (success ? 0 : 1);
207 }
208
209 //____________________________________________________________________
210 Bool_t
211 AliFMDPreprocessor::GetInfoCalibration(TList* files, 
212                                        AliFMDCalibSampleRate*&      s,
213                                        AliFMDCalibStripRange*&      r, 
214                                        AliFMDCalibZeroSuppression*& z)
215 {
216   // Get info calibrations. 
217   // Parameters:
218   //     files List of files. 
219   //     s     On return, newly allocated object 
220   //     r     On return, newly allocated object 
221   //     z     On return, newly allocated object 
222   // Return: 
223   //     kTRUE on success
224   if (!files) return kTRUE; // Should really be false
225   if (files->GetEntries() <= 0) return kTRUE;
226   
227   s = new AliFMDCalibSampleRate();
228   r = new AliFMDCalibStripRange();
229   z = new AliFMDCalibZeroSuppression();
230   
231   // AliFMDParameters*    pars     = AliFMDParameters::Instance();
232   TIter                iter(files);
233   TObjString*          fileSource;
234
235   while((fileSource = dynamic_cast<TObjString*>(iter.Next()))) {
236     const Char_t* filename = GetFile(kDAQ, "info", fileSource->GetName());
237     std::ifstream in(filename);
238     if(!in) {
239       Log(Form("File %s not found!", filename));
240       continue;
241     }
242   }
243   return kTRUE;
244 }
245
246   
247 //____________________________________________________________________
248 AliFMDCalibPedestal* 
249 AliFMDPreprocessor::GetPedestalCalibration(TList* pedFiles)
250 {
251   // Read DAQ DA produced CSV files of pedestals, and return a
252   // calibration object. 
253   // Parameters:
254   //   pedFiles     List of pedestal files 
255   // Return 
256   //   A pointer to a newly allocated AliFMDCalibPedestal object, or
257   //   null in case of errors. 
258   if(!pedFiles) return 0;
259
260   AliFMDCalibPedestal* calibPed = new AliFMDCalibPedestal();
261   AliFMDParameters*    pars     = AliFMDParameters::Instance();
262   TIter                iter(pedFiles);
263   TObjString*          fileSource;
264   
265   while((fileSource = dynamic_cast<TObjString*>(iter.Next()))) {
266     const Char_t* filename = GetFile(kDAQ, "pedestal", fileSource->GetName());
267     std::ifstream in(filename);
268     if(!in) {
269       Log(Form("File %s not found!", filename));
270       continue;
271     }
272
273     // Get header (how long is it ?)
274     TString header;
275     header.ReadLine(in);
276     header.ToLower();
277     if(!header.Contains("pedestal")) {
278       Log("File header is not from pedestal!");
279       continue;
280     }
281     Log("File contains data from pedestals");
282     
283     // Read columns line
284     int lineno = 2;
285     header.ReadLine(in);
286     
287     // Loop until EOF
288     while(!in.eof()) {
289       if(in.bad()) { 
290         Log(Form("Bad read at line %d in %s", lineno, filename));
291         break;
292       }
293       UInt_t ddl=2, board, chip, channel, strip, tb;
294       Float_t ped, noise, mu, sigma, chi2ndf;
295       Char_t c[10];
296           
297       in // >> ddl      >> c[0] 
298          >> board    >> c[1]
299          >> chip     >> c[2]
300          >> channel  >> c[3]
301          >> strip    >> c[4]
302          >> tb       >> c[5]
303          >> ped      >> c[6]
304          >> noise    >> c[7]
305          >> mu       >> c[8]
306          >> sigma    >> c[9]
307          >> chi2ndf;
308       lineno++;
309       // Ignore trailing garbage 
310       if (strip > 127) continue;
311       
312       //Setting the pedestals via the hardware address
313       UShort_t det, sec, str;
314       Char_t ring;
315           
316       pars->Hardware2Detector(ddl,board,chip,channel,det,ring,sec,str);
317       strip += str;
318       calibPed->Set(det,ring,sec,strip,ped,noise);
319     }
320   }
321   return calibPed;
322 }       
323
324 //____________________________________________________________________
325 AliFMDCalibGain* 
326 AliFMDPreprocessor::GetGainCalibration(TList* gainFiles)
327 {
328   // Read DAQ DA produced CSV files of pedestals, and return a
329   // calibration object. 
330   // Parameters:
331   //   pedFiles     List of pedestal files 
332   // Return 
333   //   A pointer to a newly allocated AliFMDCalibPedestal object, or
334   //   null in case of errors. 
335   if(!gainFiles) return 0;
336   
337   AliFMDCalibGain*  calibGain  = new AliFMDCalibGain();
338   AliFMDParameters* pars       = AliFMDParameters::Instance();
339   TIter             iter(gainFiles);
340   TObjString*       fileSource;
341   while((fileSource = dynamic_cast<TObjString *>(iter.Next()))) {
342     const Char_t* filename = GetFile(kDAQ, "gain", fileSource->GetName());
343     std::ifstream in(filename);
344     if(!in) {
345       Log(Form("File %s not found!", filename));
346       continue;
347     }
348
349     //Get header (how long is it ?)
350     TString header;
351     header.ReadLine(in);
352     header.ToLower();
353     if(!header.Contains("gain")) {
354       Log("File header is not from gain!");
355       continue;
356     }
357     Log("File contains data from pulse gain");
358
359     // Read column headers
360     header.ReadLine(in);
361
362     int lineno  = 2;
363     // Read until EOF 
364     while(!in.eof()) {
365       if(in.bad()) { 
366         Log(Form("Bad read at line %d in %s", lineno, filename));
367         break;
368       }
369       UInt_t ddl=2, board, chip, channel, strip;
370       Float_t gain,error,  chi2ndf;
371       Char_t c[7];
372               
373       in // >> ddl      >> c[0] 
374          >> board    >> c[1]
375          >> chip     >> c[2]
376          >> channel  >> c[3]
377          >> strip    >> c[4]
378          >> gain     >> c[5]
379          >> error    >> c[6]
380          >> chi2ndf;
381       lineno++;
382       // Ignore trailing garbage
383       if(strip > 127) continue;
384       
385       //Setting the pedestals via the hardware address
386       UShort_t det, sec, str;
387       Char_t ring;
388       pars->Hardware2Detector(ddl,board,chip,channel,det,ring,sec,str);
389
390       strip += str;
391       calibGain->Set(det,ring,sec,strip,gain);
392     }
393   }
394   return calibGain;
395 }
396
397 //____________________________________________________________________
398 //
399 // EOF
400 //