1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
22 #include <Riostream.h>
24 #include <AliRunLoader.h>
25 #include <AliLoader.h>
28 #include "AliITSspdTestBeam.h"
30 ClassImp(AliITSspdTestBeam)
32 //----------------------------------------------------------------------
33 AliITSspdTestBeam::AliITSspdTestBeam(){
34 // Default Constructor for the Task AliITSspdTestBeam.
40 // A default constructed AliITSspdTestBeam class
61 //----------------------------------------------------------------------
62 AliITSspdTestBeam::AliITSspdTestBeam(const Char_t *filename,const Char_t *opt,
64 // Standard Constructor for the Task AliITSspdTestBeam.
66 // const Char_t *filename File where to read in the SPD test beam data
67 // const Char_t *opt Option, 2002 => 2002 test beam data.
71 // A default constructed AliITSspdTestBeam class
94 OpenInputFile(filename,0,-1);
95 if(strcmp(opt,"2002")) cout << "2002 assumed" << endl;
98 //----------------------------------------------------------------------
99 AliITSspdTestBeam::~AliITSspdTestBeam(){
100 // Destructor. Frees up any memory allocated or closes any files opened.
117 DeletefNeventsStart();
120 //----------------------------------------------------------------------
121 void AliITSspdTestBeam::DeletefBrst(){
122 // Properly deletes fBrst object.
132 for(i=0;i<fNBrst;i++) if(fBrst[i]){
140 //----------------------------------------------------------------------
141 void AliITSspdTestBeam::DeletefNData(){
142 // Properly deletes fNData object.
149 Int_t i,np=GetNumberOfPilots();
152 for(i=0;i<np;i++) if(fNData[i]){
160 //----------------------------------------------------------------------
161 void AliITSspdTestBeam::DeletefData(){
162 // Properly deletes fData object.
169 Int_t i,j,np=GetNumberOfPilots();
172 for(i=0;i<np;i++) if(fData[i]){
173 for(j=0;j<fNEvents;j++){
174 delete[] fData[i][j];
184 //----------------------------------------------------------------------
185 void AliITSspdTestBeam::DeletefHData(){
186 // Properly deletes fHData object.
193 Int_t i,j,np=GetNumberOfPilots();
196 for(i=0;i<np;i++) if(fHData[i]){
197 for(j=0;j<fNEvents;j++){
198 delete[] fHData[i][j];
208 //----------------------------------------------------------------------
209 void AliITSspdTestBeam::DeletefTData(){
210 // Properly deletes fTData object.
217 Int_t i,j,np=GetNumberOfPilots();
220 for(i=0;i<np;i++) if(fTData[i]){
221 for(j=0;j<fNEvents;j++){
222 delete[] fTData[i][j];
232 //----------------------------------------------------------------------
233 void AliITSspdTestBeam::DeletefFiles(){
234 // Properly deletes fBrst object.
244 for(i=0;i<fMaxFiles;i++){
245 if(fFiles[i]!=0) delete fFiles[i];
249 //----------------------------------------------------------------------
250 void AliITSspdTestBeam::DeletefBrstSize(){
251 // Properly deletes fBrstSize object.
258 Int_t i,np=GetNumberOfPilots();
263 delete[] fBrstSize[i];
271 //----------------------------------------------------------------------
272 Int_t AliITSspdTestBeam::OpenInputFile(const Char_t *filename,Int_t start,Int_t end){
273 // Opens input file for reading spd test beam data.
275 // const Char_t *filename file name to read data from
279 // An error number. 0=success, -1=failure.
284 fFiles = new ifstream*[fMaxFiles];
285 for(i=0;i<fMaxFiles;i++) fFiles[i] = 0;
286 fNeventsStart = new Int_t[fMaxFiles];
287 fNeventsEnd = new Int_t[fMaxFiles];
289 if(fNfiles==fMaxFiles){// Need to expand array of open files.
290 ifstream *tmp[fMaxFiles];
291 Int_t st[fMaxFiles],en[fMaxFiles];
292 for(i=0;i<fMaxFiles;i++) { // copy pointers into tmp
295 st[i] = fNeventsStart[i];
296 en[i] = fNeventsEnd[i];
299 fMaxFiles += 5; // expand by 5.
300 fFiles = new ifstream*[fMaxFiles];
301 fNeventsStart = new Int_t[fMaxFiles];
302 fNeventsEnd = new Int_t[fMaxFiles];
303 for(i=0;i<fMaxFiles;i++) { // copy pointers back into fFiles
304 fFiles[i] = 0; // and zero rest.
305 fNeventsStart[i] = 0;
310 fNeventsStart[i] = st[i];
311 fNeventsEnd[i] = en[i];
312 } // end if i<fNfiles
314 // the array of pointers tmp is deleted automatically.
317 fFiles[fNfiles] = new ifstream(filename,ios::in|ios::binary);
318 if(fFiles[fNfiles]==0){// file open error
319 cout << "Error opening input file " << filename << endl;
323 fNeventsStart[fNfiles] = start;
324 fNeventsEnd[fNfiles] = end;
328 //----------------------------------------------------------------------
329 Int_t AliITSspdTestBeam::Read(Int_t i){
330 // Read in one buffer's worth of the file.
332 // Int_t i Which file from the array of files to be read in.
339 fFiles[i]->seekg(0,ios::end);
340 filesize = fFiles[i]->tellg();
341 fFiles[i]->seekg(0,ios::beg);
342 if(fBuff) delete[] fBuff;
343 fBuffSize = filesize;
344 fBuff = new UChar_t[fBuffSize];
345 fFiles[i]->read((Char_t*)fBuff,fBuffSize);
349 //----------------------------------------------------------------------
350 Int_t AliITSspdTestBeam::Decode(){
351 // Decode the fBuff read in.
357 // sets up the array fData fRH, and fRT
365 Int_t *ivnt,iburst,ip,np,i,j;
366 AliITSspdTestBeamData *d;
367 AliITSspdTestBeamBurst *b;
369 np = GetNumberOfPilots();
370 cout << "Sizeof(Headder)=" << fRH.SizeOf() << endl;
371 cout << "Sizeof(Tail)=" << fRT.SizeOf() << endl;
372 ivnt = new Int_t[np];
373 for(i=0;i<np;i++) ivnt[i] = 0;
374 fRH.SetBuffer(fBuff);//Set Run Header
376 size = fBuffSize - size;
377 cout <<"fBuffSize-sizeof(AliITSspdTestBeamTail) "<< size << endl;
378 fRT.SetBuffer(&(fBuff[size]));
380 size -= sizeof(UInt_t);
381 tr = (UInt_t*) &(fBuff[size]);
382 if(!(*tr==fTermination)){
383 cout << "Error Termination word not found at " << size << " tr=0x" <<
384 hex << *tr << dec << endl;
393 fNEvents = fRH.GetNumberOfEvents();
394 if(fRT.GetNumberOfEvents()>fNEvents) fNEvents = fRT.GetNumberOfEvents();
395 fNBrst = fNEvents/fRH.GetBurstSize();
396 fBrst = new AliITSspdTestBeamBurst*[fNBrst];
397 fBrstSize = new Int_t*[np];
398 fNData = new Int_t*[np];
399 fData = new AliITSspdTestBeamData**[np];
400 fHData = new AliITSspdTestBeamData**[np];
401 fTData = new AliITSspdTestBeamData**[np];
403 fBrstSize[i] = new Int_t[fNBrst];
404 fNData[i] = new Int_t[fNEvents];
405 fData[i] = new AliITSspdTestBeamData*[fNEvents];
406 fHData[i] = new AliITSspdTestBeamData*[fNEvents];
407 fTData[i] = new AliITSspdTestBeamData*[fNEvents];
408 for(j=0;j<fNEvents;j++){
418 for(iburst=0;(*(u.wd) != fTermination)&&(u.wd<tr);iburst++){
420 b = new AliITSspdTestBeamBurst(u.bt);
422 u.bt += b->SizeOf(); // increment wd byte wise
423 for(ip=0;ip<np;ip++){ // loop over pilots
424 // Get size of data stored for this pilot.
425 AliITSTestBeamData::Swapit(4,u.bt);
426 fBrstSize[ip][iburst] = (UInt_t) *(u.wd);
427 u.bt += sizeof(UInt_t); // increment wd byte wise
428 for(i=0;i<fBrstSize[ip][iburst];i++){ // loop over data
429 AliITSTestBeamData::Swapit(4,u.bt);
430 d = new AliITSspdTestBeamData(u.bt);
432 case AliITSTestBeamData::kData :
433 fNData[ip][ivnt[ip]]++;
434 // set pointer to first data member
435 if(fData[ip][ivnt[ip]] == 0 ){
436 fData[ip][ivnt[ip]] = d;
439 case AliITSTestBeamData::kHead :
440 fHData[ip][ivnt[ip]] = d;
441 fTData[ip][ivnt[ip]] = 0;
442 fNData[ip][ivnt[ip]] = 0;
443 fData[ip][ivnt[ip]] = 0;
445 case AliITSTestBeamData::kTail :
446 case AliITSTestBeamData::kAbort :
447 fTData[ip][ivnt[ip]++] = d;
450 cout << "Unknown Data Type: wd="<<hex<<*(u.wd)<<dec<<endl;
453 u.bt += d->SizeOf(); // increment wd byte wise
454 } // end for i (next data word).
455 } // end for loop over pilots (ip).
456 } // end for loop over bursts
460 //----------------------------------------------------------------------
461 Int_t AliITSspdTestBeam::DecodeModule(Int_t pilot,Int_t chip){
462 // Determines the Module number based on the pilot and chip
463 // valules and the fVersion of the simulation.
465 // Int_t pilot Pilot number
466 // Int_t chip chip number
470 // The module number (see simulations geometry).
474 if(pilot==0) return chip;
475 if(pilot==1) return 2;
476 if(pilot==2) return chip+3;
479 if(pilot==0) return chip;
480 if(pilot==1) return 2;
481 if(pilot==2) return chip+3;
486 //----------------------------------------------------------------------
487 void AliITSspdTestBeam::Digitize(Int_t evnt){
488 // Write out ITS SPD Digits.
490 // Int_t evnt events number
496 Int_t module,chip,row,colm,digit[3];
499 fLoader->GetRunLoader()->SetEventNumber(evnt);
501 if(!(fLoader->TreeD())){
502 fLoader->MakeTree("D");
505 fITS->MakeBranch("D");
506 //fITS->SetTreeAddress();
507 fITS->SetTreeAddressD(fLoader->TreeD());
509 for(p=0;p<GetNumberOfPilots();p++)
510 for(i=0;i<fNData[p][evnt];i++){
511 chip = fData[p][evnt]->Chip(i);
512 module = DecodeModule(p,chip);
513 row = fData[p][evnt]->Row(i);
514 colm = fData[p][evnt]->Colm(i);
515 digit[0] = row; digit[1] = colm, digit[2] = 1;
516 fITS->AddRealDigit(0,digit);
517 if(module!=oldmodule) { // New Module
519 fLoader->TreeD()->Fill();
523 fITS->ClearModules();
524 fLoader->TreeD()->GetEntries();
525 fLoader->TreeD()->AutoSave();
526 fLoader->TreeD()->Reset();
528 //----------------------------------------------------------------------
529 void AliITSspdTestBeam::PrintHeadder(ostream *os){
530 // Prints the Run Headder.
532 // ostream *os Output stream where the data should go to
539 //----------------------------------------------------------------------
540 void AliITSspdTestBeam::PrintTrailer(ostream *os){
541 // Prints the Run Trailer.
543 // ostream *os Output stream where the data should go to
550 //----------------------------------------------------------------------
551 void AliITSspdTestBeam::PrintBurstInfo(Int_t i,ostream *os){
552 // Prints the specified burts information
554 // Int_t i Specifies which burst to print.
555 // ostream *os Output stream where the data should go to
562 //----------------------------------------------------------------------
563 void AliITSspdTestBeam::PrintEventData(Int_t evnt,ostream *os){
564 // Prints the data for a specified event number.
566 // Int_t evnt events number
567 // ostream *os Output stream where the data should go to
574 for(p=0;p<GetNumberOfPilots();p++)
575 for(i=0;i<fNData[p][evnt];i++){
576 *os << "Pilot=" << setw(3) << p << " ";
577 fData[p][evnt]->Print(os,i);
581 //----------------------------------------------------------------------
582 void AliITSspdTestBeam::PrintEventHead(Int_t evnt,ostream *os){
583 // Prints the data Headder for a specified event number.
585 // Int_t evnt events number
586 // ostream *os Output stream where the data should go to
593 for(p=0;p<GetNumberOfPilots();p++){
594 *os << "Pilot=" << setw(3) << p << " " << *(fHData[p][evnt]);
597 //----------------------------------------------------------------------
598 void AliITSspdTestBeam::PrintEventTail(Int_t evnt,ostream *os){
599 // Prints the data Trailer for a specified event number.
601 // Int_t evnt events number
602 // ostream *os Output stream where the data should go to
609 for(p=0;p<GetNumberOfPilots();p++){
610 *os << "Pilot=" << setw(3) << p << " " << *(fTData[p][evnt]);
613 //============================================================================
614 void AliITSTestBeamData::Swapit(Int_t i,UChar_t *a){
615 // Swap the order of the bits
617 // Int_t i Size of UChar_t array a.
618 // UChar_t a Array of bytes to be swapped.
620 // UChar_t a Array with bytes swapped
626 for(j=0;j<i;j++) c[j] = a[i-j-1];
627 for(j=0;j<i;j++) a[j] = c[j];
629 //============================================================================
630 void AliITSspdTestBeamHeader::Print(ostream *os){
631 // print out the header information
633 // ostream *os Pointer to the output stream.
646 #if defined __ICC || defined __ECC
653 *os<<"Version: "<<GetVersion()<<" Written: "<<fUnion->fHead.fDate;
654 *os<<" " << fUnion->fHead.fTime << endl;
655 *os<<"Buffer Size [0], [1], [2]: " << GetBuffSize(0) << ",";
656 *os<<GetBuffSize(1)<<"," <<GetBuffSize(2) << endl;
657 *os<<"Test Pulse: " << GetTestPulse() << endl;
658 *os<<"Trigger Mode General, [0], [1], [2]: " << GetTriggerMode();
659 *os<<","<<GetTrigger(0)<<","<<GetTrigger(1)<< ",";
660 *os<<GetTrigger(2) << endl;
661 *os<<"Number of Events: " << GetNumberOfEvents() << " Burst Size: ";
662 *os<<GetBurstSize() << endl;
665 //----------------------------------------------------------------------
666 ostream &operator<<(ostream &os,AliITSspdTestBeamHeader &p){
667 // Standard output operator. See Print
669 // ostream &os the output stream.
670 // AliITSspdTestBeamHeader &p the data to be printed out.
674 // ostream &os pointing now to the end of the present stream.
679 //============================================================================
680 void AliITSspdTestBeamTail::Print(ostream *os){
681 // print out the Tail information
683 // ostream *os Pointer to the output stream.
696 #if defined __ICC || defined __ECC
703 *os << "Number of Events: "<< GetNumberOfEvents() << " Written: "
704 << fUnion->fTail.fDate;
705 *os << " " << fUnion->fTail.fTime << endl;
706 *os <<"Termination Flag: " << GetTermMode() << endl;
709 //----------------------------------------------------------------------
710 ostream &operator<<(ostream &os,AliITSspdTestBeamTail &p){
711 // Standard output operator. See Print
713 // ostream &os the output stream.
714 // AliITSspdTestBeamHeader &p the data to be printed out.
718 // ostream &os pointing now to the end of the present stream.
723 //============================================================================
724 void AliITSspdTestBeamBurst::Print(ostream *os){
725 // print out the Burst information
727 // ostream *os Pointer to the output stream.
740 #if defined __ICC || defined __ECC
747 *os << "Burst Number: "<< GetEventNumber()<< " Transfers: "
748 << GetTransfers() << endl;
751 //----------------------------------------------------------------------
752 ostream &operator<<(ostream &os,AliITSspdTestBeamBurst &p){
753 // Standard output operator. See Print
755 // ostream &os the output stream.
756 // AliITSspdTestBeamHeader &p the data to be printed out.
760 // ostream &os pointing now to the end of the present stream.
765 //======================================================================
766 void AliITSspdTestBeamData::Print(ostream *os,Int_t i){
767 // print out the the Test Beam Data information
769 // ostream *os Pointer to the output stream.
782 #if defined __ICC || defined __ECC
789 //*os << "Word=" << hex << (fUnion+i)->fBuf[0] << hex << (fUnion+i)->fBuf[1]
790 // << hex << (fUnion+i)->fBuf[2] << hex << (fUnion+i)->fBuf[3]
791 *os << "Word=" << hex << (fUnion+i)->fIBuff
793 switch (this->Mode(i)){
794 case AliITSTestBeamData::kData :
795 *os << " kData chip=" << setw(3) << Chip(i);
796 *os << " Row=" << setw(3) << Row(i);
797 *os << " Column=" << setw(3) << Colm(i);
799 case AliITSTestBeamData::kHead :
800 *os << " kHead Event Sync =" << EventCounter();
802 case AliITSTestBeamData::kTail :
803 *os << " kTail Transmitted word count =" << TransWordCount();
805 case AliITSTestBeamData::kAbort :
806 *os << " kAbort Transmitted word count =" << TransWordCount();
809 *os << " Unknown Data Type";
815 //----------------------------------------------------------------------
816 ostream &operator<<(ostream &os,AliITSspdTestBeamData &p){
817 // Standard output operator. See Print
819 // ostream &os the output stream.
820 // AliITSspdTestBeamHeader &p the data to be printed out.
824 // ostream &os pointing now to the end of the present stream.