]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/scripts/TestPreprocessor.C
Fixes to AliFMDPreprocessor from Hans.
[u/mrichter/AliRoot.git] / FMD / scripts / TestPreprocessor.C
1 /* $Id$ */
2
3 //====================================================================
4 //
5 // Helper classes
6 //
7 #include <iostream>
8 #include <fstream>
9 #ifndef __CINT__
10 # include <TString.h>
11 # include <AliPreprocessor.h>
12 # include <FMD/AliFMDPreprocessor.h>
13 # include <AliShuttleInterface.h>
14 # include <AliCDBStorage.h>
15 # include <AliCDBEntry.h>
16 # include <AliCDBManager.h>
17 # include <AliCDBId.h>
18 # include <AliCDBMetaData.h>
19 # include <AliDCSValue.h>
20 # include <AliLog.h>
21 // # include <../FMD/AliFMDCalibZeroSuppression.h>
22 // # include <../FMD/AliFMDCalibDeadMap.h>
23 # include <FMD/AliFMDParameters.h>
24 # include <SHUTTLE/TestShuttle/AliTestShuttle.h>
25 # include <TRandom.h>
26 # include <TSystem.h>
27 # include <TObjString.h>
28 # include <TMap.h>
29 # include <TError.h>
30 # include <iostream>
31 #endif
32
33 namespace { 
34   //====================================================================
35   //
36   // Helper functions
37   //
38   Float_t Hardware2Ped(int ddl, int board, int chip, int, int) 
39   {
40     return ((chip & 0xf) | ((board & 0x1F) << 4) | (ddl << 9) & 0x3);
41   }
42   Float_t Hardware2Noise(int ddl, int board, int chip, int channel, int strip)
43   {
44     return ((strip & 0x7f) | ((channel & 0xf) << 7));
45   }
46   Float_t Hardware2Gain(int ddl, int board, int chip, int channel, int strip) 
47   {
48     return (((strip   & 0x7f) <<  0) | 
49             ((channel & 0x0f) <<  7) | 
50             ((chip    & 0x07) << 11) |
51             ((board   & 0x1f) << 14) | 
52             ((ddl     & 0x03) << 19));
53   }
54
55   //====================================================================
56   //
57   // Helper classes
58   //
59   //__________________________________________________________________
60   class CreateDummyDaData
61   {
62   public:
63     CreateDummyDaData(const char* output,
64                       int firstDDL,   int lastDDL, 
65                       int firstStrip, int lastStrip)
66       : fOutput(output),
67         fFirstDDL(firstDDL),     fLastDDL(lastDDL), 
68         fFirstStrip(firstStrip), fLastStrip(lastStrip)
69     {}
70     void Exec()
71     {
72       std::cout << "Will write on " << fOutput << std::endl;
73       std::ofstream file(fOutput.Data());
74       if (file.bad()) { 
75         std::cerr << "Failed to open output file " << fOutput << std::endl;
76         return;
77       }
78       Header(file);
79       for (int ddl = fFirstDDL; ddl <= fLastDDL; ddl++) { 
80         int  boards[] = { 0, 16, (ddl==1 ? -1 : 1), (ddl==1 ? -1 : 17), -1};
81         int* bptr     = boards;
82         int  board    = -1;
83         while ((board = (*bptr++)) >= 0) { 
84           for (int chip = 0; chip < 3; chip++) { 
85             for (int channel = 0; channel < (chip == 1 ? 8 : 16); channel++) { 
86               for (int strip = fFirstStrip; strip <= fLastStrip; strip++) {
87                 Output(file, ddl, board, chip, channel, strip);
88               }
89             } // for channel 
90           } // for chip
91         } // while board 
92       } // for ddl
93       file.close();
94     }
95     virtual void Header(std::ostream& file) = 0;
96     virtual void Output(std::ostream& file, int ddl, int board, int chip, 
97                         int channel, int strip) = 0;
98   protected:
99     TString fOutput;
100     int fFirstDDL;
101     int fLastDDL;
102     int fFirstStrip;
103     int fLastStrip;
104   };
105
106   //__________________________________________________________________
107   class CreateDummyPeds : public  CreateDummyDaData
108   {
109   public:
110     CreateDummyPeds(const char* out="peds.csv", 
111                     int overSampling=4,
112                     int firstDDL=0,   int lastDDL=2, 
113                     int firstStrip=0, int lastStrip=127)
114       : CreateDummyDaData(out, firstDDL, lastDDL, firstStrip, lastStrip), 
115         fOverSampling(overSampling)
116     {}
117     void Output(std::ostream& file, int ddl, int board, int chip, int channel, 
118                 int strip)
119     {
120       // Format is
121       //   ddl,board,chip,channel,strip,sample,ped,noise,mu,sigma,chi2
122       for (int sample = 0; sample < fOverSampling; sample++) {
123         Float_t ped   = Hardware2Ped(ddl, board, chip, channel, strip);
124         Float_t noise = Hardware2Noise(ddl, board, chip, channel, strip);
125         file // << ddl     << ","
126              << board   << "," 
127              << chip    << ","
128              << channel << ","
129              << strip   << ","
130              << sample  << ","
131              << ped     << ","  
132              << noise   << ","  
133              << chip    << ","  // Predictable mu
134              << channel << ","  // Predictable sigma 
135              << strip           // Predictable chi2/ndf
136              << endl;
137       }
138     }
139     void Header(std::ostream& file) 
140     { 
141       file << "# Pedestals\n" 
142            << "# ddl,board,chip,channel,strip,sample,mean,noise,mu,sigma,chi"
143            << std::endl;
144     }
145   protected:
146     int fOverSampling;
147   };
148
149   //__________________________________________________________________
150   class CreateDummyGains : public  CreateDummyDaData
151   {
152   public:
153     CreateDummyGains(const char* out="gains.csv", 
154                      int firstDDL=0,   int lastDDL=2, 
155                      int firstStrip=0, int lastStrip=127)
156       : CreateDummyDaData(out, firstDDL, lastDDL, firstStrip, lastStrip) 
157     {}
158     void Output(std::ostream& file, int ddl, int board, int chip, int channel, 
159                 int strip)
160     {
161       // Format is
162       //   ddl,board,chip,channel,strip,gain,error,chi2
163       Float_t gain = Hardware2Gain(ddl, board, chip, channel, strip);
164       file // << ddl     << ","
165            << board   << "," 
166            << chip    << ","
167            << channel << ","
168            << strip   << ","
169            << gain    << ","  // Predictable gain
170            << board   << ","  // Predictable error
171            << strip           // Predictable chi2/ndf
172            << endl;
173     }
174     void Header(std::ostream& file) 
175     { 
176       file << "# Gains\n" 
177            << "# ddl,board,chip,channel,strip,gain,errorchi"
178            << std::endl;
179     }
180   };
181 }
182
183 //====================================================================
184 //
185 // Read back the calibrations written, and check the values 
186 //
187 void ReadBack(const char* dbBase="local://$ALICE_ROOT/FMD/")
188 {
189   // AliLog::SetModuleDebugLevel("FMD", 1);
190   // Set specific storage of FMD ALTRO map 
191   AliCDBManager::Instance()->SetDefaultStorage(Form("%s/TestCDB", dbBase));
192   AliCDBManager::Instance()->SetSpecificStorage("FMD/Calib/AltroMap",
193                                                 "local://$ALICE_ROOT");
194   AliCDBManager::Instance()->SetRun(0);
195   
196   AliFMDParameters* param = AliFMDParameters::Instance();
197   std::cout << "Getting the stuff via AliFMDParameters ... " << std::flush;
198   param->Init(kTRUE,AliFMDParameters::kPulseGain|
199               AliFMDParameters::kPedestal|
200               AliFMDParameters::kAltroMap);
201   std::cout << "done" << std::endl;
202   // param->Print("FMD1I[*,*]");
203   
204   for (UShort_t det = 3; det <= 3; det++) { 
205     Char_t  rs[] = { 'I', (det==1 ? '\0' : 'O'), '\0' };
206     Char_t* pr   = rs;
207     Char_t  rng  = '\0';
208     while ((rng = *(pr++)) != '\0') { 
209       UShort_t nsec = (rng == 'I' ?  20 :  40);
210       UShort_t nstr = (rng == 'I' ? 512 : 256);
211       for (UShort_t sec = 0; sec < nsec; sec++) { 
212         for (UShort_t str = 0; str < nstr; str++) { 
213           Float_t  gain    = param->GetPulseGain(det,rng,sec,str);
214           Float_t  ped     = param->GetPedestal(det,rng,sec,str);
215           Float_t  noise   = param->GetPedestalWidth(det,rng,sec,str);
216           UInt_t   ddl, board, chip, channel;
217           param->Detector2Hardware(det,rng,sec,str,ddl,board,chip,channel);
218           UShort_t strip   = str % 128;
219           Float_t  eped    = Hardware2Ped(ddl, board, chip, channel, strip);
220           Float_t  enoise  = Hardware2Noise(ddl, board, chip, channel, strip);
221           Float_t  egain   = Hardware2Gain(ddl, board, chip, channel, strip);
222           if (ped  != eped) 
223             Error(Form("FMD%d%c[%2d,%3d] (%d,%2d,%1d,%2d)",
224                        det,rng,sec,str,ddl,board,chip,channel),
225                   "pedestal=%14.7f != %14.7f", ped, eped);
226           if (noise  != enoise) 
227             Error(Form("FMD%d%c[%2d,%3d] (%d,%2d,%1d,%2d)",
228                        det,rng,sec,str,ddl,board,chip,channel),
229                   "noise=%14.7f != %14.7f", noise, enoise);
230 #if 0
231           // Will fail due to rounding errors.
232           if (gain  != egain) 
233             Error(Form("FMD%d%c[%2d,%3d] (%d,%2d,%1d,%2d)",
234                        det,rng,sec,str,ddl,board,chip,channel),
235                   "gain=%14.7f != %14.7f", gain, egain);
236 #endif
237         }
238       }
239     }
240   }
241 }
242
243  
244 //====================================================================
245 //
246 // This script runs the test preprocessor. It uses AliTestShuttle to
247 // simulate a full Shuttle process
248 //
249 // The input data is created in the functions
250 //
251 //   CreateDCSAliasMap()   creates input that would in the same way come
252 //                         from DCS 
253 //   ReadDCSAliasMap()     reads from a file
254 //   CreateInputFilesMap() creates a list of local files, that can be
255 //                         accessed by the shuttle 
256 //
257 void TestPreprocessor(const char* runType="PEDESTAL", 
258                       Bool_t createDummies=kTRUE,
259                       const char* dbBase="local://$ALICE_ROOT/FMD/")
260 {
261   // Dummy data
262   if (createDummies) { 
263     CreateDummyPeds pedMaker;
264     pedMaker.Exec();
265     CreateDummyGains gainMaker;
266     gainMaker.Exec();
267   }
268   
269   // load library - needs to be built using make
270   gSystem->Load("libTestSHUTTLE.so"); 
271
272    // create AliTestShuttle instance
273   // The parameters are run, startTime, endTime
274   AliTestShuttle* shuttle = new AliTestShuttle(0, 0, 1);
275   AliTestShuttle::SetMainCDB("local://$ALICE_ROOT/FMD/TestCDB");
276   AliTestShuttle::SetLocalCDB(Form("%s/TestCDB", dbBase));
277   AliTestShuttle::SetMainRefStorage(Form("%s/TestReference", dbBase));
278
279   std::cout << "Test OCDB storage URI: " << AliShuttleInterface::GetMainCDB()
280             << "\n"
281             << "Test Reference storage Uri: "
282             << AliShuttleInterface::GetMainRefStorage().Data()
283             << std::endl;
284   
285   shuttle->AddInputFile(AliShuttleInterface::kDAQ, "FMD", "pedestal", 
286                         "source1", "peds.csv");
287   shuttle->AddInputFile(AliShuttleInterface::kDAQ, "FMD", "gain", 
288                         "source2", "gains.csv");
289   shuttle->SetInputRunType(runType);
290   // shuttle->SetInputRunType("PEDESTAL PULSER PHYSICS");
291
292   new AliFMDPreprocessor(shuttle);
293   // Test the preprocessor
294   shuttle->Process();
295
296
297   // Read back 
298   ReadBack(dbBase);
299 }
300       
301 //____________________________________________________________________
302 // We do not use this functions .....yet
303 TMap* CreateDCSAliasMap()
304 {
305   // Creates a DCS structure
306   // The structure is the following:
307   // 
308   //   TMap (key --> value)
309   //     <DCSAlias> --> <valueList>
310   //     <DCSAlias> is a string
311   //     <valueList> is a TObjArray of AliDCSValue
312   //     An AliDCSValue consists of timestamp and a value in form of a
313   //     AliSimpleValue 
314   // 
315   // In this example 6 aliases exists: DCSAlias1 ... DCSAlias6
316   // Each contains 1000 values randomly generated by TRandom::Gaus +
317   // 5*nAlias 
318   TRandom random;
319   TMap*   aliasMap = new TMap;
320   aliasMap->SetOwner(1);
321
322   for(int nAlias=0;nAlias<6;nAlias++) {
323     TObjArray* valueSet = new TObjArray;
324     valueSet->SetOwner(1);
325
326     TString aliasName="DCSAlias";
327     aliasName += nAlias;
328     //printf("\n\n alias: %s\n\n",aliasName.Data());
329     for (int timeStamp = 0; timeStamp < 1000; timeStamp += 10) {
330       Float_t      x      =  Float_t(random.Gaus()+5*nAlias);
331       AliDCSValue* dcsVal = new AliDCSValue(x, timeStamp);
332       valueSet->Add(dcsVal);
333     }
334     aliasMap->Add(new TObjString(aliasName), valueSet);
335   }
336
337   return aliasMap;
338 }
339
340 //____________________________________________________________________
341 TMap* ReadDCSAliasMap()
342 {
343   // Open a file that contains DCS input data
344   // 
345   // The CDB framework is used to open the file, this means the file
346   // is located in 
347   //  
348   //   $ALICE_ROOT/FMD/TestCDB/<detector>/DCS/Data
349   // 
350   // The file contains an AliCDBEntry that contains a TMap with the
351   // DCS structure.  An explanation of the structure can be found in
352   // CreateDCSAliasMap() 
353   AliCDBEntry *entry = 
354     AliCDBManager::Instance()->GetStorage(AliShuttleInterface::GetMainCDB())
355     ->Get("DET/DCS/Data", 0);
356   return dynamic_cast<TMap*> (entry->GetObject());
357 }
358
359 //____________________________________________________________________
360 void WriteDCSAliasMap()
361 {
362   // This writes the output from CreateDCSAliasMap to a CDB file
363
364   TMap*          dcsAliasMap = CreateDCSAliasMap();
365   AliCDBMetaData metaData;
366   metaData.SetBeamPeriod(0);
367   metaData.SetResponsible("Responsible person");
368   metaData.SetComment("Test object for TestPreprocessor.C");
369
370   AliCDBId id("DET/DCS/Data", 0, 0);
371   
372   // look into AliTestShuttle's CDB main folder
373   AliCDBManager::Instance()->GetStorage(AliShuttleInterface::GetMainCDB())
374     ->Put(dcsAliasMap, id, &metaData);
375 }
376
377 #ifndef __CINT__
378 int
379 main(int argc, char** argv)
380 {
381   Bool_t createDummies = kTRUE;
382   TString dbBase   = "local://$ALICE_ROOT/FMD/";
383   for (int i = 1; i < argc; i++) { 
384     if (argv[i][0] == '-') { 
385       switch (argv[i][1])  {
386       case 'h': 
387         std::cout << "Usage: " << argv[0] << " [OPTIONS]\n\n"
388                   << "Options:\n"
389                   << "\t-h\tThis help\n"
390                   << "\t-d\tToggle dummies\n" 
391                   << "\t-b DIR\tSet database dir\n" 
392                   << std::endl;
393         return 0;
394       case 'd':  createDummies = !createDummies; break;
395       case 'b':  dbBase = argv[++i]; break;
396       }
397     }
398   }
399   
400   TestPreprocessor(createDummies, dbBase);
401   return 0;
402 }
403
404 #endif
405   
406 //____________________________________________________________________
407 //
408 // EOF
409 //