AliTPCSpaceCharge3D: fixed wrong calib maps paths
[u/mrichter/AliRoot.git] / AD / DA / ADPEDESTALda.cxx
CommitLineData
f2ce2935 1/*********************************************************************************
2- Contact: Michal Broz Michal.Broz@cern.ch
3- Link: http
4- Raw data test file :
5- Reference run number :
6- Run Type: PHYSICS
7- DA Type: MON
8- Number of events needed: >=500
9- Input Files: argument list
10- Output Files: local files AD0_Histos.root, AD0_Pedestals_On.dat (Online mapping)
11 FXS file AD0_Pedestals_Off.dat (Offline mapping)
12- Trigger types used: PHYSICS_EVENT
13**********************************************************************************/
14
15/**********************************************************************************
16* *
17* AD Detector Algorithm used for extracting calibration parameters *
18* *
19* This program connects to the DAQ data source passed as argument. *
20* It computes calibration parameters, populates local "./AD0_Pedestals_On.dat" *
21* file, exports it to the FES, and stores it into DAQ DB *
22* The program exits when being asked to shut down (daqDA_checkshutdown) *
23* or on End of Run event. *
24* We have 32 channels instead of 16 as expected for AD0 due to the two sets of *
25* charge integrators which are used by the FEE ... *
26* *
27***********************************************************************************/
28
29// DATE
30#include "event.h"
31#include "monitor.h"
32#include "daqDA.h"
33
34//AliRoot
35#include <AliADRawStream.h>
36#include <AliRawReaderDate.h>
37#include <AliRawReader.h>
38#include <AliDAQ.h>
39
40// standard
41#include <stdio.h>
42#include <stdlib.h>
43
44//ROOT
45#include "TROOT.h"
46#include "TPluginManager.h"
47#include <TFile.h>
48#include <TH1F.h>
49#include <TMath.h>
50
51Int_t GetOfflineChannel(Int_t channel);
52
53/* Main routine --- Arguments: monitoring data source */
54
55int main(int argc, char **argv) {
56
57/* magic line from Cvetan */
58 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo","*","TStreamerInfo","RIO","TStreamerInfo()");
59
60 int status;
61 if (argc!=2) {
62 printf("Wrong number of arguments\n");
63 return -1;
64 }
65
66 // Online values (using FEE channel numbering),
67 // stored into local AD0_Pedestals_On.dat:
68 Double_t adcMean[32];
69 Double_t adcSigma[32];
70 Double_t pedMean[32];
71 Double_t pedSigma[32];
72 // Offline values(same but ordered as in aliroot for offliners)
73 // stored into AD0_Pedestals_Off.dat:
74 Double_t adcMeanOff[32];
75 Double_t adcSigmaOff[32];
76 Double_t pedMeanOff[32];
77 Double_t pedSigmaOff[32];
78
79//___________________________________________________
80// Get cuts from V00DA.config file
81
82 Int_t kClockMin; // = 16; LHC Clock Min for pedestal calculation
83 Int_t kClockMax; // = 19; LHC Clock Max for pedestal calculation
84 Int_t kLowCut; // = 60; low cut on signal distribution - to be tuned
85 Int_t kHighCut; // = 50; high cut on pedestal distribution - to be tuned
86 Int_t kClockMinRef; // = 16; LHC Clock Min for Flag checking
87 Int_t kClockMaxRef; // = 20; LHC Clock Max for Flag checking
88 Float_t kChi2Max; // = 1. Maximum chi2
89
90 status = daqDA_DB_getFile("AD0_Pedestal_DA.config","./AD0_Pedestal_DA.config");
91 if (status) {
92 printf("Failed to get Config file (AD0_Pedestal_DA.config) from DAQ DB, status=%d\n", status);
93 printf("Take default values of parameters for pedestal calculation \n");
94 kClockMin = 16;
95 kClockMax = 19;
96 kLowCut = 60;
97 kHighCut = 50;
98 kClockMinRef = 16;
99 kClockMaxRef = 20;
100 kChi2Max = 100.;
101 } else {
102 /* open the config file and retrieve cuts */
103 FILE *fpConfig = fopen("AD0_Pedestal_DA.config","r");
104 int res = fscanf(fpConfig,"%d %d %d %d %d %d %f",&kClockMin,&kClockMax,&kLowCut,&kHighCut,&kClockMinRef,&kClockMaxRef,&kChi2Max);
105 if(res!=7) {
106 printf("Failed to get values from Config file (AD0_Pedestal_DA.config): wrong file format - 7 integers are expected - \n");
107 kClockMin = 16;
108 kClockMax = 19;
109 kLowCut = 60;
110 kHighCut = 50;
111 kClockMinRef = 16;
112 kClockMaxRef = 20;
113 kChi2Max = 100.;
114 }
115 fclose(fpConfig);
116 }
117
118 printf("LHC Clock Min for pedestal calculation = %d; LHC Clock Max for pedestal calculation = %d; LowCut on signal = %d ; HighCut on pedestal = %d\n",
119 kClockMin, kClockMax, kLowCut, kHighCut);
120
121//___________________________________________________
122// Book HISTOGRAMS - dynamics of p-p collisions -
123
124 char adcName[6];
125 char pedName[6];
126 TH1F *hADCname[32];
127 TH1F *hPEDname[32];
128
129 char texte[12];
130 for (Int_t i=0; i<16; i++) {
131 for(Int_t j=0; j<2; j++){
132 sprintf(adcName,"hADC%d%d",i,j);
133 sprintf(texte,"ADC cell%d int%d",i,j);
134 hADCname[i + 16*j] = new TH1F(adcName,texte,1024,-0.5, 1023.5);
135 sprintf(pedName,"hPED%d%d",i,j);
136 sprintf(texte,"PED cell%d int%d",i,j);
137 hPEDname[i + 16*j] = new TH1F(pedName,texte,1024,-0.5, 1023.5);
138 }
139 }
140//___________________________________________________
141
142 /* open result file to be exported to FES */
143 FILE *fpLocal=NULL;
144 fpLocal=fopen("./AD0_Pedestals_On.dat","w");
145 if (fpLocal==NULL) {
146 printf("Failed to open local result file\n");
147 return -1;}
148
149 /* open result file to be exported to FES */
150 FILE *fp=NULL;
151 fp=fopen("./AD0_Pedestals_Off.dat","w");
152 if (fp==NULL) {
153 printf("Failed to open result file\n");
154 return -1;}
155
156 /* define data source : this is argument 1 */
157 status=monitorSetDataSource( argv[1] );
158 if (status!=0) {
159 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
160 return -1;
161 }
162
163 /* declare monitoring program */
164 status=monitorDeclareMp( __FILE__ );
165 if (status!=0) {
166 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
167 return -1;
168 }
169
170 /* define wait event timeout - 1s max */
171 monitorSetNowait();
172 monitorSetNoWaitNetworkTimeout(1000);
173
174 /* init counters on events */
175 int neventsPhysics=0;
176 int neventsTotal=0;
177
178 /* loop on events (infinite) */
179 for(;;) {
180 struct eventHeaderStruct *event;
181 eventTypeType eventT;
182
183 /* check shutdown condition */
184 if (daqDA_checkShutdown()) {break;}
185
186 /* get next event (blocking call until timeout) */
187 status=monitorGetEventDynamic((void **)&event);
188 if (status==MON_ERR_EOF) {
189 printf ("End of File detected\n");
190 break; /* end of monitoring file has been reached */
191 }
192
193 if (status!=0) {
194 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
195 break;
196 }
197
198 /* retry if got no event */
199 if (event==NULL) continue;
200
201 /* decode event */
202 eventT=event->eventType;
203
204 switch (event->eventType){
205
206 case START_OF_RUN:
207 break;
208
209 case END_OF_RUN:
210 printf("End Of Run detected\n");
211 break;
212
213 case PHYSICS_EVENT:
214 neventsPhysics++;
215
216 AliRawReader *rawReader = new AliRawReaderDate((void*)event);
217
218 AliADRawStream* rawStream = new AliADRawStream(rawReader);
219 if (rawStream->Next()) {
220 for(Int_t i=0; i<16; i++) {
221 Int_t nFlag = 0;
222 for(Int_t j=kClockMinRef; j <= kClockMaxRef; j++) { // Check flags on clock range used for pedestal calculation
223 if((rawStream->GetBBFlag(i,j)) || (rawStream->GetBGFlag(i,j))) nFlag++;
224 }
225 if(nFlag == 0){ // Fill 16*2 pedestal histograms - 2 integrators -
226 Float_t sum[2] = {0.,0.};
227 Float_t sumwi[2] = {0.,0.};
228 for(Int_t j=kClockMin;j <= kClockMax;j++){
229 Int_t integrator = rawStream->GetIntegratorFlag(i,j);
230 Float_t pedestal = (float)(rawStream->GetPedestal(i,j));
231 sum[integrator] += pedestal;
232 sumwi[integrator] += 1.;
233 }
234 Float_t mean[2] = {0.,0.};
235 Float_t chi2[2] = {0.,0.};
236
237 for(int ii=0;ii<2;ii++) if(sumwi[ii]>1.e-6) mean[ii] = sum[ii] / sumwi[ii];
238
239 for(Int_t j=kClockMin;j <= kClockMax;j++){
240 Int_t integrator = rawStream->GetIntegratorFlag(i,j);
241 Float_t pedestal = (float)(rawStream->GetPedestal(i,j));
242 chi2[integrator] += (mean[integrator] - pedestal) * (mean[integrator] - pedestal);
243 }
244 if(chi2[0]<kChi2Max && chi2[1]<kChi2Max) {
245 for(int ii=0;ii<2;ii++){
246 if(mean[ii] >1.e-6) hPEDname[i + 16*ii]->Fill(mean[ii]);
247 }
248 }
249
250 }
251 if((rawStream->GetBBFlag(i,10)) || (rawStream->GetBGFlag(i,10))){ // Charge
252 Int_t integrator = rawStream->GetIntegratorFlag(i,10);
253 Float_t charge = (float)(rawStream->GetADC(i)); // Fill 16*2 ADCmax histograms
254 hADCname[i + 16 * integrator]->Fill(charge);
255 }
256 } // End loop over channels
257 } // End : if rawstream
258 delete rawStream;
259 rawStream = 0x0;
260 delete rawReader;
261 rawReader = 0x0;
262 } // end of switch on event type
263
264 neventsTotal++;
265 /* free resources */
266 free(event);
267
268 /* exit when last event received, no need to wait for TERM signal */
269 if (eventT==END_OF_RUN) {
270 printf("End Of Run event detected\n");
271 break;
272 }
273
274 } // loop over events
275
276 printf("%d physics events processed\n",neventsPhysics);
277
278//___________________________________________________________________________
279// Computes mean values, converts FEE channels into Offline AliRoot channels
280// and dumps the ordered values into the output text file for SHUTTLE
281
282 for(Int_t i=0; i<32; i++) {
283 hPEDname[i]->GetXaxis()->SetRange(0,kHighCut);
284 pedMean[i] = hPEDname[i]->GetMean();
285 pedSigma[i] = hPEDname[i]->GetRMS();
286 hADCname[i]->GetXaxis()->SetRange(kLowCut,1024);
287 adcMean[i] = hADCname[i]->GetMean();
288 adcSigma[i] = hADCname[i]->GetRMS();
289// printf(" i = %d, %.3f %.3f %.3f %.3f\n",i,pedMean[i],pedSigma[i],adcMean[i],adcSigma[i]);
290 fprintf(fpLocal," %.3f %.3f %.3f %.3f\n",pedMean[i],pedSigma[i],adcMean[i],adcSigma[i]);
291
292 if (i < 16) {
293 Int_t j = GetOfflineChannel(i);
294 pedMeanOff[j] = pedMean[i];
295 pedSigmaOff[j] = pedSigma[i];
296 adcMeanOff[j] = adcMean[i];
297 adcSigmaOff[j] = adcSigma[i];
298 }
299 else{
300 Int_t j = GetOfflineChannel(i-16);
301 pedMeanOff[j+16] = pedMean[i];
302 pedSigmaOff[j+16] = pedSigma[i];
303 adcMeanOff[j+16] = adcMean[i];
304 adcSigmaOff[j+16] = adcSigma[i];
305 }
306 }
307
308 for(Int_t j=0; j<32; j++) {
309// printf(" j = %d, %.3f %.3f %.3f %.3f\n",j,pedMeanOff[j],pedSigmaOff[j],
310// adcMeanOff[j],adcSigmaOff[j]);
311 fprintf(fp," %.3f %.3f %.3f %.3f\n",pedMeanOff[j],pedSigmaOff[j],
312 adcMeanOff[j],adcSigmaOff[j]);
313 }
314
315//________________________________________________________________________
316// Write root file with histos for users further check - just in case -
317
318 TFile *histoFile = new TFile("AD0_Pedestals.root","RECREATE");
319
320 for (Int_t i=0; i<32; i++) {
321 hADCname[i]->GetXaxis()->SetRange(0,1024);
322 hADCname[i]->Write();
323 hPEDname[i]->Write(); }
324
325 histoFile->Close();
326 delete histoFile;
327
328//________________________________________________________________________
329
330 /* close local result file and FXS result file*/
331 fclose(fpLocal);
332 fclose(fp);
333
334 /* export result file to FES */
335 status=daqDA_FES_storeFile("./AD0_Pedestals_Off.dat","AD0da_results");
336 if (status) {
337 printf("Failed to export file : %d\n",status);
338 return -1; }
339
340 /* store result file into Online DB */
341 status=daqDA_DB_storeFile("./AD0_Pedestals_On.dat","AD0da_results");
342 if (status) {
343 printf("Failed to store file into Online DB: %d\n",status);
344 return -1; }
345
346 return status;
347}
348
349 Int_t GetOfflineChannel(Int_t channel) {
350
351// Channel mapping Online - Offline:
352
353 Int_t fOfflineChannel[16] = {0,1,2,3,4,5,6,7,
354 8,9,10,11,12,13,14,15};
355 return fOfflineChannel[channel];
356}