]>
Commit | Line | Data |
---|---|---|
3da1c6d7 | 1 | // $Id$ |
1b9a175e | 2 | /************************************************************************** |
3 | * This file is property of and copyright by the ALICE HLT Project * | |
4 | * ALICE Experiment at CERN, All rights reserved. * | |
5 | * * | |
6 | * Primary Authors: Artur Szostak <artursz@iafrica.com> * | |
7 | * for The ALICE HLT Project. * | |
8 | * * | |
9 | * Permission to use, copy, modify and distribute this software and its * | |
10 | * documentation strictly for non-commercial purposes is hereby granted * | |
11 | * without fee, provided that the above copyright notice appears in all * | |
12 | * copies and that both the copyright notice and this permission notice * | |
13 | * appear in the supporting documentation. The authors make no claims * | |
14 | * about the suitability of this software for any purpose. It is * | |
15 | * provided "as is" without express or implied warranty. * | |
16 | **************************************************************************/ | |
17 | ||
18 | /// @file AliHLTGlobalTriggerComponent.cxx | |
19 | /// @author Artur Szostak <artursz@iafrica.com> | |
20 | /// @date 26 Nov 2008 | |
21 | /// @brief Implementation of the AliHLTGlobalTriggerComponent component class. | |
22 | /// | |
23 | /// The AliHLTGlobalTriggerComponentComponent class applies the global HLT trigger to all | |
24 | /// trigger information produced by components deriving from AliHLTTrigger. | |
25 | ||
26 | #include "AliHLTGlobalTriggerComponent.h" | |
e2bb8ddd | 27 | #include "AliHLTGlobalTriggerDecision.h" |
28 | #include "AliHLTGlobalTrigger.h" | |
52f67e50 | 29 | #include "AliHLTGlobalTriggerConfig.h" |
30 | #include "AliHLTTriggerMenu.h" | |
474952b2 | 31 | #include "AliHLTCTPData.h" |
3da1c6d7 | 32 | #include "AliCDBManager.h" |
33 | #include "AliCDBStorage.h" | |
34 | #include "AliCDBEntry.h" | |
e2bb8ddd | 35 | #include "TUUID.h" |
36 | #include "TROOT.h" | |
72bc5ab0 | 37 | #include "TSystem.h" |
52f67e50 | 38 | #include "TRegexp.h" |
39 | #include "TClonesArray.h" | |
40 | #include "TObjString.h" | |
e2bb8ddd | 41 | #include "TSystem.h" |
52f67e50 | 42 | #include "TInterpreter.h" |
d4ed5215 | 43 | #include "TDatime.h" |
81d62bb4 | 44 | #include "TClass.h" |
e2bb8ddd | 45 | #include <fstream> |
46 | #include <cerrno> | |
566a01d0 | 47 | #include <cassert> |
81d62bb4 | 48 | #include <vector> |
49 | #include <algorithm> | |
1b9a175e | 50 | |
51 | ClassImp(AliHLTGlobalTriggerComponent) | |
52 | ||
3da1c6d7 | 53 | const char* AliHLTGlobalTriggerComponent::fgkTriggerMenuCDBPath = "HLT/ConfigHLT/HLTGlobalTrigger"; |
54 | ||
1b9a175e | 55 | |
81d62bb4 | 56 | namespace |
57 | { | |
58 | /** | |
59 | * This method is used as a comparison functor with the STL sort routines. | |
60 | */ | |
61 | bool AliHLTDescendingNumbers(UInt_t a, UInt_t b) | |
62 | { | |
63 | return a > b; | |
64 | } | |
65 | } // end of namespace | |
66 | ||
67 | ||
1b9a175e | 68 | AliHLTGlobalTriggerComponent::AliHLTGlobalTriggerComponent() : |
e2bb8ddd | 69 | AliHLTTrigger(), |
52f67e50 | 70 | fTrigger(NULL), |
72bc5ab0 | 71 | fDebugMode(false), |
566a01d0 | 72 | fRuntimeCompile(true), |
73 | fSkipCTPCounters(false), | |
81d62bb4 | 74 | fDeleteCodeFile(false), |
566a01d0 | 75 | fCodeFileName(), |
81d62bb4 | 76 | fClassName(), |
77 | fCTPDecisions(NULL), | |
78 | fBufferSizeConst(2*(sizeof(AliHLTGlobalTriggerDecision) + sizeof(AliHLTReadoutList))), | |
79 | fBufferSizeMultiplier(1.), | |
80 | fIncludePaths(TObjString::Class()), | |
81 | fIncludeFiles(TObjString::Class()) | |
1b9a175e | 82 | { |
83 | // Default constructor. | |
acc7214e | 84 | |
85 | ClearInfoForNewEvent(false); | |
1b9a175e | 86 | } |
87 | ||
88 | ||
89 | AliHLTGlobalTriggerComponent::~AliHLTGlobalTriggerComponent() | |
90 | { | |
91 | // Default destructor. | |
e2bb8ddd | 92 | |
93 | if (fTrigger != NULL) delete fTrigger; | |
566a01d0 | 94 | |
95 | if (fCTPDecisions) { | |
96 | fCTPDecisions->Delete(); | |
97 | delete fCTPDecisions; | |
98 | } | |
1b9a175e | 99 | } |
100 | ||
101 | ||
102 | void AliHLTGlobalTriggerComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier) | |
103 | { | |
104 | // Returns the output data size estimate. | |
105 | ||
81d62bb4 | 106 | constBase = fBufferSizeConst; |
107 | inputMultiplier = fBufferSizeMultiplier; | |
1b9a175e | 108 | } |
109 | ||
110 | ||
52f67e50 | 111 | Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv) |
1b9a175e | 112 | { |
e2bb8ddd | 113 | // Initialises the global trigger component. |
114 | ||
52f67e50 | 115 | fDebugMode = false; |
81d62bb4 | 116 | fClassName = ""; |
117 | fCodeFileName = ""; | |
118 | fDeleteCodeFile = false; | |
52f67e50 | 119 | const char* configFileName = NULL; |
120 | const char* codeFileName = NULL; | |
81d62bb4 | 121 | fIncludePaths.Clear(); |
122 | fIncludeFiles.Clear(); | |
52f67e50 | 123 | |
124 | for (int i = 0; i < argc; i++) | |
125 | { | |
126 | if (strcmp(argv[i], "-config") == 0) | |
127 | { | |
128 | if (configFileName != NULL) | |
129 | { | |
130 | HLTWarning("Trigger configuration macro was already specified." | |
131 | " Will replace previous value given by -config." | |
132 | ); | |
133 | } | |
134 | if (argc <= i+1) | |
135 | { | |
136 | HLTError("The trigger configuration macro filename was not specified." ); | |
137 | return -EINVAL; | |
138 | } | |
139 | configFileName = argv[i+1]; | |
140 | i++; | |
141 | continue; | |
142 | } | |
143 | ||
144 | if (strcmp(argv[i], "-includepath") == 0) | |
145 | { | |
146 | if (argc <= i+1) | |
147 | { | |
148 | HLTError("The include path was not specified." ); | |
149 | return -EINVAL; | |
150 | } | |
81d62bb4 | 151 | new (fIncludePaths[fIncludePaths.GetEntriesFast()]) TObjString(argv[i+1]); |
52f67e50 | 152 | i++; |
153 | continue; | |
154 | } | |
155 | ||
156 | if (strcmp(argv[i], "-include") == 0) | |
157 | { | |
158 | if (argc <= i+1) | |
159 | { | |
160 | HLTError("The include file name was not specified." ); | |
161 | return -EINVAL; | |
162 | } | |
81d62bb4 | 163 | new (fIncludeFiles[fIncludeFiles.GetEntriesFast()]) TObjString(argv[i+1]); |
52f67e50 | 164 | i++; |
165 | continue; | |
166 | } | |
167 | ||
168 | if (strcmp(argv[i], "-debug") == 0) | |
169 | { | |
170 | if (fDebugMode == true) | |
171 | { | |
172 | HLTWarning("The debug flag was already specified. Ignoring this instance."); | |
173 | } | |
174 | fDebugMode = true; | |
566a01d0 | 175 | continue; |
176 | } | |
177 | ||
178 | if (strcmp(argv[i], "-cint") == 0) | |
179 | { | |
180 | fRuntimeCompile = false; | |
52f67e50 | 181 | continue; |
182 | } | |
183 | ||
184 | if (strcmp(argv[i], "-usecode") == 0) | |
185 | { | |
186 | if (codeFileName != NULL) | |
187 | { | |
188 | HLTWarning("Custom trigger code file was already specified." | |
189 | " Will replace previous value given by -usecode." | |
190 | ); | |
191 | } | |
192 | if (argc <= i+1) | |
193 | { | |
194 | HLTError("The custom trigger code filename was not specified." ); | |
195 | return -EINVAL; | |
196 | } | |
197 | codeFileName = argv[i+1]; | |
198 | if (argc <= i+2) | |
199 | { | |
200 | HLTError("The custom trigger class name was not specified." ); | |
201 | return -EINVAL; | |
202 | } | |
81d62bb4 | 203 | fClassName = argv[i+2]; |
52f67e50 | 204 | i += 2; |
205 | continue; | |
206 | } | |
474952b2 | 207 | |
208 | if (strcmp(argv[i], "-skipctp") == 0) | |
209 | { | |
210 | HLTInfo("Skipping CTP counters in trigger decision"); | |
566a01d0 | 211 | fSkipCTPCounters=true; |
474952b2 | 212 | continue; |
213 | } | |
214 | ||
52f67e50 | 215 | HLTError("Unknown option '%s'.", argv[i]); |
216 | return -EINVAL; | |
217 | } // for loop | |
218 | ||
219 | const AliHLTTriggerMenu* menu = NULL; | |
220 | if (configFileName != NULL) | |
221 | { | |
222 | TString cmd = ".x "; | |
223 | cmd += configFileName; | |
224 | gROOT->ProcessLine(cmd); | |
225 | menu = AliHLTGlobalTriggerConfig::Menu(); | |
226 | } | |
227 | ||
3da1c6d7 | 228 | // Try load the trigger menu from the CDB if it is not loaded yet with the |
229 | // -config option | |
81d62bb4 | 230 | int result = -ENOENT; |
231 | if (menu == NULL) | |
3da1c6d7 | 232 | { |
81d62bb4 | 233 | result = LoadTriggerMenu(fgkTriggerMenuCDBPath, menu); |
3da1c6d7 | 234 | } |
52f67e50 | 235 | if (menu == NULL) |
236 | { | |
237 | HLTError("No trigger menu configuration found or specified."); | |
81d62bb4 | 238 | return result; |
52f67e50 | 239 | } |
240 | ||
52f67e50 | 241 | if (codeFileName == NULL) |
242 | { | |
81d62bb4 | 243 | result = GenerateTrigger(menu, fClassName, fCodeFileName, fIncludePaths, fIncludeFiles); |
244 | if (result == 0) fDeleteCodeFile = true; | |
52f67e50 | 245 | } |
246 | else | |
247 | { | |
81d62bb4 | 248 | result = LoadTriggerClass(codeFileName, fIncludePaths); |
249 | if (result == 0) fCodeFileName = codeFileName; | |
52f67e50 | 250 | } |
e2bb8ddd | 251 | if (result != 0) return result; |
252 | ||
81d62bb4 | 253 | fTrigger = AliHLTGlobalTrigger::CreateNew(fClassName.Data()); |
e2bb8ddd | 254 | if (fTrigger == NULL) |
255 | { | |
81d62bb4 | 256 | HLTError("Could not create a new instance of '%s'.", fClassName.Data()); |
e2bb8ddd | 257 | return -EIO; |
258 | } | |
259 | ||
52f67e50 | 260 | fTrigger->FillFromMenu(*menu); |
474952b2 | 261 | |
262 | // setup the CTP accounting in AliHLTComponent | |
566a01d0 | 263 | SetupCTPData(); |
474952b2 | 264 | |
acc7214e | 265 | // Set the default values from the trigger menu. |
266 | SetDescription(menu->DefaultDescription()); | |
267 | SetTriggerDomain(menu->DefaultTriggerDomain()); | |
268 | ||
e2bb8ddd | 269 | return 0; |
270 | } | |
271 | ||
1b9a175e | 272 | |
e2bb8ddd | 273 | Int_t AliHLTGlobalTriggerComponent::DoDeinit() |
274 | { | |
275 | // Cleans up the global trigger component. | |
276 | ||
277 | if (fTrigger != NULL) | |
278 | { | |
279 | delete fTrigger; | |
280 | fTrigger = NULL; | |
281 | } | |
282 | ||
566a01d0 | 283 | if (fCTPDecisions) { |
284 | fCTPDecisions->Delete(); | |
285 | delete fCTPDecisions; | |
286 | } | |
287 | fCTPDecisions=NULL; | |
81d62bb4 | 288 | |
289 | Int_t result = UnloadTriggerClass(fCodeFileName); | |
290 | if (result != 0) return result; | |
291 | ||
292 | if (fDeleteCodeFile and !fCodeFileName.IsNull() && gSystem->AccessPathName(fCodeFileName)==0 && !fDebugMode) { | |
293 | fCodeFileName.ReplaceAll(".cxx", "*"); | |
294 | TString command="rm "; command+=fCodeFileName; | |
295 | gSystem->Exec(command); | |
296 | } | |
297 | fCodeFileName=""; | |
298 | fDeleteCodeFile=false; | |
566a01d0 | 299 | |
1b9a175e | 300 | return 0; |
301 | } | |
4f1d6b68 | 302 | |
303 | ||
304 | AliHLTComponent* AliHLTGlobalTriggerComponent::Spawn() | |
305 | { | |
306 | // Creates a new object instance. | |
307 | ||
308 | return new AliHLTGlobalTriggerComponent; | |
309 | } | |
310 | ||
e2bb8ddd | 311 | |
312 | int AliHLTGlobalTriggerComponent::DoTrigger() | |
313 | { | |
314 | // This method will apply the global trigger decision. | |
315 | ||
316 | if (fTrigger == NULL) | |
317 | { | |
318 | HLTFatal("Global trigger implementation object is NULL!"); | |
319 | return -EIO; | |
320 | } | |
2974f8dc | 321 | |
322 | AliHLTUInt32_t eventType=0; | |
323 | if (!IsDataEvent(&eventType)) { | |
324 | if (eventType==gkAliEventTypeEndOfRun) PrintStatistics(fTrigger, kHLTLogImportant, 0); | |
325 | return 0; | |
326 | } | |
327 | ||
81d62bb4 | 328 | // Copy the trigger counters in case we need to set them back to their original |
329 | // value because the PushBack method fails with ENOSPC. | |
330 | TArrayL64 originalCounters = fTrigger->GetCounters(); | |
331 | ||
e2bb8ddd | 332 | fTrigger->NewEvent(); |
333 | ||
334 | // Fill in the input data. | |
335 | const TObject* obj = GetFirstInputObject(); | |
336 | while (obj != NULL) | |
52f67e50 | 337 | { |
338 | fTrigger->Add(obj, GetDataType(), GetSpecification()); | |
339 | obj = GetNextInputObject(); | |
340 | } | |
341 | ||
a489a8dd | 342 | // add trigger decisions for every CTP class |
343 | const AliHLTCTPData* pCTPData=CTPData(); | |
344 | if (pCTPData) { | |
566a01d0 | 345 | AddCTPDecisions(fTrigger, pCTPData, GetTriggerData()); |
a489a8dd | 346 | } |
347 | ||
52f67e50 | 348 | // Calculate the global trigger result and trigger domain, then create and push |
349 | // back the new global trigger decision object. | |
350 | TString description; | |
acc7214e | 351 | AliHLTTriggerDomain triggerDomain; |
352 | bool triggerResult = fTrigger->CalculateTriggerDecision(triggerDomain, description); | |
acc7214e | 353 | AliHLTGlobalTriggerDecision decision( |
354 | triggerResult, | |
355 | // The following will cause the decision to be generated with default values | |
356 | // (set in fTriggerDomain and fDescription) if the trigger result is false. | |
357 | (triggerResult == true) ? triggerDomain : GetTriggerDomain(), | |
358 | (triggerResult == true) ? description.Data() : GetDescription() | |
359 | ); | |
48d98db9 | 360 | |
361 | // mask the readout list according to the CTP trigger | |
362 | // if the classes have been initialized (mask non-zero) | |
48d98db9 | 363 | if (pCTPData && pCTPData->Mask()) { |
364 | AliHLTEventDDL eventDDL=pCTPData->ReadoutList(*GetTriggerData()); | |
365 | AliHLTReadoutList ctpreadout(eventDDL); | |
45398383 | 366 | ctpreadout.Enable(AliHLTReadoutList::kHLT); |
367 | AliHLTReadoutList maskedList=decision.ReadoutList(); | |
368 | maskedList.AndEq(ctpreadout); | |
369 | decision.ReadoutList(maskedList); | |
48d98db9 | 370 | } |
371 | ||
81d62bb4 | 372 | decision.SetCounters(fTrigger->GetCounters(), GetEventCount()+1); |
d4ed5215 | 373 | static UInt_t lastTime=0; |
374 | TDatime time; | |
375 | if (time.Get()-lastTime>5) { | |
376 | lastTime=time.Get(); | |
2974f8dc | 377 | PrintStatistics(fTrigger); |
d4ed5215 | 378 | } |
52f67e50 | 379 | |
380 | // Add the input objects used to the global decision. | |
381 | obj = GetFirstInputObject(); | |
382 | while (obj != NULL) | |
e2bb8ddd | 383 | { |
384 | if (obj->IsA() == AliHLTTriggerDecision::Class()) | |
385 | { | |
52f67e50 | 386 | decision.AddTriggerInput( *static_cast<const AliHLTTriggerDecision*>(obj) ); |
e2bb8ddd | 387 | } |
388 | else | |
389 | { | |
52f67e50 | 390 | decision.AddInputObject(obj); |
e2bb8ddd | 391 | } |
392 | obj = GetNextInputObject(); | |
393 | } | |
2974f8dc | 394 | |
566a01d0 | 395 | if (!fSkipCTPCounters && CTPData()) decision.AddInputObject(CTPData()); |
474952b2 | 396 | |
2974f8dc | 397 | CreateEventDoneReadoutFilter(decision.TriggerDomain(), 3); |
398 | CreateEventDoneReadoutFilter(decision.TriggerDomain(), 4); | |
81d62bb4 | 399 | if (TriggerEvent(&decision) == -ENOSPC) |
400 | { | |
401 | // Increase the estimated buffer space required if the PushBack methods in TriggerEvent | |
402 | // returned the "no buffer space" error code. Also remember to set the trigger counters | |
403 | // back to what they were, otherwise triggers will be double counted when we try to reprocess | |
404 | // this event with larger buffers. | |
405 | fBufferSizeConst += 1024*1024; | |
406 | fBufferSizeMultiplier *= 2.; | |
407 | fTrigger->SetCounters(originalCounters); | |
408 | return -ENOSPC; | |
409 | } | |
410 | return 0; | |
411 | } | |
412 | ||
413 | ||
414 | int AliHLTGlobalTriggerComponent::Reconfigure(const char* cdbEntry, const char* chainId) | |
415 | { | |
416 | // Reconfigure the component by loading the trigger menu and recreating the | |
417 | // trigger logic class. | |
418 | ||
419 | const char* path = fgkTriggerMenuCDBPath; | |
420 | const char* id = "(unknown)"; | |
421 | if (cdbEntry != NULL) path = cdbEntry; | |
422 | if (chainId != NULL and chainId[0] != '\0') id = chainId; | |
423 | HLTInfo("Reconfiguring from '%s' for chain component '%s'.", path, id); | |
424 | ||
425 | const AliHLTTriggerMenu* menu = NULL; | |
426 | int result = LoadTriggerMenu(path, menu); | |
427 | if (result != 0) return result; | |
428 | ||
429 | TString className; | |
430 | TString codeFileName; | |
431 | result = GenerateTrigger(menu, className, codeFileName, fIncludePaths, fIncludeFiles); | |
432 | if (result != 0) return result; | |
433 | ||
434 | AliHLTGlobalTrigger* trigger = AliHLTGlobalTrigger::CreateNew(className.Data()); | |
435 | if (trigger == NULL) | |
436 | { | |
437 | HLTError("Could not create a new instance of '%s'.", className.Data()); | |
438 | // Make sure to cleanup after the new code file. | |
439 | UnloadTriggerClass(codeFileName); | |
440 | if (not codeFileName.IsNull() and gSystem->AccessPathName(codeFileName)==0 and not fDebugMode) | |
441 | { | |
442 | codeFileName.ReplaceAll(".cxx", "*"); | |
443 | TString command="rm "; command+=codeFileName; | |
444 | gSystem->Exec(command); | |
445 | } | |
446 | return -EIO; | |
447 | } | |
448 | ||
449 | if (fTrigger != NULL) | |
450 | { | |
451 | delete fTrigger; | |
452 | fTrigger = NULL; | |
453 | } | |
454 | ||
455 | fTrigger = trigger; | |
456 | fTrigger->FillFromMenu(*menu); | |
457 | ||
458 | // Set the default values from the trigger menu. | |
459 | SetDescription(menu->DefaultDescription()); | |
460 | SetTriggerDomain(menu->DefaultTriggerDomain()); | |
461 | ||
462 | // Cleanup the old class code. | |
463 | UnloadTriggerClass(fCodeFileName); | |
464 | if (fDeleteCodeFile and not fCodeFileName.IsNull() and gSystem->AccessPathName(fCodeFileName)==0 and not fDebugMode) | |
465 | { | |
466 | fCodeFileName.ReplaceAll(".cxx", "*"); | |
467 | TString command="rm "; command+=fCodeFileName; | |
468 | gSystem->Exec(command); | |
469 | } | |
470 | fCodeFileName = codeFileName; | |
471 | fDeleteCodeFile = true; // Since we generated a new class. | |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
476 | ||
477 | int AliHLTGlobalTriggerComponent::LoadTriggerMenu(const char* cdbPath, const AliHLTTriggerMenu*& menu) | |
478 | { | |
479 | // Loads the trigger menu object from the CDB path. | |
480 | ||
481 | HLTDebug("Trying to load trigger menu from '%s'.", cdbPath); | |
482 | if (AliCDBManager::Instance() == NULL) | |
483 | { | |
484 | HLTError("CDB manager object not found."); | |
485 | return -EIO; | |
486 | } | |
487 | AliCDBStorage* store = AliCDBManager::Instance()->GetDefaultStorage(); | |
488 | if (store == NULL) | |
489 | { | |
490 | HLTError("Could not get the the default storage for the CDB."); | |
491 | return -EIO; | |
492 | } | |
493 | Int_t version = store->GetLatestVersion(cdbPath, GetRunNo()); | |
494 | Int_t subVersion = store->GetLatestSubVersion(cdbPath, GetRunNo(), version); | |
495 | AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, GetRunNo(), version, subVersion); | |
496 | if (entry == NULL) | |
497 | { | |
498 | HLTError("Could not get the CDB entry for \"%s\".", cdbPath); | |
499 | return -EIO; | |
500 | } | |
501 | TObject* obj = entry->GetObject(); | |
502 | if (obj == NULL) | |
503 | { | |
504 | HLTError("Configuration object for \"%s\" is missing.", cdbPath); | |
505 | return -ENOENT; | |
506 | } | |
507 | if (obj->IsA() != AliHLTTriggerMenu::Class()) | |
508 | { | |
509 | HLTError("Wrong type for configuration object in \"%s\". Found a %s but we expect a AliHLTTriggerMenu.", | |
510 | cdbPath, obj->ClassName() | |
511 | ); | |
512 | return -EPROTO; | |
513 | } | |
514 | menu = static_cast<AliHLTTriggerMenu*>(obj); | |
e2bb8ddd | 515 | return 0; |
516 | } | |
517 | ||
518 | ||
52f67e50 | 519 | int AliHLTGlobalTriggerComponent::GenerateTrigger( |
81d62bb4 | 520 | const AliHLTTriggerMenu* menu, TString& name, TString& filename, |
52f67e50 | 521 | const TClonesArray& includePaths, const TClonesArray& includeFiles |
522 | ) | |
e2bb8ddd | 523 | { |
524 | // Generates the global trigger class that will implement the specified trigger menu. | |
52f67e50 | 525 | // See header for more details. |
e2bb8ddd | 526 | |
81d62bb4 | 527 | HLTDebug("Generating custom HLT trigger class named %s using trigger menu %p.", ((void*)menu), name.Data()); |
528 | ||
e2bb8ddd | 529 | // Create a new UUID and replace the '-' characters with '_' to make it a valid |
530 | // C++ symbol name. | |
531 | TUUID uuid; | |
532 | TString uuidstr = uuid.AsString(); | |
533 | for (Int_t i = 0; i < uuidstr.Length(); i++) | |
534 | { | |
535 | if (uuidstr[i] == '-') uuidstr[i] = '_'; | |
536 | } | |
537 | ||
538 | // Create the name of the new class. | |
539 | name = "AliHLTGlobalTriggerImpl_"; | |
540 | name += uuidstr; | |
81d62bb4 | 541 | filename = name + ".cxx"; |
e2bb8ddd | 542 | |
52f67e50 | 543 | // Open a text file to write the code and generate the new class. |
81d62bb4 | 544 | fstream code(filename.Data(), ios_base::out | ios_base::trunc); |
e2bb8ddd | 545 | if (not code.good()) |
546 | { | |
81d62bb4 | 547 | HLTError("Could not open file '%s' for writing.", filename.Data()); |
e2bb8ddd | 548 | return -EIO; |
549 | } | |
550 | ||
52f67e50 | 551 | TClonesArray symbols(AliHLTTriggerMenuSymbol::Class()); |
552 | int result = BuildSymbolList(menu, symbols); | |
553 | if (result != 0) return result; | |
554 | ||
81d62bb4 | 555 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; |
52f67e50 | 556 | code << "#include <cstring>" << endl; |
557 | code << "#include \"TString.h\"" << endl; | |
558 | code << "#include \"TClonesArray.h\"" << endl; | |
81d62bb4 | 559 | code << "#include \"AliHLTLogging.h\"" << endl; |
e2bb8ddd | 560 | code << "#include \"AliHLTGlobalTrigger.h\"" << endl; |
561 | code << "#include \"AliHLTGlobalTriggerDecision.h\"" << endl; | |
52f67e50 | 562 | code << "#include \"AliHLTDomainEntry.h\"" << endl; |
563 | code << "#include \"AliHLTTriggerDomain.h\"" << endl; | |
564 | code << "#include \"AliHLTReadoutList.h\"" << endl; | |
565 | code << "#include \"AliHLTTriggerMenu.h\"" << endl; | |
566 | code << "#include \"AliHLTTriggerMenuItem.h\"" << endl; | |
567 | code << "#include \"AliHLTTriggerMenuSymbol.h\"" << endl; | |
568 | ||
569 | // Add any include files that were specified on the command line. | |
570 | for (Int_t i = 0; i < includeFiles.GetEntriesFast(); i++) | |
571 | { | |
7da191b6 | 572 | TString file = static_cast<TObjString*>(includeFiles.UncheckedAt(i))->String(); |
52f67e50 | 573 | code << "#include \"" << file.Data() << "\"" << endl; |
574 | } | |
575 | ||
81d62bb4 | 576 | if (fDebugMode) |
577 | { | |
578 | code << "#else" << endl; | |
579 | code << "const char* gFunctionName = \"???\";" << endl; | |
580 | code << "#define HLTDebug(msg) if (CheckFilter(kHLTLogDebug) && CheckGroup(Class_Name())) SendMessage(kHLTLogDebug, Class_Name(), ::gFunctionName, __FILE__, __LINE__, msg)" << endl; | |
581 | } | |
582 | code << "#endif" << endl; | |
583 | ||
584 | code << "class " << name << " :" << endl; | |
585 | // Add appropriate #ifdef sections since we need to prevent inheritance from | |
586 | // AliHLTGlobalTrigger. CINT does not seem to support multiple inheritance nor | |
587 | // multiple levels of inheritance. Neither of the following schemes worked: | |
588 | // | |
589 | // 1) class AliHLTGlobalTrigger : public AliHLTLogging {}; | |
590 | // class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger {}; | |
591 | // | |
592 | // 2) class AliHLTGlobalTrigger {}; | |
593 | // class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger, public AliHLTLogging {}; | |
594 | // | |
595 | // Thus, we are forced to just inherit from AliHLTLogging when running in the CINT | |
596 | // interpreter. But we anyway have to call the global trigger implementation class | |
597 | // through the AliHLTGlobalTriggerWrapper so this is not such a problem. | |
598 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; | |
599 | code << " public AliHLTGlobalTrigger," << endl; | |
600 | code << "#endif" << endl; | |
601 | code << " public AliHLTLogging" << endl; | |
e2bb8ddd | 602 | code << "{" << endl; |
603 | code << "public:" << endl; | |
52f67e50 | 604 | |
81d62bb4 | 605 | // Generate constructor method. |
606 | code << " " << name << "() :" << endl; | |
607 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; | |
608 | code << " AliHLTGlobalTrigger()," << endl; | |
609 | code << "#endif" << endl; | |
610 | code << " AliHLTLogging()"; | |
52f67e50 | 611 | // Write the symbols in the trigger menu in the initialisation list. |
612 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
613 | { | |
614 | code << "," << endl; | |
615 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
616 | code << " " << symbol->Name() << "()," << endl; | |
617 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) | |
618 | { | |
619 | code << " " << symbol->Name() << "TriggerDomain()," << endl; | |
620 | } | |
621 | code << " " << symbol->Name() << "DomainEntry(kAliHLTAnyDataType)"; | |
622 | } | |
623 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) | |
624 | { | |
625 | code << "," << endl << " fMenuItemDescription" << i << "()"; | |
626 | } | |
627 | code << endl << " {" << endl; | |
628 | if (fDebugMode) | |
629 | { | |
81d62bb4 | 630 | code << "#ifdef __CINT__" << endl; |
631 | code << " gFunctionName = \"" << name.Data() <<"\";" << endl; | |
632 | code << "#endif" << endl; | |
52f67e50 | 633 | code << " SetLocalLoggingLevel(kHLTLogAll);" << endl; |
81d62bb4 | 634 | code << " HLTDebug(Form(\"Creating new instance at %p.\", this));" << endl; |
52f67e50 | 635 | } |
e2bb8ddd | 636 | code << " }" << endl; |
52f67e50 | 637 | |
e2bb8ddd | 638 | code << " virtual ~" << name << "() {" << endl; |
52f67e50 | 639 | if (fDebugMode) |
640 | { | |
81d62bb4 | 641 | code << "#ifdef __CINT__" << endl; |
642 | code << " gFunctionName = \"~" << name.Data() << "\";" << endl; | |
643 | code << "#endif" << endl; | |
644 | code << " HLTDebug(Form(\"Deleting instance at %p.\", this));" << endl; | |
52f67e50 | 645 | } |
e2bb8ddd | 646 | code << " }" << endl; |
52f67e50 | 647 | |
648 | // Generate the FillFromMenu method. | |
649 | code << " virtual void FillFromMenu(const AliHLTTriggerMenu& menu) {" << endl; | |
650 | if (fDebugMode) | |
651 | { | |
81d62bb4 | 652 | code << "#ifdef __CINT__" << endl; |
653 | code << " gFunctionName = \"FillFromMenu\";" << endl; | |
654 | code << "#endif" << endl; | |
655 | code << " HLTDebug(Form(\"Filling description entries from trigger menu for global trigger %p.\", this));" << endl; | |
52f67e50 | 656 | } |
81d62bb4 | 657 | code << " fCounter.Set(menu.NumberOfItems());" << endl; |
52f67e50 | 658 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) |
659 | { | |
660 | code << " fMenuItemDescription" << i << " = (menu.Item(" << i | |
661 | << ") != NULL) ? menu.Item(" << i << ")->Description() : \"\";" << endl; | |
662 | } | |
663 | if (fDebugMode) | |
664 | { | |
81d62bb4 | 665 | code << " HLTDebug(Form(\"Finished filling description entries from trigger menu.\"));" << endl; |
666 | code << " HLTDebug(Form(\"Filling domain entries from trigger menu symbols for global trigger %p.\", this));" << endl; | |
52f67e50 | 667 | } |
668 | code << " for (Int_t i = 0; i < menu.SymbolArray().GetEntriesFast(); i++) {" << endl; | |
669 | code << " const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const" | |
670 | " AliHLTTriggerMenuSymbol*>(menu.SymbolArray().UncheckedAt(i));" << endl; | |
671 | code << " if (symbol == NULL) continue;" << endl; | |
672 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
673 | { | |
674 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
81d62bb4 | 675 | code << " if (strcmp(symbol->Name(), \"" << symbol->Name() << "\") == 0) {" << endl; |
52f67e50 | 676 | if (fDebugMode) |
677 | { | |
81d62bb4 | 678 | code << " HLTDebug(Form(\"Assinging domain entry value corresponding with symbol '%s' to '%s'.\"," |
679 | " symbol->Name(), symbol->BlockType().AsString().Data()));" << endl; | |
52f67e50 | 680 | } |
681 | code << " " << symbol->Name() << "DomainEntry = symbol->BlockType();" << endl; | |
682 | code << " continue;" << endl; | |
683 | code << " }" << endl; | |
684 | } | |
685 | code << " }" << endl; | |
686 | if (fDebugMode) | |
687 | { | |
81d62bb4 | 688 | code << " HLTDebug(Form(\"Finished filling domain entries from trigger menu symbols.\"));" << endl; |
52f67e50 | 689 | } |
e2bb8ddd | 690 | code << " }" << endl; |
52f67e50 | 691 | |
692 | // Generate the NewEvent method. | |
693 | code << " virtual void NewEvent() {" << endl; | |
694 | if (fDebugMode) | |
695 | { | |
81d62bb4 | 696 | code << "#ifdef __CINT__" << endl; |
697 | code << " gFunctionName = \"NewEvent\";" << endl; | |
698 | code << "#endif" << endl; | |
699 | code << " HLTDebug(Form(\"New event for global trigger object %p, initialising variables to default values.\", this));" << endl; | |
52f67e50 | 700 | } |
701 | // Write code to initialise the symbols in the trigger menu to their default values. | |
702 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
703 | { | |
704 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
81d62bb4 | 705 | // CINT has problems with the implicit equals operator for complex types, so if |
706 | // the type has a equals operater we need to write the operator call explicitly. | |
707 | TClass* clas = TClass::GetClass(symbol->Type()); | |
708 | if (clas != NULL and clas->GetMethodAny("operator=") != NULL) | |
709 | { | |
710 | code << " " << symbol->Name() << ".operator = (" << symbol->DefaultValue() << ");" << endl; | |
711 | } | |
712 | else | |
713 | { | |
714 | code << " " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl; | |
715 | } | |
52f67e50 | 716 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) |
717 | { | |
718 | code << " " << symbol->Name() << "TriggerDomain.Clear();" << endl; | |
719 | } | |
720 | } | |
721 | if (fDebugMode) | |
722 | { | |
81d62bb4 | 723 | code << " HLTDebug(Form(\"Finished initialising variables.\"));" << endl; |
52f67e50 | 724 | } |
e2bb8ddd | 725 | code << " }" << endl; |
52f67e50 | 726 | |
727 | // Generate the Add method. | |
728 | code << " virtual void Add(const TObject* _object_, const AliHLTComponentDataType& _type_, AliHLTUInt32_t _spec_) {" << endl; | |
81d62bb4 | 729 | if (fDebugMode) |
730 | { | |
731 | code << "#ifdef __CINT__" << endl; | |
732 | code << " gFunctionName = \"Add\";" << endl; | |
733 | code << "#endif" << endl; | |
734 | } | |
52f67e50 | 735 | code << " AliHLTDomainEntry _type_spec_(_type_, _spec_);" << endl; |
736 | if (fDebugMode) | |
737 | { | |
81d62bb4 | 738 | code << " HLTDebug(Form(\"Adding TObject %p, with class name '%s' from data block" |
52f67e50 | 739 | " '%s', to global trigger object %p\", _object_, _object_->ClassName()," |
81d62bb4 | 740 | " _type_spec_.AsString().Data(), this));" << endl; |
52f67e50 | 741 | code << " _object_->Print();" << endl; |
742 | code << " bool _object_assigned_ = false;" << endl; | |
743 | } | |
744 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
745 | { | |
746 | // Write code to check if the block type, specification and class name is correct. | |
747 | // Then write code to assign the variable from the input object. | |
748 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
749 | TString expr = symbol->AssignExpression(); | |
750 | if (expr == "") continue; // Skip entries that have no assignment expression. | |
751 | bool isTrigDecision = strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0; | |
752 | if (fDebugMode) | |
753 | { | |
754 | if (isTrigDecision) | |
755 | { | |
81d62bb4 | 756 | code << " HLTDebug(Form(\"Trying to match input object to class '" |
f48dcb4e | 757 | << symbol->ObjectClass() << "', trigger name '" << symbol->RealName() |
52f67e50 | 758 | << "' and block type '%s'\", " << symbol->Name() |
81d62bb4 | 759 | << "DomainEntry.AsString().Data()));" << endl; |
52f67e50 | 760 | } |
761 | else | |
762 | { | |
81d62bb4 | 763 | code << " HLTDebug(Form(\"Trying to match input object to class '" |
52f67e50 | 764 | << symbol->ObjectClass() << "' and block type '%s'\", " |
81d62bb4 | 765 | << symbol->Name() << "DomainEntry.AsString().Data()));" << endl; |
52f67e50 | 766 | } |
767 | } | |
768 | code << " const " << symbol->ObjectClass() << "* " << symbol->Name() | |
769 | << "_object_ = dynamic_cast<const " << symbol->ObjectClass() | |
770 | << "*>(_object_);" << endl; | |
81d62bb4 | 771 | code << " if (" << symbol->Name() << "_object_ != NULL && "; |
52f67e50 | 772 | if (isTrigDecision) |
773 | { | |
774 | code << "strcmp(" << symbol->Name() << "_object_->Name(), \"" | |
81d62bb4 | 775 | << symbol->Name() << "\") == 0 && "; |
52f67e50 | 776 | } |
777 | code << symbol->Name() << "DomainEntry == _type_spec_) {" << endl; | |
778 | TString fullname = symbol->Name(); | |
779 | fullname += "_object_"; | |
780 | expr.ReplaceAll("this", fullname); | |
781 | code << " this->" << symbol->Name() << " = " << expr.Data() << ";" << endl; | |
782 | if (isTrigDecision) | |
783 | { | |
784 | code << " this->" << symbol->Name() << "TriggerDomain = " | |
785 | << fullname.Data() << "->TriggerDomain();" << endl; | |
786 | } | |
787 | if (fDebugMode) | |
788 | { | |
81d62bb4 | 789 | code << " HLTDebug(Form(\"Added TObject %p with class name '%s' to variable " |
790 | << symbol->Name() << "\", _object_, _object_->ClassName()));" << endl; | |
52f67e50 | 791 | code << " _object_assigned_ = true;" << endl; |
792 | } | |
793 | code << " }" << endl; | |
794 | } | |
795 | if (fDebugMode) | |
796 | { | |
81d62bb4 | 797 | code << " if (! _object_assigned_) {" << endl; |
798 | code << " HLTDebug(Form(\"Did not assign TObject %p" | |
799 | " with class name '%s' to any variable.\", _object_, _object_->ClassName()));" | |
52f67e50 | 800 | << endl; |
81d62bb4 | 801 | code << " }" << endl; |
52f67e50 | 802 | } |
e2bb8ddd | 803 | code << " }" << endl; |
52f67e50 | 804 | |
805 | // Generate the CalculateTriggerDecision method. | |
81d62bb4 | 806 | // This requires code to be generated that checks which items in the trigger menu |
807 | // have their conditions asserted and then the trigger domain is generated from | |
808 | // those fired items. | |
809 | // The processing will start from the highest priority trigger group and stop | |
810 | // after at least one trigger from the current priority group being processed | |
811 | // is positive. For each priority group all the trigger menu items are checked. | |
812 | // Their combined trigger condition expression must be true for the trigger priority | |
813 | // group to be triggered positive. The full condition expression is formed by | |
814 | // concatenating the individual condition expressions. If no trailing operators are | |
815 | // used in the individual expressions then the default condition operator is placed | |
816 | // between two concatenated condition expressions. | |
817 | // If a trigger priority group has at least one trigger fired then the trigger domain | |
818 | // is calculated such that it will give the same result as the concatenated trigger | |
819 | // domain merging expressions for all the individual trigger menu items with | |
820 | // positive results. Again, if no trailing operators are used in the individual | |
821 | // merging expressions then the default domain operator is placed between two | |
822 | // expression fragments. | |
52f67e50 | 823 | code << " virtual bool CalculateTriggerDecision(AliHLTTriggerDomain& _domain_, TString& _description_) {" << endl; |
824 | if (fDebugMode) | |
825 | { | |
81d62bb4 | 826 | code << "#ifdef __CINT__" << endl; |
827 | code << " gFunctionName = \"CalculateTriggerDecision\";" << endl; | |
828 | code << "#endif" << endl; | |
829 | code << " HLTDebug(Form(\"Calculating global HLT trigger result with trigger object at %p.\", this));" << endl; | |
52f67e50 | 830 | } |
81d62bb4 | 831 | |
832 | // Build a list of priorities used in the trigger menu. | |
833 | std::vector<UInt_t> priorities; | |
52f67e50 | 834 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) |
835 | { | |
836 | const AliHLTTriggerMenuItem* item = menu->Item(i); | |
81d62bb4 | 837 | bool priorityNotInList = std::find(priorities.begin(), priorities.end(), item->Priority()) == priorities.end(); |
838 | if (priorityNotInList) priorities.push_back(item->Priority()); | |
839 | } | |
840 | std::sort(priorities.begin(), priorities.end(), AliHLTDescendingNumbers); | |
841 | // From the priority list, build the priority groups in the correct order, | |
842 | // i.e. highest priority first. | |
843 | // The priority group is a list of vectors of integers. The integers are the | |
844 | // index numbers into the trigger menu item list for the items which form part | |
845 | // of the priority group. | |
846 | std::vector<std::vector<Int_t> > priorityGroup; | |
847 | priorityGroup.insert(priorityGroup.begin(), priorities.size(), std::vector<Int_t>()); | |
848 | for (size_t n = 0; n < priorities.size(); n++) | |
849 | { | |
850 | UInt_t priority = priorities[n]; | |
851 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) | |
52f67e50 | 852 | { |
81d62bb4 | 853 | const AliHLTTriggerMenuItem* item = menu->Item(i); |
854 | if (item->Priority() == priority) priorityGroup[n].push_back(i); | |
52f67e50 | 855 | } |
81d62bb4 | 856 | } |
857 | ||
858 | for (size_t n = 0; n < priorityGroup.size(); n++) | |
859 | { | |
52f67e50 | 860 | if (fDebugMode) |
861 | { | |
81d62bb4 | 862 | code << " HLTDebug(Form(\"Processing trigger priority group " << priorities[n] << "\"));" << endl; |
52f67e50 | 863 | } |
81d62bb4 | 864 | code << " "; |
865 | if (n == 0) code << "UInt_t "; | |
866 | code << "_previous_match_ = 0xFFFFFFFF;" << endl; | |
867 | code << " "; | |
868 | if (n == 0) code << "bool "; | |
869 | code << "_trigger_matched_ = false;" << endl; | |
870 | code << " "; | |
871 | if (n == 0) code << "bool "; | |
872 | code << "_group_result_ = false;" << endl; | |
873 | std::vector<TString> conditionOperator; | |
874 | conditionOperator.insert(conditionOperator.begin(), priorityGroup[n].size(), TString("")); | |
875 | std::vector<TString> domainOperator; | |
876 | domainOperator.insert(domainOperator.begin(), priorityGroup[n].size(), TString("")); | |
877 | for (size_t m = 0; m < priorityGroup[n].size(); m++) | |
52f67e50 | 878 | { |
81d62bb4 | 879 | UInt_t i = priorityGroup[n][m]; |
880 | const AliHLTTriggerMenuItem* item = menu->Item(i); | |
881 | TString triggerCondition = item->TriggerCondition(); | |
882 | TString mergeExpr = item->MergeExpression(); | |
883 | // Replace the symbols found in the trigger condition and merging expressions | |
884 | // with appropriate compilable versions. | |
885 | for (Int_t j = 0; j < symbols.GetEntriesFast(); j++) | |
886 | { | |
887 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(j) ); | |
888 | bool symbolNamesDifferent = strcmp(symbol->RealName(), symbol->Name()) != 0; | |
889 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) | |
890 | { | |
891 | TString newname = symbol->Name(); | |
892 | newname += "TriggerDomain"; | |
893 | mergeExpr.ReplaceAll(symbol->RealName(), newname); | |
894 | } | |
895 | else | |
896 | { | |
897 | if (symbolNamesDifferent) mergeExpr.ReplaceAll(symbol->RealName(), symbol->Name()); | |
898 | } | |
899 | if (symbolNamesDifferent) triggerCondition.ReplaceAll(symbol->RealName(), symbol->Name()); | |
900 | } | |
901 | // We allow the trigger conditions and merging expressions to have trailing operators. | |
902 | // Thus, we need to extract the operators and cleanup the expressions so that they will | |
903 | // compile. This means that we silently ignore the trailing operator if not needed. | |
904 | if (ExtractedOperator(triggerCondition, conditionOperator[m])) | |
905 | { | |
906 | // If the trailing operator is the same as the default operator then reset | |
907 | // the value in the operator list so that the default is used in the generated | |
908 | // code. This creates more compact code. | |
909 | if (conditionOperator[m] == menu->DefaultConditionOperator()) conditionOperator[m] = ""; | |
910 | } | |
911 | if (ExtractedOperator(mergeExpr, domainOperator[m])) | |
912 | { | |
913 | if (domainOperator[m] == menu->DefaultDomainOperator()) domainOperator[m] = ""; | |
914 | } | |
915 | if (fDebugMode) | |
916 | { | |
917 | code << " HLTDebug(Form(\"Trying trigger condition " << i | |
918 | << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));" | |
919 | << endl; | |
920 | } | |
921 | code << " "; | |
922 | if (n == 0 and m == 0) code << "bool "; | |
923 | code << "_item_result_ = false;" << endl; | |
924 | code << " if (" << triggerCondition << ") {" << endl; | |
925 | code << " ++fCounter[" << i << "];" << endl; | |
926 | const char* indentation = ""; | |
927 | if (item->PreScalar() != 0) | |
928 | { | |
929 | indentation = " "; | |
930 | code << " if ((fCounter[" << i << "] % " << item->PreScalar() << ") == 1) {" << endl; | |
931 | } | |
932 | code << indentation << " _item_result_ = true;" << endl; | |
933 | if (fDebugMode) | |
934 | { | |
935 | code << indentation << " HLTDebug(Form(\"Matched trigger condition " << i | |
936 | << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));" << endl; | |
937 | } | |
938 | if (item->PreScalar() != 0) | |
939 | { | |
940 | code << " }" << endl; | |
941 | } | |
942 | code << " }" << endl; | |
943 | if (m == 0) | |
944 | { | |
945 | // Since this is the first item of the trigger group, | |
946 | // the generated trigger logic can be simplified a little. | |
947 | code << " _group_result_ = _item_result_;" << endl; | |
948 | code << " if (_item_result_) {" << endl; | |
949 | code << " _domain_ = " << mergeExpr.Data() << ";" << endl; | |
950 | code << " _description_ = fMenuItemDescription" << i << ";" << endl; | |
951 | code << " _previous_match_ = " << i << ";" << endl; | |
952 | code << " _trigger_matched_ = true;" << endl; | |
953 | code << " }" << endl; | |
954 | } | |
955 | else | |
956 | { | |
957 | bool switchWillBeEmpty = true; | |
958 | for (size_t k = 0; k < m; k++) | |
959 | { | |
960 | if (conditionOperator[k] == "") continue; | |
961 | switchWillBeEmpty = false; | |
962 | } | |
963 | if (switchWillBeEmpty) | |
964 | { | |
965 | code << " _group_result_ = _group_result_ " | |
966 | << menu->DefaultConditionOperator() << " _item_result_;" << endl; | |
967 | } | |
968 | else | |
969 | { | |
970 | code << " switch(_previous_match_) {" << endl; | |
971 | for (size_t k = 0; k < m; k++) | |
972 | { | |
973 | if (conditionOperator[k] == "") continue; | |
974 | code << " case " << k << ": _group_result_ = _group_result_ " | |
975 | << conditionOperator[k] << " _item_result_; break;" << endl; | |
976 | } | |
977 | code << " default: _group_result_ = _group_result_ " | |
978 | << menu->DefaultConditionOperator() << " _item_result_;" << endl; | |
979 | code << " }" << endl; | |
980 | } | |
981 | code << " if (_item_result_) {" << endl; | |
982 | code << " if (_trigger_matched_) {" << endl; | |
983 | switchWillBeEmpty = true; | |
984 | for (size_t k = 0; k < m; k++) | |
985 | { | |
986 | if (domainOperator[k] == "") continue; | |
987 | switchWillBeEmpty = false; | |
988 | } | |
989 | if (switchWillBeEmpty) | |
990 | { | |
991 | code << " _domain_ = _domain_ " << menu->DefaultDomainOperator() << " " | |
992 | << mergeExpr.Data() << ";" << endl; | |
993 | } | |
994 | else | |
995 | { | |
996 | code << " switch(_previous_match_) {" << endl; | |
997 | for (size_t k = 0; k < m; k++) | |
998 | { | |
999 | if (domainOperator[k] == "") continue; | |
1000 | code << " case " << k << ": _domain_ = _domain_ " | |
1001 | << domainOperator[k] << " " << mergeExpr.Data() << "; break;" << endl; | |
1002 | } | |
1003 | code << " default: _domain_ = _domain_ " | |
1004 | << menu->DefaultDomainOperator() << " " << mergeExpr.Data() << ";" << endl; | |
1005 | code << " }" << endl; | |
1006 | } | |
1007 | code << " _description_ += \",\";" << endl; | |
1008 | code << " _description_ += fMenuItemDescription" << i << ";" << endl; | |
1009 | code << " } else {" << endl; | |
1010 | code << " _domain_ = " << mergeExpr.Data() << ";" << endl; | |
1011 | code << " _description_ = fMenuItemDescription" << i << ";" << endl; | |
1012 | code << " }" << endl; | |
1013 | code << " _previous_match_ = " << i << ";" << endl; | |
1014 | code << " _trigger_matched_ = true;" << endl; | |
1015 | code << " }" << endl; | |
1016 | } | |
52f67e50 | 1017 | } |
81d62bb4 | 1018 | code << " if (_group_result_) {" << endl; |
52f67e50 | 1019 | if (fDebugMode) |
1020 | { | |
81d62bb4 | 1021 | if (n < priorities.size() - 1) |
1022 | { | |
1023 | code << " HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n] | |
1024 | << ". Stopping processing here because all other trigger groups have lower priority.\"));" << endl; | |
1025 | } | |
1026 | else | |
1027 | { | |
1028 | code << " HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n] << ".\"));" << endl; | |
1029 | } | |
52f67e50 | 1030 | } |
81d62bb4 | 1031 | code << " return true;" << endl; |
52f67e50 | 1032 | code << " }" << endl; |
1033 | } | |
81d62bb4 | 1034 | code << " _domain_.Clear();" << endl; |
1035 | code << " _description_ = \"\";" << endl; | |
1036 | code << " return false;" << endl; | |
e2bb8ddd | 1037 | code << " }" << endl; |
52f67e50 | 1038 | |
81d62bb4 | 1039 | // Generate getter and setter methods for the counters. |
1040 | code << " const TArrayL64& GetCounters() const { return fCounter; }" << endl; | |
1041 | code << " void SetCounters(const TArrayL64& counters) { fCounter = counters; }" << endl; | |
52f67e50 | 1042 | |
e2bb8ddd | 1043 | code << "private:" << endl; |
52f67e50 | 1044 | // Add the symbols in the trigger menu to the list of private variables. |
1045 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
1046 | { | |
1047 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
1048 | code << " " << symbol->Type() << " " << symbol->Name() << ";" << endl; | |
1049 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) | |
1050 | { | |
1051 | code << " AliHLTTriggerDomain " << symbol->Name() << "TriggerDomain;" << endl; | |
1052 | } | |
1053 | code << " AliHLTDomainEntry " << symbol->Name() << "DomainEntry;" << endl; | |
1054 | } | |
1055 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) | |
1056 | { | |
1057 | code << " TString fMenuItemDescription" << i << ";" << endl; | |
1058 | } | |
81d62bb4 | 1059 | code << " TArrayL64 fCounter;" << endl; |
1060 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; | |
1061 | code << " ClassDef(" << name.Data() << ", 0)" << endl; | |
1062 | code << "#else" << endl; | |
1063 | code << " virtual const char* Class_Name() const { return \"" << name.Data() << "\"; }" << endl; | |
1064 | code << "#endif" << endl; | |
e2bb8ddd | 1065 | code << "};" << endl; |
81d62bb4 | 1066 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; |
1067 | code << "ClassImp(" << name.Data() << ")" << endl; | |
1068 | code << "#endif" << endl; | |
e2bb8ddd | 1069 | |
1070 | code.close(); | |
1071 | ||
52f67e50 | 1072 | // Now we need to compile and load the new class. |
81d62bb4 | 1073 | result = LoadTriggerClass(filename, includePaths); |
52f67e50 | 1074 | return result; |
1075 | } | |
1076 | ||
1077 | ||
1078 | int AliHLTGlobalTriggerComponent::LoadTriggerClass( | |
1079 | const char* filename, const TClonesArray& includePaths | |
1080 | ) | |
1081 | { | |
1082 | // Loads the code for a custom global trigger class implementation on the fly. | |
1083 | ||
81d62bb4 | 1084 | HLTDebug("Loading HLT trigger class from file '%s'.", filename); |
1085 | ||
52f67e50 | 1086 | TString compiler = gSystem->GetBuildCompilerVersion(); |
566a01d0 | 1087 | if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc"))) |
1088 | { | |
1089 | TString includePath; | |
1090 | #if defined(PKGINCLUDEDIR) | |
1091 | // this is especially for the HLT build system where the package is installed | |
1092 | // in a specific directory including proper treatment of include files | |
1093 | includePath.Form("-I%s", PKGINCLUDEDIR); | |
1094 | #else | |
1095 | // the default AliRoot behavior, all include files can be found in the | |
1096 | // $ALICE_ROOT subfolders | |
1097 | includePath = "-I${ALICE_ROOT}/include -I${ALICE_ROOT}/HLT/BASE -I${ALICE_ROOT}/HLT/trigger"; | |
1098 | #endif | |
52f67e50 | 1099 | // Add any include paths that were specified on the command line. |
1100 | for (Int_t i = 0; i < includePaths.GetEntriesFast(); i++) | |
1101 | { | |
7da191b6 | 1102 | TString path = static_cast<TObjString*>(includePaths.UncheckedAt(i))->String(); |
81d62bb4 | 1103 | includePath += " -I"; |
52f67e50 | 1104 | includePath += path; |
1105 | } | |
81d62bb4 | 1106 | HLTDebug("using include settings: %s", includePath.Data()); |
52f67e50 | 1107 | gSystem->SetIncludePath(includePath); |
1108 | gSystem->SetFlagsOpt("-O3 -DNDEBUG"); | |
1109 | gSystem->SetFlagsDebug("-g3 -DDEBUG -D__DEBUG"); | |
1110 | ||
1111 | int result = kTRUE; | |
1112 | if (fDebugMode) | |
1113 | { | |
1114 | result = gSystem->CompileMacro(filename, "g"); | |
1115 | } | |
1116 | else | |
1117 | { | |
1118 | result = gSystem->CompileMacro(filename, "O"); | |
1119 | } | |
1120 | if (result != kTRUE) | |
1121 | { | |
1122 | HLTFatal("Could not compile and load global trigger menu implementation."); | |
1123 | return -ENOENT; | |
1124 | } | |
1125 | } | |
1126 | else | |
1127 | { | |
1128 | // If we do not support the compiler then try interpret the class instead. | |
81d62bb4 | 1129 | TString cmd = ".L "; |
52f67e50 | 1130 | cmd += filename; |
1131 | Int_t errorcode = TInterpreter::kNoError; | |
1132 | gROOT->ProcessLine(cmd, &errorcode); | |
1133 | if (errorcode != TInterpreter::kNoError) | |
1134 | { | |
1135 | HLTFatal("Could not load interpreted global trigger menu implementation" | |
1136 | " (Interpreter error code = %d).", | |
1137 | errorcode | |
1138 | ); | |
1139 | return -ENOENT; | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | return 0; | |
1144 | } | |
1145 | ||
1146 | ||
81d62bb4 | 1147 | int AliHLTGlobalTriggerComponent::UnloadTriggerClass(const char* filename) |
1148 | { | |
1149 | // Unloads the code previously loaded by LoadTriggerClass. | |
1150 | ||
1151 | HLTDebug("Unloading HLT trigger class in file '%s'.", filename); | |
1152 | ||
1153 | TString compiler = gSystem->GetBuildCompilerVersion(); | |
1154 | if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc"))) | |
1155 | { | |
1156 | TString libname = filename; | |
1157 | Ssiz_t dotpos = libname.Last('.'); | |
1158 | if (0 <= dotpos and dotpos < libname.Length()) libname[dotpos] = '_'; | |
1159 | libname += "."; | |
1160 | libname += gSystem->GetSoExt(); | |
1161 | ||
1162 | char* path = NULL; | |
1163 | int result = 0; | |
1164 | if ((path = gSystem->DynamicPathName(libname)) != NULL) | |
1165 | { | |
1166 | result = gInterpreter->UnloadFile(path); | |
1167 | delete [] path; | |
1168 | } | |
1169 | if (result != TInterpreter::kNoError) return -ENOENT; | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | // If we do not support the compiler then try interpret the class instead. | |
1174 | TString cmd = ".U "; | |
1175 | cmd += filename; | |
1176 | Int_t errorcode = TInterpreter::kNoError; | |
1177 | gROOT->ProcessLine(cmd, &errorcode); | |
1178 | if (errorcode != TInterpreter::kNoError) | |
1179 | { | |
1180 | HLTFatal("Could not unload interpreted global trigger menu implementation" | |
1181 | " (Interpreter error code = %d).", | |
1182 | errorcode | |
1183 | ); | |
1184 | return -ENOENT; | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
1191 | ||
52f67e50 | 1192 | int AliHLTGlobalTriggerComponent::FindSymbol(const char* name, const TClonesArray& list) |
1193 | { | |
1194 | // Searches for the named symbol in the given list. | |
1195 | // See header for more details. | |
1196 | ||
1197 | for (int i = 0; i < list.GetEntriesFast(); i++) | |
1198 | { | |
1199 | const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const AliHLTTriggerMenuSymbol*>( list.UncheckedAt(i) ); | |
1200 | if (symbol == NULL) continue; | |
1201 | if (strcmp(symbol->Name(), name) == 0) return i; | |
1202 | } | |
1203 | return -1; | |
1204 | } | |
1205 | ||
1206 | ||
1207 | int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu, TClonesArray& list) | |
1208 | { | |
1209 | // Builds the list of symbols to use in the custom global trigger menu | |
1210 | // implementation class. | |
1211 | // See header for more details. | |
1212 | ||
1213 | for (UInt_t i = 0; i < menu->NumberOfSymbols(); i++) | |
1214 | { | |
1215 | const AliHLTTriggerMenuSymbol* symbol = menu->Symbol(i); | |
1216 | if (FindSymbol(symbol->Name(), list) != -1) | |
1217 | { | |
1218 | HLTError("Multiple symbols with the name '%s' defined in the trigger menu.", symbol->Name()); | |
1219 | return -EIO; | |
1220 | } | |
1221 | new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(*symbol); | |
1222 | } | |
1223 | ||
f48dcb4e | 1224 | TRegexp exp("[_a-zA-Z][-_a-zA-Z0-9]*"); |
3da1c6d7 | 1225 | TRegexp hexexp("x[a-fA-F0-9]+"); |
52f67e50 | 1226 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) |
1227 | { | |
1228 | const AliHLTTriggerMenuItem* item = menu->Item(i); | |
81d62bb4 | 1229 | TString str = item->TriggerCondition(); |
52f67e50 | 1230 | Ssiz_t start = 0; |
1231 | do | |
1232 | { | |
1233 | Ssiz_t length = 0; | |
1234 | Ssiz_t pos = exp.Index(str, &length, start); | |
1235 | if (pos == kNPOS) break; | |
52f67e50 | 1236 | start = pos+length; |
1237 | ||
3da1c6d7 | 1238 | // Check if there is a numerical character before the found |
1239 | // regular expression. If so, then the symbol is not a valid one | |
1240 | // and should be skipped. | |
1241 | if (pos > 0) | |
1242 | { | |
1243 | bool notValid = false; | |
1244 | switch (str[pos-1]) | |
1245 | { | |
1246 | case '0': case '1': case '2': case '3': case '4': | |
1247 | case '5': case '6': case '7': case '8': case '9': | |
1248 | notValid = true; | |
1249 | break; | |
1250 | default: | |
1251 | notValid = false; | |
1252 | break; | |
1253 | } | |
1254 | if (notValid) continue; | |
1255 | } | |
1256 | TString s = str(pos, length); | |
1257 | ||
52f67e50 | 1258 | if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or |
1259 | s == "compl" or s == "not" or s == "not_eq" or s == "or" or | |
1260 | s == "or_eq" or s == "xor" or s == "xor_eq" or s == "true" or | |
1261 | s == "false" | |
1262 | ) | |
1263 | { | |
1264 | // Ignore iso646.h and other keywords. | |
1265 | continue; | |
1266 | } | |
3da1c6d7 | 1267 | |
52f67e50 | 1268 | if (FindSymbol(s.Data(), list) == -1) |
1269 | { | |
1270 | AliHLTTriggerMenuSymbol newSymbol; | |
1271 | newSymbol.Name(s.Data()); | |
1272 | newSymbol.Type("bool"); | |
1273 | newSymbol.ObjectClass("AliHLTTriggerDecision"); | |
1274 | newSymbol.AssignExpression("this->Result()"); | |
1275 | newSymbol.DefaultValue("false"); | |
1276 | new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(newSymbol); | |
1277 | } | |
1278 | } | |
1279 | while (start < str.Length()); | |
1280 | } | |
1281 | ||
e2bb8ddd | 1282 | return 0; |
1283 | } | |
1284 | ||
81d62bb4 | 1285 | |
1286 | bool AliHLTGlobalTriggerComponent::ExtractedOperator(TString& expr, TString& op) | |
1287 | { | |
1288 | // Extracts the trailing operator from the expression. | |
1289 | ||
1290 | Ssiz_t i = 0; | |
1291 | // First skip the trailing whitespace. | |
1292 | bool whitespace = true; | |
1293 | for (i = expr.Length()-1; i >= 0 and whitespace; i--) | |
1294 | { | |
1295 | switch (expr[i]) | |
1296 | { | |
1297 | case ' ': case '\t': case '\r': case '\n': | |
1298 | whitespace = true; | |
1299 | break; | |
1300 | default: | |
1301 | whitespace = false; | |
1302 | } | |
1303 | } | |
1304 | if (i < 0 or whitespace) return false; | |
1305 | ||
1306 | // Now find the first whitespace character before the trailing symbol. | |
1307 | bool nonwhitespace = true; | |
1308 | for (; i >= 0 and nonwhitespace; i--) | |
1309 | { | |
1310 | switch (expr[i]) | |
1311 | { | |
1312 | case ' ': case '\t': case '\r': case '\n': | |
1313 | nonwhitespace = false; | |
1314 | break; | |
1315 | default: | |
1316 | nonwhitespace = true; | |
1317 | } | |
1318 | } | |
1319 | if (i < 0 or nonwhitespace) return false; | |
1320 | ||
1321 | // Extract the last symbols and check if it is a valid operator. | |
1322 | TString s = expr; | |
1323 | s.Remove(0, i+2); | |
1324 | if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or | |
1325 | s == "compl" or s == "not" or s == "not_eq" or s == "or" or | |
1326 | s == "or_eq" or s == "xor" or s == "xor_eq" or s == "&&" or | |
1327 | s == "&=" or s == "&" or s == "|" or s == "~" or s == "!" or | |
1328 | s == "!=" or s == "||" or s == "|=" or s == "^" or s == "^=" or | |
1329 | s == "==" or s == "+" or s == "-" or s == "*" or s == "/" or | |
1330 | s == "%" or s == ">" or s == "<" or s == ">=" or s == "<=" | |
1331 | ) | |
1332 | { | |
1333 | expr.Remove(i+1); | |
1334 | op = s; | |
1335 | return true; | |
1336 | } | |
1337 | ||
1338 | return false; | |
1339 | } | |
1340 | ||
1341 | ||
2974f8dc | 1342 | int AliHLTGlobalTriggerComponent::PrintStatistics(const AliHLTGlobalTrigger* pTrigger, AliHLTComponentLogSeverity level, int offset) const |
1343 | { | |
1344 | // print some statistics | |
8b29f84f | 1345 | int totalEvents=GetEventCount()+offset; |
81d62bb4 | 1346 | for (int i=0; i<pTrigger->GetCounters().GetSize(); i++) { |
1347 | ULong64_t count=pTrigger->GetCounters()[i]; | |
8b29f84f | 1348 | float ratio=0; |
1349 | if (totalEvents>0) ratio=100*(float)count/totalEvents; | |
81d62bb4 | 1350 | HLTLog(level, "Item %d: total events: %d - counted events: %llu (%.1f%%)", i, totalEvents, count, ratio); |
2974f8dc | 1351 | } |
2974f8dc | 1352 | return 0; |
1353 | } | |
566a01d0 | 1354 | |
1355 | int AliHLTGlobalTriggerComponent::AddCTPDecisions(AliHLTGlobalTrigger* pTrigger, const AliHLTCTPData* pCTPData, const AliHLTComponentTriggerData* trigData) | |
1356 | { | |
1357 | // add trigger decisions for the valid CTP classes | |
1358 | if (!pCTPData || !pTrigger) return 0; | |
1359 | ||
1360 | AliHLTUInt64_t triggerMask=pCTPData->Mask(); | |
1361 | AliHLTUInt64_t bit0=0x1; | |
1362 | if (!fCTPDecisions) { | |
1363 | fCTPDecisions=new TClonesArray(AliHLTTriggerDecision::Class(), gkNCTPTriggerClasses); | |
1364 | if (!fCTPDecisions) return -ENOMEM; | |
1365 | ||
1366 | fCTPDecisions->ExpandCreate(gkNCTPTriggerClasses); | |
1367 | for (int i=0; i<gkNCTPTriggerClasses; i++) { | |
1368 | const char* name=pCTPData->Name(i); | |
1369 | if (triggerMask&(bit0<<i) && name) { | |
1370 | AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i)); | |
1371 | assert(pDecision); | |
1372 | if (!pDecision) { | |
1373 | delete fCTPDecisions; | |
1374 | fCTPDecisions=NULL; | |
1375 | return -ENOENT; | |
1376 | } | |
1377 | pDecision->Name(name); | |
1378 | } | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | for (int i=0; i<gkNCTPTriggerClasses; i++) { | |
1383 | const char* name=pCTPData->Name(i); | |
1384 | if ((triggerMask&(bit0<<i))==0 || name==NULL) continue; | |
1385 | AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i)); | |
1386 | HLTDebug("updating CTP trigger decision %d %s (%p casted %p)", i, name, fCTPDecisions->At(i), pDecision); | |
1387 | if (!pDecision) return -ENOENT; | |
1388 | ||
1389 | bool result=false; | |
1390 | if (trigData) result=pCTPData->EvaluateCTPTriggerClass(name, *trigData); | |
1391 | else result=pCTPData->EvaluateCTPTriggerClass(name); | |
1392 | pDecision->Result(result); | |
1393 | pDecision->TriggerDomain().Clear(); | |
1394 | if (trigData) pDecision->TriggerDomain().Add(pCTPData->ReadoutList(*trigData)); | |
1395 | else pDecision->TriggerDomain().Add(pCTPData->ReadoutList()); | |
1396 | ||
1397 | pTrigger->Add(fCTPDecisions->At(i), kAliHLTDataTypeTriggerDecision, kAliHLTVoidDataSpec); | |
1398 | } | |
1399 | ||
1400 | return 0; | |
1401 | } |