Fixing warnings
[u/mrichter/AliRoot.git] / TOF / TOFda.cxx
CommitLineData
7b81ffe5 1/*
2
cee5b064 3TOF DA for online calibration
7b81ffe5 4
78c742e3 5Contact: Chiara.Zampolli@bo.infn.it
104ba366 6 Roberto.Preghenella@bo.infn.it
7
78c742e3 8Run Type: PHYSICS
9DA Type: MON
104ba366 10Number of events needed:
11Input Files: no input
12Output Files: TOFdaHits.root
13Event types used: PHYSICS_EVENT
dd635345 14
cee5b064 15*/
7b81ffe5 16
104ba366 17#define FILE_HITS "TOFdaHits.root"
016d6ae4 18#define FILE_CALIB "TOFdaCalib.root"
7b81ffe5 19
cee5b064 20// DATE
67cb6c02 21#include "event.h"
22#include "monitor.h"
23#include "daqDA.h"
24
25#include <stdio.h>
26#include <stdlib.h>
7b81ffe5 27
28//AliRoot
104ba366 29#include "TROOT.h"
30#include "AliTOFRawStream.h"
31#include "AliRawReaderDate.h"
32#include "AliRawReader.h"
33#include "AliDAQ.h"
34#include "AliTOFHitData.h"
35#include "AliTOFHitDataBuffer.h"
36#include "AliTOFDaConfigHandler.h"
37#include "AliTOFHitField.h"
104ba366 38#include "AliLog.h"
639d40ea 39#include "AliTOFGeometry.h"
7b81ffe5 40
41//ROOT
104ba366 42#include "TFile.h"
43#include "TKey.h"
44#include "TH2S.h"
45#include "TObject.h"
46#include "TMath.h"
47#include "TSystem.h"
3bc66139 48#include "TROOT.h"
49#include "TPluginManager.h"
67cb6c02 50#include "TSAXParser.h"
104ba366 51#include "TTree.h"
7b81ffe5 52
53/* Main routine
54 Arguments:
55 1- monitoring data source
56*/
104ba366 57int
58main(int argc, char **argv)
59{
60
3bc66139 61 /* magic line from Rene */
62 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
104ba366 63 "*",
64 "TStreamerInfo",
65 "RIO",
66 "TStreamerInfo()");
7b81ffe5 67
67cb6c02 68
69 /* log start of process */
70 printf("TOF DA started\n");
104ba366 71
67cb6c02 72 /* check that we got some arguments = list of files */
7b81ffe5 73 if (argc!=2) {
74 printf("Wrong number of arguments\n");
75 return -1;
76 }
77
104ba366 78 /*
79 * CONFIG
80 */
81
67cb6c02 82 /* retrieve config file */
59f6cc12 83 int getConfigFile = daqDA_DB_getFile("TOFPhysicsConfig.xml","TOFPhysicsConfig.xml");
67cb6c02 84 if (getConfigFile != 0){
85 printf("Failed to retrieve config file from DB! returning...\n");
86 return -1;
87 }
104ba366 88 /* parse config file */
59f6cc12 89 AliTOFDaConfigHandler* tofHandler = new AliTOFDaConfigHandler();
67cb6c02 90 TSAXParser *parser = new TSAXParser();
59f6cc12 91 parser->ConnectToHandler("AliTOFDaConfigHandler", tofHandler);
92 if (parser->ParseFile("./TOFPhysicsConfig.xml") != 0) {
67cb6c02 93 printf("Failed parsing config file! retunring... \n");
94 return -1;
95 }
104ba366 96 /* setup config params */
97 Int_t meanMultiplicity = tofHandler->GetMeanMultiplicity(); /* average expected TOF multiplicity */
98 Int_t maxHits = tofHandler->GetMaxHits(); /* max number of hits to be collected */
99 printf("current settings:\n");
100 printf(" - meanMultiplicity = %d\n", meanMultiplicity);
101 printf(" - maxHits = %d\n", maxHits);
102 /* constants */
103 const Int_t nChannels = 157248;
104 Int_t noiseCheckTrigger = 10; /* first noise check after 10 events */
105 Float_t meanChannelRate = (Float_t)meanMultiplicity / (Float_t)nChannels; /* average expected channel rate (hits/event) */
106 Float_t noiseThreshold = 10. * meanChannelRate; /* noise threshold (hits/event) */
107 Int_t minNoiseHits = 10; /* min number of channel hits to check noise */
108 /* counters and flags */
016d6ae4 109 Int_t nPhysicsEvents, nCalibEvents, totHits;
104ba366 110 Int_t nChHits[nChannels];
016d6ae4 111 Bool_t inhibitCollection;
104ba366 112 Bool_t noiseFlag[nChannels];
113 /* variables */
639d40ea 114 Int_t nhits, ddl, slot, trm, chain, tdc, channel, index, timebin, totbin, deltaBC, l0l1latency, det[5], dummy;
104ba366 115 Float_t noiseHitThreshold;
116
117 /*
118 * INIT
119 */
120
121 /* init counters and flags */
016d6ae4 122 nPhysicsEvents = 0;
123 nCalibEvents = 0;
104ba366 124 totHits = 0;
016d6ae4 125 inhibitCollection = kFALSE;
104ba366 126 for (Int_t ich = 0; ich < nChannels; ich++) {
127 nChHits[ich] = 0;
016d6ae4 128 noiseFlag[ich] = kFALSE;
4fc590b9 129 }
67cb6c02 130
104ba366 131 /* TOF raw data handling */
132 AliTOFRawStream *rawStream = new AliTOFRawStream();
133 AliTOFHitDataBuffer *pdb = NULL;
134 AliTOFHitData *hit = NULL;
67cb6c02 135
016d6ae4 136 /* open HITS output file */
137 TFile *fileOutHits = new TFile(FILE_HITS, "RECREATE");
104ba366 138 /* create hit field data structure */
139 AliTOFHitField *hitField = new AliTOFHitField();
140 /* create temporary tree */
141 TTree *tempTree = new TTree("tempTree", "temporary tree");
142 tempTree->Branch("hit", "AliTOFHitField", &hitField);
143 /* create output tree */
144 TTree *outTree = new TTree("hitTree", "hit tree");
145 outTree->Branch("hit", "AliTOFHitField", &hitField);
146
016d6ae4 147 /* open CALIB output file */
148 TFile *fileOutCalib = new TFile(FILE_CALIB, "RECREATE");
149 /* create calib hit histo */
150 TH1F *hCalibHit = new TH1F("hCalibHit", "Calibration events;index;N_{hits}/N_{events}", nChannels, 0., nChannels);
151
104ba366 152 /*
153 * ONLINE MONITOR
154 */
155
156 AliLog::SetGlobalLogLevel(AliLog::kFatal);
67cb6c02 157 struct eventHeaderStruct *event;
104ba366 158 int ret;
7b81ffe5 159 /* define data source : this is argument 1 */
104ba366 160 ret = monitorSetDataSource(argv[1]);
161 if (ret != 0) {
162 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(ret));
7b81ffe5 163 return -1;
164 }
7b81ffe5 165 /* declare monitoring program */
104ba366 166 ret = monitorDeclareMp("tofDA");
167 if (ret != 0) {
168 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(ret));
7b81ffe5 169 return -1;
170 }
7b81ffe5 171 /* define wait event timeout - 1s max */
172 monitorSetNowait();
173 monitorSetNoWaitNetworkTimeout(1000);
104ba366 174
175 /* loop over events */
176 while (1) {
7b81ffe5 177
178 /* check shutdown condition */
67cb6c02 179 if (daqDA_checkShutdown()) break;
7b81ffe5 180
104ba366 181 /*
182 * NOISE CHECK
183 */
184
016d6ae4 185 /* check inhibit collection */
186 if (!inhibitCollection) {
187 /* check number of events and check noise */
188 if (nPhysicsEvents >= noiseCheckTrigger || totHits >= maxHits) {
189 noiseHitThreshold = noiseThreshold * nPhysicsEvents;
190 printf("noise check triggered after %d events: threshold is %f hits\n", nPhysicsEvents, noiseHitThreshold);
191 /* loop over all channels */
192 for (Int_t ich = 0; ich < nChannels; ich++) {
193 /* check */
194 if (nChHits[ich] < minNoiseHits || noiseFlag[ich] || nChHits[ich] < noiseHitThreshold) continue;
195 printf("channel %d tagged as noisy (%d hits): disabled\n", ich, nChHits[ich]);
196 noiseFlag[ich] = kTRUE;
197 totHits -= nChHits[ich];
198 } /* end of loop over all channels */
199 /* set new noise check trigger value */
200 noiseCheckTrigger *= 10;
201 } /* end of noise check */
202
203 /* inhibit hit collection when maximum number of hits exceeded */
204 if (totHits >= maxHits) {
205 printf("maximum number of hits exceeded (%d): inhibit hit collection\n", maxHits);
206 inhibitCollection = kTRUE;
207 }
104ba366 208 }
209
210 /*
211 * GET EVENT
212 */
213
7b81ffe5 214 /* get next event (blocking call until timeout) */
104ba366 215 ret = monitorGetEventDynamic((void **)&event);
216 if (ret == MON_ERR_EOF) {
7b81ffe5 217 printf ("End of File detected\n");
218 break; /* end of monitoring file has been reached */
219 }
104ba366 220 if (ret != 0) {
221 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(ret));
7b81ffe5 222 break;
223 }
7b81ffe5 224 /* retry if got no event */
67cb6c02 225 if (event==NULL) continue;
104ba366 226 /* check event type */
cfddd113 227 if (event->eventType != PHYSICS_EVENT && event->eventType != CALIBRATION_EVENT) {
228 free(event);
229 continue;
230 }
016d6ae4 231 /* check inhibit collection */
cfddd113 232 if (event->eventType == PHYSICS_EVENT && inhibitCollection) {
233 free(event);
234 continue;
235 }
016d6ae4 236 /* increment number of physics events */
237 if (event->eventType == PHYSICS_EVENT) nPhysicsEvents++;
238 /* increment number of calib events */
239 if (event->eventType == CALIBRATION_EVENT) nCalibEvents++;
cee5b064 240
104ba366 241 /*
242 * DECODE EVENT
243 */
244
245 /* create raw reader */
246 AliRawReader *rawReader = new AliRawReaderDate((void *)event);
247 /* setup raw stream */
248 rawStream->SetRawReader(rawReader);
249 /* reset buffers */
250 rawStream->ResetBuffers();
251 /* decode */
252 rawStream->DecodeDDL(0, AliDAQ::NumberOfDdls("TOF") - 1, 0);
253
254 /*
255 * HIT MANIPULATION
256 */
257
258 /* loop over DDLs */
259 for (Int_t iddl = 0; iddl < AliDAQ::NumberOfDdls("TOF"); iddl++) {
260 /* get packed-data buffer */
261 pdb = rawStream->GetPackedDataBuffer(iddl);
262 nhits = pdb->GetEntries();
263 /* loop over hits in buffer */
264 for (Int_t ihit = 0; ihit < nhits; ihit++) {
265 /* get hit */
266 hit = pdb->GetHit(ihit);
267 /* get channel info */
268 ddl = iddl;
269 slot = hit->GetSlotID();
270 trm = slot - 3;
271 chain = hit->GetChain();
272 tdc = hit->GetTDC();
273 channel = hit->GetChan();
639d40ea 274 /* get index */
275 rawStream->EquipmentId2VolumeId(ddl, slot, chain, tdc, channel, det);
276 dummy = det[4];
277 det[4] = det[3];
278 det[3] = dummy;
279 /* check valid index */
280 if (det[0] < 0 || det[0] > 17 ||
281 det[1] < 0 || det[1] > 5 ||
282 det[2] < 0 || det[2] > 18 ||
283 det[3] < 0 || det[3] > 1 ||
284 det[4] < 0 || det[4] > 47) continue;
285 index = AliTOFGeometry::GetIndex(det);
016d6ae4 286
287 /* switch event type */
288 switch (event->eventType) {
289
290 /*
291 * PHYSICS EVENT
292 */
293
294 case PHYSICS_EVENT:
295 /* check noise flag */
296 if (noiseFlag[index]) continue;
297 /* increment number of channel hits and total hits */
298 nChHits[index]++;
299 totHits++;
300 /* get signal info */
301 timebin = hit->GetTimeBin();
302 totbin = hit->GetTOTBin();
303 deltaBC = hit->GetDeltaBunchID();
304 l0l1latency = hit->GetL0L1Latency();
305 /* set hit field data */
306 hitField->SetIndex(index);
307 hitField->SetTimeBin(timebin);
308 hitField->SetTOTBin(totbin);
309 hitField->SetDeltaBC(deltaBC);
310 hitField->SetL0L1Latency(l0l1latency);
311 /* fill temp tree */
312 tempTree->Fill();
313 break;
314
315 /*
316 * CALIBRATION EVENT
317 */
318
319 case CALIBRATION_EVENT:
320 /* fill calib hit histo */
321 hCalibHit->Fill(index);
322 break;
323
324 } /* end of switch event type */
104ba366 325 } /* end of loop over hits in buffer */
326 } /* end of loop over DDLs */
7b81ffe5 327
104ba366 328 /* delete raw reader */
329 delete rawReader;
ad31c83c 330 /* free event */
331 free(event);
104ba366 332
333 } /* end of loop over events */
334
335 /* final noise check */
016d6ae4 336 noiseHitThreshold = noiseThreshold * nPhysicsEvents;
337 printf("final noise check after %d events: threshold is %f hits\n", nPhysicsEvents, noiseHitThreshold);
104ba366 338 /* loop over all channels */
339 for (Int_t ich = 0; ich < nChannels; ich++) {
340 /* check */
341 if (nChHits[ich] < minNoiseHits || noiseFlag[ich] || nChHits[ich] < noiseHitThreshold) continue;
342 printf("channel %d tagged as noisy (%d hits): disabled\n", ich, nChHits[ich]);
343 noiseFlag[ich] = kTRUE;
344 totHits -= nChHits[ich];
345 } /* end of loop over all channels */
7b81ffe5 346
104ba366 347 /* copy hits into output tree from temp tree */
348 printf("copy hits from temporary tree into output tree\n");
349 printf("temporary tree contains %d hits\n", (Int_t)tempTree->GetEntries());
350 for (Int_t ihit = 0; ihit < tempTree->GetEntries(); ihit++) {
351 /* get entry */
352 tempTree->GetEntry(ihit);
353 /* check noise flag */
354 if (noiseFlag[hitField->GetIndex()]) continue;
355 /* fill output tree */
356 outTree->Fill();
357 } /* end of copy hits into output tree from temp tree */
358 printf("output tree contains %d hits\n", (Int_t)outTree->GetEntries());
7b81ffe5 359
016d6ae4 360 /* write output tree on HITS file */
361 fileOutHits->cd();
362 outTree->Write();
363 fileOutHits->Close();
cee5b064 364 /* export file to FXS */
104ba366 365 if (daqDA_FES_storeFile(FILE_HITS, "HITS"))
366 return -2;
7b81ffe5 367
016d6ae4 368 /* scale calib hit histo by number of calib events */
369 hCalibHit->Sumw2();
370 hCalibHit->Scale(1. / nCalibEvents);
371
372 /* write calib hit histo on CALIB file */
373 fileOutCalib->cd();
374 hCalibHit->Write();
375 fileOutCalib->Close();
376 /* export file to FXS */
377 if (daqDA_FES_storeFile(FILE_CALIB, "CALIB"))
378 return -2;
379
104ba366 380 return 0;
7b81ffe5 381}