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