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