1 /*********************************************************************************
2 - Contact: Michal Broz Michal.Broz@cern.ch
5 - Reference run number :
8 - Number of events needed: >=1000
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 **********************************************************************************/
15 /**********************************************************************************
17 * AD Detector Algorithm used for extracting calibration parameters *
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 ... *
27 ***********************************************************************************/
35 #include <AliADConst.h>
36 #include <AliADRawStream.h>
37 #include <AliRawReaderDate.h>
38 #include <AliRawReader.h>
47 #include "TPluginManager.h"
52 /* Main routine --- Arguments: monitoring data source */
54 int main(int argc, char **argv) {
56 /* magic line from Cvetan */
57 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo","*","TStreamerInfo","RIO","TStreamerInfo()");
61 printf("Wrong number of arguments\n");
65 // Online values (using FEE channel numbering),
66 // stored into local AD0_Pedestals_On.dat:
68 Double_t adcSigma[32];
70 Double_t pedSigma[32];
71 // Offline values(same but ordered as in aliroot for offliners)
72 // stored into AD0_Pedestals_Off.dat:
73 Double_t adcMeanOff[32];
74 Double_t adcSigmaOff[32];
75 Double_t pedMeanOff[32];
76 Double_t pedSigmaOff[32];
78 //___________________________________________________
79 // Get cuts from AD0_Pedestal_DA.config file
81 Int_t kClockMin; // = 16; LHC Clock Min for pedestal calculation
82 Int_t kClockMax; // = 19; LHC Clock Max for pedestal calculation
83 Int_t kLowCut; // = 60; low cut on signal distribution - to be tuned
84 Int_t kHighCut; // = 50; high cut on pedestal distribution - to be tuned
85 Int_t kClockMinRef; // = 16; LHC Clock Min for Flag checking
86 Int_t kClockMaxRef; // = 20; LHC Clock Max for Flag checking
87 Float_t kChi2Max; // = 1. Maximum chi2
89 status = daqDA_DB_getFile("AD0_Pedestal_DA.config","./AD0_Pedestal_DA.config");
91 printf("Failed to get Config file (AD0_Pedestal_DA.config) from DAQ DB, status=%d\n", status);
92 printf("Take default values of parameters for pedestal calculation \n");
101 /* open the config file and retrieve cuts */
102 FILE *fpConfig = fopen("AD0_Pedestal_DA.config","r");
103 int res = fscanf(fpConfig,"%d %d %d %d %d %d %f",&kClockMin,&kClockMax,&kLowCut,&kHighCut,&kClockMinRef,&kClockMaxRef,&kChi2Max);
105 printf("Failed to get values from Config file (AD0_Pedestal_DA.config): wrong file format - 7 integers are expected - \n");
117 printf("LHC Clock Min for pedestal calculation = %d; LHC Clock Max for pedestal calculation = %d; LowCut on signal = %d ; HighCut on pedestal = %d\n",
118 kClockMin, kClockMax, kLowCut, kHighCut);
120 //___________________________________________________
121 // Book HISTOGRAMS - dynamics of p-p collisions -
129 for (Int_t i=0; i<16; i++) {
130 for(Int_t j=0; j<2; j++){
131 sprintf(adcName,"hADC%d%d",i,j);
132 sprintf(texte,"ADC cell%d int%d",i,j);
133 hADCname[i + 16*j] = new TH1F(adcName,texte,1024,-0.5, 1023.5);
134 sprintf(pedName,"hPED%d%d",i,j);
135 sprintf(texte,"PED cell%d int%d",i,j);
136 hPEDname[i + 16*j] = new TH1F(pedName,texte,1024,-0.5, 1023.5);
139 //___________________________________________________
141 /* open result file to be exported to FES */
143 fpLocal=fopen("./AD0_Pedestals_On.dat","w");
145 printf("Failed to open local result file\n");
148 /* open result file to be exported to FES */
150 fp=fopen("./AD0_Pedestals_Off.dat","w");
152 printf("Failed to open result file\n");
155 /* define data source : this is argument 1 */
156 status=monitorSetDataSource( argv[1] );
158 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
162 /* declare monitoring program */
163 status=monitorDeclareMp( __FILE__ );
165 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
169 /* define wait event timeout - 1s max */
171 monitorSetNoWaitNetworkTimeout(1000);
173 /* init counters on events */
174 int neventsPhysics=0;
177 /* loop on events (infinite) */
179 struct eventHeaderStruct *event;
180 eventTypeType eventT;
182 /* check shutdown condition */
183 if (daqDA_checkShutdown()) {break;}
185 /* get next event (blocking call until timeout) */
186 status=monitorGetEventDynamic((void **)&event);
187 if (status==MON_ERR_EOF) {
188 printf ("End of File detected\n");
189 break; /* end of monitoring file has been reached */
193 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
197 /* retry if got no event */
198 if (event==NULL) continue;
201 eventT=event->eventType;
203 switch (event->eventType){
209 printf("End Of Run detected\n");
215 AliRawReader *rawReader = new AliRawReaderDate((void*)event);
217 AliADRawStream* rawStream = new AliADRawStream(rawReader);
218 if (rawStream->Next()) {
219 for(Int_t i=0; i<16; i++) {
221 for(Int_t j=kClockMinRef; j <= kClockMaxRef; j++) { // Check flags on clock range used for pedestal calculation
222 if((rawStream->GetBBFlag(i,j)) || (rawStream->GetBGFlag(i,j))) nFlag++;
224 if(nFlag == 0){ // Fill 16*2 pedestal histograms - 2 integrators -
225 Float_t sum[2] = {0.,0.};
226 Float_t sumwi[2] = {0.,0.};
227 for(Int_t j=kClockMin;j <= kClockMax;j++){
228 Int_t integrator = rawStream->GetIntegratorFlag(i,j);
229 Float_t pedestal = (float)(rawStream->GetPedestal(i,j));
230 sum[integrator] += pedestal;
231 sumwi[integrator] += 1.;
233 Float_t mean[2] = {0.,0.};
234 Float_t chi2[2] = {0.,0.};
236 for(int ii=0;ii<2;ii++) if(sumwi[ii]>1.e-6) mean[ii] = sum[ii] / sumwi[ii];
238 for(Int_t j=kClockMin;j <= kClockMax;j++){
239 Int_t integrator = rawStream->GetIntegratorFlag(i,j);
240 Float_t pedestal = (float)(rawStream->GetPedestal(i,j));
241 chi2[integrator] += (mean[integrator] - pedestal) * (mean[integrator] - pedestal);
243 if(chi2[0]<kChi2Max && chi2[1]<kChi2Max) {
244 for(int ii=0;ii<2;ii++){
245 if(mean[ii] >1.e-6) hPEDname[i + 16*ii]->Fill(mean[ii]);
250 if((rawStream->GetBBFlag(i,10)) || (rawStream->GetBGFlag(i,10))){ // Charge
251 Int_t integrator = rawStream->GetIntegratorFlag(i,10);
252 Float_t charge = (float)(rawStream->GetADC(i)); // Fill 16*2 ADCmax histograms
253 hADCname[i + 16 * integrator]->Fill(charge);
255 } // End loop over channels
256 } // End : if rawstream
261 } // end of switch on event type
267 /* exit when last event received, no need to wait for TERM signal */
268 if (eventT==END_OF_RUN) {
269 printf("End Of Run event detected\n");
273 } // loop over events
275 printf("%d physics events processed\n",neventsPhysics);
277 //___________________________________________________________________________
278 // Computes mean values, converts FEE channels into Offline AliRoot channels
279 // and dumps the ordered values into the output text file for SHUTTLE
281 for(Int_t i=0; i<32; i++) {
282 hPEDname[i]->GetXaxis()->SetRange(0,kHighCut);
283 pedMean[i] = hPEDname[i]->GetMean();
284 pedSigma[i] = hPEDname[i]->GetRMS();
285 hADCname[i]->GetXaxis()->SetRange(kLowCut,1024);
286 adcMean[i] = hADCname[i]->GetMean();
287 adcSigma[i] = hADCname[i]->GetRMS();
288 // printf(" i = %d, %.3f %.3f %.3f %.3f\n",i,pedMean[i],pedSigma[i],adcMean[i],adcSigma[i]);
289 fprintf(fpLocal," %.3f %.3f %.3f %.3f\n",pedMean[i],pedSigma[i],adcMean[i],adcSigma[i]);
292 Int_t j = kOfflineChannel[i];;
293 pedMeanOff[j] = pedMean[i];
294 pedSigmaOff[j] = pedSigma[i];
295 adcMeanOff[j] = adcMean[i];
296 adcSigmaOff[j] = adcSigma[i];
299 Int_t j = kOfflineChannel[i-16];
300 pedMeanOff[j+16] = pedMean[i];
301 pedSigmaOff[j+16] = pedSigma[i];
302 adcMeanOff[j+16] = adcMean[i];
303 adcSigmaOff[j+16] = adcSigma[i];
307 for(Int_t j=0; j<32; j++) {
308 // printf(" j = %d, %.3f %.3f %.3f %.3f\n",j,pedMeanOff[j],pedSigmaOff[j],
309 // adcMeanOff[j],adcSigmaOff[j]);
310 fprintf(fp," %.3f %.3f %.3f %.3f\n",pedMeanOff[j],pedSigmaOff[j],
311 adcMeanOff[j],adcSigmaOff[j]);
314 //________________________________________________________________________
315 // Write root file with histos for users further check - just in case -
317 TFile *histoFile = new TFile("AD0_Pedestals.root","RECREATE");
319 for (Int_t i=0; i<32; i++) {
320 hADCname[i]->GetXaxis()->SetRange(0,1024);
321 hADCname[i]->Write();
322 hPEDname[i]->Write(); }
327 //________________________________________________________________________
329 /* close local result file and FXS result file*/
333 /* export result file to FES */
334 status=daqDA_FES_storeFile("./AD0_Pedestals_Off.dat","AD0da_results");
336 printf("Failed to export file : %d\n",status);
339 /* store result file into Online DB */
340 status=daqDA_DB_storeFile("./AD0_Pedestals_On.dat","AD0da_results");
342 printf("Failed to store file into Online DB: %d\n",status);