1bffc87e6c8dbdaba80c8ee55734d3598a5b29e2
[u/mrichter/AliRoot.git] / HLT / trigger / AliHLTGlobalTriggerWrapper.cxx
1 // $Id:  $
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   AliHLTGlobalTriggerWrapper.cxx
19 /// @author Artur Szostak <artursz@iafrica.com>
20 /// @date   28 Oct 2009
21 /// @brief  Implementation of the AliHLTGlobalTriggerWrapper interface class.
22 ///
23 /// The AliHLTGlobalTriggerWrapper class is used to interface with an interpreted
24 /// class deriving from AliHLTGlobalTrigger. This is used when the global trigger
25 /// component is using CINT to interpret the trigger logic. The wrapper is necessary
26 /// to be able to call interpreted code from compiled code.
27
28 #include "AliHLTGlobalTriggerWrapper.h"
29 #include "TArrayL64.h"
30 #include "TClass.h"
31 #include "TInterpreter.h"
32
33 #ifdef R__BUILDING_CINT7
34 #include "cint7/Api.h"
35 #else
36 #include "Api.h"
37 #endif
38
39 ClassImp(AliHLTGlobalTriggerWrapper)
40
41
42 namespace
43 {
44   /// Variable to store the error message if an error occured in CINT when interpreting code.
45   TString gCINTErrorMessage = "";
46   
47   /**
48    * This routine is the callback for the CINT interpreter when it finds a syntax error
49    * in the source code generated by the AliHLTGlobalTriggerComponent class.
50    */
51   void AliHLTOnErrorInCINT(char* message)
52   {
53     gCINTErrorMessage += message;
54   }
55
56 } // end of namespace
57
58
59 AliHLTGlobalTriggerWrapper::AliHLTGlobalTriggerWrapper(const char* classname) :
60   AliHLTGlobalTrigger(),
61   AliHLTLogging(),
62   fClass(NULL),
63   fObject(NULL),
64   fFillFromMenuCall(),
65   fNewEventCall(),
66   fAddCall(),
67   fCalculateTriggerDecisionCall(),
68   fGetCountersCall(),
69   fSetCountersCall(),
70   fCallFailed(false)
71 {
72   // The default constructor.
73   
74   fClass = TClass::GetClass(classname);
75   if (fClass == NULL)
76   {
77     HLTError("Could not find class information for '%s'.", classname);
78     return;
79   }
80   fFillFromMenuCall.InitWithPrototype(fClass, "FillFromMenu", "const AliHLTTriggerMenu&");
81   if (not fFillFromMenuCall.IsValid())
82   {
83     HLTError("Could not initialise method call object for class '%s' and method FillFromMenu.", classname);
84     return;
85   }
86   fNewEventCall.InitWithPrototype(fClass, "NewEvent", " ");  // Must have single whitespace in last parameter or we get a segfault in G__interpret_func.
87   if (not fNewEventCall.IsValid())
88   {
89     HLTError("Could not initialise method call object for class '%s' and method NewEvent.", classname);
90     return;
91   }
92   fAddCall.InitWithPrototype(fClass, "Add", "const TObject*, const AliHLTComponentDataType&, AliHLTUInt32_t");
93   if (not fAddCall.IsValid())
94   {
95     HLTError("Could not initialise method call object for class '%s' and method Add.", classname);
96     return;
97   }
98   fCalculateTriggerDecisionCall.InitWithPrototype(fClass, "CalculateTriggerDecision", "AliHLTTriggerDomain&, TString&");
99   if (not fCalculateTriggerDecisionCall.IsValid())
100   {
101     HLTError("Could not initialise method call object for class '%s' and method CalculateTriggerDecision.", classname);
102     return;
103   }
104   fGetCountersCall.InitWithPrototype(fClass, "GetCounters", " ");  // Must have single whitespace in last parameter or we get a segfault in G__interpret_func.
105   if (not fGetCountersCall.IsValid())
106   {
107     HLTError("Could not initialise method call object for class '%s' and method GetCounters.", classname);
108     return;
109   }
110   fSetCountersCall.InitWithPrototype(fClass, "SetCounters", "TArrayL64&");
111   if (not fSetCountersCall.IsValid())
112   {
113     HLTError("Could not initialise method call object for class '%s' and method SetCounters.", classname);
114     return;
115   }
116   fObject = fClass->New();
117   if (fObject == NULL)
118   {
119     HLTError("Could not create a new object of type '%s'.", classname);
120   }
121   
122   G__set_errmsgcallback(reinterpret_cast<void*>(AliHLTOnErrorInCINT));
123 }
124
125
126 AliHLTGlobalTriggerWrapper::~AliHLTGlobalTriggerWrapper()
127 {
128   // Default destructor.
129   
130   fClass->Destructor(fObject);
131 }
132
133
134 void AliHLTGlobalTriggerWrapper::FillFromMenu(const AliHLTTriggerMenu& menu)
135 {
136   // Fills internal values from the trigger menu.
137   
138   fCallFailed = false;
139   struct Params
140   {
141     const void* fMenu;
142   } params;
143   params.fMenu = &menu;
144   fFillFromMenuCall.SetParamPtrs(&params, 1);
145   gCINTErrorMessage = "";
146   fFillFromMenuCall.Execute(fObject);
147   if (gCINTErrorMessage != "")
148   {
149     fCallFailed = true;
150     HLTError(gCINTErrorMessage.Data());
151     HLTFatal("Error interpreting the code for class '%s' at line %d.", fClass->GetName(), G__lasterror_linenum());
152   }
153 }
154
155
156 void AliHLTGlobalTriggerWrapper::NewEvent()
157 {
158   // Clears the internal buffers for a new event.
159
160   fCallFailed = false;
161   gCINTErrorMessage = "";
162   fNewEventCall.Execute(fObject);
163   if (gCINTErrorMessage != "")
164   {
165     fCallFailed = true;
166     HLTError(gCINTErrorMessage.Data());
167     HLTFatal("Error interpreting the code for class '%s' at line %d.", fClass->GetName(), G__lasterror_linenum());
168   }
169 }
170
171
172 void AliHLTGlobalTriggerWrapper::Add(
173       const TObject* object, const AliHLTComponentDataType& type,
174       AliHLTUInt32_t spec
175     )
176 {
177   // Adds parameters from the object to the internal buffers and variables.
178   
179   fCallFailed = false;
180   struct Params
181   {
182     const void* fObj;
183     const void* fType;
184     long fSpec;
185   } params;
186   params.fObj = object;
187   params.fType = &type;
188   params.fSpec = spec;
189   fAddCall.SetParamPtrs(&params, 3);
190   gCINTErrorMessage = "";
191   fAddCall.Execute(fObject);
192   if (gCINTErrorMessage != "")
193   {
194     fCallFailed = true;
195     HLTError(gCINTErrorMessage.Data());
196     HLTFatal("Error interpreting the code for class '%s' at line %d.", fClass->GetName(), G__lasterror_linenum());
197   }
198 }
199
200
201 bool AliHLTGlobalTriggerWrapper::CalculateTriggerDecision(AliHLTTriggerDomain& domain, TString& description)
202 {
203   // Calculates the global trigger decision.
204
205   fCallFailed = false;
206   struct Params
207   {
208     const void* fDomain;
209     const void* fDesc;
210   } params;
211   params.fDomain = &domain;
212   params.fDesc = &description;
213   fCalculateTriggerDecisionCall.SetParamPtrs(&params, 2);
214   Long_t retval;
215   gCINTErrorMessage = "";
216   fCalculateTriggerDecisionCall.Execute(fObject, retval);
217   if (gCINTErrorMessage != "")
218   {
219     fCallFailed = true;
220     HLTError(gCINTErrorMessage.Data());
221     HLTFatal("Error interpreting the code for class '%s' at line %d.", fClass->GetName(), G__lasterror_linenum());
222     return false;
223   }
224   return bool(retval);
225 }
226
227
228 const TArrayL64& AliHLTGlobalTriggerWrapper::GetCounters() const
229 {
230   // Returns the internal counter array.
231
232   Long_t retval = 0x0;
233   gCINTErrorMessage = "";
234   fGetCountersCall.Execute(fObject, retval);
235   if (gCINTErrorMessage != "")
236   {
237     fCallFailed = true;
238     HLTError(gCINTErrorMessage.Data());
239     HLTFatal("Error interpreting the code for class '%s' at line %d.", fClass->GetName(), G__lasterror_linenum());
240   }
241   static const TArrayL64 emptyArray;
242   const TArrayL64* ptr = &emptyArray; // Make sure we do not return a NULL pointer.
243   if (retval != 0x0) ptr = reinterpret_cast<const TArrayL64*>(retval);
244   return *ptr;
245 }
246
247
248 void AliHLTGlobalTriggerWrapper::SetCounters(const TArrayL64& counters)
249 {
250   // Fills the internal counter array with new values.
251   
252   fCallFailed = false;
253   struct Params
254   {
255     const void* fCounters;
256   } params;
257   params.fCounters = &counters;
258   fSetCountersCall.SetParamPtrs(&params, 1);
259   gCINTErrorMessage = "";
260   fSetCountersCall.Execute(fObject);
261   if (gCINTErrorMessage != "")
262   {
263     fCallFailed = true;
264     HLTError(gCINTErrorMessage.Data());
265     HLTFatal("Error interpreting the code for class '%s' at line %d.", fClass->GetName(), G__lasterror_linenum());
266   }
267 }
268
269
270 bool AliHLTGlobalTriggerWrapper::IsValid() const
271 {
272   // Checks if the wrapper class was initialised correctly.
273   
274   return fClass != NULL and fObject != NULL and fFillFromMenuCall.IsValid()
275     and fNewEventCall.IsValid() and fAddCall.IsValid()
276     and fCalculateTriggerDecisionCall.IsValid();
277 }
278