]> git.uio.no Git - u/mrichter/AliRoot.git/blob - VZERO/VZEROCHANNELDELAYTUNINGda.cxx
Switch On/Off chambers instead of stations (Javier)
[u/mrichter/AliRoot.git] / VZERO / VZEROCHANNELDELAYTUNINGda.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:   CHANNEL_DELAY_TUNING
7 - DA Type:    LDC
8 - Number of events needed: 500
9 - Input Files:  argument list
10 - Output Files: local file  V0_ChannelDelayTuning.dat
11                 FXS file    V0_ChannelDelayTuning.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 fBB and fBG flags, populates local "./V0_ChannelDelayTuning.dat"   *            
22 * file, exports it to the FES, and stores it to DAQ DB                            *
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: list of DATE raw data files */
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   status = daqDA_DB_getFile("V00da_results","./V0_Ped_Width_Gain.dat");
64   if (status) {
65       printf("Failed to get Pedestal file (V0_Ped_Width_Gain.dat) from DAQ DB, status=%d\n", status);
66       return -1;   
67   }
68   
69   Float_t MeanPed[128], SigPed[128], fdump;
70   
71   /* open the pedestal file and retrieve pedestal mean and sigma */
72   FILE *fpPed = fopen("V0_Ped_Width_Gain.dat","r");
73   for(int i=0;i<128;i++){
74       fscanf(fpPed,"%f %f %f %f \n",&MeanPed[i],&SigPed[i],&fdump,&fdump);
75 //      printf("%.3f %.3f \n",MeanPed[i],SigPed[i]);
76   }
77   fclose(fpPed);
78
79 //______________________________________________________________________________
80 // Get running parameters from V00_ChannelDelayTuning_DA.config file
81
82   Int_t    kNbEventSkipped;   // = 100;   number of events skipped - to be tuned
83   Float_t  kSigmaCut;         // = 3.0;   number of sigmas for threshold cut - to be tuned
84   
85   status = daqDA_DB_getFile("V00_ChannelDelayTuning_DA.config","./V00_ChannelDelayTuning_DA.config");
86   if (status) {
87       printf("Failed to get config file (V00_ChannelDelayTuning_DA.config) from DAQ DB, status=%d\n", status);
88       return -1;   
89   }
90   /* open the config file and retrieve running parameters */
91   FILE *fpConfig = fopen("V00_ChannelDelayTuning_DA.config","r");
92   fscanf(fpConfig,"%f %d",&kSigmaCut,&kNbEventSkipped);
93   fclose(fpConfig);
94   
95   printf("Number of events skipped = %d ; Number of sigmas for threshold cut = %f\n",kNbEventSkipped,kSigmaCut);
96 //______________________________________________________________________________
97
98   Int_t BBFlag[64];
99   Int_t BGFlag[64];
100   Int_t ChargeEoI = 0;
101   Bool_t Integrator = 0;
102   Int_t NHit[64];
103   for(Int_t i=0; i<64; i++) {
104       BBFlag[i] = 0;
105       BGFlag[i] = 0;
106       NHit[i] = 0;
107   } 
108       
109   /* log start of process */
110   printf("VZERO DA program started - Channel Delay Tuning \n");  
111
112   /* check that we got some arguments  */
113   if (argc<2)   {
114       printf("Wrong number of arguments\n");
115       return -1;}
116
117   /* open result file to be exported to FES */
118   FILE *fp=NULL;
119   fp=fopen("./V0_ChannelDelayTuning.dat","a");
120   if (fp==NULL) {
121       printf("Failed to open result file\n");
122       return -1;}
123
124   /* open log file to inform user */
125   FILE *flog=NULL;
126   flog=fopen("./V00log.txt","w");
127   if (flog==NULL) {
128       printf("Failed to open log file\n");
129       return -1;  }
130     
131   /* report progress */
132   daqDA_progressReport(10);
133
134   /* init counters on events */
135   int nevents_physics=0;
136   int nevents_total=0;
137   int iteration;
138   sscanf(argv[1],"%d",&iteration);
139   
140   /* read the n data files */
141   for (int n=2; n<argc; n++) {
142   
143   /* read the data  */
144     status=monitorSetDataSource( argv[n] );
145     if (status!=0) {
146         printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
147         return -1; }
148
149   /* report progress */
150     daqDA_progressReport(10+50*n/argc);
151
152   /* read the data file */
153     for(;;) {
154         struct eventHeaderStruct *event;
155         eventTypeType eventT;
156
157         /* get next event */
158         status=monitorGetEventDynamic((void **)&event);
159         if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
160         if (status!=0) {
161             printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
162             return -1; }
163
164         /* retry if got no event */
165         if (event==NULL) break;
166         
167         /* decode event */
168         eventT=event->eventType;
169         
170         switch (event->eventType){
171       
172         case START_OF_RUN:
173              printf("START Of Run detected\n");
174              break;
175       
176         case END_OF_RUN:
177              printf("END Of Run detected\n");
178              break;
179       
180         case PHYSICS_EVENT:
181              nevents_physics++;
182
183              AliRawReader *rawReader = new AliRawReaderDate((void*)event);
184   
185              AliVZERORawStream* rawStream  = new AliVZERORawStream(rawReader); 
186              rawStream->Next(); 
187              if(nevents_physics > kNbEventSkipped){
188                 for(Int_t i=0; i<64; i++) {
189                     ChargeEoI = rawStream->GetADC(i);
190                     Integrator = rawStream->GetIntegratorFlag(i,10);
191                     Float_t Threshold = MeanPed[i + 64 * Integrator] + kSigmaCut * SigPed[i + 64 * Integrator];
192
193                     if((float)ChargeEoI>Threshold) {
194                         NHit[i]+=1;
195                         if(rawStream->GetBBFlag(i,10)) BBFlag[i]+=1;       
196                         if(rawStream->GetBGFlag(i,10)) BGFlag[i]+=1; 
197                     }            
198                 }    
199              }
200              delete rawStream;
201              rawStream = 0x0;      
202              delete rawReader;
203              rawReader = 0x0;                                                                    
204         } // end of switch on event type 
205         
206         nevents_total++;
207         /* free resources */
208         free(event);
209
210     }    // end of loop over events
211   }      // end of loop over data files 
212    
213 //________________________________________________________________________
214 //  Computes mean values, dumps them into the output text file
215   Float_t fBB, fBG;     
216   for(Int_t i=0; i<64; i++) {      
217       if(NHit[i] > 0) {
218          fBB = (float)BBFlag[i]/(float)NHit[i];
219          fBG = (float)BGFlag[i]/(float)NHit[i];
220        }else{
221          fBB = fBG = 0.;
222        }
223        fprintf(fp," %d %d %f %f\n",iteration,i,fBB,fBG);
224        fprintf(flog," it %d ch %d BB %f BG %f BBFlag %d BGFlag %d Hit %d\n",iteration,i,fBB,fBG,BBFlag[i],BGFlag[i],NHit[i]);
225   } 
226   
227 //________________________________________________________________________
228    
229   /* write report */
230   fprintf(flog,"Run #%s, received %d physics events out of %d\n",getenv("DATE_RUN_NUMBER"),nevents_physics,nevents_total);
231   printf("Run #%s, received %d physics events out of %d\n",getenv("DATE_RUN_NUMBER"),nevents_physics,nevents_total);
232   
233   /* close result and log files */
234   fclose(fp);
235   fclose(flog); 
236   
237   /* report progress */
238   daqDA_progressReport(90);
239
240   /* export result file to FES */
241   status=daqDA_FES_storeFile("./V0_ChannelDelayTuning.dat","V00da_ChannelDelayTuning");
242   if (status)    {
243       printf("Failed to export file : %d\n",status);
244       return -1; }
245       
246   /* store result file on Online DB */
247 //   status=daqDA_DB_storeFile("./V0_ChannelDelayTuning.dat","V00da_ChannelDelayTuning");
248 //   if (status)    {
249 //       printf("Failed to store file into Online DB: %d\n",status);
250 //       return -1; }
251       
252   /* report progress */
253   daqDA_progressReport(100);
254   
255   return status;
256 }