]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/global/AliHLTGlobalDCSPublisherComponent.cxx
added a protection before writing out the corr NTuple
[u/mrichter/AliRoot.git] / HLT / global / AliHLTGlobalDCSPublisherComponent.cxx
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"
27 #include "AliESDVertex.h"
28 #include "TDatime.h"
29 #include "TMath.h"
30 #include "TSystem.h"
31 #include <algorithm>
32 #include <numeric>
33
34 /** ROOT macro for the implementation of ROOT specific class methods */
35 ClassImp(AliHLTGlobalDCSPublisherComponent)
36
37 AliHLTGlobalDCSPublisherComponent::AliHLTGlobalDCSPublisherComponent()
38   : AliHLTDataSink()
39   , fServerName()
40   , fDimdns()
41   , fpServer(NULL)
42   , fPosition(0)
43   , fEventBufferSize(1000)
44   , fLastUpdate(0)
45   , fUpdatePeriod(10)
46   , fEventBuffers()
47   , fDimServices()
48   , fDimServiceEventCount(NULL)
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
55   for (int i=0; i<kLastService; i++) fDimServices[i]=NULL;
56 }
57
58 const char* AliHLTGlobalDCSPublisherComponent::fgkServiceNames[kLastService]= {
59   "Vertex_X",
60   "Vertex_Y",
61   "Vertex_Z",
62   "RmsVertex_X",
63   "RmsVertex_Y",
64   "RmsVertex_Z",
65 };
66
67 AliHLTGlobalDCSPublisherComponent::~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
75 const char* AliHLTGlobalDCSPublisherComponent::GetComponentID()
76 {
77   // see header file for class documentation
78   return "DCSPublisher";
79 }
80
81 void AliHLTGlobalDCSPublisherComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
82 {
83   // see header file for class documentation
84   list.clear();
85   list.push_back(kAliHLTAnyDataType);
86 }
87
88 AliHLTComponent* AliHLTGlobalDCSPublisherComponent::Spawn()
89 {
90   // see header file for class documentation
91   return new AliHLTGlobalDCSPublisherComponent;
92 }
93
94 int 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
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
117   return iResult;
118 }
119
120 int AliHLTGlobalDCSPublisherComponent::ScanConfigurationArgument(int argc, const char** argv)
121 {
122   // see header file for class documentation
123
124   // -servername
125   if (argc==0) return 0;
126   int i=0;
127   TString argument=argv[0];
128   if (argument.CompareTo("-servername")==0) {
129     if (++i>=argc) return -EPROTO;
130     fServerName=argv[i];
131     return 2;
132   }
133   
134   // --dimdns
135   if (argument.CompareTo("-dimdns")==0) {
136     if (++i>=argc) return -EPROTO;
137     fDimdns=argv[i];
138     return 2;
139   }
140
141   // TODO: further options for tracklet cut and event buffer size
142
143   return -EINVAL;
144 }
145
146 int AliHLTGlobalDCSPublisherComponent::DoDeinit()
147 {
148   // see header file for class documentation
149   int iResult=0;
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
155   if (!fpServer) return -ENODEV;
156   fpServer->Stop();
157   delete fpServer;
158   fpServer=NULL;
159
160   // TODO: proper cleanup of services objects to be synchronized with
161   // the server
162
163   return iResult;
164 }
165
166 int AliHLTGlobalDCSPublisherComponent::DumpEvent( const AliHLTComponentEventData& /*evtData*/,
167                                                   AliHLTComponentTriggerData& /*trigData*/ )
168 {
169   // see header file for class documentation
170   if (!IsDataEvent()) return 0;
171
172   int iResult=0;
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   }
190   return iResult;
191 }
192
193 int 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
221 void 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
228 template <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
237 template <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 }