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