1st draft for pass0
[u/mrichter/AliRoot.git] / T0 / T0Physda.cxx
... / ...
CommitLineData
1/*
2T0 DA for online calibration
3
4Contact: Michal.Oledzki@cern.ch
5Link: http://users.jyu.fi/~mioledzk/
6Run Type: PHYSICS
7DA Type: MON
8Number of events needed: 500000
9Input Files: inPhys.dat, external parameters
10Output Files: daPhys.root, to be exported to the DAQ FXS
11Trigger types used: PHYSICS_EVENT
12------------------------------Alla
13Now trigger type changed to CALICRATION_EVENT
14to have data during test.
15SOULD BE CHANGED BACK BEFORE BEAM
16------------------------------- Alla
17*/
18
19#define FILE_OUT "daPhys.root"
20#define FILE_IN "inPhys.dat"
21#include <daqDA.h>
22#include <event.h>
23#include <monitor.h>
24
25#include <Riostream.h>
26#include <stdio.h>
27#include <stdlib.h>
28
29//AliRoot
30#include <AliRawReaderDate.h>
31#include <AliRawReader.h>
32#include <AliT0RawReader.h>
33
34//ROOT
35#include "TROOT.h"
36#include "TPluginManager.h"
37#include "TFile.h"
38#include "TKey.h"
39#include "TObject.h"
40#include "TBenchmark.h"
41#include "TString.h"
42#include "TH1.h"
43#include "TSpectrum.h"
44#include "TMath.h"
45
46
47/* Main routine
48 Arguments:
49 1- monitoring data source
50*/
51int main(int argc, char **argv) {
52//int main(){
53 int status;
54
55 /* magic line */
56 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
57 "*",
58 "TStreamerInfo",
59 "RIO",
60 "TStreamerInfo()");
61
62 if(daqDA_DB_getFile(FILE_IN, FILE_IN)){
63 printf("Couldn't get input file >>inPhys.dat<< from DAQ_DB !!!\n");
64 return -1;
65 }
66
67
68 FILE *inp;
69 char c;
70 inp = fopen(FILE_IN, "r");
71 if(!inp){
72 printf("Input file >>inPhys.dat<< not found !!!\n");
73 return -1;
74 }
75 int kcbx, kt0bx, knpmtA, knpmtC;
76 float kclx,kcmx, kt0lx, kt0hx;
77
78 while((c=getc(inp))!=EOF) {
79 switch(c) {
80 case 'a': {fscanf(inp, "%d", &kcbx ); break;} //N of X bins hCFD1_CFD
81 case 'b': {fscanf(inp, "%f", &kclx ); break;} //Low x hCFD1_CFD
82 case 'c': {fscanf(inp, "%f", &kcmx ); break;} //High x hCFD1_CF
83 case 'd': {fscanf(inp, "%d", &knpmtC ); break;} //number of reference PMTC
84 case 'e': {fscanf(inp, "%d", &knpmtA ); break;} //number of reference PMTA
85 case 'f': {fscanf(inp, "%d", &kt0bx ); break;} //N of X bins hT0
86 case 'g': {fscanf(inp, "%f", &kt0lx ); break;} //Low x hT0
87 case 'k': {fscanf(inp, "%f", &kt0hx ); break;} //High x hT0
88 }
89 }
90 fclose(inp);
91
92 if (argc!=2) {
93 printf("Wrong number of arguments\n");
94 return -1;
95 }
96
97
98 /* define data source : this is argument 1 */
99 status=monitorSetDataSource( argv[1] );
100 if (status!=0) {
101 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
102 return -1;
103 }
104
105
106 /* declare monitoring program */
107 status=monitorDeclareMp( __FILE__ );
108 if (status!=0) {
109 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
110 return -1;
111 }
112
113
114 /* define wait event timeout - 1s max */
115 monitorSetNowait();
116 monitorSetNoWaitNetworkTimeout(1000);
117
118
119 /* log start of process */
120 printf("T0 monitoring program started\n");
121
122 // Allocation of histograms - start
123
124 TH1F *hCFD1minCFD[24];
125
126 for(Int_t ic=0; ic<24; ic++) {
127 hCFD1minCFD[ic] = new TH1F(Form("CFD1minCFD%d",ic+1),"CFD-CFD",kcbx,kclx,kcmx);
128 hCFD[ic] = new TH1F(Form("CFD1minCFD%d",ic+1),"CFD",kt0bx,kt0lx,kt0hx);
129 }
130 TH1F *hVertex = new TH1F("hVertex","T0 time",kt0bx,kt0lx,kt0hx);
131
132 // Allocation of histograms - end
133
134 Int_t iev=0;
135 /* main loop (infinite) */
136 for(;;) {
137 struct eventHeaderStruct *event;
138 eventTypeType eventT;
139
140 /* check shutdown condition */
141 if (daqDA_checkShutdown()) {break;}
142
143 /* get next event (blocking call until timeout) */
144 status=monitorGetEventDynamic((void **)&event);
145 if (status==(int)MON_ERR_EOF) {
146 printf ("End of File detected\n");
147 break; /* end of monitoring file has been reached */
148 }
149
150 if (status!=0) {
151 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
152 break;
153 }
154
155 /* retry if got no event */
156 if (event==NULL) {
157 continue;
158 }
159
160 /* use event - here, just write event id to result file */
161 eventT=event->eventType;
162
163 switch (event->eventType){
164
165 case START_OF_RUN:
166 break;
167
168 case END_OF_RUN:
169 break;
170
171 case PHYSICS_EVENT:
172 // case CALIBRATION_EVENT:
173 iev++;
174
175 if(iev==1){
176 printf("First event - %i\n",iev);
177 }
178
179 // Initalize raw-data reading and decoding
180 AliRawReader *reader = new AliRawReaderDate((void*)event);
181
182 // Enable the following two lines in case of real-data
183 reader->RequireHeader(kTRUE);
184 AliT0RawReader *start = new AliT0RawReader(reader, kTRUE);
185
186 // Read raw data
187 Int_t allData[110][5];
188 for(Int_t i0=0;i0<107;i0++)
189 for(Int_t j0=0;j0<5;j0++)
190 allData[i0][j0] = 0;
191
192 if(start->Next()){
193 for (Int_t i=0; i<107; i++) {
194 for(Int_t iHit=0;iHit<5;iHit++){
195 allData[i][iHit]= start->GetData(i,iHit);
196 }
197 }
198 }
199
200 // Fill the histograms
201 Float_t besttimeA=9999999;
202 Float_t besttimeC=9999999;
203 Float_t time[24];
204 Float_t meanShift[24];
205 for (Int_t ik = 0; ik<24; ik++)
206 {
207 if(allData[ik+1][0]>0 ) hCFD[ik]->Fill(allData[ik+1][0]);
208 if(ik<12 && allData[ik+1][0]>0 && allData[knpmtC][0]>0 ){
209 hCFD1minCFD[ik]->Fill(allData[ik+1][0]-allData[knpmtC][0]);
210 }
211
212 if(ik>11 && allData[ik+45][0]>0 && allData[56+knpmtA][0]>0 )
213 {
214 hCFD1minCFD[ik]->Fill(allData[ik+45][0]-allData[56+knpmtA][0]);
215 }
216 if(iev == 10000) {
217 meanShift[ik] = hCFD1minCFD[ik]->GetMean();
218 if(ik==knpmtC || ik==(56+knpmtA)) meanShift[ik]=0;
219 }
220 }
221 //fill mean time _ fast reconstruction
222 if (iev > 10000 )
223 {
224 for (Int_t in=0; in<12; in++)
225 {
226 time[in] = allData[in+1][0] - meanShift[in] ;
227 time[in+12] = allData[in+56+1][0] ;
228 }
229 for (Int_t ipmt=0; ipmt<12; ipmt++){
230 if(time[ipmt] > 1 ) {
231 if(time[ipmt]<besttimeC)
232 besttimeC=time[ipmt]; //timeC
233 }
234 }
235 for ( Int_t ipmt=12; ipmt<24; ipmt++){
236 if(time[ipmt] > 1) {
237 if(time[ipmt]<besttimeA)
238 besttimeA=time[ipmt]; //timeA
239 }
240 }
241 if(besttimeA<9999999 &&besttimeC< 9999999) {
242 Float_t t0 =0.001* 24.4 * Float_t( besttimeA+besttimeC)/2.;
243 hVertex->Fill(t0);
244 }
245 }
246
247 delete start;
248 start = 0x0;
249 delete reader;
250 reader= 0x0;
251 // End of fill histograms
252
253 }
254
255 /* free resources */
256 free(event);
257
258 /* exit when last event received, no need to wait for TERM signal */
259 if (eventT==END_OF_RUN) {
260 printf("EOR event detected\n");
261 printf("Number of events processed - %i\n ",iev);
262 break;
263 }
264 }
265 printf("After loop, before writing histos\n");
266 // write a file with the histograms
267
268 TFile hist(FILE_OUT,"RECREATE");
269
270 for(Int_t j=0;j<24;j++){
271 hCFD1minCFD[j]->SetDirectory(&hist);
272 hCFD1minCFD[j]->Write();
273 hCFD[j]->Write();
274
275 }
276 hVertex->Write();
277 hist.Close();
278 //delete hist;
279
280 status=0;
281
282 /* export file to FXS */
283 if (daqDA_FES_storeFile(FILE_OUT, "PHYSICS")) {
284 status=-2;
285 }
286
287 return status;
288}
289
290