]> git.uio.no Git - u/mrichter/AliRoot.git/blob - VZERO/VZEROPbPbda.cxx
4f3086459c2dd4fb43f492ced566b61f2af88b65
[u/mrichter/AliRoot.git] / VZERO / VZEROPbPbda.cxx
1 /*********************************************************************************
2 - Contact:    Brigitte Cheynis     b.cheynis@ipnl.in2p3.fr
3 - Link:
4 - Raw data test file :          
5 - Reference run number : 137366       
6 - Run Type:   PHYSICS
7 - DA Type:    MON
8 - Number of events needed: >=2000
9 - Input Files:  argument list
10 - Output Files: FXS file     V0_EqualizationFactors.dat (Channel equalization factors)
11 - Trigger types used: PHYSICS_EVENT
12 **********************************************************************************/
13
14 /**********************************************************************************
15 *                                                                                 *
16 * VZERO Detector Algorithm for extracting channel equalization factors for Pb-Pb  *
17 *                                                                                 *
18 *                                                                                 *
19 ***********************************************************************************/
20
21 // DATE
22 #include "event.h"
23 #include "monitor.h"
24 #include "daqDA.h"
25
26 //AliRoot
27 #include <AliVZERORawStream.h>
28 #include <AliRawReaderDate.h>
29 #include <AliRawReader.h>
30 #include <AliDAQ.h>
31
32 // standard
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 //ROOT
37 #include "TROOT.h"
38 #include "TPluginManager.h"
39 #include <TFile.h>
40 #include <TH1F.h>
41 #include <TMath.h>
42
43 Int_t GetOfflineChannel(Int_t channel);
44
45 /* Main routine --- Arguments: monitoring data source */
46       
47 int main(int argc, char **argv) {
48
49 /* magic line from Cvetan */
50   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
51                     "*",
52                     "TStreamerInfo",
53                     "RIO",
54                     "TStreamerInfo()");
55   int status;
56   if (argc!=2) {
57      printf("Wrong number of arguments\n");
58      return -1;
59   }
60   
61 //___________________________________________________
62 // Get parameters from V00DAEqualFactors.config file
63
64   Int_t    kStartClock = 9;  // First clock in the search for max adc
65   Int_t    kEndClock = 11;   // Last clock in the search for max adc
66   Int_t    kNPreClocks = 6;  // Number of clock before max used in the charge sum
67   Int_t    kNPostClocks = 1; // Number of clock after max used in the charge sum
68
69   UChar_t  kNFlagsCut   = 63;// Minimum number of TDC flags requested, replaces the trigger selection
70
71   Int_t    kNBins = 10000;
72   Float_t  kRange = 0.1;
73
74   status = daqDA_DB_getFile("V00DAEqualFactors.config","./V00DAEqualFactors.config");
75   if (status) {
76     printf("Failed to get Config file (V00DAEqualFactors.config) from DAQ DB, status=%d\n", status);
77     printf("Take default values of parameters for pedestal calculation \n");
78   } else {
79     /* open the config file and retrieve cuts */
80     FILE *fpConfig = fopen("V00DAEqualFactors.config","r");
81     int res = fscanf(fpConfig,"%d %d %d %d %hhu %d %f",
82                      &kStartClock,&kEndClock,&kNPreClocks,&kNPostClocks,&kNFlagsCut,&kNBins,&kRange);
83     if(res!=7) {
84       printf("Failed to get values from Config file (V00DAEqualFactors.config): wrong file format - 6 integers and 1 float are expected - \n");
85     }
86     fclose(fpConfig);
87   }
88   
89   printf("First LHC Clock = %d; Last LHC Clock = %d; N Pre Clock = %d ; N Post Clock = %d; Minimum number of TDC flags = %hhu; Number of histogram bins = %d; Histogram range = %.3f\n",
90          kStartClock, kEndClock, kNPreClocks, kNPostClocks, kNFlagsCut, kNBins, kRange);
91
92   TH1D *fMedian[64];
93   for(Int_t j = 0; j < 64; ++j) fMedian[j] = new TH1D(Form("fMedian_%d",j),"Slopes weighted median, channel par channel",kNBins,0,kRange);
94
95   Bool_t fFirst = kTRUE;
96   Float_t fPrevTotCharge = 0;
97   Float_t fPrevadc[64];
98   for(Int_t j = 0; j < 64; ++j) fPrevadc[j] = 0;
99
100 //___________________________________________________ 
101    
102   /* define data source : this is argument 1 */  
103   status=monitorSetDataSource( argv[1] );
104   if (status!=0) {
105      printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
106      return -1;
107   }
108
109   /* declare monitoring program */
110   status=monitorDeclareMp( __FILE__ );
111   if (status!=0) {
112     printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
113     return -1;
114   }
115
116   /* define wait event timeout - 1s max */
117   monitorSetNowait();
118   monitorSetNoWaitNetworkTimeout(1000);
119   
120   /* init counters on events */
121   int neventsPhysics=0;
122   int neventsTotal=0;
123
124
125   /* loop on events (infinite) */
126   for(;;) {
127       struct eventHeaderStruct *event;
128       eventTypeType eventT;
129
130       /* check shutdown condition */
131       if (daqDA_checkShutdown()) {break;}   
132
133       /* get next event (blocking call until timeout) */
134       status=monitorGetEventDynamic((void **)&event);
135       if (status==MON_ERR_EOF) {
136           printf ("End of File detected\n");
137       break; /* end of monitoring file has been reached */
138       }
139     
140       if (status!=0) {
141            printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
142       break;
143       }
144
145       /* retry if got no event */
146       if (event==NULL) continue;
147                
148       /* decode event */
149       eventT=event->eventType;
150         
151       switch (event->eventType){
152       
153       case START_OF_RUN:
154            break;
155       
156       case END_OF_RUN:
157            printf("End Of Run detected\n");
158            break;
159       
160       case PHYSICS_EVENT:
161                          
162            AliRawReader *rawReader = new AliRawReaderDate((void*)event);
163   
164            AliVZERORawStream* rawStream  = new AliVZERORawStream(rawReader); 
165            if (rawStream->Next()) {     
166              UShort_t triggers = rawStream->GetTriggerInputs();
167              UChar_t nFlags = 0;
168              for(Int_t i = 0; i < 64; ++i) {
169                if (rawStream->GetBBFlag(i,10)) nFlags++;
170              }
171              if (nFlags >= kNFlagsCut) { // Check if the number of minimum number of TDC flags is reached (trigger selection)
172
173                neventsPhysics++;
174
175                Float_t adc[64];
176                Float_t totCharge = 0.;
177                for(Int_t i = 0; i < 64; ++i) {
178                  adc[i] = 0.;
179                  Float_t maxadc=0.;
180                  Int_t imax=-1;
181                  for(Int_t j = kStartClock; j <= kEndClock; ++j) {
182                    Float_t charge = (Float_t)(rawStream->GetPedestal(i,j));
183                    if(charge > maxadc){
184                      maxadc = charge;
185                      imax   = j;
186                    }
187                  }
188
189                  if (imax != -1) {
190                    Int_t start = imax - kNPreClocks;
191                    if (start < 0) start = 0;
192                    Int_t end = imax + kNPostClocks;
193                    if (end > 20) end = 20;
194                    for(Int_t iClock = start; iClock <= end; iClock++) {
195                      adc[i] += (Float_t)(rawStream->GetPedestal(i,iClock));
196                    }
197                  }
198                  totCharge += adc[i];
199                }
200
201                if (fFirst) {
202                  fFirst = kFALSE;
203                  fPrevTotCharge = totCharge;
204                  for(int i = 0; i < 64; ++i) fPrevadc[i] = adc[i];
205                }
206                else {
207                  fFirst = kTRUE;
208                  Float_t deltaTotCharge = totCharge - fPrevTotCharge;
209                  Float_t weight = deltaTotCharge*deltaTotCharge;
210                  if (weight > 1) {
211                    for(int i = 0; i < 64; ++i) {
212                      fMedian[i]->Fill((adc[i]-fPrevadc[i])/deltaTotCharge,weight);
213                    }
214                  }
215                }
216
217              }
218            }   // End : if rawstream
219            delete rawStream;
220            rawStream = 0x0;      
221            delete rawReader;
222            rawReader = 0x0;                                                                      
223       } // end of switch on event type 
224         
225       neventsTotal++;
226       /* free resources */
227       free(event);
228         
229       /* exit when last event received, no need to wait for TERM signal */
230       if (eventT==END_OF_RUN) {
231         printf("End Of Run event detected\n");
232         break;
233       }
234
235   }  // loop over events
236   
237   printf("%d physics events processed\n",neventsPhysics);
238     
239 //___________________________________________________________________________
240 //  Computes regression parameters
241 // charge_i = p0 + charge_tot * p1
242
243   if(neventsPhysics>2000){
244     /* open result file to be exported to FES */
245     FILE *fp=NULL;
246     fp=fopen("./V0_EqualizationFactors.dat","w");
247     if (fp==NULL) {
248       printf("Failed to open local result file\n");
249       return -1;}
250
251     Double_t beta[64];
252     Double_t q = 0.5;
253     for(int i = 0; i < 64; ++i) fMedian[i]->GetQuantiles(1,&beta[i],&q);
254
255     for(Int_t i=0; i<64; i++) {
256       fprintf(fp," %d %.3f\n",GetOfflineChannel(i), beta[i]*64.);                                      
257       printf(" %d %.3f\n",GetOfflineChannel(i), beta[i]*64.);                                  
258     }
259
260     /* close local result file and FXS result file*/
261     fclose(fp);
262   }
263
264 //________________________________________________________________________
265    
266   /* export result file to FES */
267   status=daqDA_FES_storeFile("./V0_EqualizationFactors.dat","V00DAEqualFactors");
268   if (status)    {
269     printf("Failed to export file : %d\n",status);
270     return -1; }
271
272   /* store result file into Online DB */
273   status=daqDA_DB_storeFile("./V0_EqualizationFactors.dat","V00DAEqualFactors");
274   if (status)    {
275     printf("Failed to store file into Online DB: %d\n",status);
276     return -1; }
277
278   return status;
279 }
280
281  Int_t GetOfflineChannel(Int_t channel) {
282
283 // Channel mapping Online - Offline:
284  
285  Int_t fOfflineChannel[64] = {39, 38, 37, 36, 35, 34, 33, 32, 
286                               47, 46, 45, 44, 43, 42, 41, 40, 
287                               55, 54, 53, 52, 51, 50, 49, 48, 
288                               63, 62, 61, 60, 59, 58, 57, 56,
289                                7,  6,  5,  4,  3,  2,  1,  0, 
290                               15, 14, 13, 12, 11, 10,  9,  8,
291                               23, 22, 21, 20, 19, 18, 17, 16, 
292                               31, 30, 29, 28, 27, 26, 25, 24};
293  return fOfflineChannel[channel];                     
294 }