Provide return value if the function is not void
[u/mrichter/AliRoot.git] / HLT / PHOS / AliHLTPHOSBaselineAnalyzerComponent.cxx
CommitLineData
5c6503dc 1//insert copyright
2
3#include "AliHLTPHOSBaselineAnalyzerComponent.h"
4#include "AliHLTPHOSBaselineAnalyzer.h"
5#include "AliHLTPHOSRcuCellEnergyDataStruct.h"
6#include "AliHLTPHOSBaseline.h"
7#include "TTree.h"
8#include "TChain.h"
9#include "AliHLTPHOSProcessor.h"
10#include "TClonesArray.h"
11#include "TFile.h"
12//#include <direct.h>
13#include <sys/stat.h>
14#include <sys/types.h>
15
16const AliHLTComponentDataType AliHLTPHOSBaselineAnalyzerComponent::fgkInputDataTypes[]={kAliHLTVoidDataType,{0,"",""}};
17
18AliHLTPHOSBaselineAnalyzerComponent gAliHLTPHOSBaselineAnalyzerComponent;
19
20AliHLTPHOSBaselineAnalyzerComponent::AliHLTPHOSBaselineAnalyzerComponent() :
21 AliHLTPHOSProcessor(),
22 fBaselineAnalyzerPtr(0),
23 fTreePtr(0),
24 fEventCount(0),
25 fWriteInterval(100),
26 fFillInterval(100),
27 fFilename(0),
28 fDirectory(0),
29 fHistPath(0),
30 fRunNb(0)
31{
32
33}
34
35AliHLTPHOSBaselineAnalyzerComponent::~AliHLTPHOSBaselineAnalyzerComponent()
36{
37}
38
39int
40AliHLTPHOSBaselineAnalyzerComponent::Deinit()
41{
42
43 fBaselineAnalyzerPtr->CalculateChannelsBaselineRMS();
44 char filename [50];
45 cout << "Writing files...";
46 sprintf(filename, "%s/run%d_baselineTree_%d.root", fDirectory, fRunNb,fEventCount/fWriteInterval);
47 fBaselineAnalyzerPtr->WriteAccumulatedBaselines(filename);
48 sprintf(filename, "%s/run%d_channelHistograms.root", fHistPath, fRunNb);
49 fBaselineAnalyzerPtr->WriteChannelHistograms(filename);
50 sprintf(filename, "%s/run%d_RMSHistogram.root", fHistPath, fRunNb);
51 fBaselineAnalyzerPtr->WriteRMSHistogram(filename);
52 cout << "Done!\n";
53
54 if(fCalculateAll)
55 {
56 CalculateAll();
57 }
58 if(fBaselineAnalyzerPtr)
59 {
60 delete fBaselineAnalyzerPtr;
61 fBaselineAnalyzerPtr = 0;
62 }
63 if(fTreePtr)
64 {
65 // delete fTreePtr;
66 fTreePtr = 0;
67 }
68 if(fFilename)
69 {
70 delete fFilename;
71 fFilename = 0;
72 }
52be7fb0 73 return 0;
5c6503dc 74}
75
76const char*
77AliHLTPHOSBaselineAnalyzerComponent::GetComponentID()
78{
79 return "PhosBaselineAnalyzer";
80}
81
82void
83
84AliHLTPHOSBaselineAnalyzerComponent::GetInputDataTypes(vector<AliHLTComponentDataType>& list)
85{
86 //Get datatypes for input
87 const AliHLTComponentDataType* pType=fgkInputDataTypes;
88 while (pType->fID!=0) {
89 list.push_back(*pType);
90 pType++;
91 }
92}
93
94AliHLTComponentDataType
95AliHLTPHOSBaselineAnalyzerComponent::GetOutputDataType()
96{
97 return AliHLTPHOSDefinitions::fgkAliHLTBaselineDataType;
98}
99
100
101void
102AliHLTPHOSBaselineAnalyzerComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
103{
104 constBase = 30;
105 inputMultiplier = 1;
106}
107
108int
109AliHLTPHOSBaselineAnalyzerComponent::DoEvent(const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks,
110 AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size,
111 std::vector<AliHLTComponentBlockData>& outputBlocks)
112{
113 //Do event
114
115 UInt_t tSize = 0;
116 UInt_t offset = 0;
117 UInt_t mysize = 0;
118 Int_t index = 0;
119
120 Int_t fileCount = 0;
121 Int_t digitCount = 0;
122 char filename [50];
123
124
125 AliHLTUInt8_t* outBPtr;
126 outBPtr = outputPtr;
127 const AliHLTComponentBlockData* iter = 0;
128 unsigned long ndx;
129
130 for( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
131 {
132 iter = blocks+ndx;
133
134 if(iter->fDataType != AliHLTPHOSDefinitions::fgkCellEnergyDataType)
135 {
136 // cout << "Warning: data type is not fgkCellEnergyDataType " << endl;
137 continue;
138
139 }
140 fBaselineAnalyzerPtr->CalculateRcuBaselines(reinterpret_cast<AliHLTPHOSRcuCellEnergyDataStruct*>(iter->fPtr));
141 }
142
143 fEventCount++;
144
145 //PushBack(fDigitArrayPtr, kAliHLTAnyDataType, (AliHLTUInt32_t)0);
146
147 if(fEventCount % 10 == 0)
148 {
149 cout << "Event #: " << fEventCount << endl;
150 }
151
152 if(fEventCount % fFillInterval == 0)
153 {
154 fBaselineAnalyzerPtr->FillTree();
155 }
156 if(fEventCount % fWriteInterval == 0)
157 {
158 char filename [50];
159 cout << "Writing file...";
160 sprintf(filename, "%s/run%d_baselineTree_%d.root", fDirectory, fRunNb,fEventCount/fWriteInterval - 1);
161 fBaselineAnalyzerPtr->WriteAccumulatedBaselines(filename);
162 cout << "Done!\n";
163 delete fTreePtr;
164 fTreePtr = new TTree("baselineTree", "Baselines");
165 fBaselineAnalyzerPtr->SetRootObjects(fTreePtr, fBaselineArrayPtr);
166 }
167
168 return 0;
169}
170
171
172int
173AliHLTPHOSBaselineAnalyzerComponent::DoInit(int argc, const char** argv )
174{
175 //Do initialization sprintf(fFilename, "/tmp/phoshlt/analysis/data/run%d/run%d_digitTree_%d.root", fRunNb,fRunNb,(fEventCount/fWriteInterval - 1));
176
177 Bool_t pathSet = false;
178 Bool_t histPathSet = false;
179 Bool_t nSamplesSet = false;
180
181
182 fFilename = new char[50];
183 fDirectory = new char[50];
184 fHistPath = new char[50];
185
186
187 fstream runNbFile;
188 //char dir [10];
189
190 Int_t newRunNb;
191 runNbFile.open("/opt/HLT-public/rundir/runNumber.txt");
192 runNbFile >> fRunNb;
193 runNbFile.close();
194 /* newRunNb = fRunNb + 1;
195 runNbFile.open("/opt/HLT-public/rundir/runNumber.txt");
196 runNbFile << newRunNb;
197 runNbFile.close();*/
198
199 // sprintf(dir, "//tmp//phoshlt//aldaqpc019//hlt//data//baselines//run%d", fRunNb);
200 // if(mkdir(dir, 0777))
201 //{
202 // cerr << "WARNING! Could not create directory!\n";
203 //}
204
205 fBaselineAnalyzerPtr = new AliHLTPHOSBaselineAnalyzer();
206
207 fTreePtr = new TTree("baselineTree", "Baselines");
208 fBaselineArrayPtr = new TClonesArray("AliHLTPHOSBaseline",N_XCOLUMNS_MOD*N_ZROWS_MOD*N_GAINS);
209 fBaselineAnalyzerPtr->SetRootObjects(fTreePtr, fBaselineArrayPtr);
210 fBaselineAnalyzerPtr->SetMaxCrazyDifference(15);
211 fBaselineAnalyzerPtr->SetMaxSignal(120);
212
213 for(int i = 0; i < argc; i++)
214 {
215 if(!strcmp("-totalbaselineoutput", argv[i]))
216 {
217 fCalculateAll = true;
218 strcpy(fFilename, argv[i+1]);
219 }
220 if(!strcmp("-path", argv[i]))
221 {
222 strcpy(fDirectory, argv[i+1]);
223 pathSet = true;
224 }
225 if( !strcmp("-histpath", argv[i]))
226 {
227 strcpy(fHistPath, argv[i+1]);
228 histPathSet = true;
229 }
230 if(!strcmp("-nsamples", argv[i]))
231 {
232 fBaselineAnalyzerPtr->SetNumberOfSamples(atoi(argv[i+1]));
233 nSamplesSet = true;
234 }
235 if(!strcmp("-maxsignal", argv[i]))
236 {
237 fBaselineAnalyzerPtr->SetMaxSignal(atoi(argv[i+1]));
238 }
239 }
240
241
242 fWriteInterval = 100;
243 fFillInterval = 100;
244 if(fCalculateAll)
245 cout << "Path to total baseline file: " << fFilename << endl;
246 cout << endl << "Run number is: " << fRunNb << " -- Check that this is correct!!!\n";
247
248 return 0;
249}
250
251AliHLTComponent*
252AliHLTPHOSBaselineAnalyzerComponent::Spawn()
253{
254 return new AliHLTPHOSBaselineAnalyzerComponent();
255}
256
257void
258AliHLTPHOSBaselineAnalyzerComponent::CalculateAll()
259{
260
261 cout << "Calculating total baselines... \n";
262 AliHLTPHOSBaseline *baselineObject = 0;
263 TChain* chain = new TChain("baselineTree");
264 TTree* totalTree = new TTree("baselineTree", "Baselines");
265 TClonesArray* baselineArray = new TClonesArray("AliHLTPHOSBaseline", N_XCOLUMNS_MOD*N_ZROWS_MOD*N_GAINS);
266 TClonesArray* totalBaselineArray = new TClonesArray("AliHLTPHOSBaseline", N_XCOLUMNS_MOD*N_ZROWS_MOD*N_GAINS);
267
268 char filepath [50];
269
270 Float_t tmpBaselines[N_XCOLUMNS_MOD][N_ZROWS_MOD][N_GAINS][2];
271
272 for(Int_t x = 0; x < N_XCOLUMNS_MOD; x++)
273 {
274 for(Int_t z = 0; z < N_ZROWS_MOD; z++)
275 {
276 for(Int_t gain = 0; gain < N_GAINS; gain++)
277 {
278 for(Int_t d = 0; d < 2; d++)
279 {
280 tmpBaselines[x][z][gain][d] = 0;
281 }
282 }
283 }
284 }
285
286 sprintf(filepath, "%s/run%d*", fDirectory, fRunNb);
287 cout << "Adding files from: " << filepath << endl;
288 chain->Add(filepath);
289
290 cout << "Gives a total number of " << chain->GetEntries() << " events.\n";
291
292 chain->SetBranchAddress("Baselines", &baselineArray);
293 totalTree->Branch("Baselines", &totalBaselineArray);
294
295 Int_t nEntries = 0;
296 Int_t totEntries = 0;
297 Float_t baseline = 0;
298 Float_t oldBaseline = 0;
299
300 Int_t x = 0;
301 Int_t z = 0;
302 Int_t gain = 0;
303
304 for(int i = 0; i < chain->GetEntries(); i++)
305 {
306 chain->GetEntry(i);
307 for(int j = 0; j < baselineArray->GetEntriesFast(); j++)
308 {
309 baselineObject = (AliHLTPHOSBaseline*)baselineArray->At(j);
310 x = baselineObject->GetX();
311 z = baselineObject->GetZ();
312 gain = baselineObject->GetGain();
313 nEntries = baselineObject->GetEntries();
314 baseline = baselineObject->GetBaseline();
315 oldBaseline = tmpBaselines[x][z][gain][0];
316 totEntries = (Int_t)tmpBaselines[x][z][gain][1];
317 tmpBaselines[x][z][gain][0] = (oldBaseline*totEntries + baseline)/(totEntries + 1);
318 tmpBaselines[x][z][gain][1] = totEntries + 1;
319 }
320 }
321
322 Int_t n = 0;
323
324 for(x = 0; x < N_XCOLUMNS_MOD; x++)
325 {
326 for(z = 0; z < N_ZROWS_MOD; z++)
327 {
328 for(gain = 0; gain < N_GAINS; gain++)
329 {
330 baselineObject = (AliHLTPHOSBaseline*)totalBaselineArray->New(n);
331 baselineObject->SetBaseline(tmpBaselines[x][z][gain][0]);
332 baselineObject->SetX(x);
333 baselineObject->SetZ(z);
334 baselineObject->SetGain(gain);
335 if( tmpBaselines[x][z][gain][1] == 0)
336 {
337 cout << "Warning! Number of entries for x: " << x << " - z: " << z << " - gain: " << gain << " = 0\n"
338 << "Setting baseline to 40\n\n";
339 baselineObject->SetBaseline(40);
340 continue;
341 }
342 if( tmpBaselines[x][z][gain][1] == 0)
343 {
344 cout << "Warning! Number of entries for x: " << x << " - z: " << z << " - gain: " << gain << " = "
345 << tmpBaselines[x][z][gain][1] << endl;
346 }
347 baselineObject->SetEntries(tmpBaselines[x][z][gain][1]);
348 n++;
349 }
350 }
351 }
352
353 totalTree->Fill();
354
355 cout << "Writing to: " << fFilename << endl;
356 TFile *outfile = new TFile(fFilename,"recreate");
357 totalTree->Write();
358 outfile->Close();
359 cout << "Done!\n";
360
361}