remove left over print statement
[u/mrichter/AliRoot.git] / ZDC / ZDCEMDda.cxx
... / ...
CommitLineData
1/*
2
3This program reads the DAQ data files passed as argument using the monitoring library.
4
5It computes the average event size and populates local "./result.txt" file with the
6result.
7
8The program reports about its processing progress.
9
10Messages on stdout are exported to DAQ log system.
11
12DA for ZDC standalone CALIBRATION_EMD runs
13
14Contact: Chiara.Oppedisano@to.infn.it
15Link:
16Run Type: STANDALONE_EMD_RUN
17DA Type: LDC
18Number of events needed: at least ~5*10^3
19Input Files: ZDCPedestal.dat
20Output Files: ZDCEMDCalib.dat, ZDCChMapping.dat
21Trigger Types Used: Standalone Trigger
22
23*/
24#define PEDDATA_FILE "ZDCPedestal.dat"
25#define MAPDATA_FILE "ZDCChMapping.dat"
26#define ENCALIBDATA_FILE "ZDCEnergyCalib.dat"
27#define TOWCALIBDATA_FILE "ZDCTowerCalib.dat"
28
29#include <stdio.h>
30#include <Riostream.h>
31#include <Riostream.h>
32
33// DATE
34#include <daqDA.h>
35#include <event.h>
36#include <monitor.h>
37
38//ROOT
39#include <TROOT.h>
40#include <TPluginManager.h>
41#include <TH1F.h>
42#include <TH2F.h>
43#include <TProfile.h>
44#include <TF1.h>
45#include <TFile.h>
46#include <TFitter.h>
47#include "TMinuitMinimizer.h"
48
49//AliRoot
50#include <AliRawReaderDate.h>
51#include <AliRawEventHeaderBase.h>
52#include <AliZDCRawStream.h>
53
54
55/* Main routine
56 Arguments:
57 1- monitoring data source
58*/
59int main(int argc, char **argv) {
60
61
62 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
63 "*",
64 "TStreamerInfo",
65 "RIO",
66 "TStreamerInfo()");
67
68 TMinuitMinimizer m;
69 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer", "Minuit","TMinuitMinimizer",
70 "Minuit", "TMinuitMinimizer(const char *)");
71 TVirtualFitter::SetDefaultFitter("Minuit");
72
73 int status = 0;
74 // No. of ZDC cabled ch.
75 int const kNChannels = 24;
76 int const kNScChannels = 32;
77 Int_t kFirstADCGeo=0, kLastADCGeo=3;
78
79 /* log start of process */
80 printf("ZDC EMD program started\n");
81
82 /* check that we got some arguments = list of files */
83 if (argc<2) {
84 printf("Wrong number of arguments\n");
85 return -1;
86 }
87
88 // --- Preparing histos for EM dissociation spectra
89 //
90 TH1F* histoEMDRaw[4];
91 TH1F* histoEMDCorr[4];
92 //
93 char namhistr[50], namhistc[50];
94 for(Int_t i=0; i<4; i++) {
95 if(i==0){
96 sprintf(namhistr,"ZN%d-EMDRaw",i+1);
97 sprintf(namhistc,"ZN%d-EMDCorr",i+1);
98 }
99 else if(i==1){
100 sprintf(namhistr,"ZP%d-EMDRaw",i);
101 sprintf(namhistc,"ZP%d-EMDCorr",i);
102 }
103 else if(i==2){
104 sprintf(namhistr,"ZN%d-EMDRaw",i);
105 sprintf(namhistc,"ZN%d-EMDCorr",i);
106 }
107 else if(i==3){
108 sprintf(namhistr,"ZP%d-EMDRaw",i-1);
109 sprintf(namhistc,"ZP%d-EMDCorr",i-1);
110 }
111 histoEMDRaw[i] = new TH1F(namhistr,namhistr,100,0.,4000.);
112 histoEMDCorr[i] = new TH1F(namhistc,namhistc,100,0.,4000.);
113 }
114
115 // --- Preparing histos for tower inter-calibration
116 //
117 TH1F* histZNCtow[4]; TH1F* histZPCtow[4];
118 TH1F* histZNAtow[4]; TH1F* histZPAtow[4];
119 //
120 char namhistznc[50], namhistzpc[50];
121 char namhistzna[50], namhistzpa[50];
122 for(Int_t i=0; i<4; i++) {
123 sprintf(namhistznc,"ZNC-tow%d",i+1);
124 sprintf(namhistzpc,"ZPC-tow%d",i+1);
125 sprintf(namhistzna,"ZNA-tow%d",i+1);
126 sprintf(namhistzpa,"ZPA-tow%d",i+1);
127 //
128 histZNCtow[i] = new TH1F(namhistznc,namhistznc,100,0.,4000.);
129 histZPCtow[i] = new TH1F(namhistzpc,namhistzpc,100,0.,4000.);
130 histZNAtow[i] = new TH1F(namhistzna,namhistzna,100,0.,4000.);
131 histZPAtow[i] = new TH1F(namhistzpa,namhistzpa,100,0.,4000.);
132 }
133
134 /* open result file */
135 FILE *fp=NULL;
136 fp=fopen("./result.txt","a");
137 if (fp==NULL) {
138 printf("Failed to open file\n");
139 return -1;
140 }
141
142 FILE *mapFile4Shuttle;
143
144 // *** To analyze EMD events you MUST have a pedestal data file!!!
145 // *** -> check if a pedestal run has been analyzed
146 int read = 0;
147 read = daqDA_DB_getFile(PEDDATA_FILE,PEDDATA_FILE);
148 if(read){
149 printf("\t ERROR!!! ZDCPedestal.dat file NOT FOUND in DAQ db!!!\n");
150 return -1;
151 }
152 else printf("\t ZDCPedestal.dat file retrieved from DAQ db\n");
153
154 FILE *filePed = fopen(PEDDATA_FILE,"r");
155 if (filePed==NULL) {
156 printf("\t ERROR!!! Can't open ZDCPedestal.dat file!!!\n");
157 return -1;
158 }
159
160 // 144 = 48 in-time + 48 out-of-time + 48 correlations
161 Float_t readValues[2][3*2*kNChannels];
162 Float_t MeanPed[2*kNChannels];
163 Float_t CorrCoeff0[2*kNChannels], CorrCoeff1[2*kNChannels];
164 // ***************************************************
165 // Unless we have a narrow correlation to fit we
166 // don't fit and store in-time vs. out-of-time
167 // histograms -> mean pedstal subtracted!!!!!!
168 // ***************************************************
169 //
170 for(int jj=0; jj<6*kNChannels; jj++){
171 for(int ii=0; ii<2; ii++){
172 fscanf(filePed,"%f",&readValues[ii][jj]);
173 }
174 if(jj<kNChannels && jj<2*kNChannels){
175 MeanPed[jj] = readValues[0][jj];
176 //printf("\t MeanPedhg[%d] = %1.1f\n",jj, MeanPedhg[jj]);
177 }
178 else if(jj>2*kNChannels && jj>4*kNChannels){
179 CorrCoeff0[jj-4*kNChannels] = readValues[0][jj];
180 CorrCoeff1[jj-4*kNChannels] = readValues[1][jj];;
181 }
182 }
183
184 /* report progress */
185 daqDA_progressReport(10);
186
187
188 /* init some counters */
189 int nevents_physics=0;
190 int nevents_total=0;
191
192 struct eventHeaderStruct *event;
193 eventTypeType eventT;
194
195 /* read the data files */
196 int n;
197 for(n=1;n<argc;n++){
198
199 status=monitorSetDataSource( argv[n] );
200 if (status!=0) {
201 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
202 return -1;
203 }
204
205 /* report progress */
206 /* in this example, indexed on the number of files */
207 daqDA_progressReport(10+80*n/argc);
208
209 /* read the file */
210 for(;;){
211
212 /* get next event */
213 status=monitorGetEventDynamic((void **)&event);
214 if(status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
215 if(status!=0) {
216 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
217 return -1;
218 }
219
220 /* retry if got no event */
221 if(event==NULL) {
222 break;
223 }
224
225 // Initalize raw-data reading and decoding
226 AliRawReader *reader = new AliRawReaderDate((void*)event);
227 reader->Select("ZDC");
228 // --- Reading event header
229 //UInt_t evtype = reader->GetType();
230 //printf("\n\t ZDCEMDda -> ev. type %d\n",evtype);
231 //printf("\t ZDCEMDda -> run # %d\n",reader->GetRunNumber());
232 //
233 AliZDCRawStream *rawStreamZDC = new AliZDCRawStream(reader);
234
235
236 /* use event - here, just write event id to result file */
237 eventT=event->eventType;
238
239 Int_t ich=0;
240 Int_t adcMod[2*kNChannels], adcCh[2*kNChannels], sigCode[2*kNChannels];
241 Int_t det[2*kNChannels], sec[2*kNChannels];
242 for(Int_t y=0; y<2*kNChannels; y++){
243 adcMod[y]=adcCh[y]=sigCode[y]=det[y]=sec[y]=0;
244 }
245
246 Int_t iScCh=0;
247 Int_t scMod[kNScChannels], scCh[kNScChannels], scSigCode[kNScChannels];
248 Int_t scDet[kNScChannels], scSec[kNScChannels];
249 for(Int_t y=0; y<kNScChannels; y++){
250 scMod[y]=scCh[y]=scSigCode[y]=scDet[y]=scSec[y]=0;
251 }
252 //
253 Int_t modNum=-1, modType=-1;
254
255 if(eventT==START_OF_DATA){
256
257 rawStreamZDC->SetSODReading(kTRUE);
258
259 // --------------------------------------------------------
260 // --- Writing ascii data file for the Shuttle preprocessor
261 mapFile4Shuttle = fopen(MAPDATA_FILE,"w");
262 if(!rawStreamZDC->Next()) printf(" \t No raw data found!! \n");
263 else{
264 while((rawStreamZDC->Next())){
265 if(rawStreamZDC->IsHeaderMapping()){ // mapping header
266 modNum = rawStreamZDC->GetADCModule();
267 modType = rawStreamZDC->GetModType();
268 }
269 if(rawStreamZDC->IsChMapping()){
270 if(modType==1){ // ADC mapping ----------------------
271 adcMod[ich] = rawStreamZDC->GetADCModFromMap(ich);
272 adcCh[ich] = rawStreamZDC->GetADCChFromMap(ich);
273 sigCode[ich] = rawStreamZDC->GetADCSignFromMap(ich);
274 det[ich] = rawStreamZDC->GetDetectorFromMap(ich);
275 sec[ich] = rawStreamZDC->GetTowerFromMap(ich);
276 //
277 fprintf(mapFile4Shuttle,"\t%d\t%d\t%d\t%d\t%d\t%d\n",
278 ich,adcMod[ich],adcCh[ich],sigCode[ich],det[ich],sec[ich]);
279 //
280 //printf(" Mapping in DA -> %d ADC: mod %d ch %d, code %d det %d, sec %d\n",
281 // ich,adcMod[ich],adcCh[ich],sigCode[ich],det[ich],sec[ich]);
282 //
283 ich++;
284 }
285 else if(modType==2){ //VME scaler mapping --------------------
286 scMod[iScCh] = rawStreamZDC->GetScalerModFromMap(iScCh);
287 scCh[iScCh] = rawStreamZDC->GetScalerChFromMap(iScCh);
288 scSigCode[iScCh] = rawStreamZDC->GetScalerSignFromMap(iScCh);
289 scDet[iScCh] = rawStreamZDC->GetScDetectorFromMap(iScCh);
290 scSec[iScCh] = rawStreamZDC->GetScTowerFromMap(iScCh);
291 //
292 fprintf(mapFile4Shuttle,"\t%d\t%d\t%d\t%d\t%d\t%d\n",
293 iScCh,scMod[iScCh],scCh[iScCh],scSigCode[iScCh],scDet[iScCh],scSec[iScCh]);
294 //
295 //printf(" Mapping in DA -> %d Scaler: mod %d ch %d, code %d det %d, sec %d\n",
296 // iScCh,scMod[iScCh],scCh[iScCh],scSigCode[iScCh],scDet[iScCh],scSec[iScCh]);
297 //
298 iScCh++;
299 }
300 }
301 }
302 }
303 fclose(mapFile4Shuttle);
304 }// SOD event
305
306 if(eventT==PHYSICS_EVENT){
307 // --- Reading data header
308 reader->ReadHeader();
309 const AliRawDataHeader* header = reader->GetDataHeader();
310 if(header){
311 UChar_t message = header->GetAttributes();
312 if((message & 0x70) == 0x70){ // DEDICATED EMD RUN
313 //printf("\t STANDALONE_EMD_RUN raw data found\n");
314 continue;
315 }
316 else{
317 printf("\t NO STANDALONE_EMD_RUN raw data found\n");
318 return -1;
319 }
320 }
321 else{
322 printf("\t ATTENTION! No Raw Data Header found!!!\n");
323 return -1;
324 }
325
326 rawStreamZDC->SetSODReading(kTRUE);
327
328 if (!rawStreamZDC->Next()) printf(" \t No raw data found!! ");
329 //
330 // ----- Setting ch. mapping -----
331 for(Int_t jk=0; jk<2*kNChannels; jk++){
332 rawStreamZDC->SetMapADCMod(jk, adcMod[jk]);
333 rawStreamZDC->SetMapADCCh(jk, adcCh[jk]);
334 rawStreamZDC->SetMapADCSig(jk, sigCode[jk]);
335 rawStreamZDC->SetMapDet(jk, det[jk]);
336 rawStreamZDC->SetMapTow(jk, sec[jk]);
337 }
338 //
339 Float_t ZDCRawADC[4], ZDCCorrADC[4], ZDCCorrADCSum[4];
340 for(Int_t g=0; g<4; g++){
341 ZDCCorrADCSum[g] = 0.;
342 ZDCRawADC[g] = 0.;
343 }
344 //
345 while(rawStreamZDC->Next()){
346 Int_t det = rawStreamZDC->GetSector(0);
347 Int_t quad = rawStreamZDC->GetSector(1);
348
349 if(rawStreamZDC->IsADCDataWord() && !(rawStreamZDC->IsUnderflow())
350 && !(rawStreamZDC->IsOverflow()) && det!=-1
351 && (rawStreamZDC->GetADCGain() == 1 && // Selecting LOW RES ch.s
352 rawStreamZDC->GetADCModule()>=kFirstADCGeo && rawStreamZDC->GetADCModule()<=kLastADCGeo)){
353
354 //printf(" IsADCWord %d, IsUnderflow %d, IsOverflow %d\n",
355 // rawStreamZDC->IsADCDataWord(),rawStreamZDC->IsUnderflow(),rawStreamZDC->IsOverflow());
356
357 // Taking LOW RES channels -> ch.+kNChannels !!!!
358 Int_t DetIndex=999, PedIndex=999;
359 // Not ZEM not PMRef && low gain chain
360 if((det!=3) && (quad!=5) && (rawStreamZDC->GetADCGain()==1)){
361 if(det == 1){
362 DetIndex = det-1;
363 PedIndex = quad+kNChannels;
364 }
365 else if(det==2){
366 DetIndex = det-1;
367 PedIndex = quad+5+kNChannels;
368 }
369 else if(det == 4){
370 DetIndex = det-2;
371 PedIndex = quad+12+kNChannels;
372 }
373 else if(det == 5){
374 DetIndex = det-2;
375 PedIndex = quad+17+kNChannels;
376 }
377 // Mean pedestal subtraction
378 Float_t Pedestal = MeanPed[PedIndex];
379 // Pedestal subtraction from correlation with out-of-time signals
380 //Float_t Pedestal = CorrCoeff0[PedIndex]+CorrCoeff1[PedIndex]*MeanPedOOT[PedIndex];
381 //
382 if(DetIndex!=999 || PedIndex!=999){
383 //
384 ZDCRawADC[DetIndex] += (Float_t) rawStreamZDC->GetADCValue();
385 //
386 //
387 ZDCCorrADC[DetIndex] = (rawStreamZDC->GetADCValue()) - Pedestal;
388 ZDCCorrADCSum[DetIndex] += ZDCCorrADC[DetIndex];
389 //
390 /*printf("\t det %d quad %d res %d pedInd %d detInd %d:"
391 "ADCCorr = %d, ZDCCorrADCSum = %d\n",
392 det,quad,rawStreamZDC->GetADCGain(),PedIndex,DetIndex,
393 (Int_t) ZDCCorrADC[DetIndex],(Int_t) ZDCCorrADCSum[DetIndex]);
394 */
395 }
396 // Not common nor ref. PM && low gain chain
397 if((quad!=0) && (rawStreamZDC->GetADCGain()==1)){
398 Float_t corrADCval = (rawStreamZDC->GetADCValue()) - Pedestal;
399 if(det==1) histZNCtow[quad]->Fill(corrADCval);
400 else if(det==2) histZPCtow[quad]->Fill(corrADCval);
401 else if(det==4) histZNAtow[quad]->Fill(corrADCval);
402 else if(det==5) histZPAtow[quad]->Fill(corrADCval);
403 }
404
405 if(DetIndex==999 || PedIndex==999)
406 printf(" WARNING! Detector a/o pedestal index are WRONG!!!\n");
407
408 }
409 }//IsADCDataWord()
410
411 }
412 //
413 nevents_physics++;
414 //
415 for(Int_t j=0; j<4; j++){
416 histoEMDRaw[j]->Fill(ZDCRawADC[j]);
417 histoEMDCorr[j]->Fill(ZDCCorrADCSum[j]);
418 }
419 }//(if PHYSICS_EVENT)
420
421 nevents_total++;
422
423 }
424
425 /* free resources */
426 free(event);
427 }
428
429 /* Analysis of the histograms */
430 //
431 FILE *fileShuttle1 = fopen(ENCALIBDATA_FILE,"w");
432 //
433 Int_t BinMax[4];
434 Float_t YMax[4];
435 Int_t NBinsx[4];
436 Float_t MeanFitVal[4];
437 TF1 *fitfun[4];
438 for(Int_t k=0; k<4; k++){
439 if(histoEMDCorr[k]->GetEntries() == 0){
440 printf("\n WARNING! Empty histos -> ending DA WITHOUT writing output\n\n");
441 return -1;
442 }
443 BinMax[k] = histoEMDCorr[k]->GetMaximumBin();
444 YMax[k] = (histoEMDCorr[k]->GetXaxis())->GetXmax();
445 NBinsx[k] = (histoEMDCorr[k]->GetXaxis())->GetNbins();
446// printf("\n\t Det%d -> BinMax = %d, ChXMax = %f\n", k+1, BinMax[k], BinMax[k]*YMax[k]/NBinsx[k]);
447 histoEMDCorr[k]->Fit("gaus","Q","",BinMax[k]*YMax[k]/NBinsx[k]*0.7,BinMax[k]*YMax[k]/NBinsx[k]*1.25);
448 fitfun[k] = histoEMDCorr[k]->GetFunction("gaus");
449 MeanFitVal[k] = (Float_t) (fitfun[k]->GetParameter(1));
450 //printf("\n\t Mean Value from gaussian fit = %f\n", MeanFitVal[k]);
451 }
452 //
453 Float_t CalibCoeff[6];
454 //Float_t icoeff[5];
455 //
456 for(Int_t j=0; j<6; j++){
457 if(j<4){
458 CalibCoeff[j] = MeanFitVal[j];
459 fprintf(fileShuttle1,"\t%f\n",CalibCoeff[j]);
460 }
461 // ZEM energy calib. coeff. = 1
462 else if(j==4 || j==5){
463 CalibCoeff[j] = 1.;
464 fprintf(fileShuttle1,"\t%f\n",CalibCoeff[j]);
465 }
466 }
467 fclose(fileShuttle1);
468
469 FILE *fileShuttle2 = fopen(TOWCALIBDATA_FILE,"w");
470 Float_t meanvalznc[4], meanvalzpc[4], meanvalzna[4], meanvalzpa[4];
471 for(Int_t j=0; j<4; j++){
472 if(histZNCtow[j]->GetEntries() == 0){
473 printf("\n WARNING! Empty histos -> ending DA WITHOUT writing output\n\n");
474 return -1;
475 }
476 meanvalznc[j] = histZNCtow[j]->GetMean();
477 meanvalzpc[j] = histZPCtow[j]->GetMean();
478 meanvalzna[j] = histZNAtow[j]->GetMean();
479 meanvalzpa[j] = histZPAtow[j]->GetMean();
480
481 // Note -> For the moment the inter-calibration coeff. are set to 1
482 /*for(Int_t k=0; k<4; k++){
483 icoeff[k] = 1.;
484 fprintf(fileShuttle2,"\t%f",icoeff[k]);
485 if(k==4) fprintf(fileShuttle2,"\n");
486 }*/
487 }
488 if(meanvalznc[1]!=0 && meanvalznc[2]!=0 && meanvalznc[3]!=0 &&
489 meanvalzpc[1]!=0 && meanvalzpc[2]!=0 && meanvalzpc[3]!=0 &&
490 meanvalzna[1]!=0 && meanvalzna[2]!=0 && meanvalzna[3]!=0 &&
491 meanvalzpa[1]!=0 && meanvalzpa[2]!=0 && meanvalzpa[3]!=0){
492 fprintf(fileShuttle2,"\t%f\t%f\t%f\t%f\n",
493 1.0,meanvalznc[0]/meanvalznc[1],meanvalznc[0]/meanvalznc[2],meanvalznc[0]/meanvalznc[3]);
494 fprintf(fileShuttle2,"\t%f\t%f\t%f\t%f\n",
495 1.0,meanvalzpc[0]/meanvalzpc[1],meanvalzpc[0]/meanvalzpc[2],meanvalzpc[0]/meanvalzpc[3]);
496 fprintf(fileShuttle2,"\t%f\t%f\t%f\t%f\n",
497 1.0,meanvalzna[0]/meanvalzna[1],meanvalzpc[0]/meanvalzna[2],meanvalzpc[0]/meanvalzna[3]);
498 fprintf(fileShuttle2,"\t%f\t%f\t%f\t%f\n",
499 1.0,meanvalzpa[0]/meanvalzpa[1],meanvalzpc[0]/meanvalzpa[2],meanvalzpc[0]/meanvalzpa[3]);
500 }
501 else{
502 printf("\n Tower intercalib. coeff. CAN'T be calculated (some mean values are ZERO)!!!\n\n");
503 return -1;
504 }
505 fclose(fileShuttle2);
506
507 for(Int_t ij=0; ij<4; ij++){
508 delete histoEMDRaw[ij];
509 delete histoEMDCorr[ij];
510 }
511
512 //delete minuitFit;
513 TVirtualFitter::SetFitter(0);
514
515 /* write report */
516 fprintf(fp,"Run #%s, received %d physics events out of %d\n",getenv("DATE_RUN_NUMBER"),nevents_physics,nevents_total);
517
518 /* close result file */
519 fclose(fp);
520
521 /* report progress */
522 daqDA_progressReport(90);
523
524 /* store the result file on FES */
525 status = daqDA_FES_storeFile(MAPDATA_FILE, "MAPPING");
526 if(status){
527 printf("Failed to export file : %d\n",status);
528 return -1;
529 }
530 //
531 status = daqDA_FES_storeFile(ENCALIBDATA_FILE, "EMDENERGYCALIB");
532 if(status){
533 printf("Failed to export file : %d\n",status);
534 return -1;
535 }
536 //
537 status = daqDA_FES_storeFile(TOWCALIBDATA_FILE, "EMDTOWERCALIB");
538 if(status){
539 printf("Failed to export file : %d\n",status);
540 return -1;
541 }
542
543 /* report progress */
544 daqDA_progressReport(100);
545
546 return status;
547}