]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtestBeam.cxx
Coding rule violations
[u/mrichter/AliRoot.git] / TRD / AliTRDtestBeam.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 #include "AliTRDtestBeam.h"
19
20 #include "AliTRDRawStreamTB.h"
21 #include "AliRawReaderMemory.h"
22
23 #include <iostream>
24 #include <fstream>
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 //#include <>
32
33 ClassImp(AliTRDtestBeam)
34
35 const Long_t AliTRDtestBeam::file_head_size = 544; // ?
36 const Long_t AliTRDtestBeam::event_head_size = 68; //?
37 const Long_t AliTRDtestBeam::ldc_head_size = 68; //?
38 const Long_t AliTRDtestBeam::equip_head_size = 28; //
39 const Int_t AliTRDtestBeam::vme_in =1; //VME event in
40 const Int_t AliTRDtestBeam::sim_in =1; //Si-strips in
41
42 //typedef char byte;
43
44 //offsets in bytes
45 const Int_t AliTRDtestBeam::pos_run = 20; //run nr. (in file and event header)
46 const Int_t AliTRDtestBeam::pos_length = 0; //event/equip. length
47 const Int_t AliTRDtestBeam::pos_eqid = 8;  //equipment id.
48 const Int_t AliTRDtestBeam::pos_sioff = 12;  //Si data size offset (3 extra words!!!)      
49      
50 using namespace std;
51
52 //____________________________________________________________________________ 
53 AliTRDtestBeam::AliTRDtestBeam() :
54   fDataStream(0),
55   fHeaderIsRead(0),
56   fEventCount(0),
57   fLimit(4), 
58   fCurrent(0),
59   fDdlOff(0),
60   fSiOff(0),
61   fQdcOff(0),
62   fDdlSize(0),
63   fFileHeader(0),
64   fEventHeader(0),
65   fEventData(0),
66   fNSi1(0),
67   fNSi2(0),
68   fCher(0),
69   fPb(0)
70 {
71
72 }
73 //____________________________________________________________________________ 
74 AliTRDtestBeam::AliTRDtestBeam(const char *filename) :
75   fDataStream(0),
76   fHeaderIsRead(0),
77   fEventCount(0),
78   fLimit(4), 
79   fCurrent(0),
80   fDdlOff(0),
81   fSiOff(0),
82   fQdcOff(0),
83   fDdlSize(0),
84   fFileHeader(0),
85   fEventHeader(0),
86   fEventData(0),
87   fNSi1(0),
88   fNSi2(0),
89   fCher(0),
90   fPb(0)
91 {
92
93   fDataStream = new ifstream(filename, ifstream::in | ifstream::binary );
94   cout << fDataStream->is_open() << endl;
95   //fHeaderIsRead = kTRUE;
96   fHeaderIsRead = kTRUE;
97
98   fFileHeader = new Char_t[file_head_size];
99   fEventHeader = new Char_t[event_head_size];
100   fEventData = new Char_t[fLimit];
101 }
102  
103 //____________________________________________________________________________ 
104
105 Int_t AliTRDtestBeam::NextEvent() {
106   
107   Long_t data_size=0,ldc_off; //,ldc_id,ldc2_id;
108   Long_t ldc_size,eq_id; //,ev_l2;
109   Long_t event_nr,ev_l1;
110   Long_t word;
111   
112   if ( !fHeaderIsRead ) {
113     fDataStream->read(fFileHeader, file_head_size);
114     if(fDataStream->fail()) {
115       cerr << "Error reading file header! " << endl;    
116       return false;
117     }
118     cout  << " Run nr.  " << Int(pos_run, fFileHeader) << endl;    
119     fHeaderIsRead=kTRUE;
120   }
121
122   fDataStream->read(fEventHeader, event_head_size);
123   if(fDataStream->fail()) {
124     cerr << "End of file, Event " << fEventCount  << endl;      
125     return false;
126   }
127   
128   data_size = Int(pos_length, fEventHeader)-event_head_size; //?
129   event_nr = Int((4+pos_run), fEventHeader); //ev.nr.
130   //cout << " Event " << event_nr <<" size "<< data_size <<endl;
131   
132     if (event_nr <= fEventCount-1) { //watch-out ...event counter starts at 1?
133       cout << fEventCount << " End of file?, Event " << fEventCount << endl;    
134       return false;
135     }
136     //cout <<  "Run " << Int(pos_run, header)<< " , Event " <<event_nr <<endl;
137     
138     // enough space for data?
139     if (fLimit < data_size) {
140       delete[] fEventData;
141       fEventData = new Char_t[data_size];
142       fLimit = data_size;
143     }
144     
145     fDataStream->read(fEventData, data_size);
146     
147     if(fDataStream->fail()) {
148       cerr << "End of file, Event " << fEventCount; // << endl; 
149         return false;
150     }
151     
152     //cout  << " ...IDs (size) : ";
153     
154     ldc_off=0; // size of data from one DDL link
155     
156     for ( size_t k = 0; k < 2; k++ ) { // 2 LDCs (DDL & VME)
157       
158       ldc_size = Int(ldc_off+pos_length, fEventData); //
159       //ldc_size1=(ldc_size-ldc_head_size);
160       eq_id = Int(ldc_off+ldc_head_size+pos_eqid, fEventData);      
161       //cout  << eq_id <<" ("<<ldc_size<<") ";      
162       
163       ev_l1 = Int((4+ldc_off+pos_run), fEventData); //ev.nr.
164       if ( ev_l1 != event_nr ){
165         //cerr << "Eq_id " <<eq_id<<" event nr. mismatch? " << event_nr <<" / "<< ev_l1 <<" ...LDC data size (header:68) " <<ldc_size<<endl;
166       }
167       
168       if (eq_id == 1024) {  //DDL data
169         fDdlOff = ldc_off; //+ldc_head_size+equip_head_size + 32;
170         fDdlSize = ldc_size;
171       }
172       
173       if (eq_id == 550) {  //Si-strip data (+QDC)
174         //cout << "550" << endl;
175         fSiOff=ldc_off+ldc_head_size+equip_head_size+pos_sioff;
176         word = Int(fSiOff, fEventData);
177         Short_t LenSi1 = (word >> 16) & 0xffff;
178         Short_t LenSi2 = word & 0xffff;
179         fQdcOff=fSiOff+4*(LenSi1+LenSi2+1)+equip_head_size+4; 
180       } 
181       else if (eq_id == 1182) {  //QDC first...
182         //cout << "1182" << endl;
183         fQdcOff=ldc_off+ldc_head_size+equip_head_size+pos_sioff;
184         fSiOff=fQdcOff+equip_head_size+4;
185       }
186       
187       ldc_off=ldc_size;
188       
189     }
190     //cout << endl;
191
192     //cout << "DDL = " << fDdlOff << endl;
193     // cout << "Si  = " << fSiOff << endl;
194     //cout << "QDC = " << fQdcOff << endl;
195     
196     DecodeSi();
197
198     fEventCount++; //event counter
199     return true;
200 }
201 //____________________________________________________________________________
202 Int_t AliTRDtestBeam::DecodeSi() {
203   
204   if (fSiOff < 0) return 0;
205   
206   // cout << "decoding Si data" << endl;
207
208   Long_t word;
209   
210   word=Int(fSiOff, fEventData);
211   fNSi1 = (word >> 16) & 0xffff;
212   fNSi2 = word & 0xffff;
213   
214   Int_t cSi=fSiOff; //   
215   for (int i = 0; i < fNSi1; i++) {
216     fSi1Address[i] =  ( Int(cSi, fEventData) >> 12 ) & 0x7ff;
217     fSi1Charge[i] = Int(cSi, fEventData)  & 0xfff;
218     cSi+=4;
219   }
220     
221   for (int i = 0; i < fNSi2; i++) {  //1,for Date!
222     fSi2Address[i] =  ( Int(cSi, fEventData) >> 12 ) & 0x7ff;
223     fSi2Charge[i] = Int(cSi, fEventData)  & 0xfff;
224     cSi+=4;
225   }  
226   
227   // reconstruction
228
229   int LenSiX = 640;
230
231   int qmaxX; int amaxX;
232   int qmaxY; int amaxY;
233   
234   qmaxX = 5;
235   qmaxY = 5;
236   amaxX = -1;
237   amaxY = -1+LenSiX;
238  
239   for( int i = 0; i < GetNSi1(); i++ ) {
240  
241     if (fSi1Address[i] == 0) continue; // noise
242    
243     if (fSi1Address[i] < LenSiX ) {
244       if( fSi1Charge[i] > qmaxX ) {
245         qmaxX = fSi1Charge[i];
246         amaxX = fSi1Address[i];
247       }
248     } else  {
249       if( fSi1Charge[i] > qmaxY ) {
250         qmaxY = fSi1Charge[i];
251         amaxY = fSi1Address[i];
252       }
253     }
254   }
255   
256   fX[0] = (float)(amaxX*0.05);  // [mm]
257   fY[0] = (float)((amaxY-LenSiX)*0.05);
258   fQx[0] = (float)qmaxX;
259   fQy[0] = (float)qmaxY;
260   
261   // 
262   qmaxX = 5;
263   qmaxY = 5;
264   amaxX = -1;
265   amaxY = -1+LenSiX;
266
267   for( int i = 0; i < GetNSi2(); i++ ) {
268     
269     if (fSi2Address[i] == 1279) continue; // noise
270     if (fSi2Address[i] == 0) continue;    // noise
271     
272     if(fSi2Address[i] < LenSiX) {
273       if( fSi2Charge[i] > qmaxX ) {
274         qmaxX = fSi2Charge[i];
275         amaxX = fSi2Address[i];
276       }
277     } else {
278       if( fSi2Charge[i] > qmaxY ) {
279         //if (fSi2Charge[i] > 50) cout << fSi2Charge[i] << " " << i << " " <<  fSi2Address[i] << endl;
280         qmaxY = fSi2Charge[i];
281         amaxY = fSi2Address[i];
282       }
283     }
284   }
285   
286   fX[1] = (float)(amaxX*0.05);  // [mm]
287   fY[1] = (float)((amaxY-LenSiX)*0.05);
288   fQx[1] = (float)qmaxX;
289   fQy[1] = (float)qmaxY;
290   
291   if (fQdcOff < 0) return 0;
292  
293   word=Int(fQdcOff, fEventData);
294   fPb   = (Double_t)((word >> 16) & 0xFFF);
295   fCher = (Double_t)((word ) & 0xFFF);
296
297   //cout << fCher << " " << fPb << endl;
298   return 1;
299 }
300 //____________________________________________________________________________ 
301 /**/
302 AliTRDRawStreamTB *AliTRDtestBeam::GetTRDrawStream() {
303   
304   // needs AliTRDRawStreamTB  
305   //cout << "Chamber reader:" << (Int_t)(fEventData+fDdlOff) << " " << fDdlSize << endl;
306   //int ifout = open("dump.dat", O_WRONLY | O_TRUNC | O_CREAT);
307   //write(ifout, (void*)(fEventData+fDdlOff+16), fDdlSize);
308   //close(ifout);
309
310   AliRawReaderMemory *reader = new AliRawReaderMemory((UChar_t*)(fEventData+fDdlOff), (UInt_t)fDdlSize);
311   reader->SetEquipmentID(1024);
312   reader->ReadHeader();
313   AliTRDRawStreamTB::RawBufferMissAligned(kTRUE);
314   AliTRDRawStreamTB::SupressWarnings(kTRUE);
315  
316   AliTRDRawStreamTB *tb = new AliTRDRawStreamTB(reader); 
317   tb->Init();
318   return tb;
319   /*
320     return 
321
322     AliRawReaderMemory *rmem = data->GetRawReader();
323     rmem->ReadHeader();
324     
325     AliTRDRawStreamTB tb(rmem);
326     tb.Init();
327     AliTRDRawStreamTB::SupressWarnings(kTRUE);
328     
329   */
330 }
331 /**/
332 //____________________________________________________________________________ 
333
334 Int_t AliTRDtestBeam::Int(Int_t i, Char_t *start) {
335   
336   bool swap = kFALSE;
337
338   if(swap) {
339     char *q=(char*)(start+i); 
340     char p[] = {q[3], q[2], q[1], q[0]};
341     return *((int*) p);
342   } else return *((int*)(start+i));
343 }
344
345 //____________________________________________________________________________