]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/PHOS/AliHLTPHOSRcuHistogramProducer.cxx
0c25d08f9f59d590d4cc386ba7b288f12aa77e6a
[u/mrichter/AliRoot.git] / HLT / PHOS / AliHLTPHOSRcuHistogramProducer.cxx
1 /**************************************************************************
2  * Copyright(c) 2006, ALICE Experiment at CERN, All rights reserved.      *
3  *                                                                        *
4  * Authors: Boris Polichtchouk & Per Thomas Hille for the ALICE           *
5  * offline/HLT Project. Contributors are mentioned in the code where      *
6  * appropriate.                                                           *
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
17 #include "AliHLTPHOSRcuHistogramProducer.h"
18 #include <iostream>
19 #include "AliHLTPHOSRcuCellEnergyDataStruct.h"
20 #include "TFile.h"
21 #include "unistd.h"
22 #include <time.h>
23
24 #define THRESHOLD 30
25
26 using  namespace std;
27
28
29 /*************************************************************************
30 * Class AliHLTPHOSRcuHistogramProducer accumulating histograms           *
31 * with amplitudes per PHOS channel                                       *
32 * It is intended to run at the HLT farm                                  *
33 * and it fills the histograms with amplitudes per channel.               * 
34 * Usage example see in PHOS/macros/Shuttle/AliPHOSCalibHistoProducer.C   *
35 **************************************************************************/
36
37
38 AliHLTPHOSRcuHistogramProducer:: AliHLTPHOSRcuHistogramProducer():  AliHLTPHOSBase(), fModuleID(0), fRcuX(0), fRcuZ(0)
39
40 //AliHLTPHOSRcuHistogramProducer:: AliHLTPHOSRcuHistogramProducer():  AliHLTPHOSRcuProcessor(), fModuleID(0), fRcuX(0), fRcuZ(0)
41 {
42   //Default constructor
43   cout << "WARNING: You cannot invoke the AliHLTPHOSRcuHistogramProducer without arguments" << endl;
44   cout << "Usage AliHLTPHOSRcuHistogramProducer(ModuleID, X. Z)" << endl;
45
46
47 //AliHLTPHOSRcuHistogramProducer::AliHLTPHOSRcuHistogramProducer(AliHLTUInt8_t moduleID, AliHLTUInt8_t rcuX, AliHLTUInt8_t rcuZ)
48 AliHLTPHOSRcuHistogramProducer::AliHLTPHOSRcuHistogramProducer(AliHLTUInt8_t moduleID, AliHLTUInt8_t rcuX, AliHLTUInt8_t rcuZ)
49 {
50   //Se header file for documentation
51   //  char *tmp = getenv("ALIHLT_BASEDIR");
52   char *tmp = getenv("HOME");
53
54   if(tmp == 0)
55     {
56       cout << "ERROR, environment vriable HOME is not set" << endl;
57
58     }
59   
60   else
61     {
62       //  sprintf(fHistoOutDir,"%s/rundir/output/histograms",tmp);
63       sprintf(fHistoOutDir,"%s/rundir/output/histograms/",tmp);
64     }
65   
66
67   SetModuleID(moduleID);
68   SetRcuX(rcuX); 
69   SetRcuZ(rcuZ); 
70   Init();
71 }
72
73 AliHLTPHOSRcuHistogramProducer::~ AliHLTPHOSRcuHistogramProducer()
74 {
75   //Destructor
76 }
77
78
79 void
80 AliHLTPHOSRcuHistogramProducer::SetHistoOutDir(char *outDir)
81 {
82   sprintf(fHistoOutDir,"%s", outDir);
83 }
84
85 void 
86 AliHLTPHOSRcuHistogramProducer::SetDefaultHistoOutDir()
87 {
88   char *tmp = getenv("HOME/rundir"); 
89   sprintf(fHistoOutDir,"%s/rundir/output/histograms", tmp);
90   //testing wether or not directry exist
91   FILE *fp = fopen(fHistoOutDir, "w");
92   
93   if(fp == 0)
94     {
95       cout << "ERROR, directory =" << fHistoOutDir << "  Doesnt exist, or you don have write permissions to the directory" << endl;
96       cout << "WARNING: Histograms will not bew written to files at end of run unless a valid directory is set" << endl;
97       cout << "INFO, You must either" << endl;
98       cout << "1) Create the directory " << fHistoOutDir <<  "Manually" <<endl;
99       cout << "OR "<< endl;
100       cout << "2) Se a valid output directory with the function AliHLTPHOSRcuHistogramProducer::SetHistoOutDir(*char outdir) "<< endl;
101    }
102   else
103     {
104       cout << "INFO: Output ddirectory for Histograms was set tot" << fHistoOutDir << endl;
105       cout << "INFO: if you want another output directory use the AliHLTPHOSRcuHistogramProducer::SetHistoOutDir(*char outdir)" << endl;
106     }
107
108
109
110 void
111 AliHLTPHOSRcuHistogramProducer::Init()
112 {
113   //See header file for documentation
114   char tmpHistoName[256];
115   int geomx;
116   int geomz;
117     
118
119   for(int gain=0; gain< N_GAINS; gain++)
120     {
121       sprintf(tmpHistoName, "DeadChanneMap_Module%d_rcuz%d_rcux%d_gain%d",(int)fModuleID,  fRcuZ, fRcuX, gain);
122       //    fDeadChannelMapHistogramPtrs[gain] = new TH2D( tmpHistoName, tmpHistoName, N_BINS, XBIN_LOW, XBIN_UP);
123
124           
125       fDeadChannelMapHistogramPtrs[gain] = new TH2D(tmpHistoName, tmpHistoName,  
126                                       N_XCOLUMNS_RCU , 0, N_XCOLUMNS_RCU , 
127                                       N_ZROWS_RCU,         0, N_ZROWS_RCU);
128       fDeadChannelMapHistogramPtrs[gain]->Reset(); 
129       //     fgCalibHistPtr[gain]->GetXaxis()->SetRange(128, 128 + 64);
130       
131
132     }
133
134
135
136   for(int x = 0; x < N_XCOLUMNS_RCU; x ++)
137     {
138       for(int z = 0; z < N_ZROWS_RCU; z ++)
139         {
140           for(int gain = 0; gain < N_GAINS; gain ++)
141             {
142               geomx = x + N_XCOLUMNS_RCU*fRcuX;
143               geomz = z + N_ZROWS_RCU*fRcuZ;
144
145               fEnergyAverageValues[x][z][gain] = 0; 
146               fAccumulatedValues[x][z][gain]   = 0;
147               fTimingAverageValues[x][z][gain] = 0; 
148               fHits[x][z][gain]                = 0;
149               fDeadChannelMap[x][z][gain]      = 0;
150
151               sprintf(tmpHistoName, "Edistribution_%d_%d_%d_%d",(int)fModuleID,  geomx, geomz, gain);
152               //              fEnergyHistogramPtrs[x][z][gain] = 0;
153               fEnergyHistogramPtrs[x][z][gain] = new TH1F( tmpHistoName, tmpHistoName, N_BINS, XBIN_LOW, XBIN_UP);
154               sprintf(tmpHistoName, "TOFdistribution_module%d_x%d_z%d_gain%d", (int)fModuleID,  geomx, geomz, gain);
155               
156               //              fTimingHistogramPtrs[x][z][gain] = 0;
157               
158               //              sprintf(tmpHistoName, "DeadChanneMap_%d_%d_%d_%d",(int)fModuleID,  geomx, geomz, gain);
159               //              fDeadChannelMapHistogramPtrs[x][z][gain] = new TH1D( tmpHistoName, tmpHistoName, N_BINS, XBIN_LOW, XBIN_UP);
160
161
162               fTimingHistogramPtrs[x][z][gain] = new TH1F(tmpHistoName , tmpHistoName, N_BINS, XBIN_LOW, XBIN_UP);
163               fCellAccEnergy.fAccumulatedEnergies[x][z][gain] = 0;
164               fCellAccEnergy.fHits[x][z][gain] = 0;
165               fCellAccEnergy.fDeadChannelMap[x][z][gain] = 0;
166               fCellAccEnergy.fModuleID = 0;
167               fCellAccEnergy.fRcuX = 0;
168               fCellAccEnergy.fRcuZ = 0; 
169             }
170         } 
171     } 
172 }
173
174
175
176 void 
177 AliHLTPHOSRcuHistogramProducer::SetRcuX(AliHLTUInt8_t X)
178 {
179   //See header file for documentation
180   fRcuX = X; 
181   fCellAccEnergy.fRcuX = X;
182 }
183
184
185
186 void 
187 AliHLTPHOSRcuHistogramProducer::SetRcuZ(AliHLTUInt8_t Z)
188 {
189   //See header file for documentation
190   fRcuZ = Z; 
191   fCellAccEnergy.fRcuZ = Z;
192 }
193
194
195
196
197 void 
198 AliHLTPHOSRcuHistogramProducer::SetModuleID(AliHLTUInt8_t moduleID)
199 {
200   //See header file for documentation
201  fModuleID = moduleID;
202 }
203
204
205 void 
206 AliHLTPHOSRcuHistogramProducer::FillEnergy(AliHLTUInt8_t x, AliHLTUInt8_t z,  AliHLTUInt8_t gain, float energy)
207 {
208   fCellAccEnergy.fAccumulatedEnergies[x][z][gain] += energy;
209   fCellAccEnergy.fHits[x][z][gain] ++;
210   fEnergyHistogramPtrs[x][z][gain]->Fill(energy); 
211 }
212
213
214 void 
215 AliHLTPHOSRcuHistogramProducer::FillTime(AliHLTUInt8_t x, AliHLTUInt8_t z, AliHLTUInt8_t gain, float time)
216 {
217   //See header file for documentation
218   fTimingHistogramPtrs[x][z][gain]->Fill(time);
219 }
220
221 const AliHLTPHOSRcuCellAccumulatedEnergyDataStruct& 
222 AliHLTPHOSRcuHistogramProducer::GetCellAccumulatedEnergies()
223 {
224   //  return &fCellAccEnergy ;
225   return fCellAccEnergy ;
226 }
227
228 void 
229 AliHLTPHOSRcuHistogramProducer::FillLiveChannels(Int_t data[], int size,  Int_t x, Int_t z, Int_t gain)
230 {
231   for(Int_t i = 0; i < size; i++)
232     {
233       if(data[i] > THRESHOLD)
234         {
235           if(data[i+1] > THRESHOLD) 
236             {
237               if(data[i+2] > THRESHOLD)
238                 {
239                   if(data[i+3] > THRESHOLD)
240                     {
241                       
242                       fCellAccEnergy.fDeadChannelMap[x][z][gain] = 10;
243                       
244                       return;
245                     }
246                 }
247             }
248         }
249     }
250 }
251
252 void 
253 AliHLTPHOSRcuHistogramProducer::FillLiveChannelHistograms()
254 {
255   for(int x = 0; x <  N_XCOLUMNS_RCU; x ++)
256     {
257       for(int z = 0; z < N_ZROWS_RCU; z ++)
258         {
259           for(int gain = 0; gain < N_GAINS; gain ++)
260             {
261               fDeadChannelMapHistogramPtrs[gain]->SetBinContent(x ,z , fCellAccEnergy.fDeadChannelMap[x][z][gain]);
262             }
263         } 
264     }
265
266 }
267
268 void
269 AliHLTPHOSRcuHistogramProducer::Reset()
270 {
271   //See header file for documentation
272   for(int x = 0; x < N_XCOLUMNS_RCU; x ++)
273     {
274       for(int z = 0; z < N_ZROWS_RCU; z ++)
275         {
276           for(int gain = 0; gain < N_GAINS; gain ++)
277             {
278               fEnergyAverageValues[x][z][gain] = 0; 
279               fAccumulatedValues[x][z][gain]   = 0;
280               fTimingAverageValues[x][z][gain] = 0; 
281               fHits[x][z][gain]                = 0;
282               fDeadChannelMap[x][z][gain]      = 0;
283             }
284         } 
285     }
286   
287   for(int i = 0; i <ALTRO_MAX_SAMPLES;  i++)
288     {
289       fTmpChannelData[i] = 0;
290     }
291 }
292
293
294 void 
295 AliHLTPHOSRcuHistogramProducer::WriteAllHistograms(char *opt)
296 {
297   printf("\nAliHLTPHOSRcuHistogramProducer::WriteAllHistogram, opt = %s\n", opt);
298
299   FillLiveChannelHistograms();
300   //  cout <<<< endl;
301
302   int runNumber = 0;
303   char tmpEFileName[256];
304   char tmpDeadFileName_gain0[256];
305   char tmpDeadFileName_gain1[256]; 
306  // char *tmpRundir = getenv("HOME");
307   char runNumberFile[256]; 
308   char timeString[256];
309
310   ResetArray(runNumberFile, 256);
311   ResetArray(tmpEFileName, 256);
312   ResetArray(timeString, 256);
313   
314
315   sprintf(runNumberFile, "%s/rundir/runNumber.txt", getenv("HOME"));
316
317   FILE *fp = fopen(runNumberFile, "r");
318   if(fp == 0)
319     {
320       ScanTimeString(timeString);  
321       cout << "WARNING, could not find file "<< runNumberFile  <<endl;
322       cout <<"Filename will be stamped with data and time instead " << endl;
323       sprintf(tmpEFileName, "%s/Energy/EnergyHistograms_%s_mod%d_rcuZ%d_rcuX%d.root", fHistoOutDir, timeString, (int)fModuleID, (int)fRcuZ, (int)fRcuZ);
324       sprintf(tmpDeadFileName_gain0, "%s/DeadMap/DeadChannelHistograms_%s_mod%d_rcuZ%d_rcuX%d_LG.root", fHistoOutDir, timeString, (int)fModuleID, (int)fRcuZ, (int)fRcuZ); 
325       sprintf(tmpDeadFileName_gain1, "%s/DeadMap/DeadChannelHistograms_%s_mod%d_rcuZ%d_rcuX%d_HG.root", fHistoOutDir, timeString, (int)fModuleID, (int)fRcuZ, (int)fRcuZ); 
326     } 
327   else
328     {
329       fscanf(fp, "%d", &runNumber);
330       sprintf(tmpEFileName, "%s/Energy/EnergyHisttograms_run%d_mod%d_rcuZ%d_rcuX%d.root", fHistoOutDir, runNumber, (int)fModuleID, (int)fRcuZ, (int)fRcuX);
331       sprintf(tmpDeadFileName_gain0, "%s/DeadMap/DeadChannleHistograms_run%d_mod%d_rcuZ%d_rcuX%d_LG.root", fHistoOutDir, runNumber, (int)fModuleID, (int)fRcuZ, (int)fRcuX);
332       sprintf(tmpDeadFileName_gain1, "%s/DeadMap/DeadChannleHistograms_run%d_mod%d_rcuZ%d_rcuX%d_HG.root", fHistoOutDir, runNumber, (int)fModuleID, (int)fRcuZ, (int)fRcuX);
333       fclose(fp);
334     }
335
336   //  cout << "tmpEFileName = "<< tmpEFileName <<endl;
337   //  cout << "tmpDeadFileName_gain0 (low gain)   = " << tmpDeadFileName_gain0 <<endl;
338   //  cout << "tmpDeadFileName_gain1 (high gain)  = " << tmpDeadFileName_gain1 <<endl;
339
340   //  sprintf(tmpEFileName,"/home/aliphoshlt/rundir/outdata/calibHisto_%d_%d_%d.root", (int)fModuleID, (int)fRcuX, (int)fRcuZ);
341
342
343   //  TFile *energyHistoFile =  new TFile(tmpEFileName,"update");
344   //  TFile *energyHistoFile =  new TFile(tmpEFileName,"recreate");
345   //  TFile *energyHistoFile =  new TFile(tmpEFileName,"recreate");
346
347   cout << "tmpEFileName = "<< tmpEFileName  << endl;
348
349   TFile *energyHistoFile =  new TFile(tmpEFileName, opt);
350   if(!energyHistoFile) return;
351   if(!energyHistoFile->IsOpen()) return;
352
353   cout <<"printing histograms"<< endl;
354
355   for(int x = 0; x <  N_XCOLUMNS_RCU; x ++)
356     {
357       for(int z = 0; z < N_ZROWS_RCU; z ++)
358         {
359           for(int gain = 0; gain < N_GAINS; gain ++)
360             {
361               //     cout << "the number of entries is " <<fEnergyHistogramPtrs[x][z][gain]->GetEntries()<< endl;
362               fEnergyHistogramPtrs[x][z][gain]->Write();
363             }
364         } 
365     }
366   energyHistoFile->Close();
367
368
369   //LOW GAIN
370   //TFile *deadHistoFile_gain0 =  new TFile(tmpDeadFileName_gain0,"update");
371   TFile *deadHistoFile_gain0 =  new TFile(tmpDeadFileName_gain0, opt);
372
373   if(!deadHistoFile_gain0) return;
374   if(!deadHistoFile_gain0->IsOpen()) return;
375   fDeadChannelMapHistogramPtrs[0]->Write();
376
377
378   //HIGH GAIN
379   //  TFile *deadHistoFile_gain1 =  new TFile(tmpDeadFileName_gain1,"update");
380   TFile *deadHistoFile_gain1 =  new TFile(tmpDeadFileName_gain1, opt);
381   if(!deadHistoFile_gain1) return;
382   if(!deadHistoFile_gain1->IsOpen()) return;
383   fDeadChannelMapHistogramPtrs[1]->Write();
384
385
386   deadHistoFile_gain0->Close();
387   deadHistoFile_gain1->Close(); 
388
389   cout << "printing histograms, finished"<< endl;
390 }
391
392 void
393 AliHLTPHOSRcuHistogramProducer::ScanTimeString(char *timeString)
394 {
395   time_t timePtr;
396   tm *tmPtr;
397   //  char *timeString;
398   time(&timePtr); 
399   tmPtr=localtime(&timePtr);
400   timeString=asctime(tmPtr);
401   char tShort [strlen(timeString)+1];
402   char day[10];
403   char month[10];
404   int date;
405   int hour;
406   int min;
407   int sec;
408   int year;
409   char sDate[5];
410   char sHour[5];
411   char sMin[5];
412   char sSec[5];
413   char sYear[10];
414   sscanf(timeString, "%s %s %d %d:%d:%d %d\n", day, month, &date, &hour, &min, &sec, &year);
415   
416 }