]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/trigger/test/testTriggerCounterComponent.C
Adding fixes to count CTP trigger correctly for multiple global trigger instances...
[u/mrichter/AliRoot.git] / HLT / trigger / test / testTriggerCounterComponent.C
1 /**************************************************************************
2  * This file is property of and copyright by the ALICE HLT Project        *
3  * ALICE Experiment at CERN, All rights reserved.                         *
4  *                                                                        *
5  * Primary Authors: Artur Szostak <artursz@iafrica.com>                   *
6  *                  for The ALICE HLT Project.                            *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /**
18  * @file   testTriggerCounterComponent.C
19  * @author Artur Szostak <artursz@iafrica.com>
20  * @date   5 Nov 2010
21  *
22  * This macro is used to test the AliHLTTriggerCounterComponent class.
23  * Tests are run with component within the AliHLTSystem framework to check that
24  * the trigger counters are generated correctly by the component.
25  */
26
27 #if !defined(__CINT__) || defined(__MAKECINT__)
28 #include "TSystem.h"
29 #include "TFile.h"
30 #include "AliLog.h"
31 #include "AliHLTTriggerCounters.h"
32 #include "AliHLTTriggerDecision.h"
33 #include "AliHLTGlobalTriggerDecision.h"
34 #include "AliHLTSystem.h"
35 #include "AliHLTConfiguration.h"
36 #include "AliHLTCTPData.h"
37 #include "Riostream.h"
38 #endif
39
40 /**
41  * Generates some sample input data and writes it into 9 files named
42  * testInputFileTriggerCounter1.root ... testInputFileTriggerCounter9.root
43  */
44 void GenerateInputData()
45 {
46         AliHLTTriggerDecision td1(true, "TRIGGER-A", AliHLTTriggerDomain("TRACKS:TPC"));
47         AliHLTTriggerDecision td1f(false, "TRIGGER-A", AliHLTTriggerDomain());
48         AliHLTTriggerDecision td2(true, "TRIGGER-B", AliHLTTriggerDomain("CLUSTERS:TPC"));
49         AliHLTTriggerDecision td2f(false, "TRIGGER-B", AliHLTTriggerDomain());
50         AliHLTTriggerDecision td3(true, "TRIGGER-C", AliHLTTriggerDomain("*******:***"));
51         AliHLTTriggerDecision td3f(false, "TRIGGER-C", AliHLTTriggerDomain());
52         AliHLTGlobalTriggerDecision gd1(true, AliHLTTriggerDomain("TRACKS:TPC"), "TRIGGER-A");
53         AliHLTGlobalTriggerDecision gd2(true, AliHLTTriggerDomain("CLUSTERS:TPC"), "TRIGGER-B");
54         AliHLTGlobalTriggerDecision gd3(true, AliHLTTriggerDomain("*******:***"), "TRIGGER-B,TRIGGER-C");
55         
56         AliHLTCTPData ctp("CTP_TRIGGER_CLASS=00:CINTA:17,01:CINTB:00-01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17,02:CMUB:00-10-11-17");
57         gd1.AddInputObjectRef(&ctp);
58         gd2.AddInputObjectRef(&ctp);
59         gd3.AddInputObjectRef(&ctp);
60         
61         TFile* file = new TFile("testInputFileTriggerCounter1.root", "RECREATE");
62         ctp.Increment(int(0));
63         gd1.Write("HLTGlobalTrigger");
64         td1.Write("Trigger");
65         delete file;
66         
67         file = new TFile("testInputFileTriggerCounter2.root", "RECREATE");
68         ctp.Increment(int(1));
69         gd2.Write("HLTGlobalTrigger");
70         td1f.Write("Trigger");
71         td2.Write("Trigger");
72         td3f.Write("Trigger");
73         delete file;
74         
75         file = new TFile("testInputFileTriggerCounter3.root", "RECREATE");
76         ctp.Increment(int(2));
77         gd3.Write("HLTGlobalTrigger");
78         td1f.Write("Trigger");
79         td2.Write("Trigger");
80         td3.Write("Trigger");
81         delete file;
82         
83         gd1.AddTriggerInput(td1);
84         gd2.AddTriggerInput(td1f);
85         gd2.AddTriggerInput(td2);
86         gd2.AddTriggerInput(td3f);
87         gd3.AddTriggerInput(td1f);
88         gd3.AddTriggerInput(td2);
89         gd3.AddTriggerInput(td3);
90         
91         file = new TFile("testInputFileTriggerCounter4.root", "RECREATE");
92         ctp.Increment(int(0));
93         gd1.Write("HLTGlobalTrigger");
94         td1.Write("Trigger");
95         delete file;
96         
97         file = new TFile("testInputFileTriggerCounter5.root", "RECREATE");
98         ctp.Increment(int(1));
99         gd2.Write("HLTGlobalTrigger");
100         td1f.Write("Trigger");
101         td2.Write("Trigger");
102         delete file;
103         
104         file = new TFile("testInputFileTriggerCounter6.root", "RECREATE");
105         ctp.Increment(int(2));
106         gd3.Write("HLTGlobalTrigger");
107         td1f.Write("Trigger");
108         td2.Write("Trigger");
109         td3.Write("Trigger");
110         delete file;
111         
112         file = new TFile("testInputFileTriggerCounter7.root", "RECREATE");
113         ctp.Increment(int(0));
114         gd1.Write("HLTGlobalTrigger");
115         delete file;
116         
117         file = new TFile("testInputFileTriggerCounter8.root", "RECREATE");
118         ctp.Increment(int(1));
119         gd2.Write("HLTGlobalTrigger");
120         delete file;
121         
122         file = new TFile("testInputFileTriggerCounter9.root", "RECREATE");
123         ctp.Increment(int(2));
124         gd3.Write("HLTGlobalTrigger");
125         delete file;
126 }
127
128 /**
129  * Runs a small chain with the HLTTriggerCounter component to generate output that
130  * can be checked.
131  * \param debug  If true then full debug logging is enabled.
132  * \param numOfEvents  The number of events to run the chain for.
133  */
134 void RunTriggerCounter(bool debug = false, int numOfEvents = 9)
135 {
136         AliHLTSystem sys;
137         sys.ScanOptions("ECS=CTP_TRIGGER_CLASS=00:CINTA:17,01:CINTB:00-01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17,02:CMUB:00-10-11-17");
138         sys.LoadComponentLibraries("libAliHLTUtil.so");
139         sys.LoadComponentLibraries("libAliHLTTRD.so");
140         sys.LoadComponentLibraries("libAliHLTMUON.so");
141         sys.LoadComponentLibraries("libAliHLTTrigger.so");
142         if (debug)
143         {
144                 AliLog::SetGlobalLogLevel(AliLog::kMaxType);
145                 sys.SetGlobalLoggingLevel(kHLTLogAll);
146         }
147         
148         TString cmdline = "-datatype GLOBTRIG 'HLT ' -objectname HLTGlobalTrigger ";
149         for (int i = 1; i <= 9; i++)
150         {
151                 if (i > 1) cmdline += " -nextevent";
152                 cmdline += Form(" -datafile testInputFileTriggerCounter%d.root", i);
153         }
154         AliHLTConfiguration pub1("pub1", "ROOTFilePublisher", NULL, cmdline.Data());
155         cmdline = "-datatype TRIG_DEC 'HLT ' -objectname Trigger ";
156         for (int i = 1; i <= 9; i++)
157         {
158                 if (i > 1) cmdline += " -nextevent";
159                 cmdline += Form(" -datafile testInputFileTriggerCounter%d.root", i);
160         }
161         AliHLTConfiguration pub2("pub2", "ROOTFilePublisher", NULL, cmdline.Data());
162         AliHLTConfiguration proc("proc", "HLTTriggerCounter", "pub1 pub2", "-skipcdb -config InitialCounterConfig.C");
163         AliHLTConfiguration sink("sink", "ROOTFileWriter", "proc", "-datafile testOutputFileTriggerCounter.root -concatenate-events");
164         sys.BuildTaskList("sink");
165         AliHLTUInt64_t trigMask[9] = {0x1, 0x2, 0x4, 0x1, 0x2, 0x4, 0x1, 0x2, 0x4};
166         for (int i = 0; i < 9 && i < numOfEvents; i++)
167         {
168                 sys.Run(
169                         1,  // number of events
170                         (i == 8 ? 1 : 0),  // Stop chain at end of run.
171                         trigMask[i], // Active CTP trigger mask.
172                         0,   // Time stamp.
173                         0    // Event type.
174                 );
175         }
176 }
177
178 /**
179  * Checks that the input and output counters have the expected values or not.
180  * If they do not an appropriate error message is printed.
181  * \param eventNum  The number of the event being checked.
182  * \param inputCounters  The input counters to check.
183  * \param outputCounters  The output counters to check.
184  * \param expectedInputCounters  The expected input counter values to check against.
185  * \param expectedOutputCounters  The expected output counter values to check against.
186  * \param expectedDescription  The expected resulting trigger description.
187  * \returns true if the counters are correct and false otherwise.
188  */
189 bool CheckCounters(
190                 int eventNum,
191                 AliHLTTriggerCounters* inputCounters,
192                 AliHLTTriggerCounters* outputCounters,
193                 AliHLTTriggerCounters& expectedInputCounters,
194                 AliHLTTriggerCounters& expectedOutputCounters
195         )
196 {
197         if (inputCounters == NULL)
198         {
199                 cerr << "ERROR: No trigger input counters were found for event "
200                      << eventNum << "." << endl;
201                 return false;
202         }
203         if (outputCounters == NULL)
204         {
205                 cerr << "ERROR: No trigger output counters were found for event "
206                      << eventNum << "." << endl;
207                 return false;
208         }
209         // Copy over the Rates because these might not be identical since they are a
210         // run time parameter. The counter value and name must be identical however.
211         for (UInt_t i = 0; i < inputCounters->NumberOfCounters() && i < expectedInputCounters.NumberOfCounters(); ++i)
212         {
213                 expectedInputCounters.GetCounterN(i).Rate( inputCounters->GetCounterN(i).Rate() );
214         }
215         for (UInt_t i = 0; i < outputCounters->NumberOfCounters() && i < expectedOutputCounters.NumberOfCounters(); ++i)
216         {
217                 expectedOutputCounters.GetCounterN(i).Rate( outputCounters->GetCounterN(i).Rate() );
218         }
219         bool inputCountersAsExpected = (inputCounters->NumberOfCounters() == expectedInputCounters.NumberOfCounters());
220         bool outputCountersAsExpected = (outputCounters->NumberOfCounters() == expectedOutputCounters.NumberOfCounters());
221         for (UInt_t i = 0; i < inputCounters->NumberOfCounters() && i < expectedInputCounters.NumberOfCounters(); ++i)
222         {
223                 if (strcmp(expectedInputCounters.GetCounterN(i).Name(), inputCounters->GetCounterN(i).Name()) != 0)
224                 {
225                         inputCountersAsExpected = false;
226                         break;
227                 }
228                 if (expectedInputCounters.GetCounterN(i).Counter() != inputCounters->GetCounterN(i).Counter())
229                 {
230                         inputCountersAsExpected = false;
231                         break;
232                 }
233         }
234         for (UInt_t i = 0; i < outputCounters->NumberOfCounters() && i < expectedOutputCounters.NumberOfCounters(); ++i)
235         {
236                 if (strcmp(expectedOutputCounters.GetCounterN(i).Name(), outputCounters->GetCounterN(i).Name()) != 0)
237                 {
238                         outputCountersAsExpected = false;
239                         break;
240                 }
241                 if (expectedOutputCounters.GetCounterN(i).Counter() != outputCounters->GetCounterN(i).Counter())
242                 {
243                         outputCountersAsExpected = false;
244                         break;
245                 }
246         }
247         if (! inputCountersAsExpected)
248         {
249                 cerr << "ERROR: The trigger input counters do not match the expected set of counters for event "
250                      << eventNum << "." << endl;
251                 cout << "Found the following input counters: " << endl;  // using cout since Print() goes there.
252                 inputCounters->Print();
253                 cout << "But expected the following input counters: " << endl;
254                 expectedInputCounters.Print();
255                 return false;
256         }
257         if (! outputCountersAsExpected)
258         {
259                 cerr << "ERROR: The trigger output counters do not match the expected set of counters for event "
260                      << eventNum << "." << endl;
261                 cout << "Found the following output counters: " << endl;  // using cout since Print() goes there.
262                 outputCounters->Print();
263                 cout << "But expected the following output counters: " << endl;
264                 expectedOutputCounters.Print();
265                 return false;
266         }
267         return true;
268 }
269
270
271 /// Routine for checking the results file generated by the RunTriggerCounter routine.
272 bool CheckTriggerCounterOutput()
273 {
274         AliHLTTriggerCounters expectedInputCounters[11];
275         AliHLTTriggerCounters expectedOutputCounters[11];
276         
277         // Start-of-run event (should contain no values)
278         expectedInputCounters[0].Add("CINTA", "", 0, 0);
279         expectedInputCounters[0].Add("CINTB", "", 0, 0);
280         expectedInputCounters[0].Add("CMUB", "", 0, 0);
281         expectedOutputCounters[0].Add("TRIGGER-A", "", 0, 0);
282         
283         // Data events
284         expectedInputCounters[1].Add("CINTA", "", 0, 1);
285         expectedInputCounters[1].Add("CINTB", "", 0, 0);
286         expectedInputCounters[1].Add("CMUB", "", 0, 0);
287         expectedInputCounters[1].Add("TRIGGER-A", "", 0, 1);
288         expectedOutputCounters[1].Add("TRIGGER-A", "", 0, 1);
289         
290         expectedInputCounters[2].Add("CINTA", "", 0, 1);
291         expectedInputCounters[2].Add("CINTB", "", 0, 1);
292         expectedInputCounters[2].Add("CMUB", "", 0, 0);
293         expectedInputCounters[2].Add("TRIGGER-A", "", 0, 1);
294         expectedInputCounters[2].Add("TRIGGER-B", "", 0, 1);
295         expectedOutputCounters[2].Add("TRIGGER-A", "", 0, 1);
296         expectedOutputCounters[2].Add("TRIGGER-B", "", 0, 1);
297         
298         expectedInputCounters[3].Add("CINTA", "", 0, 1);
299         expectedInputCounters[3].Add("CINTB", "", 0, 1);
300         expectedInputCounters[3].Add("CMUB", "", 0, 1);
301         expectedInputCounters[3].Add("TRIGGER-A", "", 0, 1);
302         expectedInputCounters[3].Add("TRIGGER-B", "", 0, 2);
303         expectedInputCounters[3].Add("TRIGGER-C", "", 0, 1);
304         expectedOutputCounters[3].Add("TRIGGER-A", "", 0, 1);
305         expectedOutputCounters[3].Add("TRIGGER-B", "", 0, 2);
306         expectedOutputCounters[3].Add("TRIGGER-C", "", 0, 1);
307         
308         expectedInputCounters[4].Add("CINTA", "", 0, 2);
309         expectedInputCounters[4].Add("CINTB", "", 0, 1);
310         expectedInputCounters[4].Add("CMUB", "", 0, 1);
311         expectedInputCounters[4].Add("TRIGGER-A", "", 0, 2);
312         expectedInputCounters[4].Add("TRIGGER-B", "", 0, 2);
313         expectedInputCounters[4].Add("TRIGGER-C", "", 0, 1);
314         expectedOutputCounters[4].Add("TRIGGER-A", "", 0, 2);
315         expectedOutputCounters[4].Add("TRIGGER-B", "", 0, 2);
316         expectedOutputCounters[4].Add("TRIGGER-C", "", 0, 1);
317         
318         expectedInputCounters[5].Add("CINTA", "", 0, 2);
319         expectedInputCounters[5].Add("CINTB", "", 0, 2);
320         expectedInputCounters[5].Add("CMUB", "", 0, 1);
321         expectedInputCounters[5].Add("TRIGGER-A", "", 0, 2);
322         expectedInputCounters[5].Add("TRIGGER-B", "", 0, 3);
323         expectedInputCounters[5].Add("TRIGGER-C", "", 0, 1);
324         expectedOutputCounters[5].Add("TRIGGER-A", "", 0, 2);
325         expectedOutputCounters[5].Add("TRIGGER-B", "", 0, 3);
326         expectedOutputCounters[5].Add("TRIGGER-C", "", 0, 1);
327         
328         expectedInputCounters[6].Add("CINTA", "", 0, 2);
329         expectedInputCounters[6].Add("CINTB", "", 0, 2);
330         expectedInputCounters[6].Add("CMUB", "", 0, 2);
331         expectedInputCounters[6].Add("TRIGGER-A", "", 0, 2);
332         expectedInputCounters[6].Add("TRIGGER-B", "", 0, 4);
333         expectedInputCounters[6].Add("TRIGGER-C", "", 0, 2);
334         expectedOutputCounters[6].Add("TRIGGER-A", "", 0, 2);
335         expectedOutputCounters[6].Add("TRIGGER-B", "", 0, 4);
336         expectedOutputCounters[6].Add("TRIGGER-C", "", 0, 2);
337         
338         expectedInputCounters[7].Add("CINTA", "", 0, 3);
339         expectedInputCounters[7].Add("CINTB", "", 0, 2);
340         expectedInputCounters[7].Add("CMUB", "", 0, 2);
341         expectedInputCounters[7].Add("TRIGGER-A", "", 0, 3);
342         expectedInputCounters[7].Add("TRIGGER-B", "", 0, 4);
343         expectedInputCounters[7].Add("TRIGGER-C", "", 0, 2);
344         expectedOutputCounters[7].Add("TRIGGER-A", "", 0, 3);
345         expectedOutputCounters[7].Add("TRIGGER-B", "", 0, 4);
346         expectedOutputCounters[7].Add("TRIGGER-C", "", 0, 2);
347         
348         expectedInputCounters[8].Add("CINTA", "", 0, 3);
349         expectedInputCounters[8].Add("CINTB", "", 0, 3);
350         expectedInputCounters[8].Add("CMUB", "", 0, 2);
351         expectedInputCounters[8].Add("TRIGGER-A", "", 0, 3);
352         expectedInputCounters[8].Add("TRIGGER-B", "", 0, 5);
353         expectedInputCounters[8].Add("TRIGGER-C", "", 0, 2);
354         expectedOutputCounters[8].Add("TRIGGER-A", "", 0, 3);
355         expectedOutputCounters[8].Add("TRIGGER-B", "", 0, 5);
356         expectedOutputCounters[8].Add("TRIGGER-C", "", 0, 2);
357         
358         expectedInputCounters[9].Add("CINTA", "", 0, 3);
359         expectedInputCounters[9].Add("CINTB", "", 0, 3);
360         expectedInputCounters[9].Add("CMUB", "", 0, 3);
361         expectedInputCounters[9].Add("TRIGGER-A", "", 0, 3);
362         expectedInputCounters[9].Add("TRIGGER-B", "", 0, 6);
363         expectedInputCounters[9].Add("TRIGGER-C", "", 0, 3);
364         expectedOutputCounters[9].Add("TRIGGER-A", "", 0, 3);
365         expectedOutputCounters[9].Add("TRIGGER-B", "", 0, 6);
366         expectedOutputCounters[9].Add("TRIGGER-C", "", 0, 3);
367         
368         // End-of-run event
369         expectedInputCounters[10].Add("CINTA", "", 0, 3);
370         expectedInputCounters[10].Add("CINTB", "", 0, 3);
371         expectedInputCounters[10].Add("CMUB", "", 0, 3);
372         expectedInputCounters[10].Add("TRIGGER-A", "", 0, 3);
373         expectedInputCounters[10].Add("TRIGGER-B", "", 0, 6);
374         expectedInputCounters[10].Add("TRIGGER-C", "", 0, 3);
375         expectedOutputCounters[10].Add("TRIGGER-A", "", 0, 3);
376         expectedOutputCounters[10].Add("TRIGGER-B", "", 0, 6);
377         expectedOutputCounters[10].Add("TRIGGER-C", "", 0, 3);
378         
379         TFile* file = new TFile("testOutputFileTriggerCounter.root", "READ");
380         for (int i = 0; i < 11; ++i)
381         {
382                 AliHLTTriggerCounters* inputCounters = dynamic_cast<AliHLTTriggerCounters*>( file->Get(Form("AliHLTTriggerCounters;%d", i*2+1)) );
383                 AliHLTTriggerCounters* outputCounters = dynamic_cast<AliHLTTriggerCounters*>( file->Get(Form("AliHLTTriggerCounters;%d", i*2+2)) );
384                 if (! CheckCounters(i-1, inputCounters, outputCounters, expectedInputCounters[i], expectedOutputCounters[i]))
385                 {
386                         delete file;
387                         return false;
388                 }
389         }
390         delete file;
391         return true;
392 }
393
394 /**
395  * Runs several tests for the AliHLTTriggerCounterComponent class.
396  * We specifically test if the generated counters are correctly generated.
397  * \param debug  If true then full debug logging is enabled.
398  * \param numOfEvents  The number of events to run the chain over.
399  * \returns true if all the tests succeeded and false otherwise.
400  */
401 bool testTriggerCounterComponent(bool debug = false, int numOfEvents = 9)
402 {
403         GenerateInputData();
404         RunTriggerCounter(debug, numOfEvents);
405         if (! CheckTriggerCounterOutput()) return false;
406         // Cleanup all temporary files generated.
407         gSystem->Exec("rm -f testOutputFileTriggerCounter.root testInputFileTriggerCounter*.root");
408         return true;
409 }
410
411
412 #ifndef __MAKECINT__
413
414 int main(int /*argc*/, const char** /*argv*/)
415 {
416         bool resultOk = testTriggerCounterComponent();
417         if (not resultOk) return 1;
418         return 0;
419 }
420
421 #endif // __MAKECINT__