StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StTrgDatReader.cxx
1 // $Id: StTrgDatReader.cxx,v 1.11 2018/05/29 20:01:36 akio Exp $
2 //
3 // $Log: StTrgDatReader.cxx,v $
4 // Revision 1.11 2018/05/29 20:01:36 akio
5 // Fixing run# problem when running with StFile list
6 //
7 // Revision 1.10 2010/01/25 17:41:53 akio
8 // Remove RecordSize, and RecordUnixTime to return 2019686401 (Sat Dec 31 19:00:01 2033)
9 //
10 // Revision 1.9 2010/01/15 21:26:51 fine
11 // RT #1816. Eliminate the side effect from RT 1803 fix
12 //
13 // Revision 1.8 2010/01/07 17:51:45 fine
14 // fix regexp to match the Akio filename format
15 //
16 // Revision 1.7 2010/01/07 17:37:58 fine
17 // introduce closeFileSignal to process several DAT files at once. RT # 1794
18 //
19 // Revision 1.6 2010/01/06 20:57:24 fine
20 // Adjust file pattern
21 //
22 // Revision 1.5 2010/01/06 20:42:26 fine
23 // Fix type EventNumber shoould be RunNumber . Thanks Akio
24 //
25 // Revision 1.4 2010/01/06 20:24:29 fine
26 // fix typo
27 //
28 // Revision 1.3 2010/01/06 20:09:39 fine
29 // RT #1794. Add EventNumber method to the StStreamFile interface RT # 1794
30 //
31 // Revision 1.2 2009/10/27 14:23:20 jeromel
32 // Fixed declaration of assert
33 //
34 // Revision 1.1 2009/10/13 15:53:31 fine
35 // Akio\'s DAT file format reader
36 //
37 //
38 
39 /***************************************************************************
40  * Author: akio ogawa
41  * Description: Trigger Data file (run*.*.dat) reader
42  **************************************************************************/
43 
44 #include "StTrgDatReader.h"
45 #include "StArchInfo.h"
46 #include "StMessMgr.h"
47 #include <iostream>
48 #include <assert.h>
49 #include <regex.h>
50 
51 namespace {
52 //__________________________________________________________________________
53 inline unsigned int swapI(unsigned int var){
54  return
55  (var & 0xff000000) >> 24 |
56  (var & 0x00ff0000) >> 8 |
57  (var & 0x0000ff00) << 8 |
58  (var & 0x000000ff) << 24 ;
59 }
60 }
61 const int StTrgDatReader::mLheader=8;
62 //__________________________________________________________________________
63 StTrgDatReader::StTrgDatReader():StStreamFile(), mLength(0),mVersion(0), mRunNumber(-1)
64  , mData(0) , mAllocated(0)
65 {
66  Buffer(mLheader);
67 }
68 
69 //__________________________________________________________________________
70 StTrgDatReader::StTrgDatReader(const char *fileName, ios_base::openmode mode)
71  :StStreamFile(fileName,mode), mLength(0), mVersion(0),mRunNumber(-1), mData(0),mAllocated(0)
72 {
73  Buffer(mLheader);
74 }
75 
76 
77 //__________________________________________________________________________
78 StTrgDatReader::~StTrgDatReader() { Buffer(0) ; }
79 
80 //__________________________________________________________________________
81 char *StTrgDatReader::Buffer(streamsize n)
82 {
83  if ( (n >mAllocated) || (n==0)) {
84  mData = (char *)realloc(mData,n);
85  assert(!n || (mData && "There is no memory to allocate the I/O buffer"));
86  mAllocated=n;
87  }
88  return mData;
89 }
90 
91 //__________________________________________________________________________
92 fstream &StTrgDatReader::Read(){
93  if (is_open() ) {
94  read(Buffer(mLheader), mLheader);
95  if (good()) {
96  mVersion = swapI(*(unsigned int*)mData);
97  mLength = swapI(*(unsigned int*)(mData+4));
98  assert( (mLength - mLheader > 0) && "The file is too short!!!");
99  if(Debug() >0) {
100  printf("Version = %x\nLength = %d; data=%p\n",Version(), Length(),mData);
101  }
102  read(Buffer(mLength)+mLheader, mLength - mLheader);
103  }
104  if (!good()) {
105  mLength = 0;
106  if (!eof()) Perror("*********** Attention DAT errors:");
107  }
108  }
109  return stream();
110 }
111 //__________________________________________________________________________
112 int StTrgDatReader::RunNumber() const {
113  if (mRunNumber == -1) {
114  TString ts(filename());
115  ts.ReplaceAll("_",".");
116  Ssiz_t last=ts.Last('/');
117  if(last>0) {last++; ts.Remove(0,last);}
118  string f = ts.Data();
119  //string f = filename();
120  //cout << "filename ="<<f<<endl;
121  regex_t rx;
122  const char *pattern = "^.*run([0-9]+)\\..+\\.dat$";
123  int rc;
124  char buffer[100];
125  if ((rc = regcomp(&rx, pattern, REG_EXTENDED))) {
126  regerror(rc, &rx, buffer, 100);
127  LOG_ERROR << "Can not extract the event number from the file name \'"
128  << f << "\' because \'"
129  << buffer << "\'"
130  << endm;
131  assert(0&&"Can not extract the event number from the file name");
132  } else {
133  //cout << "1filename ="<<f<<" run#="<<mRunNumber<<endl;
134  regmatch_t matchptr[2];
135  if ( !(regexec (&rx, f.c_str(), sizeof(matchptr)/sizeof(regmatch_t), matchptr, 0)) ) {
136  ((StTrgDatReader*) this)->mRunNumber = atoi(f.substr(matchptr[1].rm_so,matchptr[1].rm_eo).c_str());
137  //cout << "2filename ="<<f<<" run#="<<mRunNumber<<endl;
138  }
139  }
140  //cout << "3filename ="<<f<<" run#="<<mRunNumber<<endl;
141  }
142  return mRunNumber;
143 }
144 
145 //__________________________________________________________________________
146 int StTrgDatReader::RecordUnixTime() const
147 {
148  // the current record /event unix time (-1 = "unknown")
149  return 2019686401;
150 }
151 
152 //__________________________________________________________________________
153 int StTrgDatReader::Length() const { return mLength; }
154 
155 //__________________________________________________________________________
156 int StTrgDatReader::Version() const { return mVersion;}
157 
158 //__________________________________________________________________________
159 char* StTrgDatReader::Record() { return mData; }
160 //__________________________________________________________________________
161 bool StTrgDatReader::closeFileSignal()
162 {
163 //< the method to be overriden in subclass to customize the "new file has been open" status
164  mRunNumber = -1;
165  return true;
166 }