]> git.uio.no Git - u/mrichter/AliRoot.git/blame - FMD/scripts/TestPreprocessor.C
Fixed AliFMDPreprocessor to use GetFromOCDB. It is still AliFMDParameters
[u/mrichter/AliRoot.git] / FMD / scripts / TestPreprocessor.C
CommitLineData
f6449cc0 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>
dc02d468 12# include <FMD/AliFMDPreprocessor.h>
f6449cc0 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>
dc02d468 23# include <FMD/AliFMDParameters.h>
24# include <SHUTTLE/TestShuttle/AliTestShuttle.h>
f6449cc0 25# include <TRandom.h>
26# include <TSystem.h>
27# include <TObjString.h>
28# include <TMap.h>
29# include <TError.h>
dc02d468 30# include <iostream>
f6449cc0 31#endif
32
33namespace {
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//
187void ReadBack(const char* dbBase="local://$ALICE_ROOT/FMD/")
188{
dc02d468 189 // AliLog::SetModuleDebugLevel("FMD", 1);
f6449cc0 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//
257void TestPreprocessor(Bool_t createDummies=kTRUE,
258 const char* dbBase="local://$ALICE_ROOT/FMD/")
259{
260 // Dummy data
261 if (createDummies) {
262 CreateDummyPeds pedMaker;
263 pedMaker.Exec();
264 CreateDummyGains gainMaker;
265 gainMaker.Exec();
266 }
267
268 // load library - needs to be built using make
269 gSystem->Load("libTestSHUTTLE.so");
270
271 // create AliTestShuttle instance
272 // The parameters are run, startTime, endTime
273 AliTestShuttle* shuttle = new AliTestShuttle(0, 0, 1);
dc02d468 274 AliTestShuttle::SetMainCDB("local://$ALICE_ROOT");
275 AliTestShuttle::SetLocalCDB(Form("%s/TestCDB", dbBase));
f6449cc0 276 AliTestShuttle::SetMainRefStorage(Form("%s/TestReference", dbBase));
277
278 std::cout << "Test OCDB storage URI: " << AliShuttleInterface::GetMainCDB()
279 << "\n"
280 << "Test Reference storage Uri: "
281 << AliShuttleInterface::GetMainRefStorage().Data()
282 << std::endl;
283
284 shuttle->AddInputFile(AliShuttleInterface::kDAQ, "FMD", "pedestal",
285 "source1", "peds.csv");
286 shuttle->AddInputFile(AliShuttleInterface::kDAQ, "FMD", "gain",
287 "source2", "gains.csv");
dc02d468 288 shuttle->SetInputRunType("PEDESTAL PULSER PHYSICS");
f6449cc0 289
290 new AliFMDPreprocessor(shuttle);
291 // Test the preprocessor
292 shuttle->Process();
293
294
295 // Read back
296 ReadBack(dbBase);
297}
298
299//____________________________________________________________________
300// We do not use this functions .....yet
301TMap* CreateDCSAliasMap()
302{
303 // Creates a DCS structure
304 // The structure is the following:
305 //
306 // TMap (key --> value)
307 // <DCSAlias> --> <valueList>
308 // <DCSAlias> is a string
309 // <valueList> is a TObjArray of AliDCSValue
310 // An AliDCSValue consists of timestamp and a value in form of a
311 // AliSimpleValue
312 //
313 // In this example 6 aliases exists: DCSAlias1 ... DCSAlias6
314 // Each contains 1000 values randomly generated by TRandom::Gaus +
315 // 5*nAlias
316 TRandom random;
317 TMap* aliasMap = new TMap;
318 aliasMap->SetOwner(1);
319
320 for(int nAlias=0;nAlias<6;nAlias++) {
321 TObjArray* valueSet = new TObjArray;
322 valueSet->SetOwner(1);
323
324 TString aliasName="DCSAlias";
325 aliasName += nAlias;
326 //printf("\n\n alias: %s\n\n",aliasName.Data());
327 for (int timeStamp = 0; timeStamp < 1000; timeStamp += 10) {
328 Float_t x = Float_t(random.Gaus()+5*nAlias);
329 AliDCSValue* dcsVal = new AliDCSValue(x, timeStamp);
330 valueSet->Add(dcsVal);
331 }
332 aliasMap->Add(new TObjString(aliasName), valueSet);
333 }
334
335 return aliasMap;
336}
337
338//____________________________________________________________________
339TMap* ReadDCSAliasMap()
340{
341 // Open a file that contains DCS input data
342 //
343 // The CDB framework is used to open the file, this means the file
344 // is located in
345 //
346 // $ALICE_ROOT/FMD/TestCDB/<detector>/DCS/Data
347 //
348 // The file contains an AliCDBEntry that contains a TMap with the
349 // DCS structure. An explanation of the structure can be found in
350 // CreateDCSAliasMap()
351 AliCDBEntry *entry =
352 AliCDBManager::Instance()->GetStorage(AliShuttleInterface::GetMainCDB())
353 ->Get("DET/DCS/Data", 0);
354 return dynamic_cast<TMap*> (entry->GetObject());
355}
356
357//____________________________________________________________________
358void WriteDCSAliasMap()
359{
360 // This writes the output from CreateDCSAliasMap to a CDB file
361
362 TMap* dcsAliasMap = CreateDCSAliasMap();
363 AliCDBMetaData metaData;
364 metaData.SetBeamPeriod(0);
365 metaData.SetResponsible("Responsible person");
366 metaData.SetComment("Test object for TestPreprocessor.C");
367
368 AliCDBId id("DET/DCS/Data", 0, 0);
369
370 // look into AliTestShuttle's CDB main folder
371 AliCDBManager::Instance()->GetStorage(AliShuttleInterface::GetMainCDB())
372 ->Put(dcsAliasMap, id, &metaData);
373}
374
dc02d468 375#ifndef __CINT__
376int
377main(int argc, char** argv)
378{
379 Bool_t createDummies = kTRUE;
380 TString dbBase = "local://$ALICE_ROOT/FMD/";
381 for (int i = 1; i < argc; i++) {
382 if (argv[i][0] == '-') {
383 switch (argv[i][1]) {
384 case 'h':
385 std::cout << "Usage: " << argv[0] << " [OPTIONS]\n\n"
386 << "Options:\n"
387 << "\t-h\tThis help\n"
388 << "\t-d\tToggle dummies\n"
389 << "\t-b DIR\tSet database dir\n"
390 << std::endl;
391 return 0;
392 case 'd': createDummies = !createDummies; break;
393 case 'b': dbBase = argv[++i]; break;
394 }
395 }
396 }
397
398 TestPreprocessor(createDummies, dbBase);
399 return 0;
400}
401
402#endif
403
f6449cc0 404//____________________________________________________________________
405//
406// EOF
407//