]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PMD/PMDPEDESTALda.cxx
prtection against detector number not to crash for database access
[u/mrichter/AliRoot.git] / PMD / PMDPEDESTALda.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: PEDESTAL
8 DA Type: LDC
9 Number of events needed: 1000
10 Input Files: 
11 Output Files: pmd_ped.root, to be exported to the DAQ FXS, pedestal230*.ped
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 "AliLog.h"
30 #include "AliCDBManager.h"
31
32 //ROOT
33 #include "TFile.h"
34 #include "TH1F.h"
35 #include "TBenchmark.h"
36 #include "TTree.h"
37 #include "TROOT.h"
38 #include "TPluginManager.h"
39 #include "TSystem.h"
40
41 /* Main routine
42       Arguments: 
43       1- monitoring data source
44 */
45 int main(int argc, char **argv) {
46   
47     /* magic line from Rene */
48     gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
49                                           "*",
50                                           "TStreamerInfo",
51                                           "RIO",
52                                           "TStreamerInfo()");
53
54     
55     AliPMDCalibPedestal calibped;
56
57     TTree *ped = NULL;
58
59     //    TH1F::AddDirectory(0);
60   
61       
62     // decoding the events
63   
64     int status;
65     
66     if (argc!=2) {
67         printf("Wrong number of arguments\n");
68         return -1;
69     }
70
71     
72     /* define data source : this is argument 1 */  
73     status=monitorSetDataSource( argv[1] );
74     if (status!=0) {
75         printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
76         return -1;
77     }
78     
79     /* declare monitoring program */
80     status=monitorDeclareMp( __FILE__ );
81     if (status!=0) {
82         printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
83         return -1;
84     }
85     
86     /* define wait event timeout - 1s max */
87     monitorSetNowait();
88     monitorSetNoWaitNetworkTimeout(1000);
89     
90     /* log start of process */
91     printf("PMD PED DA - started generating mean and rms of each channel\n");  
92     
93     /* init some counters */
94
95
96     struct eventHeaderStruct *event;
97     eventTypeType eventT = 0;
98     Int_t iev=0;
99
100     // Get run number
101     /*
102     if (getenv("DATE_RUN_NUMBER")==0) {
103       printf("DATE_RUN_NUMBER not properly set.\n");
104       return -1;
105     }
106     int runNr = atoi(getenv("DATE_RUN_NUMBER"));
107     */
108     int runNr = 0;
109
110
111     if (gSystem->AccessPathName("localOCDB/PMD/Calib/Mapping",kFileExists))
112       {
113         if (gSystem->mkdir("localOCDB/PMD/Calib/Mapping",kTRUE) != 0)
114           {
115             printf("Failed to create directory: localOCDB/PMD/Calib/Mapping");
116             return -1;
117           }
118       }
119     status = daqDA_DB_getFile("PMD/Calib/Mapping","localOCDB/PMD/Calib/Mapping/Run0_999999999_v0_s0.root");
120     if (status)
121       {
122         printf("Failed to get PMD-Mapping file (PMD/Calib/Mapping) from DAQdetDB, status=%d\n", status);
123         return -1;
124       }
125
126     // Global initializations
127     AliLog::SetGlobalLogLevel(AliLog::kError);
128     AliCDBManager *man = AliCDBManager::Instance();
129     man->SetDefaultStorage("local://localOCDB");
130     man->SetRun(runNr);
131
132     
133     /* main loop (infinite) */
134     for(;;) {
135         
136         /* check shutdown condition */
137         if (daqDA_checkShutdown()) {break;}
138         
139         /* get next event (blocking call until timeout) */
140         status=monitorGetEventDynamic((void **)&event);
141         if (status==MON_ERR_EOF) {
142             printf ("End of File detected\n");
143             break; /* end of monitoring file has been reached */
144         }
145         
146         if (status!=0) {
147             printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
148             break;
149         }
150         
151         /* retry if got no event */
152         if (event==NULL) {
153             continue;
154         }
155         
156         iev++; 
157         
158         /* use event - here, just write event id to result file */
159         eventT=event->eventType;
160         switch (event->eventType){
161             
162             /* START OF RUN */
163             case START_OF_RUN:
164                 break;
165                 /* END START OF RUN */
166                 
167                 /* END OF RUN */
168             case END_OF_RUN:
169                 break;
170                 
171             case PHYSICS_EVENT:
172
173                 AliRawReader *rawReader = new AliRawReaderDate((void*)event);
174                 TObjArray *pmdddlcont = new TObjArray();
175                 calibped.ProcessEvent(rawReader,pmdddlcont);
176                 
177                 delete pmdddlcont;
178                 pmdddlcont = 0;
179                 delete rawReader;
180                 rawReader = 0x0;
181         }
182         
183         /* free resources */
184         free(event);
185         
186         /* exit when last event received, no need to wait for TERM signal */
187
188         // either END_OF_RUN or 1000 events
189
190         if (iev == 1000)
191           {
192             printf("-- 1000 pedestal events processed : terminating --\n");
193             printf("-- eventT is set to END_OF_RUN --\n");
194             eventT = END_OF_RUN;
195             break;
196           }
197
198     }
199
200     printf(" Total number of events processed = %i \n",iev);
201
202     ped = new TTree("ped","PMD Pedestal tree");
203     
204     if (eventT==END_OF_RUN) {
205       printf("EOR event detected\n");
206       calibped.Analyse(ped);
207     }
208     
209     TFile * pedRun = new TFile ("PMD_PED.root","RECREATE"); 
210     ped->Write();
211     pedRun->Close();
212
213     delete ped;
214     ped = 0;
215
216 /* store the pedestal file in database */
217
218     status = daqDA_DB_storeFile("PMD_PED.root","PMD_PED.root");
219
220     if (!status)
221       {
222         printf("--- PEDESTAL root FILE IS IN THE DAQ DATABASE\n");
223       }
224     else
225       {
226         printf("--- Storing the root file into the database failed\n");
227       }
228
229 /* store the pedestal file on FES */
230  
231     status = daqDA_FES_storeFile("PMD_PED.root","PMD_PED.root");
232     if (status) {
233         status = -2;
234     }
235
236
237
238
239     return status;
240 }