Fixes for bug #62149 AliITSTrackleterSPDEff returns an error in case of 'empty' events
[u/mrichter/AliRoot.git] / PHOS / PHOSGAINda.cxx
CommitLineData
62adef6c 1/*
2contact: Boris.Polishchuk@cern.ch
3link: see comments in the $ALICE_ROOT/PHOS/AliPHOSRcuDA1.cxx
567f4fbc 4reference run: /alice/data/2009/LHC09b_PHOS/000075883/raw/09000075883017.20.root
82127abf 5run type: PHYSICS
62adef6c 6DA type: MON
7number of events needed: 1000
e6444b8b 8input files: Mod0RCU0.data Mod0RCU1.data Mod0RCU2.data Mod0RCU3.data Mod1RCU0.data Mod1RCU1.data Mod1RCU2.data Mod1RCU3.data Mod2RCU0.data Mod2RCU1.data Mod2RCU2.data Mod2RCU3.data Mod3RCU0.data Mod3RCU1.data Mod3RCU2.data Mod3RCU3.data Mod4RCU0.data Mod4RCU1.data Mod4RCU2.data Mod4RCU3.data zs.txt
ed35ce87 9Output files: PHOS_Calib.root
82127abf 10Trigger types used: PHYSICS
62adef6c 11*/
12
13
14#include "event.h"
15#include "monitor.h"
16extern "C" {
17#include "daqDA.h"
18}
19
20#include <stdio.h>
21#include <stdlib.h>
22
23#include <TSystem.h>
c59c9912 24#include <TROOT.h>
25#include <TPluginManager.h>
62adef6c 26
27#include "AliRawReader.h"
28#include "AliRawReaderDate.h"
29#include "AliPHOSRcuDA1.h"
8083e819 30#include "AliPHOSRawFitterv0.h"
62adef6c 31#include "AliCaloAltroMapping.h"
8083e819 32#include "AliCaloRawStreamV3.h"
567f4fbc 33#include "AliLog.h"
62adef6c 34
35/* Main routine
36 Arguments:
37 1- monitoring data source
38*/
39int main(int argc, char **argv) {
40
c59c9912 41 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
42 "*",
43 "TStreamerInfo",
44 "RIO",
45 "TStreamerInfo()");
46
567f4fbc 47 AliLog::SetGlobalDebugLevel(0) ;
48 AliLog::SetGlobalLogLevel(AliLog::kFatal);
49
62adef6c 50 int status;
51
52 if (argc!=2) {
53 printf("Wrong number of arguments\n");
54 return -1;
55 }
a9b2b02d 56
57 /* Retrieve ZS parameters from DAQ DB */
58 const char* zsfile = "zs.txt";
59 int failZS = daqDA_DB_getFile(zsfile, zsfile);
60
61 Int_t offset,threshold;
62
63 if(!failZS) {
64 FILE *f = fopen(zsfile,"r");
65 int scan = fscanf(f,"%d %d",&offset,&threshold);
66 }
67
c59c9912 68 /* Retrieve mapping files from DAQ DB */
e6444b8b 69 const char* mapFiles[20] = {
70 "Mod0RCU0.data",
71 "Mod0RCU1.data",
72 "Mod0RCU2.data",
73 "Mod0RCU3.data",
74 "Mod1RCU0.data",
75 "Mod1RCU1.data",
76 "Mod1RCU2.data",
77 "Mod1RCU3.data",
78 "Mod2RCU0.data",
79 "Mod2RCU1.data",
80 "Mod2RCU2.data",
81 "Mod2RCU3.data",
82 "Mod3RCU0.data",
83 "Mod3RCU1.data",
84 "Mod3RCU2.data",
85 "Mod3RCU3.data",
86 "Mod4RCU0.data",
87 "Mod4RCU1.data",
88 "Mod4RCU2.data",
89 "Mod4RCU3.data"
90 };
a9b2b02d 91
e6444b8b 92 for(Int_t iFile=0; iFile<20; iFile++) {
c59c9912 93 int failed = daqDA_DB_getFile(mapFiles[iFile], mapFiles[iFile]);
94 if(failed) {
95 printf("Cannot retrieve file %s from DAQ DB. Exit.\n",mapFiles[iFile]);
96 return -1;
97 }
98 }
62adef6c 99
100 /* Open mapping files */
e6444b8b 101 AliAltroMapping *mapping[20];
62adef6c 102 TString path = "./";
e6444b8b 103
104 path += "Mod";
62adef6c 105 TString path2;
e6444b8b 106 TString path3;
107 Int_t iMap = 0;
108
109 for(Int_t iMod = 0; iMod < 5; iMod++) {
62adef6c 110 path2 = path;
e6444b8b 111 path2 += iMod;
112 path2 += "RCU";
113
114 for(Int_t iRCU=0; iRCU<4; iRCU++) {
115 path3 = path2;
116 path3 += iRCU;
117 path3 += ".data";
118 mapping[iMap] = new AliCaloAltroMapping(path3.Data());
119 iMap++;
120 }
62adef6c 121 }
122
62adef6c 123 /* define data source : this is argument 1 */
124 status=monitorSetDataSource( argv[1] );
125 if (status!=0) {
126 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
127 return -1;
128 }
129
130
131 /* declare monitoring program */
132 status=monitorDeclareMp( __FILE__ );
133 if (status!=0) {
134 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
135 return -1;
136 }
137
138
139 /* define wait event timeout - 1s max */
140 monitorSetNowait();
141 monitorSetNoWaitNetworkTimeout(1000);
142
143 /* init some counters */
144 int nevents_physics=0;
145 int nevents_total=0;
146
147 AliRawReader *rawReader = NULL;
a9b2b02d 148 AliPHOSRcuDA1* dAs[5];
62adef6c 149
a9b2b02d 150 for(Int_t iMod=0; iMod<5; iMod++) {
151 dAs[iMod] = 0;
152 }
62adef6c 153
154 Float_t e[64][56][2];
155 Float_t t[64][56][2];
156
a9b2b02d 157 for(Int_t iX=0; iX<64; iX++) {
158 for(Int_t iZ=0; iZ<56; iZ++) {
159 for(Int_t iGain=0; iGain<2; iGain++) {
160 e[iX][iZ][iGain] = 0.;
161 t[iX][iZ][iGain] = 0.;
162 }
163 }
164 }
165
8083e819 166 Int_t cellX = -1;
167 Int_t cellZ = -1;
168 Int_t nBunches = 0;
169 Int_t sigStart, sigLength;
a9b2b02d 170 Int_t caloFlag;
62adef6c 171
172 /* main loop (infinite) */
173 for(;;) {
174 struct eventHeaderStruct *event;
175 eventTypeType eventT;
176
177 /* check shutdown condition */
178 if (daqDA_checkShutdown()) {break;}
179
180 /* get next event (blocking call until timeout) */
181 status=monitorGetEventDynamic((void **)&event);
182 if (status==MON_ERR_EOF) {
183 printf ("End of File detected\n");
184 break; /* end of monitoring file has been reached */
185 }
186
187 if (status!=0) {
188 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
189 break;
190 }
191
192 /* retry if got no event */
193 if (event==NULL) {
194 continue;
195 }
196
197
198 /* use event - here, just write event id to result file */
199 eventT=event->eventType;
200
201 if (eventT==PHYSICS_EVENT) {
202
62adef6c 203 rawReader = new AliRawReaderDate((void*)event);
8083e819 204 AliCaloRawStreamV3 stream(rawReader,"PHOS",mapping);
a9b2b02d 205 AliPHOSRawFitterv0 fitter;
8083e819 206 fitter.SubtractPedestals(kTRUE); // assume that data is non-ZS
62adef6c 207
a9b2b02d 208 if(!failZS) {
209 fitter.SubtractPedestals(kFALSE);
210 fitter.SetAmpOffset(offset);
211 fitter.SetAmpThreshold(threshold);
212 }
213
8083e819 214 while (stream.NextDDL()) {
215 while (stream.NextChannel()) {
e6444b8b 216
217 /* Retrieve ZS parameters from data*/
218 if(failZS) {
219 short value = stream.GetAltroCFG1();
220 bool ZeroSuppressionEnabled = (value >> 15) & 0x1;
221 bool AutomaticBaselineSubtraction = (value >> 14) & 0x1;
222 if(ZeroSuppressionEnabled) {
223 offset = (value >> 10) & 0xf;
224 threshold = value & 0x3ff;
225 fitter.SubtractPedestals(kFALSE);
226 fitter.SetAmpOffset(offset);
227 fitter.SetAmpThreshold(threshold);
228 }
229 }
230
8083e819 231 cellX = stream.GetCellX();
232 cellZ = stream.GetCellZ();
233 caloFlag = stream.GetCaloFlag(); // 0=LG, 1=HG, 2=TRU
2905f08d 234
235 if(caloFlag!=0 && caloFlag!=1) continue; //TRU data!
236
a9b2b02d 237 // In case of oscillating signals with ZS,
238 //a channel can have several bunches.
239
8083e819 240 nBunches = 0;
241 while (stream.NextBunch()) {
242 nBunches++;
243 if (nBunches > 1) continue;
a9b2b02d 244 sigStart = stream.GetStartTimeBin();
245 sigLength = stream.GetBunchLength();
92236b27 246 fitter.SetChannelGeo(stream.GetModule(),cellX,cellZ,caloFlag);
247 fitter.Eval(stream.GetSignals(),sigStart,sigLength);
8083e819 248 } // End of NextBunch()
249
ed35ce87 250 if (nBunches != 1) continue;
8083e819 251
252 e[cellX][cellZ][caloFlag] = fitter.GetEnergy();
253 t[cellX][cellZ][caloFlag] = fitter.GetTime();
254 }
2905f08d 255
256 if(stream.GetModule()<0 || stream.GetModule()>4) continue;
257
a9b2b02d 258 if(dAs[stream.GetModule()])
259 dAs[stream.GetModule()]->FillHistograms(e,t);
2905f08d 260 else {
ed35ce87 261 dAs[stream.GetModule()] = new AliPHOSRcuDA1(stream.GetModule(),-1,0);
2905f08d 262 dAs[stream.GetModule()]->FillHistograms(e,t);
263 }
a9b2b02d 264
265 for(Int_t iX=0; iX<64; iX++) {
266 for(Int_t iZ=0; iZ<56; iZ++) {
267 for(Int_t iGain=0; iGain<2; iGain++) {
268 e[iX][iZ][iGain] = 0.;
269 t[iX][iZ][iGain] = 0.;
270 }
271 }
272 }
273
62adef6c 274 }
275
a9b2b02d 276// da1.FillHistograms(e,t);
277// //da1.UpdateHistoFile();
62adef6c 278
a9b2b02d 279 delete rawReader;
280 nevents_physics++;
62adef6c 281 }
282
283 nevents_total++;
284
285 /* free resources */
286 free(event);
287
288 /* exit when last event received, no need to wait for TERM signal */
289 if (eventT==END_OF_RUN) {
290 printf("EOR event detected\n");
291 break;
292 }
293 }
294
e6444b8b 295 for(Int_t i = 0; i < 20; i++) delete mapping[i];
62adef6c 296
c59c9912 297 /* Be sure that all histograms are saved */
ed35ce87 298
e6444b8b 299 const TH2F* h2=0;
300 const TH1F* h1=0;
301 char localfile[128];
302
303 sprintf(localfile,"PHOS_Calib.root");
304 TFile* f = new TFile(localfile,"recreate");
305
306 for(Int_t iMod=0; iMod<5; iMod++) {
307 if(!dAs[iMod]) continue;
a9b2b02d 308
ed35ce87 309 printf("DA1 for module %d detected.\n",iMod);
310
e6444b8b 311 for(Int_t iX=0; iX<64; iX++) {
312 for(Int_t iZ=0; iZ<56; iZ++) {
313
314 h1 = dAs[iMod]->GetHgLgRatioHistogram(iX,iZ); // High Gain/Low Gain ratio
315 if(h1) h1->Write();
316
317 for(Int_t iGain=0; iGain<2; iGain++) {
318 h2 = dAs[iMod]->GetTimeEnergyHistogram(iX,iZ,iGain); // Time vs Energy
319 if(h2) h2->Write();
320 }
321 }
322 }
c59c9912 323 }
324
e6444b8b 325 f->Close();
326
327 /* Store output files to the File Exchange Server */
328 daqDA_FES_storeFile(localfile,"AMPLITUDES");
329
62adef6c 330 return status;
331}