Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / T0 / T0Physda.cxx
CommitLineData
6145b1a7 1/*
2T0 DA for online calibration
aef81aa2 3Contact: AllaMaevskaya@cern.ch
6145b1a7 4Run Type: PHYSICS
5DA Type: MON
aef81aa2 6Number of events needed: 10000
7Input Files: inPhys.dat, external parameters, T0/Calib/Slewing_Walk
6145b1a7 8Output Files: daPhys.root, to be exported to the DAQ FXS
9Trigger types used: PHYSICS_EVENT
49976464 10------------------------------Alla
11Now trigger type changed to CALICRATION_EVENT
12to have data during test.
13SOULD BE CHANGED BACK BEFORE BEAM
14------------------------------- Alla
6145b1a7 15*/
99194e07 16
6145b1a7 17#define FILE_OUT "daPhys.root"
18#define FILE_IN "inPhys.dat"
19#include <daqDA.h>
99194e07 20#include <event.h>
21#include <monitor.h>
22
23#include <Riostream.h>
24#include <stdio.h>
25#include <stdlib.h>
26
27//AliRoot
28#include <AliRawReaderDate.h>
29#include <AliRawReader.h>
30#include <AliT0RawReader.h>
aef81aa2 31#include <AliT0CalibWalk.h>
32#include <AliCDBManager.h>
33#include <AliCDBEntry.h>
99194e07 34//ROOT
6145b1a7 35#include "TROOT.h"
36#include "TPluginManager.h"
99194e07 37#include "TFile.h"
38#include "TKey.h"
99194e07 39#include "TObject.h"
40#include "TBenchmark.h"
99194e07 41#include "TString.h"
42#include "TH1.h"
eb6102d2 43#include "TMath.h"
f824d160 44
99194e07 45
46/* Main routine
47 Arguments:
48 1- monitoring data source
49*/
50int main(int argc, char **argv) {
51//int main(){
52 int status;
6145b1a7 53
54 /* magic line */
55 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
56 "*",
57 "TStreamerInfo",
58 "RIO",
59 "TStreamerInfo()");
b95e8d87 60
19e2ac29 61 if(daqDA_DB_getFile(FILE_IN, FILE_IN)){
b95e8d87 62 printf("Couldn't get input file >>inPhys.dat<< from DAQ_DB !!!\n");
63 return -1;
19e2ac29 64 }
b95e8d87 65
99194e07 66
6145b1a7 67 FILE *inp;
68 char c;
69 inp = fopen(FILE_IN, "r");
19e2ac29 70 if(!inp){
b95e8d87 71 printf("Input file >>inPhys.dat<< not found !!!\n");
72 return -1;
19e2ac29 73 }
e93eb66f 74 int kcbx, kt0bx, knpmtA, knpmtC;
75 float kclx,kcmx, kt0lx, kt0hx;
b95e8d87 76
6145b1a7 77 while((c=getc(inp))!=EOF) {
78 switch(c) {
eb6102d2 79 case 'a': {fscanf(inp, "%d", &kcbx ); break;} //N of X bins hCFD1_CFD
80 case 'b': {fscanf(inp, "%f", &kclx ); break;} //Low x hCFD1_CFD
81 case 'c': {fscanf(inp, "%f", &kcmx ); break;} //High x hCFD1_CF
82 case 'd': {fscanf(inp, "%d", &knpmtC ); break;} //number of reference PMTC
83 case 'e': {fscanf(inp, "%d", &knpmtA ); break;} //number of reference PMTA
84 case 'f': {fscanf(inp, "%d", &kt0bx ); break;} //N of X bins hT0
85 case 'g': {fscanf(inp, "%f", &kt0lx ); break;} //Low x hT0
86 case 'k': {fscanf(inp, "%f", &kt0hx ); break;} //High x hT0
6145b1a7 87 }
88 }
89 fclose(inp);
90
99194e07 91 if (argc!=2) {
92 printf("Wrong number of arguments\n");
93 return -1;
94 }
b95e8d87 95
99194e07 96
97 /* define data source : this is argument 1 */
98 status=monitorSetDataSource( argv[1] );
99 if (status!=0) {
100 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
101 return -1;
102 }
b95e8d87 103
104
99194e07 105 /* declare monitoring program */
106 status=monitorDeclareMp( __FILE__ );
107 if (status!=0) {
108 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
109 return -1;
110 }
b95e8d87 111
112
99194e07 113 /* define wait event timeout - 1s max */
114 monitorSetNowait();
115 monitorSetNoWaitNetworkTimeout(1000);
116
b95e8d87 117
99194e07 118 /* log start of process */
119 printf("T0 monitoring program started\n");
b95e8d87 120
aef81aa2 121 // Get run number
122 if (getenv("DATE_RUN_NUMBER")==0) {
123 printf("DATE_RUN_NUMBER not properly set.\n");
124 return -1;
125 }
126 int runNr = atoi(getenv("DATE_RUN_NUMBER"));
127
128 // Get the necessary OCDB files from the DAQ detector DB
129 if (gSystem->AccessPathName("localOCDB/T0/Calib/Slewing_Walk/",kFileExists)) {
130 if (gSystem->mkdir("localOCDB/T0/Calib/Slewing_Walk/",kTRUE) != 0) {
131 printf("Failed to create directory: localOCDB/T0/Calib/Slewing_Walk/");
132 return -1;
133 }
134 }
135
136 status = daqDA_DB_getFile("T0/Calib/Slewing_Walk","localOCDB/T0/Calib/Slewing_Walk/Run0_999999999_v0_s0.root");
137 if (status) {
138 printf("Failed to get geometry file (GRP/Geometry/Data) from DAQdetDB, status=%d\n", status);
139 return -1;
140 }
141 TGraph *gr[24]; TGraph *gramp[24];
142 AliCDBManager *man = AliCDBManager::Instance();
143 man->SetDefaultStorage("local://localOCDB");
144 man->SetRun(runNr);
145 AliCDBEntry *entry = AliCDBManager::Instance()->Get("T0/Calib/Slewing_Walk");
146 if(entry) {
147 AliT0CalibWalk *fParam = (AliT0CalibWalk*)entry->GetObject();
148 for (Int_t i=0; i<24; i++) {
149 gr[i] = fParam->GetWalk(i);
150 gramp[i] = fParam->GetQTC(i);
151 }
152 }
153 Int_t chargeQT0[24], chargeQT1[24];
154 Float_t adc ,walk, amp;
155
99194e07 156 // Allocation of histograms - start
157
ae4882b1 158 TH1F *hCFD1minCFD[24];
320746b0 159 TH1F *hCFD[24];
6145b1a7 160
b95e8d87 161 for(Int_t ic=0; ic<24; ic++) {
eb6102d2 162 hCFD1minCFD[ic] = new TH1F(Form("CFD1minCFD%d",ic+1),"CFD-CFD",kcbx,kclx,kcmx);
320746b0 163 hCFD[ic] = new TH1F(Form("CFD%d",ic+1),"CFD",kt0bx,kt0lx,kt0hx);
b95e8d87 164 }
eb6102d2 165 TH1F *hVertex = new TH1F("hVertex","T0 time",kt0bx,kt0lx,kt0hx);
b95e8d87 166
eb6102d2 167 // Allocation of histograms - end
99194e07 168
aef81aa2 169
170 Int_t iev=0;
99194e07 171 /* main loop (infinite) */
172 for(;;) {
173 struct eventHeaderStruct *event;
174 eventTypeType eventT;
b95e8d87 175
99194e07 176 /* check shutdown condition */
177 if (daqDA_checkShutdown()) {break;}
178
179 /* get next event (blocking call until timeout) */
180 status=monitorGetEventDynamic((void **)&event);
181 if (status==(int)MON_ERR_EOF) {
182 printf ("End of File detected\n");
183 break; /* end of monitoring file has been reached */
184 }
185
186 if (status!=0) {
187 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
188 break;
189 }
b95e8d87 190
99194e07 191 /* retry if got no event */
192 if (event==NULL) {
193 continue;
194 }
b95e8d87 195
99194e07 196 /* use event - here, just write event id to result file */
197 eventT=event->eventType;
b95e8d87 198
99194e07 199 switch (event->eventType){
b95e8d87 200
d1623495 201 case START_OF_RUN:
202 break;
b95e8d87 203
204 case END_OF_RUN:
205 break;
206
d1623495 207 case PHYSICS_EVENT:
f824d160 208 // case CALIBRATION_EVENT:
19e2ac29 209 iev++;
b95e8d87 210
19e2ac29 211 if(iev==1){
b95e8d87 212 printf("First event - %i\n",iev);
19e2ac29 213 }
b95e8d87 214
99194e07 215 // Initalize raw-data reading and decoding
216 AliRawReader *reader = new AliRawReaderDate((void*)event);
b95e8d87 217
99194e07 218 // Enable the following two lines in case of real-data
b95e8d87 219 reader->RequireHeader(kTRUE);
99194e07 220 AliT0RawReader *start = new AliT0RawReader(reader, kTRUE);
b95e8d87 221
99194e07 222 // Read raw data
1e7108ce 223 Int_t allData[110][5];
224 for(Int_t i0=0;i0<107;i0++)
99194e07 225 for(Int_t j0=0;j0<5;j0++)
b95e8d87 226 allData[i0][j0] = 0;
227
228 if(start->Next()){
1e7108ce 229 for (Int_t i=0; i<107; i++) {
b95e8d87 230 for(Int_t iHit=0;iHit<5;iHit++){
231 allData[i][iHit]= start->GetData(i,iHit);
232 }
233 }
99194e07 234 }
b95e8d87 235
99194e07 236 // Fill the histograms
aef81aa2 237 walk = adc = amp = -999;
238 for (Int_t in=0; in<12; in++)
239 {
240 chargeQT0[in]=allData[2*in+25][0];
241 chargeQT1[in]=allData[2*in+26][0];
242 }
243 for (Int_t in=12; in<24; in++)
244 {
245 chargeQT0[in]=allData[2*in+57][0];
246 chargeQT1[in]=allData[2*in+58][0];
247 }
248 Float_t besttimeA=9999999;
b95e8d87 249 Float_t besttimeC=9999999;
250 Float_t time[24];
f824d160 251 Float_t meanShift[24];
252 for (Int_t ik = 0; ik<24; ik++)
aef81aa2 253 {
254 if( ( chargeQT0[ik] - chargeQT1[ik])>0) {
255 adc = chargeQT0[ik] - chargeQT1[ik];
256 // cout<<ik <<" "<<adc<<endl;
257 }
258 if(gramp[ik])
259 amp = gramp[ik]->Eval(Double_t(adc));
260 if(amp < 0.8) continue;
261 if(gr[ik])
262 walk = Int_t(gr[ik]->Eval(Double_t(adc) ) );
263
eb6102d2 264 if(ik<12 && allData[ik+1][0]>0 && allData[knpmtC][0]>0 ){
265 hCFD1minCFD[ik]->Fill(allData[ik+1][0]-allData[knpmtC][0]);
aef81aa2 266 if( walk >-100) hCFD[ik]->Fill(allData[ik+1][0] - walk);
267 // cout<<ik<<" "<<allData[ik+1][0]<<" adc "<<adc<<" walk "<<walk<<endl;
f824d160 268 }
269
eb6102d2 270 if(ik>11 && allData[ik+45][0]>0 && allData[56+knpmtA][0]>0 )
271 {
272 hCFD1minCFD[ik]->Fill(allData[ik+45][0]-allData[56+knpmtA][0]);
aef81aa2 273 if( walk >-100) hCFD[ik]->Fill(allData[ik+45][0] - walk);
274 // cout<<ik<<" "<<allData[ik+1][0]<<" adc "<<adc<<" walk "<<walk<<endl;
eb6102d2 275 }
8702c91e 276 if(iev == 10000) {
eb6102d2 277 meanShift[ik] = hCFD1minCFD[ik]->GetMean();
278 if(ik==knpmtC || ik==(56+knpmtA)) meanShift[ik]=0;
f824d160 279 }
280 }
281 //fill mean time _ fast reconstruction
8702c91e 282 if (iev > 10000 )
f824d160 283 {
284 for (Int_t in=0; in<12; in++)
285 {
286 time[in] = allData[in+1][0] - meanShift[in] ;
287 time[in+12] = allData[in+56+1][0] ;
288 }
289 for (Int_t ipmt=0; ipmt<12; ipmt++){
290 if(time[ipmt] > 1 ) {
291 if(time[ipmt]<besttimeC)
292 besttimeC=time[ipmt]; //timeC
293 }
b95e8d87 294 }
f824d160 295 for ( Int_t ipmt=12; ipmt<24; ipmt++){
296 if(time[ipmt] > 1) {
297 if(time[ipmt]<besttimeA)
298 besttimeA=time[ipmt]; //timeA
299 }
300 }
301 if(besttimeA<9999999 &&besttimeC< 9999999) {
302 Float_t t0 =0.001* 24.4 * Float_t( besttimeA+besttimeC)/2.;
f824d160 303 hVertex->Fill(t0);
304 }
b95e8d87 305 }
f824d160 306
307 delete start;
d1623495 308 start = 0x0;
309 delete reader;
310 reader= 0x0;
99194e07 311 // End of fill histograms
b95e8d87 312
99194e07 313 }
b95e8d87 314
99194e07 315 /* free resources */
316 free(event);
317
318 /* exit when last event received, no need to wait for TERM signal */
319 if (eventT==END_OF_RUN) {
320 printf("EOR event detected\n");
19e2ac29 321 printf("Number of events processed - %i\n ",iev);
99194e07 322 break;
323 }
324 }
6145b1a7 325 printf("After loop, before writing histos\n");
99194e07 326 // write a file with the histograms
c083a8f8 327
d1623495 328 TFile hist(FILE_OUT,"RECREATE");
99194e07 329
330 for(Int_t j=0;j<24;j++){
d1623495 331 hCFD1minCFD[j]->SetDirectory(&hist);
332 hCFD1minCFD[j]->Write();
1e7108ce 333 hCFD[j]->Write();
334
d1623495 335 }
b95e8d87 336 hVertex->Write();
d1623495 337 hist.Close();
338 //delete hist;
99194e07 339
6145b1a7 340 status=0;
341
342 /* export file to FXS */
387638f7 343 if (daqDA_FES_storeFile(FILE_OUT, "PHYSICS")) {
6145b1a7 344 status=-2;
345 }
346
99194e07 347 return status;
348}
349
350