23 #ifndef GW_GPSICONVERTER_H
54 fHeader.fNbSpectra = 0;
55 fHeader.fFileSize = 0;
58 for (
int i = 0; i <
MAX_SP; i++) {
59 fHeader.fWhere[i] = 0;
63 for (
int j = 0; j < 12; j++) fLabel[i].fSpName[j] = 0;
68 if ( tmp.EndsWith(
fFileExt) == kFALSE ) {
69 SetError(HistoConverter::kZombie,
"extension must be .s for gpsi files !");
73 fStreamIN.open(name,ios::in);
75 if ( fStreamIN.is_open() ) {
79 if ( ReadHeaders() == false ) {
82 "Problems when reading the header part");
89 "Currently, only possible to read existing gpsi files");
109 if ( fStreamIN.is_open() ) { fStreamIN.close(); }
121 bool GPSIConverter::ReadHeaders()
123 if ( fStreamIN.is_open() == false )
return false;
125 char tmp[512];
for (
int i = 0; i < 512; i++) tmp[i] = 0;
129 fStreamIN.seekg(0,ios::end); real_size = fStreamIN.tellg();
130 if ( real_size < 512*512 ) {
135 fLog <<
debug <<
"File too small to be a gpsi file"
145 short nbspectra1 = 0, nbspectra2 = 0;
int filesize1 = 0, filesize2 = 0;
148 fStreamIN.seekg(0,ios::beg);
149 fStreamIN.read(tmp,2); nbspectra1 =
Short(tmp);
150 fStreamIN.read(tmp,2); filesize1 =
Short(tmp); filesize1 = 512*(filesize1-1);
152 fStreamIN.seekg(0,ios::beg);
153 fStreamIN.read(tmp,2); nbspectra2 =
Short(tmp);
154 fStreamIN.read(tmp,2); filesize2 =
Short(tmp); filesize2 = 512*(filesize2-1);
157 short which_swap = 0;
158 if ( 0 < nbspectra1 && nbspectra1 < 254 ) which_swap = 1;
159 if ( 0 < nbspectra2 && nbspectra2 < 254 ) which_swap = 2;
161 if ( which_swap == 0 ) {
166 fLog <<
error <<
"# of spectra not in the range [1,253]" <<
nline ;
176 if ( real_size == 512*512 ) {
180 if ( (real_size-5) < filesize1 && filesize1 < (real_size+5) ) which_swap += 10;
181 if ( (real_size-5) < filesize2 && filesize2 < (real_size+5) ) which_swap += 20;
183 if ( which_swap == 11 || which_swap == 22 ) {
191 fLog <<
error <<
"Cannot determine endian type swap " << which_swap <<
nline ;
198 fStreamIN.seekg(0,ios::beg);
199 fStreamIN.read(tmp,2); fHeader.fNbSpectra =
Short(tmp);
202 for (
int i = 0; i < fHeader.fNbSpectra; i++ ) {
205 fStreamIN.seekg(2*i+4,ios::beg); fStreamIN.read(tmp,2); fHeader.fWhere[i] =
Short(tmp);
208 fStreamIN.seekg((fHeader.fWhere[i]-1)*512,ios::beg);
209 fStreamIN.read(tmp,512);
211 unsigned short test_label1 =
UShort(tmp);
212 unsigned short test_label2 =
UShort(tmp+2);
213 if ( test_label1 != 0xFFFF || test_label2 != 0xFFFF ) {
219 << i <<
" has an incorrect header .. probably not a gpsi file" <<
nline;
223 int len =
Short(tmp+16) - 12;
224 strncpy(fLabel[i].fSpName, (tmp + 31), len);
225 fLabel[i].fNx =
Short(tmp+70);
226 fLabel[i].fNy =
Short(tmp+66);
234 HistoConverter::ls(o);
235 cout << fHeader.fNbSpectra <<
" spectra found in the file " <<
endl;
236 for (
int i = 0; i < fHeader.fNbSpectra; i++){
237 cout <<
" [" << i <<
"] " << fLabel[i].fSpName;
245 if ( (i+1) % 5 == 0 ) cout <<
endl;
254 if ( which < 0 || which >= fHeader.fNbSpectra )
return NULL;
255 else return Get(fLabel[which].fSpName);
261 TH1F *h =
new TH1F();
264 if ( !
Read((*h)) ) {
delete h; h = NULL; }
272 bool ok =
false; TString hname = h.GetName();
276 for (
int i = 0; i < fHeader.fNbSpectra; i++ ) {
277 if ( hname.CompareTo(fLabel[i].fSpName) == 0 ) {
278 h.SetBins(fLabel[i].fNx*fLabel[i].fNy,0,fLabel[i].fNx*fLabel[i].fNy); which = i;
break;
284 Form(
"SP %s not found",h.GetName()));
289 fStreamIN.clear(); fStreamIN.open(
fName.Data(),ios::in);
290 if ( fStreamIN.is_open() ) {
292 char *tmp =
new char[4*fLabel[which].fNx*fLabel[which].fNy];
293 if (tmp == NULL)
return false;
295 fStreamIN.seekg(fHeader.fWhere[which]*512,ios::beg);
296 fStreamIN.read(tmp,4*fLabel[which].fNx*fLabel[which].fNy);
298 if ( fStreamIN.good() ) {
299 for (
int i = 0; i < fLabel[which].fNx*fLabel[which].fNy; i++ )
300 h.Fill(i,
Float(tmp+4*i)) ;
303 fStreamIN.close();
delete []
tmp;
315 "CANNOT WRITE - gpsi is a deprecated DB system");
return false;
323 "NO IMPLEMENTATION !!") ;
return false;
331 "CANNOT WRITE - gpsi is a deprecated DB system");
return false;
337 TString tmphs = hs.GetName();
339 Bool_t wildcard =
false;
340 if ( tmphs.Contains(
"*") )
342 TRegexp all(
"*",kTRUE), pattern(hs.GetName(),wildcard);
344 if ( pattern.Status() != TRegexp::kOK ) {
345 if ( gDebug > 0 ) cerr <<
"pattern * " << hs.GetName() <<
endl ;
348 else {
if ( gDebug > 0 ) cerr <<
"pattern " << hs.GetName() <<
endl ; }
351 for (
int i = 0; i < fHeader.fNbSpectra; i++ ) {
357 fLog <<
debug <<
"Comparing " << fLabel[i].fSpName <<
" ..";
360 tmp = fLabel[i].fSpName;
361 if ( tmp.Contains(pattern) ) h =
Get(fLabel[i].fSpName);
364 if ( h ) { hs.Add(h);
if ( gDebug > 0 ) cerr <<
" IN STACK " <<
endl; }
365 else {
if ( gDebug > 0 ) cerr <<
" NOT IN STACK " <<
endl; }
375 "CANNOT WRITE - gpsi is a deprecated DB system");
return false;
void SetError(HistoConverter::EStatus status=kGood, const char *msgerror="")
to set/reset the status of this converter
LogMessage & error(LogMessage &)
static void AddPrototype(const char *, HistoConverter *)
to add a new type of database service
LogMessage & nline(LogMessage &)
virtual bool Write(const TH1 &)
to write an histogram
virtual const char * GetType() const
to know what type of HistoConverter system it is
virtual void ls(Option_t *) const
to show the spectra in this DB system on the standard output
unsigned short UShort(char *buf)
virtual bool Read(TH1 &)
to read an histogram
virtual HistoConverter * NewDB(const char *) const
to creat a new DB system
LogMessage & debug(LogMessage &)
virtual std::string & GetProcessName()
To get the Process name.
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual TH1 * Get(int)
to get histo number i in the list
virtual void CheckDirectory(const char *name)
check if the given directory can be read/written
virtual std::string & GetProcessMethod()
To get the current method.