]>
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 | /// | |
def2ec97 | 23 | /// The AliHLTGlobalTriggerComponent class applies the global HLT trigger to all |
1b9a175e | 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" |
579a2996 | 36 | #include "TMD5.h" |
37 | #include "TRandom3.h" | |
e2bb8ddd | 38 | #include "TROOT.h" |
72bc5ab0 | 39 | #include "TSystem.h" |
52f67e50 | 40 | #include "TRegexp.h" |
41 | #include "TClonesArray.h" | |
42 | #include "TObjString.h" | |
1b820a65 | 43 | #include "TString.h" |
52f67e50 | 44 | #include "TInterpreter.h" |
d4ed5215 | 45 | #include "TDatime.h" |
81d62bb4 | 46 | #include "TClass.h" |
d543303d | 47 | #include "TNamed.h" |
e2bb8ddd | 48 | #include <fstream> |
49 | #include <cerrno> | |
566a01d0 | 50 | #include <cassert> |
81d62bb4 | 51 | #include <vector> |
52 | #include <algorithm> | |
1b9a175e | 53 | |
54 | ClassImp(AliHLTGlobalTriggerComponent) | |
55 | ||
3da1c6d7 | 56 | const char* AliHLTGlobalTriggerComponent::fgkTriggerMenuCDBPath = "HLT/ConfigHLT/HLTGlobalTrigger"; |
57 | ||
1b9a175e | 58 | |
81d62bb4 | 59 | namespace |
60 | { | |
61 | /** | |
62 | * This method is used as a comparison functor with the STL sort routines. | |
63 | */ | |
64 | bool AliHLTDescendingNumbers(UInt_t a, UInt_t b) | |
65 | { | |
66 | return a > b; | |
67 | } | |
68 | } // end of namespace | |
69 | ||
70 | ||
1b9a175e | 71 | AliHLTGlobalTriggerComponent::AliHLTGlobalTriggerComponent() : |
e2bb8ddd | 72 | AliHLTTrigger(), |
52f67e50 | 73 | fTrigger(NULL), |
72bc5ab0 | 74 | fDebugMode(false), |
566a01d0 | 75 | fRuntimeCompile(true), |
81d62bb4 | 76 | fDeleteCodeFile(false), |
566a01d0 | 77 | fCodeFileName(), |
81d62bb4 | 78 | fClassName(), |
79 | fCTPDecisions(NULL), | |
80 | fBufferSizeConst(2*(sizeof(AliHLTGlobalTriggerDecision) + sizeof(AliHLTReadoutList))), | |
81 | fBufferSizeMultiplier(1.), | |
82 | fIncludePaths(TObjString::Class()), | |
1b820a65 | 83 | fIncludeFiles(TObjString::Class()), |
6598c82b | 84 | fLibStateAtLoad(), |
3d6ea577 | 85 | fBits(0), |
86 | fDataEventsOnly(true) | |
b375374f | 87 | , fMonitorPeriod(-1) |
1b9a175e | 88 | { |
89 | // Default constructor. | |
acc7214e | 90 | |
91 | ClearInfoForNewEvent(false); | |
1b9a175e | 92 | } |
93 | ||
94 | ||
95 | AliHLTGlobalTriggerComponent::~AliHLTGlobalTriggerComponent() | |
96 | { | |
97 | // Default destructor. | |
e2bb8ddd | 98 | |
99 | if (fTrigger != NULL) delete fTrigger; | |
566a01d0 | 100 | |
101 | if (fCTPDecisions) { | |
102 | fCTPDecisions->Delete(); | |
103 | delete fCTPDecisions; | |
104 | } | |
1b9a175e | 105 | } |
106 | ||
107 | ||
025443e0 | 108 | void AliHLTGlobalTriggerComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& list) const |
109 | { | |
110 | // Returns the kAliHLTDataTypeGlobalTrigger type as output. | |
111 | list.push_back(kAliHLTDataTypeGlobalTrigger); | |
112 | } | |
113 | ||
114 | ||
1b9a175e | 115 | void AliHLTGlobalTriggerComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier) |
116 | { | |
117 | // Returns the output data size estimate. | |
118 | ||
81d62bb4 | 119 | constBase = fBufferSizeConst; |
120 | inputMultiplier = fBufferSizeMultiplier; | |
1b9a175e | 121 | } |
122 | ||
123 | ||
52f67e50 | 124 | Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv) |
1b9a175e | 125 | { |
e2bb8ddd | 126 | // Initialises the global trigger component. |
127 | ||
52f67e50 | 128 | fDebugMode = false; |
81d62bb4 | 129 | fClassName = ""; |
130 | fCodeFileName = ""; | |
131 | fDeleteCodeFile = false; | |
52f67e50 | 132 | const char* configFileName = NULL; |
133 | const char* codeFileName = NULL; | |
81d62bb4 | 134 | fIncludePaths.Clear(); |
135 | fIncludeFiles.Clear(); | |
6598c82b | 136 | SetBit(kIncludeInput); |
3d6ea577 | 137 | fDataEventsOnly = true; |
52f67e50 | 138 | |
139 | for (int i = 0; i < argc; i++) | |
140 | { | |
141 | if (strcmp(argv[i], "-config") == 0) | |
142 | { | |
143 | if (configFileName != NULL) | |
144 | { | |
145 | HLTWarning("Trigger configuration macro was already specified." | |
146 | " Will replace previous value given by -config." | |
147 | ); | |
148 | } | |
149 | if (argc <= i+1) | |
150 | { | |
151 | HLTError("The trigger configuration macro filename was not specified." ); | |
152 | return -EINVAL; | |
153 | } | |
154 | configFileName = argv[i+1]; | |
155 | i++; | |
156 | continue; | |
157 | } | |
158 | ||
159 | if (strcmp(argv[i], "-includepath") == 0) | |
160 | { | |
161 | if (argc <= i+1) | |
162 | { | |
163 | HLTError("The include path was not specified." ); | |
164 | return -EINVAL; | |
165 | } | |
dda123c3 | 166 | try |
167 | { | |
168 | new (fIncludePaths[fIncludePaths.GetEntriesFast()]) TObjString(argv[i+1]); | |
169 | } | |
170 | catch (const std::bad_alloc&) | |
171 | { | |
172 | HLTError("Could not allocate more memory for the fIncludePaths array."); | |
173 | return -ENOMEM; | |
174 | } | |
52f67e50 | 175 | i++; |
176 | continue; | |
177 | } | |
178 | ||
179 | if (strcmp(argv[i], "-include") == 0) | |
180 | { | |
181 | if (argc <= i+1) | |
182 | { | |
183 | HLTError("The include file name was not specified." ); | |
184 | return -EINVAL; | |
185 | } | |
dda123c3 | 186 | try |
187 | { | |
188 | new (fIncludeFiles[fIncludeFiles.GetEntriesFast()]) TObjString(argv[i+1]); | |
189 | } | |
190 | catch (const std::bad_alloc&) | |
191 | { | |
192 | HLTError("Could not allocate more memory for the fIncludeFiles array."); | |
193 | return -ENOMEM; | |
194 | } | |
52f67e50 | 195 | i++; |
196 | continue; | |
197 | } | |
198 | ||
199 | if (strcmp(argv[i], "-debug") == 0) | |
200 | { | |
201 | if (fDebugMode == true) | |
202 | { | |
203 | HLTWarning("The debug flag was already specified. Ignoring this instance."); | |
204 | } | |
205 | fDebugMode = true; | |
566a01d0 | 206 | continue; |
207 | } | |
208 | ||
209 | if (strcmp(argv[i], "-cint") == 0) | |
210 | { | |
211 | fRuntimeCompile = false; | |
52f67e50 | 212 | continue; |
213 | } | |
214 | ||
215 | if (strcmp(argv[i], "-usecode") == 0) | |
216 | { | |
217 | if (codeFileName != NULL) | |
218 | { | |
219 | HLTWarning("Custom trigger code file was already specified." | |
220 | " Will replace previous value given by -usecode." | |
221 | ); | |
222 | } | |
223 | if (argc <= i+1) | |
224 | { | |
225 | HLTError("The custom trigger code filename was not specified." ); | |
226 | return -EINVAL; | |
227 | } | |
228 | codeFileName = argv[i+1]; | |
229 | if (argc <= i+2) | |
230 | { | |
231 | HLTError("The custom trigger class name was not specified." ); | |
232 | return -EINVAL; | |
233 | } | |
81d62bb4 | 234 | fClassName = argv[i+2]; |
52f67e50 | 235 | i += 2; |
236 | continue; | |
237 | } | |
474952b2 | 238 | |
239 | if (strcmp(argv[i], "-skipctp") == 0) | |
240 | { | |
241 | HLTInfo("Skipping CTP counters in trigger decision"); | |
6598c82b | 242 | SetBit(kSkipCTP); |
243 | continue; | |
244 | } | |
245 | ||
246 | if (strcmp(argv[i], "-forward-input") == 0) | |
247 | { | |
248 | HLTInfo("Forwarding input objects and trigger decisions"); | |
249 | SetBit(kForwardInput); | |
250 | SetBit(kIncludeShort); | |
251 | SetBit(kIncludeInput, false); | |
252 | continue; | |
253 | } | |
254 | ||
d543303d | 255 | if (strstr(argv[i], "-include-input") == argv[i]) |
6598c82b | 256 | { |
257 | SetBit(kForwardInput,false); | |
258 | TString param=argv[i]; | |
259 | param.ReplaceAll("-include-input", ""); | |
260 | if (param.CompareTo("=none")==0) | |
261 | { | |
262 | HLTInfo("skipping objects and trigger decisions"); | |
263 | SetBit(kIncludeShort, false); | |
264 | SetBit(kIncludeInput, false); | |
265 | } | |
266 | else if (param.CompareTo("=short")==0) | |
267 | { | |
d543303d | 268 | HLTInfo("including short info on objects and trigger decisions"); |
6598c82b | 269 | SetBit(kIncludeShort); |
270 | SetBit(kIncludeInput, false); | |
271 | } | |
d543303d | 272 | else if (param.CompareTo("=both")==0) |
273 | { | |
274 | HLTInfo("including input objects, trigger decisions and short info"); | |
275 | SetBit(kIncludeShort); | |
276 | SetBit(kIncludeInput); | |
277 | } | |
6598c82b | 278 | else if (param.CompareTo("=objects")==0 || param.IsNull()) |
279 | { | |
280 | HLTInfo("including input objects and trigger decisions"); | |
281 | SetBit(kIncludeShort, false); | |
282 | SetBit(kIncludeInput); | |
283 | } | |
284 | else | |
285 | { | |
286 | HLTError("unknown parameter '%s' for argument '-include-input'", param.Data()); | |
287 | } | |
474952b2 | 288 | continue; |
289 | } | |
3d6ea577 | 290 | |
291 | if (strcmp(argv[i], "-process-all-events") == 0) | |
292 | { | |
293 | fDataEventsOnly = false; | |
294 | continue; | |
295 | } | |
b375374f | 296 | |
297 | if (strstr(argv[i], "-monitoring") == argv[i]) | |
298 | { | |
299 | TString param=argv[i]; | |
300 | param.ReplaceAll("-monitoring", ""); | |
301 | if (param.IsNull()) | |
302 | { | |
303 | // -monitoring | |
304 | // enable monitoring trigger for all events | |
305 | fMonitorPeriod=0; | |
306 | } else { | |
307 | // -monitoring=n | |
308 | // enable monitoring trigger once every n seconds | |
309 | param.ReplaceAll("=", ""); | |
310 | if (param.IsDigit()) { | |
311 | fMonitorPeriod=param.Atoi(); | |
312 | } else { | |
313 | HLTError("expecting number as parameter for argument '-monitoring=', got %s, skipping monitoring trigger", param.Data()); | |
314 | } | |
315 | } | |
316 | continue; | |
317 | } | |
3d6ea577 | 318 | |
52f67e50 | 319 | HLTError("Unknown option '%s'.", argv[i]); |
320 | return -EINVAL; | |
321 | } // for loop | |
322 | ||
323 | const AliHLTTriggerMenu* menu = NULL; | |
324 | if (configFileName != NULL) | |
325 | { | |
326 | TString cmd = ".x "; | |
327 | cmd += configFileName; | |
328 | gROOT->ProcessLine(cmd); | |
329 | menu = AliHLTGlobalTriggerConfig::Menu(); | |
330 | } | |
331 | ||
3da1c6d7 | 332 | // Try load the trigger menu from the CDB if it is not loaded yet with the |
333 | // -config option | |
81d62bb4 | 334 | int result = -ENOENT; |
335 | if (menu == NULL) | |
3da1c6d7 | 336 | { |
81d62bb4 | 337 | result = LoadTriggerMenu(fgkTriggerMenuCDBPath, menu); |
3da1c6d7 | 338 | } |
52f67e50 | 339 | if (menu == NULL) |
340 | { | |
341 | HLTError("No trigger menu configuration found or specified."); | |
81d62bb4 | 342 | return result; |
52f67e50 | 343 | } |
344 | ||
52f67e50 | 345 | if (codeFileName == NULL) |
346 | { | |
81d62bb4 | 347 | result = GenerateTrigger(menu, fClassName, fCodeFileName, fIncludePaths, fIncludeFiles); |
348 | if (result == 0) fDeleteCodeFile = true; | |
52f67e50 | 349 | } |
350 | else | |
351 | { | |
81d62bb4 | 352 | result = LoadTriggerClass(codeFileName, fIncludePaths); |
353 | if (result == 0) fCodeFileName = codeFileName; | |
52f67e50 | 354 | } |
e2bb8ddd | 355 | if (result != 0) return result; |
356 | ||
dda123c3 | 357 | try |
358 | { | |
359 | fTrigger = AliHLTGlobalTrigger::CreateNew(fClassName.Data()); | |
360 | } | |
361 | catch (const std::bad_alloc&) | |
362 | { | |
363 | HLTError("Could not allocate memory for the AliHLTGlobalTrigger instance."); | |
364 | return -ENOMEM; | |
365 | } | |
e2bb8ddd | 366 | if (fTrigger == NULL) |
367 | { | |
81d62bb4 | 368 | HLTError("Could not create a new instance of '%s'.", fClassName.Data()); |
e2bb8ddd | 369 | return -EIO; |
370 | } | |
371 | ||
52f67e50 | 372 | fTrigger->FillFromMenu(*menu); |
325e5e42 | 373 | if (fTrigger->CallFailed()) return -EPROTO; |
474952b2 | 374 | |
375 | // setup the CTP accounting in AliHLTComponent | |
566a01d0 | 376 | SetupCTPData(); |
474952b2 | 377 | |
acc7214e | 378 | // Set the default values from the trigger menu. |
379 | SetDescription(menu->DefaultDescription()); | |
380 | SetTriggerDomain(menu->DefaultTriggerDomain()); | |
381 | ||
e2bb8ddd | 382 | return 0; |
383 | } | |
384 | ||
1b9a175e | 385 | |
e2bb8ddd | 386 | Int_t AliHLTGlobalTriggerComponent::DoDeinit() |
387 | { | |
388 | // Cleans up the global trigger component. | |
389 | ||
390 | if (fTrigger != NULL) | |
391 | { | |
392 | delete fTrigger; | |
393 | fTrigger = NULL; | |
394 | } | |
395 | ||
566a01d0 | 396 | if (fCTPDecisions) { |
397 | fCTPDecisions->Delete(); | |
398 | delete fCTPDecisions; | |
399 | } | |
400 | fCTPDecisions=NULL; | |
81d62bb4 | 401 | |
402 | Int_t result = UnloadTriggerClass(fCodeFileName); | |
403 | if (result != 0) return result; | |
404 | ||
405 | if (fDeleteCodeFile and !fCodeFileName.IsNull() && gSystem->AccessPathName(fCodeFileName)==0 && !fDebugMode) { | |
406 | fCodeFileName.ReplaceAll(".cxx", "*"); | |
407 | TString command="rm "; command+=fCodeFileName; | |
408 | gSystem->Exec(command); | |
409 | } | |
410 | fCodeFileName=""; | |
411 | fDeleteCodeFile=false; | |
566a01d0 | 412 | |
1b9a175e | 413 | return 0; |
414 | } | |
4f1d6b68 | 415 | |
416 | ||
417 | AliHLTComponent* AliHLTGlobalTriggerComponent::Spawn() | |
418 | { | |
419 | // Creates a new object instance. | |
dda123c3 | 420 | AliHLTComponent* comp = NULL; |
421 | try | |
422 | { | |
423 | comp = new AliHLTGlobalTriggerComponent; | |
424 | } | |
425 | catch (const std::bad_alloc&) | |
426 | { | |
427 | HLTError("Could not allocate memory for a new instance of AliHLTGlobalTriggerComponent."); | |
428 | return NULL; | |
429 | } | |
430 | return comp; | |
4f1d6b68 | 431 | } |
432 | ||
e2bb8ddd | 433 | |
434 | int AliHLTGlobalTriggerComponent::DoTrigger() | |
435 | { | |
436 | // This method will apply the global trigger decision. | |
437 | ||
438 | if (fTrigger == NULL) | |
439 | { | |
440 | HLTFatal("Global trigger implementation object is NULL!"); | |
441 | return -EIO; | |
442 | } | |
2974f8dc | 443 | |
444 | AliHLTUInt32_t eventType=0; | |
445 | if (!IsDataEvent(&eventType)) { | |
446 | if (eventType==gkAliEventTypeEndOfRun) PrintStatistics(fTrigger, kHLTLogImportant, 0); | |
3d6ea577 | 447 | if (fDataEventsOnly) |
448 | { | |
2f251ae6 | 449 | IgnoreEvent(); // dont generate any trigger decision. |
3d6ea577 | 450 | return 0; |
451 | } | |
2974f8dc | 452 | } |
453 | ||
81d62bb4 | 454 | // Copy the trigger counters in case we need to set them back to their original |
455 | // value because the PushBack method fails with ENOSPC. | |
456 | TArrayL64 originalCounters = fTrigger->GetCounters(); | |
325e5e42 | 457 | if (fTrigger->CallFailed()) return -EPROTO; |
81d62bb4 | 458 | |
e2bb8ddd | 459 | fTrigger->NewEvent(); |
325e5e42 | 460 | if (fTrigger->CallFailed()) return -EPROTO; |
e2bb8ddd | 461 | |
462 | // Fill in the input data. | |
463 | const TObject* obj = GetFirstInputObject(); | |
464 | while (obj != NULL) | |
52f67e50 | 465 | { |
466 | fTrigger->Add(obj, GetDataType(), GetSpecification()); | |
325e5e42 | 467 | if (fTrigger->CallFailed()) return -EPROTO; |
52f67e50 | 468 | obj = GetNextInputObject(); |
469 | } | |
470 | ||
a489a8dd | 471 | // add trigger decisions for every CTP class |
472 | const AliHLTCTPData* pCTPData=CTPData(); | |
473 | if (pCTPData) { | |
566a01d0 | 474 | AddCTPDecisions(fTrigger, pCTPData, GetTriggerData()); |
a489a8dd | 475 | } |
476 | ||
52f67e50 | 477 | // Calculate the global trigger result and trigger domain, then create and push |
478 | // back the new global trigger decision object. | |
479 | TString description; | |
acc7214e | 480 | AliHLTTriggerDomain triggerDomain; |
481 | bool triggerResult = fTrigger->CalculateTriggerDecision(triggerDomain, description); | |
325e5e42 | 482 | if (fTrigger->CallFailed()) return -EPROTO; |
acc7214e | 483 | AliHLTGlobalTriggerDecision decision( |
484 | triggerResult, | |
485 | // The following will cause the decision to be generated with default values | |
486 | // (set in fTriggerDomain and fDescription) if the trigger result is false. | |
487 | (triggerResult == true) ? triggerDomain : GetTriggerDomain(), | |
488 | (triggerResult == true) ? description.Data() : GetDescription() | |
489 | ); | |
48d98db9 | 490 | |
81d62bb4 | 491 | decision.SetCounters(fTrigger->GetCounters(), GetEventCount()+1); |
325e5e42 | 492 | if (fTrigger->CallFailed()) return -EPROTO; |
493 | ||
2f251ae6 | 494 | TClonesArray shortInfo(TNamed::Class(), GetNumberOfInputBlocks()); |
52f67e50 | 495 | |
2f251ae6 | 496 | // Add the input objects used to make the global decision. |
52f67e50 | 497 | obj = GetFirstInputObject(); |
498 | while (obj != NULL) | |
e2bb8ddd | 499 | { |
2f251ae6 | 500 | const AliHLTTriggerDecision* intrig = dynamic_cast<const AliHLTTriggerDecision*>(obj); |
501 | ||
502 | if (TestBit(kForwardInput)) Forward(obj); | |
503 | ||
504 | if (TestBit(kIncludeInput)) | |
e2bb8ddd | 505 | { |
2f251ae6 | 506 | if (intrig != NULL) |
507 | { | |
508 | decision.AddTriggerInput(*intrig); | |
509 | } | |
510 | else | |
511 | { | |
512 | // The const_cast should be safe in this case because the list of inputObjects | |
513 | // will be added to the global decision with AddInputObjectRef, which only | |
514 | // modifies the kCanDelete bit and nothing else. | |
515 | // This is necessary since GetFirstInputObject only returns const objects. | |
516 | decision.AddInputObjectRef( const_cast<TObject*>(obj) ); | |
517 | } | |
e2bb8ddd | 518 | } |
2f251ae6 | 519 | |
520 | if (TestBit(kIncludeShort)) | |
e2bb8ddd | 521 | { |
2f251ae6 | 522 | int entries = shortInfo.GetEntriesFast(); |
dda123c3 | 523 | try |
524 | { | |
2f251ae6 | 525 | new (shortInfo[entries]) TNamed(obj->GetName(), obj->GetTitle()); |
dda123c3 | 526 | } |
527 | catch (const std::bad_alloc&) | |
528 | { | |
529 | HLTError("Could not allocate more memory for the short list of input objects."); | |
dda123c3 | 530 | return -ENOMEM; |
531 | } | |
2f251ae6 | 532 | if (intrig != NULL) |
533 | { | |
534 | shortInfo[entries]->SetBit(BIT(16)); // indicate that this is a trigger decision | |
535 | shortInfo[entries]->SetBit(BIT(15), intrig->Result()); | |
d543303d | 536 | } |
537 | } | |
538 | ||
e2bb8ddd | 539 | obj = GetNextInputObject(); |
540 | } | |
2f251ae6 | 541 | if (TestBit(kIncludeShort)) decision.AddInputObjectRef(&shortInfo); |
542 | ||
543 | // The const_cast should be safe in this case because AddInputObjectRef just | |
544 | // modifies the kCanDelete bit and nothing else. | |
545 | if (!TestBit(kSkipCTP) && CTPData()) decision.AddInputObjectRef(const_cast<AliHLTCTPData*>(CTPData())); | |
546 | ||
547 | static UInt_t lastTime=0; | |
548 | TDatime time; | |
549 | if (time.Get()-lastTime>60) { | |
550 | lastTime=time.Get(); | |
551 | PrintStatistics(fTrigger, kHLTLogImportant); | |
d543303d | 552 | } |
2974f8dc | 553 | |
0744f75d | 554 | // add readout filter to event done data |
2974f8dc | 555 | CreateEventDoneReadoutFilter(decision.TriggerDomain(), 3); |
b375374f | 556 | |
557 | // add monitoring filter to event done data if enabled by setting | |
558 | // a monitoring period >=0: -1 means off, 0 means for every event | |
559 | // configured by argument '-monitoring[=n]' | |
560 | if (fMonitorPeriod>=0) { | |
561 | static UInt_t lastMonitorEvent=0; | |
562 | ||
563 | AliHLTTriggerDomain monitoringFilter(decision.TriggerDomain()); | |
564 | if (decision.Result() && | |
dd2e5959 | 565 | int(time.Get()-lastMonitorEvent)>fMonitorPeriod) { |
b375374f | 566 | lastMonitorEvent=time.Get(); |
567 | // add monitoring event command for triggered events | |
568 | CreateEventDoneReadoutFilter(decision.TriggerDomain(), 5); | |
569 | } else { | |
570 | // empty filter list if events are not triggered | |
571 | // or within the monitoring interval | |
572 | monitoringFilter.Clear(); | |
573 | } | |
574 | // add monitoring filter list | |
575 | CreateEventDoneReadoutFilter(monitoringFilter, 4); | |
0744f75d | 576 | } |
b375374f | 577 | |
52fec07c | 578 | if (TriggerEvent(&decision, kAliHLTDataTypeGlobalTrigger) == -ENOSPC) |
81d62bb4 | 579 | { |
580 | // Increase the estimated buffer space required if the PushBack methods in TriggerEvent | |
581 | // returned the "no buffer space" error code. Also remember to set the trigger counters | |
582 | // back to what they were, otherwise triggers will be double counted when we try to reprocess | |
583 | // this event with larger buffers. | |
584 | fBufferSizeConst += 1024*1024; | |
585 | fBufferSizeMultiplier *= 2.; | |
586 | fTrigger->SetCounters(originalCounters); | |
325e5e42 | 587 | if (fTrigger->CallFailed()) return -EPROTO; |
81d62bb4 | 588 | return -ENOSPC; |
589 | } | |
590 | return 0; | |
591 | } | |
592 | ||
593 | ||
594 | int AliHLTGlobalTriggerComponent::Reconfigure(const char* cdbEntry, const char* chainId) | |
595 | { | |
596 | // Reconfigure the component by loading the trigger menu and recreating the | |
597 | // trigger logic class. | |
598 | ||
599 | const char* path = fgkTriggerMenuCDBPath; | |
600 | const char* id = "(unknown)"; | |
601 | if (cdbEntry != NULL) path = cdbEntry; | |
602 | if (chainId != NULL and chainId[0] != '\0') id = chainId; | |
603 | HLTInfo("Reconfiguring from '%s' for chain component '%s'.", path, id); | |
604 | ||
605 | const AliHLTTriggerMenu* menu = NULL; | |
606 | int result = LoadTriggerMenu(path, menu); | |
607 | if (result != 0) return result; | |
608 | ||
609 | TString className; | |
610 | TString codeFileName; | |
611 | result = GenerateTrigger(menu, className, codeFileName, fIncludePaths, fIncludeFiles); | |
612 | if (result != 0) return result; | |
613 | ||
dda123c3 | 614 | AliHLTGlobalTrigger* trigger = NULL; |
615 | try | |
616 | { | |
617 | trigger = AliHLTGlobalTrigger::CreateNew(className.Data()); | |
618 | } | |
619 | catch (const std::bad_alloc&) | |
620 | { | |
621 | HLTError("Could not allocate memory for the AliHLTGlobalTrigger instance."); | |
622 | return -ENOMEM; | |
623 | } | |
81d62bb4 | 624 | if (trigger == NULL) |
625 | { | |
626 | HLTError("Could not create a new instance of '%s'.", className.Data()); | |
627 | // Make sure to cleanup after the new code file. | |
628 | UnloadTriggerClass(codeFileName); | |
629 | if (not codeFileName.IsNull() and gSystem->AccessPathName(codeFileName)==0 and not fDebugMode) | |
630 | { | |
631 | codeFileName.ReplaceAll(".cxx", "*"); | |
632 | TString command="rm "; command+=codeFileName; | |
633 | gSystem->Exec(command); | |
634 | } | |
635 | return -EIO; | |
636 | } | |
637 | ||
638 | if (fTrigger != NULL) | |
639 | { | |
640 | delete fTrigger; | |
641 | fTrigger = NULL; | |
642 | } | |
643 | ||
644 | fTrigger = trigger; | |
645 | fTrigger->FillFromMenu(*menu); | |
325e5e42 | 646 | if (fTrigger->CallFailed()) return -EPROTO; |
81d62bb4 | 647 | |
648 | // Set the default values from the trigger menu. | |
649 | SetDescription(menu->DefaultDescription()); | |
650 | SetTriggerDomain(menu->DefaultTriggerDomain()); | |
651 | ||
652 | // Cleanup the old class code. | |
653 | UnloadTriggerClass(fCodeFileName); | |
654 | if (fDeleteCodeFile and not fCodeFileName.IsNull() and gSystem->AccessPathName(fCodeFileName)==0 and not fDebugMode) | |
655 | { | |
656 | fCodeFileName.ReplaceAll(".cxx", "*"); | |
657 | TString command="rm "; command+=fCodeFileName; | |
658 | gSystem->Exec(command); | |
659 | } | |
660 | fCodeFileName = codeFileName; | |
661 | fDeleteCodeFile = true; // Since we generated a new class. | |
662 | ||
663 | return 0; | |
664 | } | |
665 | ||
666 | ||
667 | int AliHLTGlobalTriggerComponent::LoadTriggerMenu(const char* cdbPath, const AliHLTTriggerMenu*& menu) | |
668 | { | |
669 | // Loads the trigger menu object from the CDB path. | |
670 | ||
671 | HLTDebug("Trying to load trigger menu from '%s'.", cdbPath); | |
672 | if (AliCDBManager::Instance() == NULL) | |
673 | { | |
674 | HLTError("CDB manager object not found."); | |
675 | return -EIO; | |
676 | } | |
677 | AliCDBStorage* store = AliCDBManager::Instance()->GetDefaultStorage(); | |
678 | if (store == NULL) | |
679 | { | |
680 | HLTError("Could not get the the default storage for the CDB."); | |
681 | return -EIO; | |
682 | } | |
683 | Int_t version = store->GetLatestVersion(cdbPath, GetRunNo()); | |
684 | Int_t subVersion = store->GetLatestSubVersion(cdbPath, GetRunNo(), version); | |
685 | AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, GetRunNo(), version, subVersion); | |
686 | if (entry == NULL) | |
687 | { | |
688 | HLTError("Could not get the CDB entry for \"%s\".", cdbPath); | |
689 | return -EIO; | |
690 | } | |
691 | TObject* obj = entry->GetObject(); | |
692 | if (obj == NULL) | |
693 | { | |
694 | HLTError("Configuration object for \"%s\" is missing.", cdbPath); | |
695 | return -ENOENT; | |
696 | } | |
697 | if (obj->IsA() != AliHLTTriggerMenu::Class()) | |
698 | { | |
699 | HLTError("Wrong type for configuration object in \"%s\". Found a %s but we expect a AliHLTTriggerMenu.", | |
700 | cdbPath, obj->ClassName() | |
701 | ); | |
702 | return -EPROTO; | |
703 | } | |
704 | menu = static_cast<AliHLTTriggerMenu*>(obj); | |
e2bb8ddd | 705 | return 0; |
706 | } | |
707 | ||
708 | ||
579a2996 | 709 | void AliHLTGlobalTriggerComponent::GenerateFileName(TString& name, TString& filename) const |
710 | { | |
711 | // Creates a unique file name for the generated code. | |
712 | ||
713 | // Start by creating a new UUID. We cannot use the one automatically generated | |
714 | // by ROOT because the algorithm used will not guarantee unique IDs when generating | |
715 | // these UUIDs at a high rate in parallel. | |
716 | TUUID uuid; | |
717 | // We then use the generated UUID to form part of the random number seeds which | |
718 | // will be used to generate a proper random UUID. For good measure we use a MD5 | |
719 | // hash also. Note that we want to use the TUUID class because it will combine the | |
720 | // host address information into the UUID. Using gSystem->GetHostByName() apparently | |
721 | // can cause problems on Windows machines with a firewall, because it always tries | |
722 | // to contact a DNS. The TUUID class handles this case appropriately. | |
723 | union | |
724 | { | |
725 | UChar_t buf[16]; | |
726 | UShort_t word[8]; | |
727 | UInt_t dword[4]; | |
728 | }; | |
729 | uuid.GetUUID(buf); | |
730 | TMD5 md5; | |
731 | md5.Update(buf, sizeof(buf)); | |
f69da953 | 732 | TMD5 md52 = md5; |
579a2996 | 733 | md5.Final(buf); |
734 | dword[0] += gSystem->GetUid(); | |
735 | dword[1] += gSystem->GetGid(); | |
736 | dword[2] += gSystem->GetPid(); | |
737 | for (int i = 0; i < 4; ++i) | |
738 | { | |
739 | gRandom->SetSeed(dword[i]); | |
740 | dword[i] = gRandom->Integer(0xFFFFFFFF); | |
741 | } | |
f69da953 | 742 | md52.Update(buf, sizeof(buf)); |
743 | md52.Final(buf); | |
579a2996 | 744 | // To keep to the standard we need to set the version and reserved bits. |
745 | word[3] = (word[3] & 0x0FFF) | 0x4000; | |
746 | buf[8] = (buf[8] & 0x3F) | 0x80; | |
747 | ||
748 | // Create the name of the new class and file. | |
749 | char uuidstr[64]; | |
750 | sprintf(uuidstr, "%08x_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x", | |
751 | dword[0], word[2], word[3], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15] | |
752 | ); | |
753 | name = "AliHLTGlobalTriggerImpl_"; | |
754 | name += uuidstr; | |
755 | filename = name + ".cxx"; | |
756 | ||
757 | // For good measure, check that the file names are not used. If they are then regenerate. | |
758 | fstream file(filename.Data(), ios_base::in); | |
759 | if (file.good()) | |
760 | { | |
761 | file.close(); | |
762 | GenerateFileName(name, filename); | |
763 | } | |
764 | } | |
765 | ||
766 | ||
52f67e50 | 767 | int AliHLTGlobalTriggerComponent::GenerateTrigger( |
81d62bb4 | 768 | const AliHLTTriggerMenu* menu, TString& name, TString& filename, |
52f67e50 | 769 | const TClonesArray& includePaths, const TClonesArray& includeFiles |
770 | ) | |
e2bb8ddd | 771 | { |
772 | // Generates the global trigger class that will implement the specified trigger menu. | |
52f67e50 | 773 | // See header for more details. |
e2bb8ddd | 774 | |
579a2996 | 775 | GenerateFileName(name, filename); |
776 | HLTDebug("Generating custom HLT trigger class named %s, in file %s, using trigger menu %p.", | |
777 | name.Data(), filename.Data(), ((void*)menu) | |
778 | ); | |
e2bb8ddd | 779 | |
52f67e50 | 780 | // Open a text file to write the code and generate the new class. |
81d62bb4 | 781 | fstream code(filename.Data(), ios_base::out | ios_base::trunc); |
e2bb8ddd | 782 | if (not code.good()) |
783 | { | |
81d62bb4 | 784 | HLTError("Could not open file '%s' for writing.", filename.Data()); |
e2bb8ddd | 785 | return -EIO; |
786 | } | |
787 | ||
52f67e50 | 788 | TClonesArray symbols(AliHLTTriggerMenuSymbol::Class()); |
789 | int result = BuildSymbolList(menu, symbols); | |
790 | if (result != 0) return result; | |
791 | ||
81d62bb4 | 792 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; |
52f67e50 | 793 | code << "#include <cstring>" << endl; |
2f251ae6 | 794 | code << "#include \"TClass.h\"" << endl; |
52f67e50 | 795 | code << "#include \"TString.h\"" << endl; |
796 | code << "#include \"TClonesArray.h\"" << endl; | |
81d62bb4 | 797 | code << "#include \"AliHLTLogging.h\"" << endl; |
e2bb8ddd | 798 | code << "#include \"AliHLTGlobalTrigger.h\"" << endl; |
799 | code << "#include \"AliHLTGlobalTriggerDecision.h\"" << endl; | |
52f67e50 | 800 | code << "#include \"AliHLTDomainEntry.h\"" << endl; |
801 | code << "#include \"AliHLTTriggerDomain.h\"" << endl; | |
802 | code << "#include \"AliHLTReadoutList.h\"" << endl; | |
803 | code << "#include \"AliHLTTriggerMenu.h\"" << endl; | |
804 | code << "#include \"AliHLTTriggerMenuItem.h\"" << endl; | |
805 | code << "#include \"AliHLTTriggerMenuSymbol.h\"" << endl; | |
806 | ||
807 | // Add any include files that were specified on the command line. | |
808 | for (Int_t i = 0; i < includeFiles.GetEntriesFast(); i++) | |
809 | { | |
7da191b6 | 810 | TString file = static_cast<TObjString*>(includeFiles.UncheckedAt(i))->String(); |
52f67e50 | 811 | code << "#include \"" << file.Data() << "\"" << endl; |
812 | } | |
813 | ||
81d62bb4 | 814 | if (fDebugMode) |
815 | { | |
816 | code << "#else" << endl; | |
817 | code << "const char* gFunctionName = \"???\";" << endl; | |
818 | code << "#define HLTDebug(msg) if (CheckFilter(kHLTLogDebug) && CheckGroup(Class_Name())) SendMessage(kHLTLogDebug, Class_Name(), ::gFunctionName, __FILE__, __LINE__, msg)" << endl; | |
819 | } | |
820 | code << "#endif" << endl; | |
821 | ||
822 | code << "class " << name << " :" << endl; | |
823 | // Add appropriate #ifdef sections since we need to prevent inheritance from | |
824 | // AliHLTGlobalTrigger. CINT does not seem to support multiple inheritance nor | |
825 | // multiple levels of inheritance. Neither of the following schemes worked: | |
826 | // | |
827 | // 1) class AliHLTGlobalTrigger : public AliHLTLogging {}; | |
828 | // class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger {}; | |
829 | // | |
830 | // 2) class AliHLTGlobalTrigger {}; | |
831 | // class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger, public AliHLTLogging {}; | |
832 | // | |
833 | // Thus, we are forced to just inherit from AliHLTLogging when running in the CINT | |
834 | // interpreter. But we anyway have to call the global trigger implementation class | |
835 | // through the AliHLTGlobalTriggerWrapper so this is not such a problem. | |
836 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; | |
837 | code << " public AliHLTGlobalTrigger," << endl; | |
838 | code << "#endif" << endl; | |
839 | code << " public AliHLTLogging" << endl; | |
e2bb8ddd | 840 | code << "{" << endl; |
841 | code << "public:" << endl; | |
52f67e50 | 842 | |
81d62bb4 | 843 | // Generate constructor method. |
844 | code << " " << name << "() :" << endl; | |
845 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; | |
846 | code << " AliHLTGlobalTrigger()," << endl; | |
847 | code << "#endif" << endl; | |
848 | code << " AliHLTLogging()"; | |
52f67e50 | 849 | // Write the symbols in the trigger menu in the initialisation list. |
850 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
851 | { | |
852 | code << "," << endl; | |
853 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
854 | code << " " << symbol->Name() << "()," << endl; | |
855 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) | |
856 | { | |
857 | code << " " << symbol->Name() << "TriggerDomain()," << endl; | |
858 | } | |
859 | code << " " << symbol->Name() << "DomainEntry(kAliHLTAnyDataType)"; | |
860 | } | |
861 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) | |
862 | { | |
863 | code << "," << endl << " fMenuItemDescription" << i << "()"; | |
864 | } | |
865 | code << endl << " {" << endl; | |
866 | if (fDebugMode) | |
867 | { | |
81d62bb4 | 868 | code << "#ifdef __CINT__" << endl; |
869 | code << " gFunctionName = \"" << name.Data() <<"\";" << endl; | |
870 | code << "#endif" << endl; | |
52f67e50 | 871 | code << " SetLocalLoggingLevel(kHLTLogAll);" << endl; |
81d62bb4 | 872 | code << " HLTDebug(Form(\"Creating new instance at %p.\", this));" << endl; |
52f67e50 | 873 | } |
e2bb8ddd | 874 | code << " }" << endl; |
52f67e50 | 875 | |
e2bb8ddd | 876 | code << " virtual ~" << name << "() {" << endl; |
52f67e50 | 877 | if (fDebugMode) |
878 | { | |
81d62bb4 | 879 | code << "#ifdef __CINT__" << endl; |
880 | code << " gFunctionName = \"~" << name.Data() << "\";" << endl; | |
881 | code << "#endif" << endl; | |
882 | code << " HLTDebug(Form(\"Deleting instance at %p.\", this));" << endl; | |
52f67e50 | 883 | } |
e2bb8ddd | 884 | code << " }" << endl; |
52f67e50 | 885 | |
886 | // Generate the FillFromMenu method. | |
887 | code << " virtual void FillFromMenu(const AliHLTTriggerMenu& menu) {" << endl; | |
888 | if (fDebugMode) | |
889 | { | |
81d62bb4 | 890 | code << "#ifdef __CINT__" << endl; |
891 | code << " gFunctionName = \"FillFromMenu\";" << endl; | |
892 | code << "#endif" << endl; | |
893 | code << " HLTDebug(Form(\"Filling description entries from trigger menu for global trigger %p.\", this));" << endl; | |
52f67e50 | 894 | } |
81d62bb4 | 895 | code << " fCounter.Set(menu.NumberOfItems());" << endl; |
52f67e50 | 896 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) |
897 | { | |
898 | code << " fMenuItemDescription" << i << " = (menu.Item(" << i | |
899 | << ") != NULL) ? menu.Item(" << i << ")->Description() : \"\";" << endl; | |
900 | } | |
901 | if (fDebugMode) | |
902 | { | |
81d62bb4 | 903 | code << " HLTDebug(Form(\"Finished filling description entries from trigger menu.\"));" << endl; |
904 | code << " HLTDebug(Form(\"Filling domain entries from trigger menu symbols for global trigger %p.\", this));" << endl; | |
52f67e50 | 905 | } |
906 | code << " for (Int_t i = 0; i < menu.SymbolArray().GetEntriesFast(); i++) {" << endl; | |
325e5e42 | 907 | // 30 Oct 2009 - CINT sometimes evaluates the dynamic_cast incorrectly. |
908 | // Have to use the TClass system for extra protection. | |
909 | code << " if (menu.SymbolArray().UncheckedAt(i) == NULL) continue;" << endl; | |
910 | code << " if (menu.SymbolArray().UncheckedAt(i)->IsA() != AliHLTTriggerMenuSymbol::Class()) continue;" << endl; | |
52f67e50 | 911 | code << " const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const" |
912 | " AliHLTTriggerMenuSymbol*>(menu.SymbolArray().UncheckedAt(i));" << endl; | |
913 | code << " if (symbol == NULL) continue;" << endl; | |
914 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
915 | { | |
916 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
025443e0 | 917 | code << " if (strcmp(symbol->RealName(), \"" << symbol->RealName() << "\") == 0) {" << endl; |
52f67e50 | 918 | if (fDebugMode) |
919 | { | |
81d62bb4 | 920 | code << " HLTDebug(Form(\"Assinging domain entry value corresponding with symbol '%s' to '%s'.\"," |
025443e0 | 921 | " symbol->RealName(), symbol->BlockType().AsString().Data()));" << endl; |
52f67e50 | 922 | } |
923 | code << " " << symbol->Name() << "DomainEntry = symbol->BlockType();" << endl; | |
924 | code << " continue;" << endl; | |
925 | code << " }" << endl; | |
926 | } | |
927 | code << " }" << endl; | |
2f251ae6 | 928 | // The following is an optimisation where symbols without any assignment operators |
929 | // are treated as constant and only initialised in FillFromMenu rather than reseting | |
930 | // them in the NewEvent method. | |
931 | // Note: we putting this initialisation into the constructor can lead to seg faults | |
932 | // under CINT interpretation. Thus we must put it into the FillFromMenu method instead. | |
933 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
934 | { | |
935 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
936 | if (TString(symbol->AssignExpression()) != "") continue; | |
937 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) continue; | |
938 | // CINT has problems with the implicit equals operator for complex types, so if | |
939 | // the type has an equals operater we need to write the operator call explicitly. | |
940 | TClass* clas = TClass::GetClass(symbol->Type()); | |
941 | if (clas != NULL and clas->GetMethodAny("operator=") != NULL) | |
942 | { | |
943 | code << " " << symbol->Name() << ".operator = (" << symbol->DefaultValue() << ");" << endl; | |
944 | } | |
945 | else | |
946 | { | |
947 | code << " " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl; | |
948 | } | |
949 | } | |
52f67e50 | 950 | if (fDebugMode) |
951 | { | |
81d62bb4 | 952 | code << " HLTDebug(Form(\"Finished filling domain entries from trigger menu symbols.\"));" << endl; |
52f67e50 | 953 | } |
e2bb8ddd | 954 | code << " }" << endl; |
52f67e50 | 955 | |
956 | // Generate the NewEvent method. | |
957 | code << " virtual void NewEvent() {" << endl; | |
958 | if (fDebugMode) | |
959 | { | |
81d62bb4 | 960 | code << "#ifdef __CINT__" << endl; |
961 | code << " gFunctionName = \"NewEvent\";" << endl; | |
962 | code << "#endif" << endl; | |
963 | code << " HLTDebug(Form(\"New event for global trigger object %p, initialising variables to default values.\", this));" << endl; | |
52f67e50 | 964 | } |
965 | // Write code to initialise the symbols in the trigger menu to their default values. | |
966 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
967 | { | |
968 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
2f251ae6 | 969 | // The following is an optimisation. If the symbol does not have an assignment expression |
970 | // then it is effectively a constant symbol and can be initialised earlier and only once. | |
971 | // In this case we initialise it in the FillFromMenu method instead. | |
972 | if (TString(symbol->AssignExpression()) == "") continue; | |
81d62bb4 | 973 | // CINT has problems with the implicit equals operator for complex types, so if |
2f251ae6 | 974 | // the type has an equals operater we need to write the operator call explicitly. |
81d62bb4 | 975 | TClass* clas = TClass::GetClass(symbol->Type()); |
976 | if (clas != NULL and clas->GetMethodAny("operator=") != NULL) | |
977 | { | |
978 | code << " " << symbol->Name() << ".operator = (" << symbol->DefaultValue() << ");" << endl; | |
979 | } | |
980 | else | |
981 | { | |
982 | code << " " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl; | |
983 | } | |
52f67e50 | 984 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) |
985 | { | |
986 | code << " " << symbol->Name() << "TriggerDomain.Clear();" << endl; | |
987 | } | |
988 | } | |
989 | if (fDebugMode) | |
990 | { | |
81d62bb4 | 991 | code << " HLTDebug(Form(\"Finished initialising variables.\"));" << endl; |
52f67e50 | 992 | } |
e2bb8ddd | 993 | code << " }" << endl; |
52f67e50 | 994 | |
995 | // Generate the Add method. | |
8066c87b | 996 | bool haveAssignments = false; |
997 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
998 | { | |
999 | // First check if we have any symbols with assignment expressions. | |
1000 | // This is needed to get rid of the on the fly compilation warning about '_object_' not being used. | |
1001 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
1002 | TString expr = symbol->AssignExpression(); | |
1003 | if (expr == "") continue; // Skip entries that have no assignment expression. | |
1004 | haveAssignments = true; | |
1005 | break; | |
1006 | } | |
1007 | if (haveAssignments or fDebugMode) | |
1008 | { | |
1009 | code << " virtual void Add(const TObject* _object_, const AliHLTComponentDataType& _type_, AliHLTUInt32_t _spec_) {" << endl; | |
1010 | } | |
1011 | else | |
1012 | { | |
1013 | code << " virtual void Add(const TObject* /*_object_*/, const AliHLTComponentDataType& _type_, AliHLTUInt32_t _spec_) {" << endl; | |
1014 | } | |
81d62bb4 | 1015 | if (fDebugMode) |
1016 | { | |
1017 | code << "#ifdef __CINT__" << endl; | |
1018 | code << " gFunctionName = \"Add\";" << endl; | |
1019 | code << "#endif" << endl; | |
1020 | } | |
52f67e50 | 1021 | code << " AliHLTDomainEntry _type_spec_(_type_, _spec_);" << endl; |
1022 | if (fDebugMode) | |
1023 | { | |
81d62bb4 | 1024 | code << " HLTDebug(Form(\"Adding TObject %p, with class name '%s' from data block" |
52f67e50 | 1025 | " '%s', to global trigger object %p\", _object_, _object_->ClassName()," |
81d62bb4 | 1026 | " _type_spec_.AsString().Data(), this));" << endl; |
52f67e50 | 1027 | code << " _object_->Print();" << endl; |
1028 | code << " bool _object_assigned_ = false;" << endl; | |
1029 | } | |
1030 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
1031 | { | |
1032 | // Write code to check if the block type, specification and class name is correct. | |
1033 | // Then write code to assign the variable from the input object. | |
1034 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
1035 | TString expr = symbol->AssignExpression(); | |
1036 | if (expr == "") continue; // Skip entries that have no assignment expression. | |
1037 | bool isTrigDecision = strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0; | |
1038 | if (fDebugMode) | |
1039 | { | |
1040 | if (isTrigDecision) | |
1041 | { | |
81d62bb4 | 1042 | code << " HLTDebug(Form(\"Trying to match input object to class '" |
f48dcb4e | 1043 | << symbol->ObjectClass() << "', trigger name '" << symbol->RealName() |
52f67e50 | 1044 | << "' and block type '%s'\", " << symbol->Name() |
81d62bb4 | 1045 | << "DomainEntry.AsString().Data()));" << endl; |
52f67e50 | 1046 | } |
1047 | else | |
1048 | { | |
81d62bb4 | 1049 | code << " HLTDebug(Form(\"Trying to match input object to class '" |
52f67e50 | 1050 | << symbol->ObjectClass() << "' and block type '%s'\", " |
81d62bb4 | 1051 | << symbol->Name() << "DomainEntry.AsString().Data()));" << endl; |
52f67e50 | 1052 | } |
1053 | } | |
325e5e42 | 1054 | // 30 Oct 2009 - CINT sometimes evaluates the dynamic_cast incorrectly. |
1055 | // Have to use the TClass system for extra protection. | |
1056 | code << " const " << symbol->ObjectClass() << "* " << symbol->Name() << "_object_ = NULL;" << endl; | |
f1cac1f9 | 1057 | code << " if (_object_->InheritsFrom(" << symbol->ObjectClass() << "::Class())) " << symbol->Name() |
52f67e50 | 1058 | << "_object_ = dynamic_cast<const " << symbol->ObjectClass() |
1059 | << "*>(_object_);" << endl; | |
81d62bb4 | 1060 | code << " if (" << symbol->Name() << "_object_ != NULL && "; |
52f67e50 | 1061 | if (isTrigDecision) |
1062 | { | |
1063 | code << "strcmp(" << symbol->Name() << "_object_->Name(), \"" | |
e9f09f57 | 1064 | << symbol->RealName() << "\") == 0 && "; |
52f67e50 | 1065 | } |
1066 | code << symbol->Name() << "DomainEntry == _type_spec_) {" << endl; | |
1067 | TString fullname = symbol->Name(); | |
1068 | fullname += "_object_"; | |
1069 | expr.ReplaceAll("this", fullname); | |
1070 | code << " this->" << symbol->Name() << " = " << expr.Data() << ";" << endl; | |
1071 | if (isTrigDecision) | |
1072 | { | |
1073 | code << " this->" << symbol->Name() << "TriggerDomain = " | |
1074 | << fullname.Data() << "->TriggerDomain();" << endl; | |
1075 | } | |
1076 | if (fDebugMode) | |
1077 | { | |
81d62bb4 | 1078 | code << " HLTDebug(Form(\"Added TObject %p with class name '%s' to variable " |
1079 | << symbol->Name() << "\", _object_, _object_->ClassName()));" << endl; | |
52f67e50 | 1080 | code << " _object_assigned_ = true;" << endl; |
1081 | } | |
1082 | code << " }" << endl; | |
1083 | } | |
1084 | if (fDebugMode) | |
1085 | { | |
81d62bb4 | 1086 | code << " if (! _object_assigned_) {" << endl; |
1087 | code << " HLTDebug(Form(\"Did not assign TObject %p" | |
1088 | " with class name '%s' to any variable.\", _object_, _object_->ClassName()));" | |
52f67e50 | 1089 | << endl; |
81d62bb4 | 1090 | code << " }" << endl; |
52f67e50 | 1091 | } |
e2bb8ddd | 1092 | code << " }" << endl; |
52f67e50 | 1093 | |
1094 | // Generate the CalculateTriggerDecision method. | |
81d62bb4 | 1095 | // This requires code to be generated that checks which items in the trigger menu |
1096 | // have their conditions asserted and then the trigger domain is generated from | |
1097 | // those fired items. | |
1098 | // The processing will start from the highest priority trigger group and stop | |
1099 | // after at least one trigger from the current priority group being processed | |
1100 | // is positive. For each priority group all the trigger menu items are checked. | |
1101 | // Their combined trigger condition expression must be true for the trigger priority | |
1102 | // group to be triggered positive. The full condition expression is formed by | |
1103 | // concatenating the individual condition expressions. If no trailing operators are | |
1104 | // used in the individual expressions then the default condition operator is placed | |
1105 | // between two concatenated condition expressions. | |
1106 | // If a trigger priority group has at least one trigger fired then the trigger domain | |
1107 | // is calculated such that it will give the same result as the concatenated trigger | |
1108 | // domain merging expressions for all the individual trigger menu items with | |
1109 | // positive results. Again, if no trailing operators are used in the individual | |
1110 | // merging expressions then the default domain operator is placed between two | |
1111 | // expression fragments. | |
52f67e50 | 1112 | code << " virtual bool CalculateTriggerDecision(AliHLTTriggerDomain& _domain_, TString& _description_) {" << endl; |
1113 | if (fDebugMode) | |
1114 | { | |
81d62bb4 | 1115 | code << "#ifdef __CINT__" << endl; |
1116 | code << " gFunctionName = \"CalculateTriggerDecision\";" << endl; | |
1117 | code << "#endif" << endl; | |
1118 | code << " HLTDebug(Form(\"Calculating global HLT trigger result with trigger object at %p.\", this));" << endl; | |
52f67e50 | 1119 | } |
81d62bb4 | 1120 | |
1121 | // Build a list of priorities used in the trigger menu. | |
1122 | std::vector<UInt_t> priorities; | |
52f67e50 | 1123 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) |
1124 | { | |
1125 | const AliHLTTriggerMenuItem* item = menu->Item(i); | |
81d62bb4 | 1126 | bool priorityNotInList = std::find(priorities.begin(), priorities.end(), item->Priority()) == priorities.end(); |
1127 | if (priorityNotInList) priorities.push_back(item->Priority()); | |
1128 | } | |
1129 | std::sort(priorities.begin(), priorities.end(), AliHLTDescendingNumbers); | |
1130 | // From the priority list, build the priority groups in the correct order, | |
1131 | // i.e. highest priority first. | |
1132 | // The priority group is a list of vectors of integers. The integers are the | |
1133 | // index numbers into the trigger menu item list for the items which form part | |
1134 | // of the priority group. | |
1135 | std::vector<std::vector<Int_t> > priorityGroup; | |
1136 | priorityGroup.insert(priorityGroup.begin(), priorities.size(), std::vector<Int_t>()); | |
1137 | for (size_t n = 0; n < priorities.size(); n++) | |
1138 | { | |
1139 | UInt_t priority = priorities[n]; | |
1140 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) | |
52f67e50 | 1141 | { |
81d62bb4 | 1142 | const AliHLTTriggerMenuItem* item = menu->Item(i); |
1143 | if (item->Priority() == priority) priorityGroup[n].push_back(i); | |
52f67e50 | 1144 | } |
81d62bb4 | 1145 | } |
1146 | ||
1147 | for (size_t n = 0; n < priorityGroup.size(); n++) | |
1148 | { | |
52f67e50 | 1149 | if (fDebugMode) |
1150 | { | |
81d62bb4 | 1151 | code << " HLTDebug(Form(\"Processing trigger priority group " << priorities[n] << "\"));" << endl; |
52f67e50 | 1152 | } |
81d62bb4 | 1153 | code << " "; |
1154 | if (n == 0) code << "UInt_t "; | |
1155 | code << "_previous_match_ = 0xFFFFFFFF;" << endl; | |
1156 | code << " "; | |
1157 | if (n == 0) code << "bool "; | |
1158 | code << "_trigger_matched_ = false;" << endl; | |
1159 | code << " "; | |
1160 | if (n == 0) code << "bool "; | |
1161 | code << "_group_result_ = false;" << endl; | |
1162 | std::vector<TString> conditionOperator; | |
1163 | conditionOperator.insert(conditionOperator.begin(), priorityGroup[n].size(), TString("")); | |
1164 | std::vector<TString> domainOperator; | |
1165 | domainOperator.insert(domainOperator.begin(), priorityGroup[n].size(), TString("")); | |
1166 | for (size_t m = 0; m < priorityGroup[n].size(); m++) | |
52f67e50 | 1167 | { |
81d62bb4 | 1168 | UInt_t i = priorityGroup[n][m]; |
1169 | const AliHLTTriggerMenuItem* item = menu->Item(i); | |
1170 | TString triggerCondition = item->TriggerCondition(); | |
1171 | TString mergeExpr = item->MergeExpression(); | |
1172 | // Replace the symbols found in the trigger condition and merging expressions | |
1173 | // with appropriate compilable versions. | |
1174 | for (Int_t j = 0; j < symbols.GetEntriesFast(); j++) | |
1175 | { | |
1176 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(j) ); | |
1177 | bool symbolNamesDifferent = strcmp(symbol->RealName(), symbol->Name()) != 0; | |
1178 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) | |
1179 | { | |
1180 | TString newname = symbol->Name(); | |
1181 | newname += "TriggerDomain"; | |
1182 | mergeExpr.ReplaceAll(symbol->RealName(), newname); | |
1183 | } | |
1184 | else | |
1185 | { | |
1186 | if (symbolNamesDifferent) mergeExpr.ReplaceAll(symbol->RealName(), symbol->Name()); | |
1187 | } | |
1188 | if (symbolNamesDifferent) triggerCondition.ReplaceAll(symbol->RealName(), symbol->Name()); | |
1189 | } | |
1190 | // We allow the trigger conditions and merging expressions to have trailing operators. | |
1191 | // Thus, we need to extract the operators and cleanup the expressions so that they will | |
1192 | // compile. This means that we silently ignore the trailing operator if not needed. | |
1193 | if (ExtractedOperator(triggerCondition, conditionOperator[m])) | |
1194 | { | |
1195 | // If the trailing operator is the same as the default operator then reset | |
1196 | // the value in the operator list so that the default is used in the generated | |
1197 | // code. This creates more compact code. | |
1198 | if (conditionOperator[m] == menu->DefaultConditionOperator()) conditionOperator[m] = ""; | |
1199 | } | |
1200 | if (ExtractedOperator(mergeExpr, domainOperator[m])) | |
1201 | { | |
1202 | if (domainOperator[m] == menu->DefaultDomainOperator()) domainOperator[m] = ""; | |
1203 | } | |
1204 | if (fDebugMode) | |
1205 | { | |
1206 | code << " HLTDebug(Form(\"Trying trigger condition " << i | |
1207 | << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));" | |
1208 | << endl; | |
1209 | } | |
1210 | code << " "; | |
1211 | if (n == 0 and m == 0) code << "bool "; | |
1212 | code << "_item_result_ = false;" << endl; | |
1213 | code << " if (" << triggerCondition << ") {" << endl; | |
1214 | code << " ++fCounter[" << i << "];" << endl; | |
1215 | const char* indentation = ""; | |
1216 | if (item->PreScalar() != 0) | |
1217 | { | |
1218 | indentation = " "; | |
1219 | code << " if ((fCounter[" << i << "] % " << item->PreScalar() << ") == 1) {" << endl; | |
1220 | } | |
1221 | code << indentation << " _item_result_ = true;" << endl; | |
1222 | if (fDebugMode) | |
1223 | { | |
1224 | code << indentation << " HLTDebug(Form(\"Matched trigger condition " << i | |
1225 | << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));" << endl; | |
1226 | } | |
1227 | if (item->PreScalar() != 0) | |
1228 | { | |
1229 | code << " }" << endl; | |
1230 | } | |
1231 | code << " }" << endl; | |
1232 | if (m == 0) | |
1233 | { | |
1234 | // Since this is the first item of the trigger group, | |
1235 | // the generated trigger logic can be simplified a little. | |
1236 | code << " _group_result_ = _item_result_;" << endl; | |
1237 | code << " if (_item_result_) {" << endl; | |
1238 | code << " _domain_ = " << mergeExpr.Data() << ";" << endl; | |
1239 | code << " _description_ = fMenuItemDescription" << i << ";" << endl; | |
1240 | code << " _previous_match_ = " << i << ";" << endl; | |
1241 | code << " _trigger_matched_ = true;" << endl; | |
1242 | code << " }" << endl; | |
1243 | } | |
1244 | else | |
1245 | { | |
325e5e42 | 1246 | if (conditionOperator[m-1] == "") |
81d62bb4 | 1247 | { |
1248 | code << " _group_result_ = _group_result_ " | |
1249 | << menu->DefaultConditionOperator() << " _item_result_;" << endl; | |
1250 | } | |
1251 | else | |
1252 | { | |
325e5e42 | 1253 | code << " _group_result_ = _group_result_ " |
1254 | << conditionOperator[m-1] << " _item_result_;" << endl; | |
81d62bb4 | 1255 | } |
1256 | code << " if (_item_result_) {" << endl; | |
1257 | code << " if (_trigger_matched_) {" << endl; | |
325e5e42 | 1258 | bool switchWillBeEmpty = true; |
81d62bb4 | 1259 | for (size_t k = 0; k < m; k++) |
1260 | { | |
1261 | if (domainOperator[k] == "") continue; | |
1262 | switchWillBeEmpty = false; | |
1263 | } | |
1264 | if (switchWillBeEmpty) | |
1265 | { | |
1266 | code << " _domain_ = _domain_ " << menu->DefaultDomainOperator() << " " | |
1267 | << mergeExpr.Data() << ";" << endl; | |
1268 | } | |
1269 | else | |
1270 | { | |
1271 | code << " switch(_previous_match_) {" << endl; | |
1272 | for (size_t k = 0; k < m; k++) | |
1273 | { | |
1274 | if (domainOperator[k] == "") continue; | |
1275 | code << " case " << k << ": _domain_ = _domain_ " | |
1276 | << domainOperator[k] << " " << mergeExpr.Data() << "; break;" << endl; | |
1277 | } | |
1278 | code << " default: _domain_ = _domain_ " | |
1279 | << menu->DefaultDomainOperator() << " " << mergeExpr.Data() << ";" << endl; | |
1280 | code << " }" << endl; | |
1281 | } | |
1282 | code << " _description_ += \",\";" << endl; | |
1283 | code << " _description_ += fMenuItemDescription" << i << ";" << endl; | |
1284 | code << " } else {" << endl; | |
1285 | code << " _domain_ = " << mergeExpr.Data() << ";" << endl; | |
1286 | code << " _description_ = fMenuItemDescription" << i << ";" << endl; | |
1287 | code << " }" << endl; | |
1288 | code << " _previous_match_ = " << i << ";" << endl; | |
1289 | code << " _trigger_matched_ = true;" << endl; | |
1290 | code << " }" << endl; | |
1291 | } | |
52f67e50 | 1292 | } |
81d62bb4 | 1293 | code << " if (_group_result_) {" << endl; |
52f67e50 | 1294 | if (fDebugMode) |
1295 | { | |
81d62bb4 | 1296 | if (n < priorities.size() - 1) |
1297 | { | |
1298 | code << " HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n] | |
1299 | << ". Stopping processing here because all other trigger groups have lower priority.\"));" << endl; | |
1300 | } | |
1301 | else | |
1302 | { | |
1303 | code << " HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n] << ".\"));" << endl; | |
1304 | } | |
52f67e50 | 1305 | } |
81d62bb4 | 1306 | code << " return true;" << endl; |
52f67e50 | 1307 | code << " }" << endl; |
1308 | } | |
81d62bb4 | 1309 | code << " _domain_.Clear();" << endl; |
1310 | code << " _description_ = \"\";" << endl; | |
1311 | code << " return false;" << endl; | |
e2bb8ddd | 1312 | code << " }" << endl; |
52f67e50 | 1313 | |
81d62bb4 | 1314 | // Generate getter and setter methods for the counters. |
1315 | code << " const TArrayL64& GetCounters() const { return fCounter; }" << endl; | |
1316 | code << " void SetCounters(const TArrayL64& counters) { fCounter = counters; }" << endl; | |
52f67e50 | 1317 | |
e2bb8ddd | 1318 | code << "private:" << endl; |
52f67e50 | 1319 | // Add the symbols in the trigger menu to the list of private variables. |
1320 | for (Int_t i = 0; i < symbols.GetEntriesFast(); i++) | |
1321 | { | |
1322 | AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) ); | |
1323 | code << " " << symbol->Type() << " " << symbol->Name() << ";" << endl; | |
1324 | if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) | |
1325 | { | |
1326 | code << " AliHLTTriggerDomain " << symbol->Name() << "TriggerDomain;" << endl; | |
1327 | } | |
1328 | code << " AliHLTDomainEntry " << symbol->Name() << "DomainEntry;" << endl; | |
1329 | } | |
1330 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) | |
1331 | { | |
1332 | code << " TString fMenuItemDescription" << i << ";" << endl; | |
1333 | } | |
81d62bb4 | 1334 | code << " TArrayL64 fCounter;" << endl; |
1335 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; | |
1336 | code << " ClassDef(" << name.Data() << ", 0)" << endl; | |
1337 | code << "#else" << endl; | |
1338 | code << " virtual const char* Class_Name() const { return \"" << name.Data() << "\"; }" << endl; | |
1339 | code << "#endif" << endl; | |
e2bb8ddd | 1340 | code << "};" << endl; |
81d62bb4 | 1341 | code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl; |
1342 | code << "ClassImp(" << name.Data() << ")" << endl; | |
1343 | code << "#endif" << endl; | |
e2bb8ddd | 1344 | |
1345 | code.close(); | |
1346 | ||
52f67e50 | 1347 | // Now we need to compile and load the new class. |
81d62bb4 | 1348 | result = LoadTriggerClass(filename, includePaths); |
52f67e50 | 1349 | return result; |
1350 | } | |
1351 | ||
1352 | ||
1353 | int AliHLTGlobalTriggerComponent::LoadTriggerClass( | |
1354 | const char* filename, const TClonesArray& includePaths | |
1355 | ) | |
1356 | { | |
1357 | // Loads the code for a custom global trigger class implementation on the fly. | |
1358 | ||
81d62bb4 | 1359 | HLTDebug("Loading HLT trigger class from file '%s'.", filename); |
1360 | ||
52f67e50 | 1361 | TString compiler = gSystem->GetBuildCompilerVersion(); |
566a01d0 | 1362 | if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc"))) |
1363 | { | |
1364 | TString includePath; | |
1365 | #if defined(PKGINCLUDEDIR) | |
1366 | // this is especially for the HLT build system where the package is installed | |
1367 | // in a specific directory including proper treatment of include files | |
1368 | includePath.Form("-I%s", PKGINCLUDEDIR); | |
1369 | #else | |
1370 | // the default AliRoot behavior, all include files can be found in the | |
1371 | // $ALICE_ROOT subfolders | |
1372 | includePath = "-I${ALICE_ROOT}/include -I${ALICE_ROOT}/HLT/BASE -I${ALICE_ROOT}/HLT/trigger"; | |
1373 | #endif | |
52f67e50 | 1374 | // Add any include paths that were specified on the command line. |
1375 | for (Int_t i = 0; i < includePaths.GetEntriesFast(); i++) | |
1376 | { | |
7da191b6 | 1377 | TString path = static_cast<TObjString*>(includePaths.UncheckedAt(i))->String(); |
81d62bb4 | 1378 | includePath += " -I"; |
52f67e50 | 1379 | includePath += path; |
1380 | } | |
81d62bb4 | 1381 | HLTDebug("using include settings: %s", includePath.Data()); |
52f67e50 | 1382 | gSystem->SetIncludePath(includePath); |
1383 | gSystem->SetFlagsOpt("-O3 -DNDEBUG"); | |
1384 | gSystem->SetFlagsDebug("-g3 -DDEBUG -D__DEBUG"); | |
1385 | ||
1386 | int result = kTRUE; | |
1387 | if (fDebugMode) | |
1388 | { | |
1389 | result = gSystem->CompileMacro(filename, "g"); | |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | result = gSystem->CompileMacro(filename, "O"); | |
1394 | } | |
1395 | if (result != kTRUE) | |
1396 | { | |
1397 | HLTFatal("Could not compile and load global trigger menu implementation."); | |
1398 | return -ENOENT; | |
1399 | } | |
1400 | } | |
1401 | else | |
1402 | { | |
1b820a65 | 1403 | // Store the library state to be checked later in UnloadTriggerClass. |
1404 | fLibStateAtLoad = gSystem->GetLibraries(); | |
1405 | ||
52f67e50 | 1406 | // If we do not support the compiler then try interpret the class instead. |
81d62bb4 | 1407 | TString cmd = ".L "; |
52f67e50 | 1408 | cmd += filename; |
1409 | Int_t errorcode = TInterpreter::kNoError; | |
1410 | gROOT->ProcessLine(cmd, &errorcode); | |
1411 | if (errorcode != TInterpreter::kNoError) | |
1412 | { | |
1413 | HLTFatal("Could not load interpreted global trigger menu implementation" | |
1414 | " (Interpreter error code = %d).", | |
1415 | errorcode | |
1416 | ); | |
1417 | return -ENOENT; | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | return 0; | |
1422 | } | |
1423 | ||
1424 | ||
81d62bb4 | 1425 | int AliHLTGlobalTriggerComponent::UnloadTriggerClass(const char* filename) |
1426 | { | |
1427 | // Unloads the code previously loaded by LoadTriggerClass. | |
1428 | ||
1429 | HLTDebug("Unloading HLT trigger class in file '%s'.", filename); | |
1430 | ||
1431 | TString compiler = gSystem->GetBuildCompilerVersion(); | |
1432 | if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc"))) | |
1433 | { | |
1b820a65 | 1434 | // Generate the library name. |
81d62bb4 | 1435 | TString libname = filename; |
1436 | Ssiz_t dotpos = libname.Last('.'); | |
1437 | if (0 <= dotpos and dotpos < libname.Length()) libname[dotpos] = '_'; | |
1438 | libname += "."; | |
1439 | libname += gSystem->GetSoExt(); | |
1440 | ||
1b820a65 | 1441 | // This is a workaround for a problem with unloading shared libraries in ROOT. |
1442 | // If the trigger logic library is loaded before the libAliHLTHOMER.so library | |
1443 | // or any other library is loaded afterwards, then during the gInterpreter->UnloadFile | |
1444 | // call all the subsequent libraries get unloded. This means that any objects created | |
1445 | // from classes implemented in the libAliHLTHOMER.so library will generate segfaults | |
1446 | // since the executable code has been unloaded. | |
1447 | // We need to check if there are any more libraries loaded after the class we | |
1448 | // are unloading and in that case don't unload the class. | |
1449 | TString libstring = gSystem->GetLibraries(); | |
1450 | TString token, lastlib; | |
1451 | Ssiz_t from = 0; | |
1452 | Int_t numOfLibs = 0, posOfLib = -1; | |
1453 | while (libstring.Tokenize(token, from, " ")) | |
1454 | { | |
1455 | ++numOfLibs; | |
1456 | lastlib = token; | |
1457 | if (token.Contains(libname)) posOfLib = numOfLibs; | |
1458 | } | |
1459 | if (numOfLibs != posOfLib) | |
1460 | { | |
1461 | HLTWarning(Form("ROOT limitation! Cannot properly cleanup and unload the shared" | |
1462 | " library '%s' since another library '%s' was loaded afterwards. Trying to" | |
1463 | " unload this library will remove the others and lead to serious memory faults.", | |
1464 | libname.Data(), lastlib.Data() | |
1465 | )); | |
1466 | return 0; | |
1467 | } | |
1468 | ||
81d62bb4 | 1469 | char* path = NULL; |
1470 | int result = 0; | |
1471 | if ((path = gSystem->DynamicPathName(libname)) != NULL) | |
1472 | { | |
1473 | result = gInterpreter->UnloadFile(path); | |
1474 | delete [] path; | |
1475 | } | |
1476 | if (result != TInterpreter::kNoError) return -ENOENT; | |
1477 | } | |
1478 | else | |
1479 | { | |
1b820a65 | 1480 | // This is again a workaround for the problem with unloading files in ROOT. |
1481 | // If the trigger logic class is loaded before the libAliHLTHOMER.so library | |
1482 | // or any other library is loaded afterwards, then during the gInterpreter->UnloadFile | |
1483 | // call all the subsequent libraries get unloded. | |
1484 | // We need to check if the list of loaded libraries has changed since the last | |
1485 | // call to LoadTriggerClass. If it has then don't unload the class. | |
1486 | if (fLibStateAtLoad != gSystem->GetLibraries()) | |
1487 | { | |
1488 | TString libstring = gSystem->GetLibraries(); | |
1489 | TString token; | |
1490 | Ssiz_t from = 0; | |
1491 | while (libstring.Tokenize(token, from, " ")) | |
1492 | { | |
1493 | if (not fLibStateAtLoad.Contains(token)) break; | |
1494 | } | |
1495 | HLTWarning(Form("ROOT limitation! Cannot properly cleanup and unload the file" | |
1496 | " '%s' since another library '%s' was loaded afterwards. Trying to unload" | |
1497 | " this file will remove the other library and lead to serious memory faults.", | |
1498 | filename, token.Data() | |
1499 | )); | |
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | // If we did not compile the trigger logic then remove the interpreted class. | |
81d62bb4 | 1504 | TString cmd = ".U "; |
1505 | cmd += filename; | |
1506 | Int_t errorcode = TInterpreter::kNoError; | |
1507 | gROOT->ProcessLine(cmd, &errorcode); | |
1508 | if (errorcode != TInterpreter::kNoError) | |
1509 | { | |
1510 | HLTFatal("Could not unload interpreted global trigger menu implementation" | |
1511 | " (Interpreter error code = %d).", | |
1512 | errorcode | |
1513 | ); | |
1514 | return -ENOENT; | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | return 0; | |
1519 | } | |
1520 | ||
1521 | ||
52f67e50 | 1522 | int AliHLTGlobalTriggerComponent::FindSymbol(const char* name, const TClonesArray& list) |
1523 | { | |
1524 | // Searches for the named symbol in the given list. | |
1525 | // See header for more details. | |
1526 | ||
1527 | for (int i = 0; i < list.GetEntriesFast(); i++) | |
1528 | { | |
1529 | const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const AliHLTTriggerMenuSymbol*>( list.UncheckedAt(i) ); | |
1530 | if (symbol == NULL) continue; | |
1531 | if (strcmp(symbol->Name(), name) == 0) return i; | |
1532 | } | |
1533 | return -1; | |
1534 | } | |
1535 | ||
1536 | ||
1537 | int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu, TClonesArray& list) | |
1538 | { | |
1539 | // Builds the list of symbols to use in the custom global trigger menu | |
1540 | // implementation class. | |
1541 | // See header for more details. | |
1542 | ||
025443e0 | 1543 | // Note: when we build the symbol list we must use the symbol name as returned |
1544 | // by the Name() method and not the RealName() method when using FindSymbol. | |
1545 | // This is so that we avoid problems with the generated code not compiling | |
1546 | // because names like "abc-xyz" and "abc_xyz" are synonymous. | |
1547 | // Name() returns the converted C++ symbol name as used in the generated code. | |
1548 | ||
52f67e50 | 1549 | for (UInt_t i = 0; i < menu->NumberOfSymbols(); i++) |
1550 | { | |
1551 | const AliHLTTriggerMenuSymbol* symbol = menu->Symbol(i); | |
1552 | if (FindSymbol(symbol->Name(), list) != -1) | |
1553 | { | |
1554 | HLTError("Multiple symbols with the name '%s' defined in the trigger menu.", symbol->Name()); | |
1555 | return -EIO; | |
1556 | } | |
dda123c3 | 1557 | try |
1558 | { | |
1559 | new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(*symbol); | |
1560 | } | |
1561 | catch (const std::bad_alloc&) | |
1562 | { | |
1563 | HLTError("Could not allocate more memory for the symbols list when adding a trigger menu symbol."); | |
1564 | return -ENOMEM; | |
1565 | } | |
52f67e50 | 1566 | } |
1567 | ||
f48dcb4e | 1568 | TRegexp exp("[_a-zA-Z][-_a-zA-Z0-9]*"); |
3da1c6d7 | 1569 | TRegexp hexexp("x[a-fA-F0-9]+"); |
52f67e50 | 1570 | for (UInt_t i = 0; i < menu->NumberOfItems(); i++) |
1571 | { | |
1572 | const AliHLTTriggerMenuItem* item = menu->Item(i); | |
81d62bb4 | 1573 | TString str = item->TriggerCondition(); |
52f67e50 | 1574 | Ssiz_t start = 0; |
1575 | do | |
1576 | { | |
1577 | Ssiz_t length = 0; | |
1578 | Ssiz_t pos = exp.Index(str, &length, start); | |
1579 | if (pos == kNPOS) break; | |
52f67e50 | 1580 | start = pos+length; |
1581 | ||
3da1c6d7 | 1582 | // Check if there is a numerical character before the found |
1583 | // regular expression. If so, then the symbol is not a valid one | |
1584 | // and should be skipped. | |
1585 | if (pos > 0) | |
1586 | { | |
1587 | bool notValid = false; | |
1588 | switch (str[pos-1]) | |
1589 | { | |
1590 | case '0': case '1': case '2': case '3': case '4': | |
1591 | case '5': case '6': case '7': case '8': case '9': | |
1592 | notValid = true; | |
1593 | break; | |
1594 | default: | |
1595 | notValid = false; | |
1596 | break; | |
1597 | } | |
1598 | if (notValid) continue; | |
1599 | } | |
1600 | TString s = str(pos, length); | |
1601 | ||
52f67e50 | 1602 | if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or |
1603 | s == "compl" or s == "not" or s == "not_eq" or s == "or" or | |
1604 | s == "or_eq" or s == "xor" or s == "xor_eq" or s == "true" or | |
1605 | s == "false" | |
1606 | ) | |
1607 | { | |
1608 | // Ignore iso646.h and other keywords. | |
1609 | continue; | |
1610 | } | |
3da1c6d7 | 1611 | |
025443e0 | 1612 | // Need to create the symbols first and check if its name is in the list |
1613 | // before actually adding it to the symbols list. | |
1614 | AliHLTTriggerMenuSymbol newSymbol; | |
1615 | newSymbol.Name(s.Data()); | |
1616 | newSymbol.Type("bool"); | |
1617 | newSymbol.ObjectClass("AliHLTTriggerDecision"); | |
1618 | newSymbol.AssignExpression("this->Result()"); | |
1619 | newSymbol.DefaultValue("false"); | |
1620 | if (FindSymbol(newSymbol.Name(), list) == -1) | |
52f67e50 | 1621 | { |
dda123c3 | 1622 | try |
1623 | { | |
1624 | new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(newSymbol); | |
1625 | } | |
1626 | catch (const std::bad_alloc&) | |
1627 | { | |
1628 | HLTError("Could not allocate more memory for the symbols list when adding a trigger name symbol."); | |
1629 | return -ENOMEM; | |
1630 | } | |
52f67e50 | 1631 | } |
1632 | } | |
1633 | while (start < str.Length()); | |
1634 | } | |
1635 | ||
e2bb8ddd | 1636 | return 0; |
1637 | } | |
1638 | ||
81d62bb4 | 1639 | |
1640 | bool AliHLTGlobalTriggerComponent::ExtractedOperator(TString& expr, TString& op) | |
1641 | { | |
1642 | // Extracts the trailing operator from the expression. | |
1643 | ||
1644 | Ssiz_t i = 0; | |
1645 | // First skip the trailing whitespace. | |
1646 | bool whitespace = true; | |
1647 | for (i = expr.Length()-1; i >= 0 and whitespace; i--) | |
1648 | { | |
1649 | switch (expr[i]) | |
1650 | { | |
1651 | case ' ': case '\t': case '\r': case '\n': | |
1652 | whitespace = true; | |
1653 | break; | |
1654 | default: | |
1655 | whitespace = false; | |
1656 | } | |
1657 | } | |
1658 | if (i < 0 or whitespace) return false; | |
1659 | ||
1660 | // Now find the first whitespace character before the trailing symbol. | |
1661 | bool nonwhitespace = true; | |
1662 | for (; i >= 0 and nonwhitespace; i--) | |
1663 | { | |
1664 | switch (expr[i]) | |
1665 | { | |
1666 | case ' ': case '\t': case '\r': case '\n': | |
1667 | nonwhitespace = false; | |
1668 | break; | |
1669 | default: | |
1670 | nonwhitespace = true; | |
1671 | } | |
1672 | } | |
1673 | if (i < 0 or nonwhitespace) return false; | |
1674 | ||
1675 | // Extract the last symbols and check if it is a valid operator. | |
1676 | TString s = expr; | |
1677 | s.Remove(0, i+2); | |
1678 | if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or | |
1679 | s == "compl" or s == "not" or s == "not_eq" or s == "or" or | |
1680 | s == "or_eq" or s == "xor" or s == "xor_eq" or s == "&&" or | |
1681 | s == "&=" or s == "&" or s == "|" or s == "~" or s == "!" or | |
1682 | s == "!=" or s == "||" or s == "|=" or s == "^" or s == "^=" or | |
1683 | s == "==" or s == "+" or s == "-" or s == "*" or s == "/" or | |
1684 | s == "%" or s == ">" or s == "<" or s == ">=" or s == "<=" | |
1685 | ) | |
1686 | { | |
1687 | expr.Remove(i+1); | |
1688 | op = s; | |
1689 | return true; | |
1690 | } | |
1691 | ||
1692 | return false; | |
1693 | } | |
1694 | ||
1695 | ||
2974f8dc | 1696 | int AliHLTGlobalTriggerComponent::PrintStatistics(const AliHLTGlobalTrigger* pTrigger, AliHLTComponentLogSeverity level, int offset) const |
1697 | { | |
1698 | // print some statistics | |
8b29f84f | 1699 | int totalEvents=GetEventCount()+offset; |
325e5e42 | 1700 | const TArrayL64& counters = pTrigger->GetCounters(); |
1701 | if (pTrigger->CallFailed()) return -EPROTO; | |
1702 | for (int i = 0; i < counters.GetSize(); i++) { | |
1703 | ULong64_t count = counters[i]; | |
8b29f84f | 1704 | float ratio=0; |
1705 | if (totalEvents>0) ratio=100*(float)count/totalEvents; | |
81d62bb4 | 1706 | HLTLog(level, "Item %d: total events: %d - counted events: %llu (%.1f%%)", i, totalEvents, count, ratio); |
2974f8dc | 1707 | } |
2974f8dc | 1708 | return 0; |
1709 | } | |
566a01d0 | 1710 | |
1711 | int AliHLTGlobalTriggerComponent::AddCTPDecisions(AliHLTGlobalTrigger* pTrigger, const AliHLTCTPData* pCTPData, const AliHLTComponentTriggerData* trigData) | |
1712 | { | |
1713 | // add trigger decisions for the valid CTP classes | |
1714 | if (!pCTPData || !pTrigger) return 0; | |
1715 | ||
1716 | AliHLTUInt64_t triggerMask=pCTPData->Mask(); | |
1717 | AliHLTUInt64_t bit0=0x1; | |
1718 | if (!fCTPDecisions) { | |
dda123c3 | 1719 | try |
1720 | { | |
1721 | fCTPDecisions=new TClonesArray(AliHLTTriggerDecision::Class(), gkNCTPTriggerClasses); | |
1722 | } | |
1723 | catch (const std::bad_alloc&) | |
1724 | { | |
1725 | HLTError("Could not allocate memory for the CTP decisions array."); | |
1726 | return -ENOMEM; | |
1727 | } | |
566a01d0 | 1728 | if (!fCTPDecisions) return -ENOMEM; |
1729 | ||
dda123c3 | 1730 | try |
1731 | { | |
1732 | fCTPDecisions->ExpandCreate(gkNCTPTriggerClasses); | |
1733 | } | |
1734 | catch (const std::bad_alloc&) | |
1735 | { | |
1736 | HLTError("Could not allocate more memory for the CTP decisions array."); | |
1737 | return -ENOMEM; | |
1738 | } | |
566a01d0 | 1739 | for (int i=0; i<gkNCTPTriggerClasses; i++) { |
1740 | const char* name=pCTPData->Name(i); | |
1741 | if (triggerMask&(bit0<<i) && name) { | |
1742 | AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i)); | |
1743 | assert(pDecision); | |
1744 | if (!pDecision) { | |
1745 | delete fCTPDecisions; | |
1746 | fCTPDecisions=NULL; | |
1747 | return -ENOENT; | |
1748 | } | |
1749 | pDecision->Name(name); | |
1750 | } | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | for (int i=0; i<gkNCTPTriggerClasses; i++) { | |
1755 | const char* name=pCTPData->Name(i); | |
1756 | if ((triggerMask&(bit0<<i))==0 || name==NULL) continue; | |
1757 | AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i)); | |
1758 | HLTDebug("updating CTP trigger decision %d %s (%p casted %p)", i, name, fCTPDecisions->At(i), pDecision); | |
1759 | if (!pDecision) return -ENOENT; | |
1760 | ||
1761 | bool result=false; | |
2f251ae6 | 1762 | // 13 March 2010 - Optimisation: |
1763 | // Dont use the EvaluateCTPTriggerClass method, which uses slow TFormula objects. | |
1764 | AliHLTUInt64_t triggers = 0; | |
1765 | if (trigData) triggers = pCTPData->ActiveTriggers(*trigData); | |
1766 | else triggers = pCTPData->Triggers(); | |
1767 | result = (triggers&((AliHLTUInt64_t)0x1<<i)) ? true : false; | |
1768 | //if (trigData) result=pCTPData->EvaluateCTPTriggerClass(name, *trigData); | |
1769 | //else result=pCTPData->EvaluateCTPTriggerClass(name); | |
566a01d0 | 1770 | pDecision->Result(result); |
1771 | pDecision->TriggerDomain().Clear(); | |
1772 | if (trigData) pDecision->TriggerDomain().Add(pCTPData->ReadoutList(*trigData)); | |
1773 | else pDecision->TriggerDomain().Add(pCTPData->ReadoutList()); | |
1774 | ||
1775 | pTrigger->Add(fCTPDecisions->At(i), kAliHLTDataTypeTriggerDecision, kAliHLTVoidDataSpec); | |
325e5e42 | 1776 | if (pTrigger->CallFailed()) return -EPROTO; |
566a01d0 | 1777 | } |
1778 | ||
1779 | return 0; | |
1780 | } |