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