initializing variables that were declare without a value
[u/mrichter/AliRoot.git] / HLT / global / AliHLTGlobalDCSPublisherComponent.cxx
CommitLineData
3e8728ed 1// $Id$
2
3//**************************************************************************
4//* This file is property of and copyright by the ALICE HLT Project *
5//* ALICE Experiment at CERN, All rights reserved. *
6//* *
7//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8//* for The ALICE HLT Project. *
9//* *
10//* Permission to use, copy, modify and distribute this software and its *
11//* documentation strictly for non-commercial purposes is hereby granted *
12//* without fee, provided that the above copyright notice appears in all *
13//* copies and that both the copyright notice and this permission notice *
14//* appear in the supporting documentation. The authors make no claims *
15//* about the suitability of this software for any purpose. It is *
16//* provided "as is" without express or implied warranty. *
17//**************************************************************************
18
19/** @file AliHLTGlobalDCSPublisherComponent.cxx
20 @author Matthias Richter
21 @date 20010-03-10
22 @brief DIM publisher component for global HLT data
23*/
24
25#include "AliHLTGlobalDCSPublisherComponent.h"
26#include "AliHLTDimServer.h"
f5eb29aa 27#include "AliESDVertex.h"
28#include "TDatime.h"
29#include "TMath.h"
30#include "TSystem.h"
31#include <algorithm>
32#include <numeric>
3e8728ed 33
34/** ROOT macro for the implementation of ROOT specific class methods */
35ClassImp(AliHLTGlobalDCSPublisherComponent)
36
37AliHLTGlobalDCSPublisherComponent::AliHLTGlobalDCSPublisherComponent()
38 : AliHLTDataSink()
f5eb29aa 39 , fServerName()
40 , fDimdns()
3e8728ed 41 , fpServer(NULL)
f5eb29aa 42 , fPosition(0)
43 , fEventBufferSize(1000)
44 , fLastUpdate(0)
45 , fUpdatePeriod(10)
46 , fEventBuffers()
47 , fDimServices()
48 , fDimServiceEventCount(NULL)
3e8728ed 49{
50 // see header file for class documentation
51 // or
52 // refer to README to build package
53 // or
54 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
f5eb29aa 55 for (int i=0; i<kLastService; i++) fDimServices[i]=NULL;
3e8728ed 56}
57
f5eb29aa 58const char* AliHLTGlobalDCSPublisherComponent::fgkServiceNames[kLastService]= {
59 "Vertex_X",
60 "Vertex_Y",
61 "Vertex_Z",
62 "RmsVertex_X",
63 "RmsVertex_Y",
64 "RmsVertex_Z",
65};
66
3e8728ed 67AliHLTGlobalDCSPublisherComponent::~AliHLTGlobalDCSPublisherComponent()
68{
69 // see header file for class documentation
70
71 // file list and file name list are owner of their objects and
72 // delete all the objects
73}
74
75const char* AliHLTGlobalDCSPublisherComponent::GetComponentID()
76{
77 // see header file for class documentation
78 return "DCSPublisher";
79}
80
81void AliHLTGlobalDCSPublisherComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
82{
83 // see header file for class documentation
84 list.clear();
85 list.push_back(kAliHLTAnyDataType);
86}
87
88AliHLTComponent* AliHLTGlobalDCSPublisherComponent::Spawn()
89{
90 // see header file for class documentation
91 return new AliHLTGlobalDCSPublisherComponent;
92}
93
94int AliHLTGlobalDCSPublisherComponent::DoInit( int argc, const char** argv )
95{
96 // see header file for class documentation
97 int iResult=0;
98 iResult=ConfigureFromArgumentString(argc, argv);
99
f5eb29aa 100 fpServer=new AliHLTDimServer(fServerName.c_str());
101 if (!fpServer) return -ENOMEM;
102 if ((iResult=fpServer->Init(fDimdns.c_str()))>=0) {
103 // add services
104 for (int service=0; service<kLastService; service++) {
105 fDimServices[service]=fpServer->CreateService(AliHLTDimServer::kDataTypeFloat, fgkServiceNames[service]);
106 }
107 fDimServiceEventCount=fpServer->CreateService(AliHLTDimServer::kDataTypeInt, "EventCount");
108
109 // now start the server
110 iResult=fpServer->Start();
111 }
112
113 fPosition=0;
114 for (int i=0; i<kDimensions; i++)
115 Reset(fEventBuffers[i], fEventBufferSize);
116
3e8728ed 117 return iResult;
118}
119
120int AliHLTGlobalDCSPublisherComponent::ScanConfigurationArgument(int argc, const char** argv)
121{
122 // see header file for class documentation
3e8728ed 123
124 // -servername
125 if (argc==0) return 0;
126 int i=0;
3e8728ed 127 TString argument=argv[0];
128 if (argument.CompareTo("-servername")==0) {
129 if (++i>=argc) return -EPROTO;
f5eb29aa 130 fServerName=argv[i];
131 return 2;
132 }
133
134 // --dimdns
135 if (argument.CompareTo("-dimdns")==0) {
3e8728ed 136 if (++i>=argc) return -EPROTO;
f5eb29aa 137 fDimdns=argv[i];
138 return 2;
3e8728ed 139 }
140
f5eb29aa 141 // TODO: further options for tracklet cut and event buffer size
3e8728ed 142
f5eb29aa 143 return -EINVAL;
3e8728ed 144}
145
146int AliHLTGlobalDCSPublisherComponent::DoDeinit()
147{
148 // see header file for class documentation
149 int iResult=0;
f5eb29aa 150 Publish(true);
151 // just wait a few seconds to get the updated services through before
152 // terminating the server
153 gSystem->Sleep(fUpdatePeriod<30?fUpdatePeriod:30);
154
3e8728ed 155 if (!fpServer) return -ENODEV;
156 fpServer->Stop();
157 delete fpServer;
158 fpServer=NULL;
159
f5eb29aa 160 // TODO: proper cleanup of services objects to be synchronized with
161 // the server
162
3e8728ed 163 return iResult;
164}
165
166int AliHLTGlobalDCSPublisherComponent::DumpEvent( const AliHLTComponentEventData& /*evtData*/,
167 AliHLTComponentTriggerData& /*trigData*/ )
168{
169 // see header file for class documentation
f5eb29aa 170 if (!IsDataEvent()) return 0;
171
3e8728ed 172 int iResult=0;
f5eb29aa 173 for (const TObject* pObject=GetFirstInputObject(kAliHLTDataTypeESDVertex);
174 pObject!=NULL;
175 pObject=GetNextInputObject()) {
176 const AliESDVertex* pVertex=dynamic_cast<const AliESDVertex*>(pObject);
177 if (pVertex && pVertex->GetNContributors()>=5) {
178 (fEventBuffers[kVertexX])[fPosition%fEventBuffers[kVertexX].size()]=pVertex->GetX();
179 (fEventBuffers[kVertexY])[fPosition%fEventBuffers[kVertexY].size()]=pVertex->GetY();
180 (fEventBuffers[kVertexZ])[fPosition%fEventBuffers[kVertexZ].size()]=pVertex->GetZ();
181 fPosition++;
182 }
183 }
184
185 TDatime time;
186 if ((time.Get()-fLastUpdate>fUpdatePeriod) && fPosition>0) {
187 fLastUpdate=time.Get();
188 Publish();
189 }
3e8728ed 190 return iResult;
191}
f5eb29aa 192
193int AliHLTGlobalDCSPublisherComponent::Publish(bool reset)
194{
195 // publish the values to DCS
196 float mean[kDimensions];
197 float rms[kDimensions];
198 for (int dimension=0; dimension<kDimensions; dimension++) {
199 mean[dimension]= Mean(fEventBuffers[dimension], fPosition);
200 rms[dimension] = Rms(fEventBuffers[dimension], mean[dimension], fPosition);
201 ((AliHLTDimServer::AliHLTDimServiceFloat*)fDimServices[dimension])->Update(mean[dimension]);
202 ((AliHLTDimServer::AliHLTDimServiceFloat*)fDimServices[dimension+kDimensions])->Update(rms[dimension]);
203 }
204 ((AliHLTDimServer::AliHLTDimServiceInt*)fDimServiceEventCount)->Update(fPosition<fEventBufferSize?fPosition:fEventBufferSize);
205
206 HLTInfo("Vertex from %d samples: X:Y:Z Mean %f:%f:%f RMS %f:%f:%f",
207 fPosition<fEventBufferSize?fPosition:fEventBufferSize,
208 mean[kVertexX], mean[kVertexY], mean[kVertexZ],
209 rms[kVertexX], rms[kVertexY], rms[kVertexZ]
210 );
211
212 if (reset) {
213 for (int i=0; i<kDimensions; i++)
214 Reset(fEventBuffers[i], fEventBufferSize);
215 fPosition=0;
216 }
217
218 return 0;
219}
220
221void AliHLTGlobalDCSPublisherComponent::Reset(vector<float>& sample, int size) const
222{
223 // reset the event buffer
224 if (size>0) sample.resize(size);
225 fill(sample.begin(), sample.end(), 0.0);
226}
227
228template <class T> T AliHLTGlobalDCSPublisherComponent::Mean(const vector<T>& sample, int count) const
229{
230 // calculate mean of the event buffer
231 int samplesize=count<(int)sample.size()?count:sample.size();
232 if (samplesize==0) return 0.0;
233 T sum=std::accumulate(sample.begin(), sample.begin()+samplesize, (T)0, std::plus<T>());
234 return sum/samplesize;
235}
236
237template <class T> T AliHLTGlobalDCSPublisherComponent::Rms(const vector<T>& sample, T mean, int count) const
238{
239 // calculate sigma of the event buffer
240 int samplesize=count<(int)sample.size()?count:sample.size();
241 if (samplesize==0) return 0.0;
242 T msd=std::accumulate(sample.begin(), sample.begin()+samplesize, (T)0, MeanSqtOp<T>(mean))/samplesize;
243 if (msd<0.00000001) return 0.0;
244 return TMath::Sqrt(msd);
245}