]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDPreprocessor.cxx
Geometry checked and corrected with sampling option.
[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 //____________________________________________________
88 AliFMDPreprocessor::AliFMDPreprocessor(AliShuttleInterface* shuttle)
89   : AliPreprocessor("FMD", shuttle)
90 {
91   AddRunType("PHYSICS");
92   AddRunType("STANDALONE");
93   AddRunType("PEDESTAL");
94   AddRunType("GAIN");
95 }
96
97
98 //____________________________________________________
99 Bool_t AliFMDPreprocessor::GetAndCheckFileSources(TList*&     list,
100                                                   Int_t       system, 
101                                                   const char* id) 
102 {
103   // Convinience function 
104   // Parameters: 
105   //   list     On return, list of files. 
106   //   system   Alice system (DAQ, DCS, ...)
107   //   id       File id
108   // Return:
109   //   kTRUE on success. 
110   list = GetFileSources(system, id);
111   if (!list) { 
112     TString sys;
113     switch (system) { 
114     case kDAQ: sys = "DAQ";     break;
115     case kDCS: sys = "DCS";     break;
116     default:   sys = "unknown"; break;
117     }
118     Log(Form("Failed to get file sources for %s/%s", sys.Data(), system));
119     return kFALSE;
120   }
121   return kTRUE;
122 }
123
124 //____________________________________________________
125 AliCDBEntry* 
126 AliFMDPreprocessor::GetFromCDB(const char* second, const char* third)
127 {
128   return GetFromOCDB(second, third);
129 }
130
131
132 //____________________________________________________
133 UInt_t AliFMDPreprocessor::Process(TMap* /* dcsAliasMap */)
134 {
135   // Main member function. 
136   // Parameters: 
137   //    dcsAliassMap   Map of DCS data point aliases.
138   // Return 
139   //    0 on success, >0 otherwise 
140   Bool_t resultPed   = kTRUE;
141   Bool_t resultGain  = kTRUE;
142   Bool_t resultRange = kTRUE;
143   Bool_t resultRate  = kTRUE;
144   Bool_t resultZero  = kTRUE;
145
146   // Do we need this ?
147   // if(!dcsAliasMap) return 1;
148   // 
149   // Invoking the cdb manager and the FMD parameters class
150   // AliCDBManager* cdb   = AliCDBManager::Instance();
151   // cdb->SetDefaultStorage("local://$ALICE_ROOT");
152   // cdb->SetRun(0);
153   AliFMDParameters* pars = AliFMDParameters::Instance();
154   pars->Init(this, false, AliFMDParameters::kAltroMap);
155   // This is if the SOR contains Fee parameters, and we run a DA to
156   // extract these parameters.   The same code could work if we get
157   // the information from DCS via the FXS 
158   TList* files = 0;
159   AliFMDCalibSampleRate*      calibRate  = 0;
160   AliFMDCalibStripRange*      calibRange = 0;
161   AliFMDCalibZeroSuppression* calibZero  = 0;
162   // Disabled for now. 
163   //#if 0
164   if (GetAndCheckFileSources(files, kDAQ,pars->GetConditionsShuttleID()))
165     GetInfoCalibration(files, calibRate, calibRange, calibZero);
166   resultRate  = (!calibRate  ? kFALSE : kTRUE);
167   resultRange = (!calibRange ? kFALSE : kTRUE);
168   resultZero  = (!calibZero  ? kFALSE : kTRUE);
169   //#endif
170
171   // Gt the run type 
172   TString runType(GetRunType()); 
173
174   //Creating calibration objects
175   AliFMDCalibPedestal* calibPed  = 0;
176   AliFMDCalibGain*     calibGain = 0;
177   if (runType.Contains("PEDESTAL", TString::kIgnoreCase)) { 
178     if (GetAndCheckFileSources(files, kDAQ, pars->GetPedestalShuttleID())) {
179       if(files->GetSize())
180         calibPed = GetPedestalCalibration(files);
181     }
182     resultPed = (calibPed ? kTRUE : kFALSE);
183   }
184   if (runType.Contains("GAIN", TString::kIgnoreCase)) {
185     if (GetAndCheckFileSources(files, kDAQ, pars->GetGainShuttleID())) {
186       if(files->GetSize())
187         calibGain = GetGainCalibration(files);
188     }
189     resultGain = (calibGain ? kTRUE : kFALSE);
190   }
191   
192   //Storing Calibration objects  
193   AliCDBMetaData metaData;
194   metaData.SetBeamPeriod(0);
195   metaData.SetResponsible("Hans H. Dalsgaard");
196   metaData.SetComment("Preprocessor stores pedestals and gains for the FMD.");
197   
198   if(calibPed)  { 
199     resultPed  = Store("Calib","Pedestal", calibPed, &metaData, 0, kTRUE);
200     delete calibPed;
201   }
202   if(calibGain) { 
203     resultGain = Store("Calib","PulseGain", calibGain, &metaData, 0, kTRUE);
204     delete calibGain;
205   }
206   if(calibRange) { 
207     resultRange = Store("Calib","StripRange", calibRange, &metaData, 0, kTRUE);
208     delete calibRange;
209   }
210   if(calibRate) { 
211     resultRate = Store("Calib","SampleRate", calibRate, &metaData, 0, kTRUE);
212     delete calibRate;
213   }
214   if(calibZero) { 
215     resultZero = Store("Calib","ZeroSuppression", calibZero,&metaData,0,kTRUE);
216     delete calibZero;
217   }
218
219   //#if 0
220   // Disabled until we implement GetInfoCalibration properly
221   Bool_t success = (resultPed && resultGain  && resultRange && 
222                     resultRate  && resultZero);
223   //#endif
224   //  Bool_t success = resultPed && resultGain;
225   Log(Form("FMD preprocessor was %s", (success ? "successful" : "failed")));
226   return (success ? 0 : 1);
227 }
228
229 //____________________________________________________________________
230 Bool_t
231 AliFMDPreprocessor::GetInfoCalibration(TList* files, 
232                                        AliFMDCalibSampleRate*&      s,
233                                        AliFMDCalibStripRange*&      r, 
234                                        AliFMDCalibZeroSuppression*& z)
235 {
236   // Get info calibrations. 
237   // Parameters:
238   //     files List of files. 
239   //     s     On return, newly allocated object 
240   //     r     On return, newly allocated object 
241   //     z     On return, newly allocated object 
242   // Return: 
243   //     kTRUE on success
244   if (!files) return kTRUE; // Should really be false
245   if (files->GetEntries() <= 0) return kTRUE;
246   
247   s = new AliFMDCalibSampleRate();
248   r = new AliFMDCalibStripRange();
249   z = new AliFMDCalibZeroSuppression();
250   
251   AliFMDParameters*    pars     = AliFMDParameters::Instance();
252   TIter                iter(files);
253   TObjString*          fileSource;
254
255   while((fileSource = dynamic_cast<TObjString*>(iter.Next()))) {
256     const Char_t* filename = GetFile(kDAQ, pars->GetConditionsShuttleID(), fileSource->GetName());
257     std::ifstream in(filename);
258     if(!in) {
259       Log(Form("File %s not found!", filename));
260       continue;
261     }
262     s->ReadFromFile(in);
263     r->ReadFromFile(in);
264   }
265   return kTRUE;
266 }
267
268   
269 //____________________________________________________________________
270 AliFMDCalibPedestal* 
271 AliFMDPreprocessor::GetPedestalCalibration(TList* pedFiles)
272 {
273   // Read DAQ DA produced CSV files of pedestals, and return a
274   // calibration object. 
275   // Parameters:
276   //   pedFiles     List of pedestal files 
277   // Return 
278   //   A pointer to a newly allocated AliFMDCalibPedestal object, or
279   //   null in case of errors. 
280   if(!pedFiles) return 0;
281
282   AliFMDCalibPedestal* calibPed = new AliFMDCalibPedestal();
283   AliFMDParameters*    pars     = AliFMDParameters::Instance();
284   TIter                iter(pedFiles);
285   TObjString*          fileSource;
286   
287   while((fileSource = dynamic_cast<TObjString*>(iter.Next()))) {
288     const Char_t* filename = GetFile(kDAQ, pars->GetPedestalShuttleID(), fileSource->GetName());
289     std::ifstream in(filename);
290     if(!in) {
291       Log(Form("File %s not found!", filename));
292       continue;
293     }
294
295     // Get header (how long is it ?)
296     TString header;
297     header.ReadLine(in);
298     header.ToLower();
299     if(!header.Contains("pedestal")) {
300       Log("File header is not from pedestal!");
301       continue;
302     }
303     Log("File contains data from pedestals");
304     
305     // Read columns line
306     int lineno = 2;
307     header.ReadLine(in);
308     
309     // Loop until EOF
310     while(in.peek()!=EOF) {
311       if(in.bad()) { 
312         Log(Form("Bad read at line %d in %s", lineno, filename));
313         break;
314       }
315       UShort_t det, sec, strip;
316       Char_t ring;
317       Float_t ped, noise, mu, sigma, chi2ndf;
318       Char_t c[8];
319           
320       in >> det      >> c[0] 
321          >> ring     >> c[1]
322          >> sec      >> c[2]
323          >> strip    >> c[3]
324          >> ped      >> c[4]
325          >> noise    >> c[5]
326          >> mu       >> c[6]
327          >> sigma    >> c[7]
328          >> chi2ndf;
329       lineno++;
330       
331       // Ignore trailing garbage 
332       // if (strip > 127) continue;
333       
334       //Setting DDL to comply with the FMD in DAQ
335       // UInt_t FmdDDLBase = 3072; 
336       // ddl = ddl - FmdDDLBase;
337       //Setting the pedestals via the hardware address
338       
339       
340       // pars->Hardware2Detector(ddl,board,chip,channel,det,ring,sec,str);
341       // strip += str;
342      
343       calibPed->Set(det,ring,sec,strip,ped,noise);
344      
345     }
346   }
347   return calibPed;
348 }       
349
350 //____________________________________________________________________
351 AliFMDCalibGain* 
352 AliFMDPreprocessor::GetGainCalibration(TList* gainFiles)
353 {
354   // Read DAQ DA produced CSV files of pedestals, and return a
355   // calibration object. 
356   // Parameters:
357   //   pedFiles     List of pedestal files 
358   // Return 
359   //   A pointer to a newly allocated AliFMDCalibPedestal object, or
360   //   null in case of errors. 
361   if(!gainFiles) return 0;
362   
363   AliFMDCalibGain*  calibGain  = new AliFMDCalibGain();
364   AliFMDParameters* pars       = AliFMDParameters::Instance();
365   TIter             iter(gainFiles);
366   TObjString*       fileSource;
367   while((fileSource = dynamic_cast<TObjString *>(iter.Next()))) {
368     const Char_t* filename = GetFile(kDAQ, pars->GetGainShuttleID(), fileSource->GetName());
369     std::ifstream in(filename);
370     if(!in) {
371       Log(Form("File %s not found!", filename));
372       continue;
373     }
374
375     //Get header (how long is it ?)
376     TString header;
377     header.ReadLine(in);
378     header.ToLower();
379     if(!header.Contains("gain")) {
380       Log("File header is not from gain!");
381       continue;
382     }
383     Log("File contains data from pulse gain");
384
385     // Read column headers
386     header.ReadLine(in);
387
388     int lineno  = 2;
389     // Read until EOF 
390     while(in.peek()!=EOF) {
391       if(in.bad()) { 
392         Log(Form("Bad read at line %d in %s", lineno, filename));
393         break;
394       }
395       UShort_t det, sec, strip;
396       Char_t ring;
397       
398       Float_t gain,error,  chi2ndf;
399       Char_t c[6];
400       
401       in >> det      >> c[0] 
402          >> ring     >> c[1]
403          >> sec      >> c[2]
404          >> strip    >> c[3]
405          >> gain     >> c[4]
406          >> error    >> c[5]
407          >> chi2ndf;
408       lineno++;
409       // Ignore trailing garbage
410       //if(strip > 127) continue;
411       
412       //Setting DDL to comply with the FMD in DAQ
413       // UInt_t FmdDDLBase = 3072; 
414       // ddl = ddl - FmdDDLBase;
415       //Setting the pedestals via the hardware address
416       //   pars->Hardware2Detector(ddl,board,chip,channel,det,ring,sec,str);
417
418       // strip += str;
419       calibGain->Set(det,ring,sec,strip,gain);
420     }
421   }
422   return calibGain;
423 }
424
425 //____________________________________________________________________
426 //
427 // EOF
428 //