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