DA for tuning FEE parameters
[u/mrichter/AliRoot.git] / VZERO / VZEROTUNING2da.cxx
1 /*********************************************************************************
2 - Contact:    Brigitte Cheynis     b.cheynis@ipnl.in2p3.fr
3 - Link:       http
4 - Raw data test file : 
5 - Reference run number :              
6 - Run Type:   STANDALONE
7 - DA Type:    LDC
8 - Number of events needed: 500
9 - Input Files:  argument list
10 - Output Files: local file  V0_Tuning2.dat
11                 FXS file    V0_Tuning2.dat
12 - Trigger types used: PHYSICS_EVENT
13 **********************************************************************************/
14
15
16 /**********************************************************************************
17 *                                                                                 *
18 * VZERO Detector Algorithm used for tuning FEE parameters                         *
19 *                                                                                 *
20 * This program reads data on the LDC                                              *
21 * It cumulates mean ADC responses (above pedestals),  populates local             * 
22 * "./V0_Tuning2.dat"  file and exports it to the FES.                             *                                             *
23 * We have 128 channels instead of 64 as expected for V0 due to the two sets of    *
24 * charge integrators which are used by the FEE ...                                *
25 * The program reports about its processing progress.                              *
26 *                                                                                 *
27 ***********************************************************************************/
28
29 // DATE
30 #include "event.h"
31 #include "monitor.h"
32 #include "daqDA.h"
33
34 //AliRoot
35 #include <AliVZERORawStream.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
51
52 /* Main routine --- Arguments: iteration number, data file */
53       
54 int main(int argc, char **argv) {
55
56 /* magic line from Cvetan */
57   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
58                     "*",
59                     "TStreamerInfo",
60                     "RIO",
61                     "TStreamerInfo()");
62   int status;
63
64   Float_t ADC_Mean[128];
65   for(Int_t i=0; i<128; i++) {
66       ADC_Mean[i] = 0.0;
67   } 
68       
69   /* log start of process */
70   printf("VZERO DA program started - Tuning FEE parameters\n");  
71
72   /* check that we got some arguments = list of files */
73   if (argc<2)   {
74       printf("Wrong number of arguments\n");
75       return -1;}
76       
77   /* open pedestal data file to read pedestal values for zero suppression */
78   FILE *file=NULL;
79   file=fopen("./V0_Ped_Width_Gain.dat","read");
80   if (file==NULL) {
81       printf("Failed to open pedestal data file\n");
82       return -1;}      
83   float Pedestal[128], Sigma[128];
84   for(Int_t j=0; j<128; j++){
85       fscanf(file,"%f %f ", &Pedestal[j], &Sigma[j]);
86 //      printf("Pedestals = %f %f\n",Pedestal[j], Sigma[j]);
87   }
88   fclose(file); 
89   /* end of pedestal data retrieval */
90          
91   /* open result file to be exported to FES */
92   FILE *fp=NULL;
93   fp=fopen("./V0_Tuning2.dat","a");
94   if (fp==NULL) {
95       printf("Failed to open result file\n");
96       return -1;}
97
98   /* open log file to inform user */
99   FILE *flog=NULL;
100   flog=fopen("./V00log.txt","a");
101   if (flog==NULL) {
102       printf("Failed to open log file\n");
103       return -1;  }
104     
105   /* report progress */
106   daqDA_progressReport(10);
107
108   /* init counters on events */
109   int nevents_physics=0;
110   int nevents_total=0;
111
112   /* read the data  */
113   
114   status=monitorSetDataSource( argv[1] );
115   if (status!=0) {
116       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
117       return -1; }
118
119   /* report progress */
120   daqDA_progressReport(50);
121
122   /* read the data file */
123   for(;;) {
124         struct eventHeaderStruct *event;
125         eventTypeType eventT;
126
127         /* get next event */
128         status=monitorGetEventDynamic((void **)&event);
129         if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
130         if (status!=0) {
131             printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
132             return -1; }
133
134         /* retry if got no event */
135         if (event==NULL) break;
136         
137         /* decode event */
138         eventT=event->eventType;
139         
140         switch (event->eventType){
141       
142         case START_OF_RUN:
143              break;
144       
145         case END_OF_RUN:
146              printf("End Of Run detected\n");
147              break;
148       
149         case PHYSICS_EVENT:
150              nevents_physics++;
151              
152 //              fprintf(flog,"Run #%lu, event size: %lu, BC:%u, Orbit:%u, Period:%u\n",
153 //                  (unsigned long)event->eventRunNb,
154 //                  (unsigned long)event->eventSize,
155 //                  EVENT_ID_GET_BUNCH_CROSSING(event->eventId),
156 //                  EVENT_ID_GET_ORBIT(event->eventId),
157 //                  EVENT_ID_GET_PERIOD(event->eventId) );
158                  
159              AliRawReader *rawReader = new AliRawReaderDate((void*)event);
160   
161              AliVZERORawStream* rawStream  = new AliVZERORawStream(rawReader); 
162              rawStream->Next(); 
163              for(Int_t i=0; i<64; i++) {
164                 if(!rawStream->GetIntegratorFlag(i,10))
165                 {
166                     if((rawStream->GetADC(i)-Pedestal[i]) > (Pedestal[i]+3*Sigma[i]) )
167                     ADC_Mean[i]=ADC_Mean[i]+(rawStream->GetADC(i)-Pedestal[i]);  // even integrator 
168                 }
169                 else 
170                 {                  
171                     if((rawStream->GetADC(i)-Pedestal[i+64]) > (Pedestal[i+64]+3*Sigma[i+64]) )
172                     ADC_Mean[i+64]=ADC_Mean[i+64]+(rawStream->GetADC(i)-Pedestal[i+64]); // odd integrator
173                 }
174              }    
175              delete rawStream;
176              rawStream = 0x0;      
177              delete rawReader;
178              rawReader = 0x0;                                                                    
179         } // end of switch on event type 
180         
181         nevents_total++;
182         /* free resources */
183         free(event);
184
185   }  // loop over events
186     
187 //________________________________________________________________________
188 //  Computes mean values, dumps them into the output text file
189         
190   for(Int_t i=0; i<128; i++) {
191       ADC_Mean[i]=ADC_Mean[i]/nevents_physics;
192       fprintf(fp," %d %d %f\n",argc,i,ADC_Mean[i]);
193       fprintf(flog," %d %d %f\n",argc,i,ADC_Mean[i]);
194   } 
195   
196 //________________________________________________________________________
197    
198   /* write report */
199   fprintf(flog,"Run #%s, received %d physics events out of %d\n",getenv("DATE_RUN_NUMBER"),nevents_physics,nevents_total);
200   printf("Run #%s, received %d physics events out of %d\n",getenv("DATE_RUN_NUMBER"),nevents_physics,nevents_total);
201   
202   /* close result and log files */
203   fclose(fp);
204   fclose(flog); 
205   
206   /* report progress */
207   daqDA_progressReport(90);
208
209   /* export result file to FES */
210   status=daqDA_FES_storeFile("./V0_Tuning2.dat","V00da_results");
211   if (status)    {
212       printf("Failed to export file : %d\n",status);
213       return -1; }
214
215   /* report progress */
216   daqDA_progressReport(100);
217   
218   return status;
219 }