]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/trigger/AliHLTTriggerCounterComponent.h
Adding trigger counting component.
[u/mrichter/AliRoot.git] / HLT / trigger / AliHLTTriggerCounterComponent.h
1 //-*- Mode: C++ -*-
2 // $Id: $
3 #ifndef ALIHLTTRIGGERCOUNTERCOMPONENT_H
4 #define ALIHLTTRIGGERCOUNTERCOMPONENT_H
5 /* This file is property of and copyright by the ALICE HLT Project        *
6  * ALICE Experiment at CERN, All rights reserved.                         *
7  * See cxx source for full Copyright notice                               */
8
9 /// \file   AliHLTTriggerCounterComponent.h
10 /// \author Artur Szostak <artursz@iafrica.com>
11 /// \date   3 Nov 2010
12 /// \brief  Declaration of the AliHLTTriggerCounterComponent component class.
13
14 #include "AliHLTProcessor.h"
15 #include "AliHLTTriggerCounters.h"
16 #include "THashTable.h"
17 #include "TTimeStamp.h"
18 #include "TMap.h"
19
20 /**
21  * \class AliHLTTriggerCounterComponent
22  * This component is used to calculate online the trigger counters and rates for
23  * output HLT and input CTP / HLT triggers. The component should be connected to
24  * all the global trigger component instances in the chain and there should be
25  * only one instance of AliHLTTriggerCounterComponent in the chain. If more instances
26  * of AliHLTTriggerCounterComponent exist in the chain then the statistics will not
27  * be calculated correctly.
28  *
29  * <h2>General properties:</h2>
30  *
31  * Component ID: \b HLTTriggerCounter <br>
32  * Library: \b libAliHLTTrigger.so   <br>
33  * Input Data Types:  kAliHLTDataTypeGlobalTrigger | kAliHLTDataTypeTriggerDecision <br>
34  * Output Data Types: kAliHLTDataTypeInputTriggerCounters | kAliHLTDataTypeOutputTriggerCounters <br>
35  *
36  * <h2>Mandatory arguments:</h2>
37  * None.
38  *
39  * <h2>Optional arguments:</h2>
40  * \li -config <i>filename</i> <br>
41  *      Indicates the configuration macro file to use for the initial trigger counter lists.
42  * \li -publishperiod <i>period</i> <br>
43  *      Sets the period between publishing of the trigger counters in seconds.
44  *      The default is to publish for every event.
45  * \li -skipcdb  <br>
46  *      If specified then the initial counter configuration is not loaded from the CDB.
47  *
48  * <h2>Configuration:</h2>
49  * Can only be configured with the command line arguments.
50  *
51  * <h2>Default CDB entries:</h2>
52  * HLT/ConfigHLT/HLTTriggerCounter - Contains the initial counter configuration.
53  *
54  * <h2>Performance:</h2>
55  * Can run over 2kHz in HLT online system.
56  *
57  * <h2>Memory consumption:</h2>
58  * Negligible.
59  *
60  * <h2>Output size:</h2>
61  * Same order of magnitude as the input data size.
62  *
63  * \ingroup alihlt_tpc_components
64  */
65 class AliHLTTriggerCounterComponent : public AliHLTProcessor
66 {
67 public:
68         
69         AliHLTTriggerCounterComponent();
70         virtual ~AliHLTTriggerCounterComponent();
71         
72         // Methods inherited from AliHLTComponent:
73         virtual const char* GetComponentID();
74         virtual void GetInputDataTypes(AliHLTComponentDataTypeList& list);
75         virtual AliHLTComponentDataType GetOutputDataType();
76         virtual int GetOutputDataTypes(AliHLTComponentDataTypeList& list);
77         virtual void GetOutputDataSize(unsigned long& constBase, double& inputMultiplier);
78         virtual AliHLTComponent* Spawn();
79         virtual Int_t DoInit(int argc, const char** argv);
80         virtual Int_t DoDeinit();
81         
82         /**
83          * Returns a reference to the initial counter configuration to use.
84          * This should be used in the configuration file passed to the component with
85          * the -config option to setup the initial counter configuration. The following
86          * is an example of such a config file.
87          * \code
88          *   void SetupConfig() {
89          *     TMap& counters = AliHLTTriggerCounterComponent::InitialCounterConfig();
90          *     counters.Add(new TObjString("TRIGGER-A"), new TObjString("Some input trigger"));
91          *     TObjString* outname = new TObjString("TRIGGER-B");
92          *     outname->SetBit(1<<14);
93          *     counters.Add(outname, new TObjString("Some output trigger"));
94          *   }
95          * \endcode
96          */
97         static TMap& InitialCounterConfig()
98         {
99                 fgInitialCounterConfig.SetOwner(kTRUE); // Make sure its the owner of all objects.
100                 return fgInitialCounterConfig;
101         }
102         
103 protected:
104         
105         // Method inherited from AliHLTProcessor:
106         virtual int DoEvent(const AliHLTComponentEventData& evtData, AliHLTComponentTriggerData& trigData);
107         using AliHLTProcessor::DoEvent;
108         
109 private:
110         
111         /// Implements a ring buffer for old counter and time stamp values.
112         class AliRingBuffer : public TObject
113         {
114         public:
115                 enum 
116                 {
117                         /// The maximum number of time stamp entries held in the ring buffers for rate calculations.
118                         /// The uncertainty in the rate should be ~ rate/sqrt(kTimeStampEntries)
119                         kTimeStampEntries = 100
120                 };
121                 
122                 /// Constructor initialises the buffer
123                 AliRingBuffer(const char* counterName, Double_t startTime) : TObject(), fCounterName(counterName), fPos(0)
124                 {
125                         for (size_t i = 0; i < kTimeStampEntries; ++i) {
126                                 fCounterBuffer[i] = 0; fTimeBuffer[i] = startTime;
127                         }
128                 }
129                 
130                 /// Inherited form TObject. Returns the counter name this buffer is associated to.
131                 virtual const char* GetName() const { return fCounterName.Data(); }
132                 
133                 /// Inherited from TObject. Returns a hash value calculated from the counter name.
134                 virtual ULong_t Hash() const { return fCounterName.Hash(); }
135                 
136                 /// Returns the oldest counter value.
137                 ULong64_t OldestCounter() const { return fCounterBuffer[fPos]; }
138                 
139                 /// Returns the oldest time value.
140                 Double_t OldestTime() const { return fTimeBuffer[fPos]; }
141                 
142                 /**
143                  * Increments the buffer. The new time and counter values are put into the
144                  * current location to replace the existing values, then the current position
145                  * is incremented (and wrapped around if necessary).
146                  * \param newCounter  The new counter value to put into the buffer.
147                  * \param newTime  The new time value to put into the buffer.
148                  */
149                 void Increment(ULong64_t newCounter, Double_t newTime)
150                 {
151                         fCounterBuffer[fPos] = newCounter;
152                         fTimeBuffer[fPos] = newTime;
153                         fPos = (fPos+1) % kTimeStampEntries;
154                 }
155                 
156         private:
157                 
158                 TString fCounterName;  // The name of the counter.
159                 UInt_t fPos;  // Current position in the buffers.
160                 ULong64_t fCounterBuffer[kTimeStampEntries];  // The counter elements of the buffer.
161                 Double_t fTimeBuffer[kTimeStampEntries];  // The time elements of the buffer.
162         };
163         
164         // Do not allow copying of this class.
165         AliHLTTriggerCounterComponent(const AliHLTTriggerCounterComponent& obj);
166         AliHLTTriggerCounterComponent& operator = (const AliHLTTriggerCounterComponent& obj);
167         
168         /// Updates the counter rate value.
169         void UpdateCounterRate(AliHLTTriggerCounters::AliCounter* counter, AliRingBuffer* timeBuf, Double_t newTime);
170         
171         /// Loads the initial configuration of counters from the given CDB path.
172         int LoadConfigFromCDB(const char* cdbPath);
173         
174         /// Loads the initial configuration of counters from the given configuration file.
175         int LoadConfigFromFile(const char* configFile);
176         
177         /**
178          * Sets the initial counter values from a TMap object containing name description pairs.
179          * \param counters  The list of counters to initialise. This TMap should contain TObjString
180          *     pairs where the key is the name of the counter and the TMap value is the description.
181          *     If the 14th bit is set in the key TObjString then the counter is considered an
182          *     output counter and an input counter otherwise.
183          */
184         void SetInitialCounters(const TMap* counters);
185         
186         double fOutputMultiplier;    //! Multiplier value for the output size estimate.
187         AliHLTTriggerCounters fInputCounters; //! Input trigger counters.
188         AliHLTTriggerCounters fOutputCounters; //! Output trigger counters.
189         THashTable fInputTimes;  //! Cyclic buffer storing the time stamps when the input counters were received.
190         THashTable fOutputTimes;  //! Cyclic buffer storing the time stamps when the output counters were received.
191         Double_t fLastPublishTime;  //! The last time the counters were pushed back to the output.
192         Double_t fPublishPeriod;  //! The time between calls to push back the counters to output.
193         
194         static const char* fgkConfigCDBPath;  //! CDB configuration path.
195         static TMap fgInitialCounterConfig;   //! Initial configuration information for the counters.
196         
197         ClassDef(AliHLTTriggerCounterComponent, 0)  // Component for counting HLT triggers.
198 };
199
200 #endif // ALIHLTTRIGGERCOUNTERCOMPONENT_H