]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSspdTestBeam.cxx
205f0e4b71029e358a730597f09dd87954c514ef
[u/mrichter/AliRoot.git] / ITS / AliITSspdTestBeam.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 /*
17   $Id$
18 */
19
20 // A set of classes/routines that can read the SPD test beam data of 2002
21 // and create AliITSdigits. The posibility to use these routines to do the
22 // same for later and other detectors has yet to be demonstrated. At present
23 // there remains a bug in that the TreeE of event headders isn't created
24 // properly. See the macro AliITSspdTestBeam2Digits.C. The geometry from
25 // the class AliITSvSPD002 must be read in, one way or the other, so that
26 // the Geometry transoformation class AliITSgeom will prpoerly be inilized.
27
28 #include <stdlib.h>
29 #include <stddef.h>
30 #include <iomanip>
31 #include <Riostream.h>
32 #include <fstream>
33 #include <TArrayI.h>
34
35 #include "AliITSspdTestBeam.h"
36
37 ClassImp(AliITSspdTestBeam)
38
39 //----------------------------------------------------------------------
40 AliITSspdTestBeam::AliITSspdTestBeam(){
41     // Default Constructor for the Task AliITSspdTestBeam.
42     // Inputs:
43     //    none.
44     // Outputs:
45     //    none.
46     // Return:
47     //     A default constructed AliITSspdTestBeam class
48
49     fRH           = 0;
50     fRT           = 0;
51     fNBrst        = 0;
52     fBrstSize     = 0;
53     fBrst         = 0;
54     fNData        = 0;
55     fData         = 0;
56     fHData        = 0;
57     fTData        = 0;
58     SetTerminationWord();
59     fNEvents      = 0;
60     fBuffSize     = 0;
61     fBuff         = 0;
62     fITS          = 0;
63     fNfiles       = 0;
64     fMaxFiles     = 0;
65     fFiles        = 0;
66     fNeventsStart = 0;
67     fNeventsEnd   = 0;
68 }
69 //----------------------------------------------------------------------
70 AliITSspdTestBeam::AliITSspdTestBeam(const Char_t *filename,const Char_t *opt,
71                                      AliITS *its){
72     // Standard Constructor for the Task AliITSspdTestBeam.
73     // Inputs:
74     //    const Char_t *filename   File where to read in the SPD test beam data
75     //    const Char_t *opt        Option, 2002 => 2002 test beam data.
76     // Outputs:
77     //    none.
78     // Return:
79     //     A default constructed AliITSspdTestBeam class
80
81     fRH           = 0;
82     fRT           = 0;
83     fNBrst        = 0;
84     fBrstSize     = 0;
85     fBrst         = 0;
86     fNData        = 0;
87     fData         = 0;
88     fHData        = 0;
89     fTData        = 0;
90     SetTerminationWord();
91     fNEvents      = 0;
92     fBuffSize     = 0;
93     fBuff         = 0;
94     fITS          = 0;
95     fNfiles       = 0;
96     fMaxFiles     = 0;
97     fFiles        = 0;
98     fNeventsStart = 0;
99     fNeventsEnd   = 0;
100     //
101     fITS          = its;
102     fNfiles       = 0;
103     OpenInputFile(filename,0,-1);
104     if(strcmp(opt,"2002")) cout << "2002 assumed" << endl;
105 }
106
107 //----------------------------------------------------------------------
108 AliITSspdTestBeam::~AliITSspdTestBeam(){
109     // Destructor. Frees up any memory allocated or closes any files opened.
110     // Inputs:
111     //    none.
112     // Outputs:
113     //    none.
114     // Return:
115     //     nothing.
116     Int_t i,np;
117
118     np = GetNumberOfPilots();
119     fRH = 0;  // Just a Pointer into fBuff.
120     fRT = 0;  // Just a Pointer into fBuff.
121     if(fBrst){delete[] fBrst; fBrst = 0;}
122     if(fNData)for(i=0;i<np;i++){
123         if(fNData[i]) delete[] fNData[i];
124     } // end if
125     if(fNData) {delete[] fNData; fNData = 0;}
126     if(fData)for(i=0;i<np;i++){
127         if(fData[i]) delete[] fData[i];
128     } // end if
129     if(fData) delete[] fData;
130     fData = 0;
131     if(fHData)for(i=0;i<np;i++){
132         if(fHData[i]) delete[] fHData[i];
133     } // end if
134     if(fHData) delete[] fHData;
135     fHData = 0;
136     if(fTData)for(i=0;i<np;i++){
137         if(fTData[i]) delete[] fTData[i];
138     } // end if
139     if(fTData) delete[] fTData;
140     fTData = 0;
141     for(i=0;i<fMaxFiles;i++){
142         if(fFiles[i]!=0) delete fFiles[i];
143     } // end for i
144     if(fBrstSize)for(i=0;i<np;i++){
145         if(fBrstSize[i]) delete[] fBrstSize[i];
146     } // end if
147     if(fBrstSize) {delete[] fBrstSize; fBrstSize = 0;}
148     delete[] fBuff;
149     fITS = 0; //delete fITS;
150     delete[] fFiles;
151     delete[] fNeventsStart;
152     delete[] fNeventsEnd;
153 }
154 //----------------------------------------------------------------------
155 Int_t AliITSspdTestBeam::OpenInputFile(const Char_t *filename,Int_t start,Int_t end){
156     // Opens input file for reading spd test beam data.
157     // Inputs:
158     //    const Char_t *filename    file name to read data from
159     // Outputs:
160     //    none.
161     // Return:
162     //    An error number. 0=success, -1=failure.
163     Int_t stat = 0,i;
164
165     if(fMaxFiles==0) {
166         fMaxFiles     = 5;
167         fFiles        = new ifstream*[fMaxFiles];
168         fNeventsStart = new Int_t[fMaxFiles];
169         fNeventsEnd   = new Int_t[fMaxFiles];
170     } // end if
171     if(fNfiles==fMaxFiles){// Need to expand array of open files.
172         ifstream **tmp = new ifstream*[fMaxFiles];
173         TArrayI st(fMaxFiles);
174         TArrayI  en(fMaxFiles);
175         for(i=0;i<fMaxFiles;i++) { // copy pointers into tmp
176             tmp[i]    = fFiles[i];
177             fFiles[i] = 0;
178             st[i]     = fNeventsStart[i];
179             en[i]     = fNeventsEnd[i];
180         } // end for i
181         delete fFiles;
182         fMaxFiles    += 5;  // expand by 5.
183         fFiles        = new ifstream*[fMaxFiles];
184         fNeventsStart = new Int_t[fMaxFiles];
185         fNeventsEnd   = new Int_t[fMaxFiles];
186         for(i=0;i<fMaxFiles;i++) { // copy pointers back into fFiles
187             fFiles[i]        = 0;  // and zero rest.
188             fNeventsStart[i] = 0;
189             fNeventsEnd[i]   = 0;
190             if(i<fNfiles) {
191                 fFiles[i]        = tmp[i];
192                 tmp[i]           = 0;
193                 fNeventsStart[i] = st[i];
194                 fNeventsEnd[i]   = en[i];
195             } // end if i<fNfiles
196         } // end for i
197         // the array of pointers tmp is deleted automatically.
198         delete [] tmp;
199     } // end if
200     // Open file
201     fFiles[fNfiles] = new ifstream(filename,ios::in|ios::binary);
202     if(fFiles[fNfiles]==0){// file open error
203         cout << "Error opening input file " << filename << endl;
204         stat = -1;
205         return stat;
206     } // end if
207     fNeventsStart[fNfiles] = start;
208     fNeventsEnd[fNfiles]   = end;
209     fNfiles++;
210     return stat;
211 }
212 //----------------------------------------------------------------------
213 Int_t AliITSspdTestBeam::Read(Int_t i){
214     // Read in one buffer's worth of the file.
215     // Inputs:
216     //    Int_t i  Which file from the array of files to be read in.
217     // Outputs:
218     //    none.
219     // Return:
220     //    size of file.
221     Int_t filesize=0;
222
223     fFiles[i]->seekg(0,ios::end);
224     filesize  = fFiles[i]->tellg();
225     fFiles[i]->seekg(0,ios::beg);
226     if(fBuff) delete[] fBuff;
227     fBuffSize = filesize;
228     fBuff     = new UChar_t[fBuffSize];
229     fFiles[i]->read((Char_t*)fBuff,fBuffSize);
230     fFiles[i] ->close();
231     return filesize;
232 }
233 //----------------------------------------------------------------------
234 Int_t AliITSspdTestBeam::Decode(){
235     // Decode the fBuff read in.
236     // Inputs:
237     //    none.
238     // Outputs:
239     //    none.
240     // Return:
241     //    sets up the array fData fRH, and fRT
242     Int_t stat=0;
243     UInt_t *tr;
244     union {
245         UInt_t  *wd;
246         UChar_t *bt;
247     }u; // end union
248     Int_t size;
249     Int_t *ivnt,iburst,ip,np,i,j;
250     AliITSspdTestBeamData  *d;
251     AliITSspdTestBeamBurst *b;
252
253     np = GetNumberOfPilots();
254     ivnt = new Int_t[np];
255     for(i=0;i<np;i++) ivnt[i] = 0;
256     fRH  = (AliITSspdTestBeamHeader*) &(fBuff[0]); // Sets up the Run Header.
257     fRT  = (AliITSspdTestBeamTail*)&(fBuff[fBuffSize-fRT->SizeOf()]);
258     // Check termination
259     tr   = (UInt_t*) &(fBuff[fBuffSize-(fRT->SizeOf())-sizeof(UInt_t)]);
260     if(!(*tr==fTermination)){
261         cout << "Error Termination word not found at "<<tr<<endl;
262         exit(-1);
263     } // end if
264     if(fNData)for(i=0;i<np;i++){
265         if(fNData[i]) delete[] fNData[i];
266     } // end if
267     if(fNData) {delete[] fNData; fNData = 0;}
268     if(fData)for(i=0;i<np;i++){
269         if(fData[i]) delete[] fData[i];
270     } // end if
271     if(fData) {delete[] fData; fData = 0;}
272     if(fHData)for(i=0;i<np;i++){
273         if(fHData[i]) delete[] fHData[i];
274     } // end if
275     if(fHData) {delete[] fHData; fHData = 0;}
276     if(fTData)for(i=0;i<np;i++){
277         if(fTData[i]) delete[] fTData[i];
278     } // end if
279     if(fTData) {delete[] fTData; fTData = 0;}
280     if(fBrstSize)for(i=0;i<np;i++){
281         if(fBrstSize[i]) delete[] fBrstSize[i];
282     } // end if
283     if(fBrstSize) {delete[] fBrstSize; fBrstSize = 0;}
284     fNEvents  = fRH->GetNumberOfEvents();
285     fNBrst    = fNEvents/fRH->GetBurstSize();
286     fBrst     = new AliITSspdTestBeamBurst*[fNBrst];
287     fBrstSize = new Int_t*[np];
288     fNData    = new Int_t*[np];
289     fData     = new AliITSspdTestBeamData**[np];
290     fHData    = new AliITSspdTestBeamData**[np];
291     fTData    = new AliITSspdTestBeamData**[np];
292     for(i=0;i<np;i++){
293         fBrstSize[i] = new Int_t[fNBrst];
294         fNData[i]    = new Int_t[fNEvents];
295         fData[i]     = new AliITSspdTestBeamData*[fNEvents];
296         fHData[i]    = new AliITSspdTestBeamData*[fNEvents];
297         fTData[i]    = new AliITSspdTestBeamData*[fNEvents];
298         for(j=0;j<fNEvents;j++){
299             fNData[i][j] = 0;
300             fData[i][j]  = 0;
301             fHData[i][j] = 0;
302             fTData[i][j] = 0;
303         } // end for j
304     } // end for i
305     if(fBrst){delete[] fBrst; fBrst = 0;}
306     size      = fRH->SizeOf();
307     u.bt      = &fBuff[size];
308     //
309     for(iburst=0;(*(u.wd) != fTermination)&&(u.wd<tr);iburst++){ // loop over Bursts
310         b   = (AliITSspdTestBeamBurst *) u.wd;
311         fBrst[iburst] = b;
312         u.bt += b->SizeOf(); // increment wd byte wise
313         for(ip=0;ip<np;ip++){  // loop over pilots
314             // Get size of data stored for this pilot.
315             fBrstSize[ip][iburst] = (UInt_t) u.wd;
316             u.bt += sizeof(UInt_t); // increment wd byte wise
317             for(i=0;i<fBrstSize[ip][iburst];i++){ // loop over data
318                 d = (AliITSspdTestBeamData *) u.wd;
319                 switch (d->Mode()){
320                 case AliITSTestBeamData::kData :
321                     fNData[ip][ivnt[ip]]++;
322                     // set pointer to first data member
323                     if(fData[ip][ivnt[ip]] == 0 ) fData[ip][ivnt[ip]] = d;
324                     break;
325                 case AliITSTestBeamData::kHead :
326                     fNData[ip][ivnt[ip]]   = 0;
327                     fData[ip][ivnt[ip]]    = 0;
328                     fHData[ip][ivnt[ip]++] = d;
329                     break;
330                 case AliITSTestBeamData::kTail  : 
331                 case AliITSTestBeamData::kAbort :
332                     fTData[ip][ivnt[ip]] = d;
333                     break;
334                 default:
335                     cout << "Unknown Data Type: wd="<<hex<<*(u.wd)<<dec<<endl; 
336                     break;
337                 } // end switch
338                 u.bt += d->SizeOf(); // increment wd byte wise
339             } // end for i (next data word).
340         } // end for loop over pilots (ip).
341     } // end for loop over bursts
342     delete[] ivnt;
343     return stat;
344 }
345 //============================================================================
346 void AliITSspdTestBeamHeader::Print(ostream *os)const{
347     // print out the header information
348     // Inputs:
349     //    ostream *os  Pointer to the output stream.
350     // Outputs:
351     //    none.
352     // Return:
353     //    none.
354 /*
355 #if defined __GNUC__
356 #if __GNUC__ > 2
357     ios::fmtflags fmt;
358 #else
359     Int_t fmt;
360 #endif
361 #else
362 #if defined __ICC || defined __ECC
363     ios::fmtflags fmt;
364 #else
365     Int_t fmt;
366 #endif
367 #endif
368 */
369     *os<<"Version: "<<fUnion.fHead.fVersion<<" Written: "<<fUnion.fHead.fDate;
370     *os<<" " << fUnion.fHead.fTime << endl;
371     *os<<"Buffer Size [0], [1], [2]: " << fUnion.fHead.fBuffSize[0] << ",";
372     *os<<fUnion.fHead.fBuffSize[1]<<"," <<fUnion.fHead.fBuffSize[2] << endl;
373     *os<<"Test Pulse: " << fUnion.fHead.fTestPulse << endl;
374     *os<<"Trigger Mode General, [0], [1], [2]: " << fUnion.fHead.fTriggerMode;
375     *os<<","<<fUnion.fHead.fTrigger[0]<<","<<fUnion.fHead.fTrigger[1]<< ",";
376     *os<<fUnion.fHead.fTrigger[2] << endl;
377     *os<<"Number of Events: " << fUnion.fHead.fNEvents << " Burst Size: ";
378     *os<<fUnion.fHead.fBurstSize << endl;
379     return;
380 }
381 //----------------------------------------------------------------------
382 ostream &operator<<(ostream &os,AliITSspdTestBeamHeader &p){
383     // Standard output operator. See Print
384     // Inputs:
385     //    ostream                 &os  the output stream.
386     //    AliITSspdTestBeamHeader &p   the data to be printed out.
387     // Outputs:
388     //    none.
389     // Return:
390     //    ostream &os pointing now to the end of the present stream.
391
392     p.Print(&os);
393     return os;
394 }
395 //============================================================================
396 void AliITSspdTestBeamTail::Print(ostream *os)const{
397     // print out the Tail information
398     // Inputs:
399     //    ostream *os  Pointer to the output stream.
400     // Outputs:
401     //    none.
402     // Return:
403     //    none.
404 /*
405 #if defined __GNUC__
406 #if __GNUC__ > 2
407     ios::fmtflags fmt;
408 #else
409     Int_t fmt;
410 #endif
411 #else
412 #if defined __ICC || defined __ECC
413     ios::fmtflags fmt;
414 #else
415     Int_t fmt;
416 #endif
417 #endif
418 */
419     *os << "Number of Events: "<< fUnion.fTail.fEvents << " Written: "
420         << fUnion.fTail.fDate;
421     *os << " " << fUnion.fTail.fTime << endl;
422     *os <<"Termination Flag: " << fUnion.fTail.fTermMode << endl; 
423     return;
424 }
425 //----------------------------------------------------------------------
426 ostream &operator<<(ostream &os,AliITSspdTestBeamTail &p){
427     // Standard output operator. See Print
428     // Inputs:
429     //    ostream                 &os  the output stream.
430     //    AliITSspdTestBeamHeader &p   the data to be printed out.
431     // Outputs:
432     //    none.
433     // Return:
434     //    ostream &os pointing now to the end of the present stream.
435
436     p.Print(&os);
437     return os;
438 }
439 //============================================================================
440 void AliITSspdTestBeamBurst::Print(ostream *os)const{
441     // print out the Burst information
442     // Inputs:
443     //    ostream *os  Pointer to the output stream.
444     // Outputs:
445     //    none.
446     // Return:
447     //    none.
448 /*
449 #if defined __GNUC__
450 #if __GNUC__ > 2
451     ios::fmtflags fmt;
452 #else
453     Int_t fmt;
454 #endif
455 #else
456 #if defined __ICC || defined __ECC
457     ios::fmtflags fmt;
458 #else
459     Int_t fmt;
460 #endif
461 #endif
462 */
463     *os << "Burst Number: "<< fUnion.fBrst.fNumber << " Transfers: " 
464         << fUnion.fBrst.fTransfers << endl; 
465     return;
466 }
467 //----------------------------------------------------------------------
468 ostream &operator<<(ostream &os,AliITSspdTestBeamBurst &p){
469     // Standard output operator. See Print
470     // Inputs:
471     //    ostream                 &os  the output stream.
472     //    AliITSspdTestBeamHeader &p   the data to be printed out.
473     // Outputs:
474     //    none.
475     // Return:
476     //    ostream &os pointing now to the end of the present stream.
477
478     p.Print(&os);
479     return os;
480 }
481 //======================================================================
482 void AliITSspdTestBeamData::Print(ostream *os)const{
483     // print out the the Test Beam Data information
484     // Inputs:
485     //    ostream *os  Pointer to the output stream.
486     // Outputs:
487     //    none.
488     // Return:
489     //    none.
490 /*
491 #if defined __GNUC__
492 #if __GNUC__ > 2
493     ios::fmtflags fmt;
494 #else
495     Int_t fmt;
496 #endif
497 #else
498 #if defined __ICC || defined __ECC
499     ios::fmtflags fmt;
500 #else
501     Int_t fmt;
502 #endif
503 #endif
504 */
505     *os << "Word=" << hex << fUnion.fBuf[0] << hex << fUnion.fBuf[1] 
506                    << hex << fUnion.fBuf[2] << hex << fUnion.fBuf[3] << dec;
507     switch (this->Mode()){
508     case AliITSTestBeamData::kData :
509         *os << " kData chip=" << setw(3) << fUnion.fDataD.fChip; 
510         *os << " Row="        << setw(3) << fUnion.fDataD.fRow;
511         *os << " Column="     << setw(3) << fUnion.fDataD.fColm;
512         break;
513     case AliITSTestBeamData::kHead :
514         *os << " kHead Event Sync =" << fUnion.fDataH.fEventSync;
515         break;
516     case AliITSTestBeamData::kTail  :
517         *os << " kTail Transmitted word count =" << fUnion.fDataT.fTrans;
518         break;
519     case AliITSTestBeamData::kAbort :
520         *os << " kAbort Transmitted word count =" << fUnion.fDataA.fTrans;
521         break;
522     default:
523         *os << " Unknown Data Type"; 
524         break;
525     } // end switch
526     *os << endl;
527     return;
528 }
529 //----------------------------------------------------------------------
530 ostream &operator<<(ostream &os,AliITSspdTestBeamData &p){
531     // Standard output operator. See Print
532     // Inputs:
533     //    ostream                 &os  the output stream.
534     //    AliITSspdTestBeamHeader &p   the data to be printed out.
535     // Outputs:
536     //    none.
537     // Return:
538     //    ostream &os pointing now to the end of the present stream.
539
540     p.Print(&os);
541     return os;
542 }