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