corrected TRD/TOF MV position
[u/mrichter/AliRoot.git] / TOF / DA / TOFpulserda.cxx
CommitLineData
6b034754 1/*
2
67cb6c02 3TOF DA for pulser data
6b034754 4
5Contact: Chiara.Zampolli@bo.infn.it
6Link: www.bo.infn.it/~zampolli
7fffa85b 7Run Type: PULSER
6b034754 8DA Type: LDC
9Number of events needed: 10000
10Input Files: TOF<nrun>.raw, where <nrun> is the run number
11Output Files: TOFoutPulserLDC_<LDCid>.root, where <LDCid> is the id of the LDC which is read (2 characters field, e.g. TOFoutPulserLDC_03.root),to be exported to the DAQ FXS
67cb6c02 12Trigger types used: PHYSICS_EVENT (for the time being)
6b034754 13
14*/
15
16// DATE
17#include "event.h"
18#include "monitor.h"
89b3ea8c 19#include "daqDA.h"
6b034754 20
21#include <stdio.h>
22#include <stdlib.h>
23
24//AliRoot
25#include <AliTOFRawStream.h>
26#include <AliRawReaderDate.h>
27#include <AliRawReader.h>
28#include <AliTOFGeometry.h>
29#include <AliDAQ.h>
30#include <AliTOFHitData.h>
31#include <AliTOFHitDataBuffer.h>
32#include <AliTOFDecoder.h>
67cb6c02 33#include <AliTOFNoiseConfigHandler.h>
6b034754 34
35//ROOT
36#include <TFile.h>
37#include <TKey.h>
38#include <TH1I.h>
39#include <TObject.h>
40#include <TMath.h>
41#include <TSystem.h>
3bc66139 42#include "TROOT.h"
43#include "TPluginManager.h"
67cb6c02 44#include "TSAXParser.h"
6b034754 45
46/* Main routine
47 Arguments: list of DATE raw data files
48*/
49int main(int argc, char **argv) {
50
3bc66139 51/* magic line from Rene */
52 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
53 "*",
54 "TStreamerInfo",
55 "RIO",
56 "TStreamerInfo()");
57
6b034754 58 AliTOFGeometry * geom = new AliTOFGeometry();
59
60 static const Int_t size = AliTOFGeometry::NPadXSector()*AliTOFGeometry::NSectors();
61 TH1F::AddDirectory(0);
62 TH1I * htofPulser = new TH1I("hTOFpulser","histo with signals on TOF during pulser", size,-0.5,size-0.5);
67cb6c02 63 for (Int_t ibin =1;ibin<=size;ibin++)
6b034754 64 htofPulser->SetBinContent(ibin,-1);
6b034754 65
67cb6c02 66 UInt_t ldcId=99;
67 UInt_t ldcIdOLD=99;
6b034754 68
69 int status;
70
71 /* log start of process */
72 printf("TOF DA started\n");
73
74 /* check that we got some arguments = list of files */
75 if (argc<2) {
76 printf("Wrong number of arguments\n");
77 return -1;
78 }
79
67cb6c02 80 /* retrieve config file */
81 int getConfigFile = daqDA_DB_getFile("TOFNoiseConfig.xml","TOFNoiseConfig.xml");
82 if (getConfigFile != 0){
83 printf("Failed to retrieve config file from DB! returning...\n");
6b034754 84 return -1;
85 }
67cb6c02 86
87 AliTOFNoiseConfigHandler* tofHandler = new AliTOFNoiseConfigHandler();
88 TSAXParser *parser = new TSAXParser();
89 parser->ConnectToHandler("AliTOFNoiseConfigHandler", tofHandler);
90 if (parser->ParseFile("./TOFNoiseConfig.xml") != 0) {
91 printf("Failed parsing config file! retunring... \n");
92 return -1;
93 }
94
95 Int_t debugFlag = tofHandler->GetDebugFlag();
96 printf("the debug flag is %i\n",debugFlag);
97
6b034754 98 /* init some counters */
99 int nevents_physics=0;
100 int nevents_total=0;
101
67cb6c02 102 Int_t nPDBEntriesToT = 0;
103 Int_t nDBEntriesToT = 0;
104 AliTOFHitData *HitData = 0;
105 Int_t dummy = -1;
106 Int_t Volume[5];
107 for (Int_t i=0;i<5;i++) Volume[i]=-1;
108 AliTOFRawStream *rawStreamTOF = new AliTOFRawStream();
109 AliTOFDecoder * decoderTOF = new AliTOFDecoder();
110 AliTOFHitDataBuffer *DataBuffer[AliDAQ::NumberOfDdls("TOF")];
111 AliTOFHitDataBuffer *PackedDataBuffer[AliDAQ::NumberOfDdls("TOF")];
112 for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++) {
113 DataBuffer[i]=new AliTOFHitDataBuffer();
114 PackedDataBuffer[i]=new AliTOFHitDataBuffer();
115 }
116 Int_t currentEquipment;
117 Int_t currentDDL;
118 UChar_t *data = 0x0;
119 Int_t nchDDL = 0;
120 Int_t nDBEntries = 0;
121 Int_t nPDBEntries = 0;
122
123 struct eventHeaderStruct *event;
124 eventTypeType eventT;
125
6b034754 126 /* read the data files */
127 int n;
128 for (n=1;n<argc;n++) {
129
130 status=monitorSetDataSource( argv[n] );
131 if (status!=0) {
132 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
133 return -1;
134 }
135
136 /* read the file */
137 for(;;) {
67cb6c02 138
139 rawStreamTOF->Clear();
140 currentEquipment = 0;
141 currentDDL = 0;
142 data = 0x0;
143 nPDBEntriesToT = 0;
144 nDBEntriesToT = 0;
145 dummy = -1;
146 nchDDL = 0;
147 nDBEntries = 0;
148 nPDBEntries = 0;
149 HitData = 0;
6b034754 150
151 /* get next event */
152 status=monitorGetEventDynamic((void **)&event);
153 if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
154 if (status!=0) {
155 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
156 return -1;
157 }
158
159 /* retry if got no event */
67cb6c02 160 if (event==NULL)
6b034754 161 break;
6b034754 162
163 /* use event - here, just write event id to result file */
164 eventT=event->eventType;
165
166 if (eventT==PHYSICS_EVENT) {
167 //printf ("event %i \n", nevents_physics);
67cb6c02 168
6b034754 169 AliRawReader *rawReader = new AliRawReaderDate((void*)event);
67cb6c02 170 rawStreamTOF->SetRawReader(rawReader);
171
172 while (rawReader->ReadHeader()) {
173 ldcId = rawReader->GetLDCId();
174 //debugging printings
175 if (debugFlag && ldcId!=ldcIdOLD) {
176 ldcIdOLD = ldcId;
177 printf ("ldcId = %i \n",ldcId);
178 }
6b034754 179
180 //memory leak prevention (actually data should be always 0x0 here)
67cb6c02 181 if (data != 0x0) delete [] data;
6b034754 182
183 //get equipment infos
184 currentEquipment = rawReader->GetEquipmentId();
185 currentDDL = rawReader->GetDDLID();
67cb6c02 186 const AliRawDataHeader *currentCDH = (AliRawDataHeader*)rawReader->GetDataHeader();
64c00731 187 const AliRawDataHeaderV3 *currentCDHV3 = (AliRawDataHeaderV3*)rawReader->GetDataHeaderV3();
67cb6c02 188 if (currentDDL%2==0)
6b034754 189 nchDDL = 2160;
67cb6c02 190 else
6b034754 191 nchDDL = 2208;
67cb6c02 192
193 //Int_t * array = new Int_t[nchDDL];
194 Int_t array[nchDDL];
195 for (Int_t ii=0; ii<nchDDL; ii++) array[ii] = 0;
6b034754 196 decoderTOF->GetArrayDDL(array, currentDDL);
67cb6c02 197
198 for (Int_t i=0;i<nchDDL;i++)
6b034754 199 if (htofPulser->GetBinContent(array[i]+1)<0) htofPulser->SetBinContent(array[i]+1,0);
67cb6c02 200
201 //debugging printings
202 //if (debugFlag) printf(" Equipment = %i, and DDL = %i \n", currentEquipment,currentDDL);
6b034754 203 const Int_t kDataSize = rawReader->GetDataSize();
204 const Int_t kDataWords = kDataSize / 4;
205 data = new UChar_t[kDataSize];
206 decoderTOF->SetDataBuffer(DataBuffer[currentDDL]);
207 decoderTOF->SetPackedDataBuffer(PackedDataBuffer[currentDDL]);
208 //start decoding
67cb6c02 209 if (!rawReader->ReadNext(data, kDataSize)) {
6b034754 210 rawReader->AddMajorErrorLog(AliTOFRawStream::kDDLdataReading);
211 printf("Error while reading DDL data. Go to next equipment \n");
212 delete [] data;
213 data = 0x0;
214 continue;
215 }
64c00731 216 if (decoderTOF->Decode((UInt_t *)data, kDataWords, currentCDH, currentCDHV3) == kTRUE) {
6b034754 217 rawReader->AddMajorErrorLog(AliTOFRawStream::kDDLDecoder,Form("DDL # = %d",currentDDL));
218 printf("Error while decoding DDL # %d: decoder returned with errors \n", currentDDL);
219 }
67cb6c02 220
221 nDBEntries = DataBuffer[currentDDL]->GetEntries();
222 nPDBEntries = PackedDataBuffer[currentDDL]->GetEntries();
6b034754 223 nPDBEntriesToT+=nPDBEntries;
224 nDBEntriesToT+=nDBEntries;
225 /* reset buffer */
226 DataBuffer[currentDDL]->Reset();
67cb6c02 227
6b034754 228 /* read data buffer hits */
229 for (Int_t iHit = 0; iHit < nPDBEntries; iHit++){
230 HitData = PackedDataBuffer[currentDDL]->GetHit(iHit);
231 /* add volume information */
232 HitData->SetDDLID(currentDDL);
67cb6c02 233 for (Int_t i=0;i<5;i++) Volume[i]=-1;
6b034754 234 rawStreamTOF->EquipmentId2VolumeId(HitData, Volume);
235 if (Volume[0]==-1 ||
236 Volume[1]==-1 ||
237 Volume[2]==-1 ||
238 Volume[3]==-1 ||
239 Volume[4]==-1) continue;
240 else {
241 dummy = Volume[3];
242 Volume[3] = Volume[4];
243 Volume[4] = dummy;
244 Int_t index = geom->GetIndex(Volume);
67cb6c02 245 // to check array indexes
246 /*
247 Bool_t found =kFALSE;
248 for (Int_t j=0;j<nchDDL;j++){
249 if (index==array[j]) {
250 found = kTRUE;
251 break;
252 }
253 }
254 printf ("index = %6d, found = %6d\n",index, (Int_t)found);
255 */
256 //printf ("index = %6d \n",index);
6b034754 257 htofPulser->Fill(index); //channel index start from 0, bin index from 1
258 //debugging printings
67cb6c02 259 //if (debugFlag) printf("sector %2d, plate %1d, strip %2d, padz %1d, padx %2d \n",Volume[0],Volume[1],Volume[2],Volume[3],Volume[4]); // too verbose
6b034754 260 }
261 }
262 /* reset buffer */
263 PackedDataBuffer[currentDDL]->Reset();
264 delete [] data;
265 data = 0x0;
67cb6c02 266 //delete [] array;
6b034754 267 }
67cb6c02 268
269 //debugging printings
270 //if (debugFlag) {
271 // printf(" Packed Hit Buffer Entries = %i \n",nPDBEntriesToT); // too verbose
272 // printf(" Hit Buffer Entries = %i \n",nDBEntriesToT); // too verbose
273 //}
6b034754 274
275 delete rawReader;
276 rawReader = 0x0;
277 nevents_physics++;
278 }
279 nevents_total++;
280
281 /* free resources */
282 free(event);
283 }
67cb6c02 284
285 }
286
287 delete decoderTOF;
288 decoderTOF=0x0;
289
290 for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
291 delete DataBuffer[i];
292 delete PackedDataBuffer[i];
6b034754 293 }
294
67cb6c02 295 delete rawStreamTOF;
296 rawStreamTOF = 0x0;
297
6b034754 298 delete geom;
299 geom = 0x0;
300
301 //write the Run level file
302 char filename[100];
303 sprintf(filename,"TOFoutPulserLDC_%02i.root",ldcId);
304 TFile * fileRun = new TFile (filename,"RECREATE");
305 htofPulser->Write();
306 fileRun->Close();
67cb6c02 307
6b034754 308 /* write report */
67cb6c02 309 printf("Run #%s, received %d physics events out of %d\n",
310 getenv("DATE_RUN_NUMBER"),nevents_physics,nevents_total);
311
312 status = 0;
6b034754 313
6b034754 314 /* store the result file on FES */
98a837bf 315 status=daqDA_FES_storeFile(filename,"PULSER");
67cb6c02 316 if (status)
6b034754 317 status = -2;
6b034754 318
319 return status;
320}