]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PMD/PMDGAINda.cxx
reference run added
[u/mrichter/AliRoot.git] / PMD / PMDGAINda.cxx
1 /*
2 PMD DA for online calibration
3
4 contact: basanta@phy.iitb.ac.in
5 Link:
6 Reference run:/afs/cern.ch/user/s/sjena/public/run83496.raw
7 Run Type: PHYSICS
8 DA Type: MON
9 Number of events needed: 1 million for PB+PB, 200 milion for p+p
10 Input Files: Run0_999999999_v0_s0.root,PMD_PED.root, PMD_GAIN_CONFIGFILE, pmd_gain_tempfile.dat
11 Output Files: PMDGAINS.root, to be exported to the DAQ FES
12 Trigger types used: PHYSICS_EVENT
13
14 */
15 extern "C" {
16 #include <daqDA.h>
17 }
18
19 #include "event.h"
20 #include "monitor.h"
21
22 #include <Riostream.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 //AliRoot
27 #include "AliRawReaderDate.h"
28 #include "AliPMDCalibPedestal.h"
29 #include "AliPMDCalibGain.h"
30 #include "AliLog.h"
31 #include "AliCDBManager.h"
32
33 //ROOT
34 #include "TFile.h"
35 #include "TH1F.h"
36 #include "TBenchmark.h"
37 #include "TTree.h"
38 #include "TROOT.h"
39 #include "TPluginManager.h"
40 #include "TSystem.h"
41
42
43 /* Main routine
44       Arguments: 
45       1- monitoring data source
46 */
47 int main(int argc, char **argv) {
48   
49     /* magic line from Rene */
50     gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
51                                           "*",
52                                           "TStreamerInfo",
53                                           "RIO",
54                                           "TStreamerInfo()");
55
56
57     int status = 0;
58
59
60     Int_t filestatus = -1, xvar = 5;
61     Int_t totevt = -1, maxevt = -1;
62     Int_t hotevtsize = -1;
63     Bool_t hotfilestatus = false;
64
65     // Reads the pedestal file and keep the values in memory for subtraction
66
67     AliPMDCalibGain calibgain;
68
69     // Fetch the pedestal file - PMD_PED.root 
70
71     status = daqDA_DB_getFile("PMD_PED.root","PMD_PED.root");
72
73     if(!status)
74       {
75         printf("*** Pedestal file retrieved from DB *** \n");
76       }
77     else
78       {
79         printf("*** Pedestal file NOT retrieved from DB *** \n");
80         return -1;
81       }
82     
83     Int_t pstatus = calibgain.ExtractPedestal("PMD_PED.root");
84
85     if(pstatus == -3) return -3;
86
87     TTree *ic    = NULL;
88     TTree *meanc = NULL;
89
90     // Retrieve the PMD_GAIN_CONFIGFILE
91     status = daqDA_DB_getFile("PMD_GAIN_CONFIGFILE","PMD_GAIN_CONFIGFILE");
92
93     FILE *fp1 = NULL;
94
95     fp1 = fopen("PMD_GAIN_CONFIGFILE","r");
96
97     if (fp1 == NULL)
98       {
99         printf("*** PMD GAIN Configfile doesn't exist,Provide one ***\n");
100         return -1;
101       }
102     else
103       {
104         fscanf(fp1,"%d %d %d %d %d\n",&filestatus, &xvar, &totevt, &maxevt, &hotevtsize);
105         //printf("%d %d %d %d %d\n",filestatus, xvar, totevt, maxevt, hotevtsize);
106       }
107     fclose(fp1);
108
109     
110     if (filestatus == 1)
111       {
112         // Retrieve the Temporray ascii file from DB
113         status = daqDA_DB_getFile("pmd_gain_tempfile.dat","pmd_gain_tempfile.dat");
114         if(!status)
115           {
116             calibgain.ReadTempFile("pmd_gain_tempfile.dat");
117           }
118         else
119           {
120             printf("--- pmd_gain_tempfile.dat: not retrieved from DB ---\n");
121           }
122         // Retrieve the hot cell file from DB - PMD_HOT.root
123         status = daqDA_DB_getFile("PMD_HOT.root","PMD_HOT.root");
124         if(!status)
125           {
126             calibgain.ExtractHotChannel("PMD_HOT.root");
127           }
128         else
129           {
130             printf("--- pmd_gain_tempfile.dat: not retrieved from DB ---\n");
131           }
132       }
133
134
135     // decoding the events
136     
137
138     if (argc!=2) {
139         printf("Wrong number of arguments\n");
140         return -1;
141     }
142     
143     
144     /* define data source : this is argument 1 */  
145     status=monitorSetDataSource( argv[1] );
146     if (status!=0) {
147         printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
148         return -1;
149     }
150     
151     /* declare monitoring program */
152     status=monitorDeclareMp( __FILE__ );
153     if (status!=0) {
154         printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
155         return -1;
156     }
157     
158     /* define wait event timeout - 1s max */
159     monitorSetNowait();
160     monitorSetNoWaitNetworkTimeout(1000);
161     
162     /* log start of process */
163     printf("PMD GAIN DA - strted generating the gain of a cell\n");  
164     
165     /* init some counters */
166     int nevents_physics=0;
167     int nevents_total=0;
168     
169     struct eventHeaderStruct *event;
170     eventTypeType eventT = 0;
171
172     Int_t iev=0;
173
174     // Get run number
175     /*
176     if (getenv("DATE_RUN_NUMBER")==0) {
177       printf("DATE_RUN_NUMBER not properly set.\n");
178       return -1;
179     }
180     int runNr = atoi(getenv("DATE_RUN_NUMBER"));
181     */
182
183     int runNr = 0;
184
185     if (gSystem->AccessPathName("localOCDB/PMD/Calib/Mapping",kFileExists))
186       {
187         if (gSystem->mkdir("localOCDB/PMD/Calib/Mapping",kTRUE) != 0)
188           {
189             printf("Failed to create directory: localOCDB/PMD/Calib/Mapping");
190             return -1;
191           }
192       }
193     status = daqDA_DB_getFile("PMD/Calib/Mapping","localOCDB/PMD/Calib/Mapping/Run0_999999999_v0_s0.root");
194     if (status)
195       {
196         printf("Failed to get PMD-Mapping file (PMD/Calib/Mapping) from DAQdetDB, status=%d\n", status);
197         return -1;
198       }
199
200     // Global initializations
201     AliLog::SetGlobalLogLevel(AliLog::kError);
202     AliCDBManager *man = AliCDBManager::Instance();
203     man->SetDefaultStorage("local://localOCDB");
204     man->SetRun(runNr);
205
206     
207     /* main loop (infinite) */
208     for(;;) {
209         
210         /* check shutdown condition */
211         if (daqDA_checkShutdown()) {break;}
212         
213         /* get next event (blocking call until timeout) */
214         status=monitorGetEventDynamic((void **)&event);
215         if (status==MON_ERR_EOF) {
216             printf ("End of File detected\n");
217             break; /* end of monitoring file has been reached */
218         }
219         
220         if (status!=0) {
221             printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
222             break;
223         }
224         
225         /* retry if got no event */
226         if (event==NULL) {
227             continue;
228         }
229         
230         iev++; 
231         
232         /* use event - here, just write event id to result file */
233         nevents_total++;
234         eventT=event->eventType;
235         switch (event->eventType){
236       
237             /* START OF RUN */
238             case START_OF_RUN:
239                 break;
240                 /* END START OF RUN */
241                 
242                 /* END OF RUN */
243             case END_OF_RUN:
244                 break;
245                 
246             case PHYSICS_EVENT:
247                 nevents_physics++;
248                 totevt++;
249                 //if(nevents_physics%100 == 0)printf("Physis Events = %d\n",nevents_physics);
250                 AliRawReader *rawReader = new AliRawReaderDate((void*)event);
251                 TObjArray *pmdddlcont = new TObjArray();
252                 calibgain.ProcessEvent(rawReader, pmdddlcont);
253
254                 if (totevt%hotevtsize == 0) hotfilestatus = true;
255                 delete pmdddlcont;
256                 pmdddlcont = 0x0;
257                 delete rawReader;
258                 rawReader = 0x0;
259                 
260         }
261        
262         /* free resources */
263         free(event);
264         
265     }
266
267     /* exit when last event received, no need to wait for TERM signal */
268
269
270     ic    = new TTree("ic","PMD Gain tree");
271     meanc = new TTree("meanc","PMD Module mean tree");
272
273     if (filestatus == 0)
274       {
275         TFile *hotRun = new TFile ("PMD_HOT.root","RECREATE");
276
277         TTree *hot = new TTree("hot","PMD Hot cell tree");
278         
279         calibgain.FindHotCell(hot,xvar);
280         
281         hot->Write();
282         hotRun->Close();
283
284         // store the hot cell root file in the DB
285
286         status = daqDA_DB_storeFile("PMD_HOT.root","PMD_HOT.root");
287
288         // store the hot cell root file in the file exchange server
289
290         printf("root file for hot cell is created and getting exported\n");
291         status = daqDA_FES_storeFile("PMD_HOT.root","PMD_HOT.root");
292       }
293
294     if (hotfilestatus)
295       {
296         TFile *hotRun = new TFile ("PMD_HOT.root","RECREATE");
297
298         TTree *hot = new TTree("hot","PMD Hot cell tree");
299         
300         calibgain.FindHotCell(hot,xvar);
301         
302         hot->Write();
303         hotRun->Close();
304
305         // store the hot cell root file in the DB
306
307         status = daqDA_DB_storeFile("PMD_HOT.root","PMD_HOT.root");
308         
309         // store the hot cell root file in the file exchange server
310
311         printf("root file for hot cell is created and getting exported\n");
312         status = daqDA_FES_storeFile("PMD_HOT.root","PMD_HOT.root");
313       }
314
315
316     fp1 = fopen("PMD_GAIN_CONFIGFILE","w+");
317
318     if (totevt < maxevt)
319       {
320         printf("-----------------------------------------------\n");
321         printf("***  Required Number of Events not reached  ***\n");
322         printf("***  Number of Events processed = %d        ***\n",totevt);
323         printf("***  Writing the intermediate ASCII file    ***\n");
324         printf("-----------------------------------------------\n");
325
326         calibgain.WriteTempFile("pmd_gain_tempfile.dat");
327
328         // Store the Intermediate ascii file in the DB
329         status = daqDA_DB_storeFile("pmd_gain_tempfile.dat","pmd_gain_tempfile.dat");
330
331         filestatus = 1;
332         fprintf(fp1,"%d %d %d %d %d\n",filestatus,xvar,totevt,maxevt,hotevtsize);
333         fclose(fp1);
334
335         // Store the configfile in the DB
336         status = daqDA_DB_storeFile("PMD_GAIN_CONFIGFILE","PMD_GAIN_CONFIGFILE");
337
338       }
339     else if (totevt >= maxevt)
340       {
341         printf("-----------------------------------------------\n");
342         printf("***  Required Number of Events reached = %d ***\n",totevt);
343         printf("***  Writing the PMDGAINS.root file           ***\n");
344         printf("-----------------------------------------------\n");
345
346         calibgain.Analyse(ic, meanc);
347
348         TFile * gainRun = new TFile ("PMDGAINS.root","RECREATE"); 
349         ic->Write();
350         gainRun->Close();
351
352         TFile * meanRun = new TFile ("PMD_MEAN_SM.root","RECREATE"); 
353         meanc->Write();
354         meanRun->Close();
355
356
357         filestatus = 0;
358         totevt     = 0;
359         fprintf(fp1,"%d %d %d %d %d\n",filestatus,xvar,totevt,maxevt,hotevtsize);
360         fclose(fp1);
361
362         // Store the configfile in the DB
363         status = daqDA_DB_storeFile("PMD_GAIN_CONFIGFILE","PMD_GAIN_CONFIGFILE");
364       }
365     
366     delete ic;
367     ic = 0;
368
369     delete meanc;
370     meanc = 0;
371     
372
373     /* store the result file on FES */
374  
375     if (filestatus == 0)
376       {
377         printf("root file for cell gain is created and getting exported\n");
378         status = daqDA_FES_storeFile("PMDGAINS.root","PMDGAINS.root");
379         printf("root file for normalised means of different modules\n");
380         status = daqDA_FES_storeFile("PMD_MEAN_SM.root","PMD_MEAN_SM.root");
381       }
382
383     if (hotfilestatus)
384       {
385       }
386
387     
388     if (status) {
389       status = -2;
390     }
391
392
393
394     return status;
395 }