23 #ifndef GW_RADCONVERTER_H
36 #ifndef ROOT_TSystemDirectory
37 #include <TSystemDirectory.h>
40 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
77 HistoConverter::ls(o);
78 TSystemDirectory dir; dir.SetDirectory(
fName.Data()); TList *list = dir.GetListOfFiles();
80 TIter iter(list); TObject *entry;
int count = 0;
81 while ( (entry = iter()) ) {
82 TString
tmp = entry->GetName();
85 cout <<
" [" << count++ <<
"] " << entry->GetName() ;
87 if ( (count+1) % 5 == 0 ) cout <<
endl;
91 cout <<
endl;
if ( list )
delete list;
104 TSystemDirectory dir; dir.SetDirectory(
fName.Data()); TList *list = dir.GetListOfFiles();
106 TIter iter(list); TObject *entry;
int count = 0; TString result =
"_EMPTY_";
107 while ( (entry = iter()) ) {
108 TString
tmp = entry->GetName();
110 if ( which == count ) result = entry->GetName(); count++;
113 if ( list )
delete list;
115 if ( result !=
"_EMPTY_" )
return Get(result.Data());
118 Form(
"Cannot find entry # %d",which));
126 TH1F *h =
new TH1F();
129 if ( !
Read((*h)) ) {
delete h; h = NULL; }
137 TString filename =
fName.Data();
138 if ( filename.IsNull() ) filename =
".";
140 if ( ! filename.EndsWith(
"/") ) filename+=
"/";
142 filename += h.GetName();
150 file.open(filename.Data());
151 if ( ! file.is_open() ){
154 Form(
"Cannot open file %s",filename.Data()));
159 char nom[24], nam[8];
int ndim[4], dum[2], i;
164 if ( gDebug > 0 )
fLog <<
debug <<
"Spectrum name : " << nam <<
nline;
166 file.read((
char*)ndim, 16);
167 if (ndim[0] >= 65536) {
169 for(i=0; i<4; i++) ndim[i] =
Int(((
char*)&(ndim[i])));
172 if ( gDebug > 0 )
fLog <<
debug <<
"Spectrum size : " << ndim[0] <<
nline;
177 file.read((
char*)dum, 8);
178 file.read((
char*)buffer, 4*ndim[0]);
185 h.SetBins(ndim[0],0,ndim[0]);
186 for (i=0; i<ndim[0]; i++)
187 h.Fill(i,
Float((
char*)&(buffer[i])));
196 TString hname, filename;
202 filename.Prepend(
fName.Data());
204 fLog <<
error <<
"File to store the spectrum "
205 << filename <<
nline ;
213 int i, j = 24, toto, tab[4] = {0,1,1,1},
size;
char spname[8];
215 ::strncpy(spname,h.GetName(),8);
216 tab[0] =
size = h.GetNbinsX(); toto =
size *
sizeof(float);
218 fprad = fopen(filename.Data(),
"w");
221 Form(
"[RADConverter::Write] Cannot open %s in writing mode",filename.Data()));
224 float *spectre =
new float[h.GetNbinsX()];
225 for ( i = 0; i < tab[0]; i++ )
226 spectre[i] = h.GetBinContent(i+1);
236 fwrite(&j,
sizeof(
int), 1, fprad);
237 fwrite(spname, 8, 1, fprad);
238 fwrite(tab,
sizeof(
int), 4, fprad);
239 fwrite(&j,
sizeof(
int), 1, fprad);
240 fwrite(&toto,
sizeof(
int), 1, fprad);
241 fwrite(spectre,
sizeof(
float),
size, fprad);
242 fwrite(&toto,
sizeof(
int),1,fprad);
244 fclose(fprad);
delete spectre;
return true;
251 TString filename =
fName.Data(); filename += h.GetName();
257 if ( filename.EndsWith(
".mat") ) whichext = 1;
258 if ( filename.EndsWith(
".m4b") ) whichext = 2;
259 if ( filename.EndsWith(
".spn") ) whichext = 3;
261 if ( whichext == 0 ) filename +=
".mat";
269 file.open(filename.Data());
270 if ( ! file.is_open() ){
272 Form(
"Cannot open file %s",filename.Data()));
277 file.seekg(0,ios::end);
long filesize = file.tellg() ;
279 if ( (filesize != 2*4096*4096) && (filesize != 4*4096*4096) ) {
282 Form(
"File %s cannot be a Radware matrix",filename.Data()));
283 file.close();
return false;
285 else { itemsize = file.tellg() / (4096*4096) ; }
286 file.seekg(0,ios::beg);
288 if ( itemsize == 2 ) { ok = DecodeMAT(h,file); }
289 if ( itemsize == 4 ) { ok = DecodeSPN(h,file); }
291 file.close();
return ok;
295 bool RADConverter::DecodeMAT(TH2 &h, ifstream & file)
302 int i,j;
char buffer[2*4096];
304 double swap = 0.0, noswap = 0.0;
305 for(i=0; i<4096 ; i++){
307 file.read(buffer,2*4096);
311 "Cannot read correctly the file");
315 for (j=0; j<4096; j++) {
325 file.seekg(0,ios::beg);
327 h.SetBins(4096,0,4096,4096,0,4096);
328 for(i=0; i<4096 ; i++){
329 printf(
"Reading row #%d \r",i);
330 file.read(buffer,2*4096);
331 for (j=0; j<4096; j++) h.Fill(i,j,
Short(buffer+2*j));
338 bool RADConverter::DecodeSPN(TH2 &h, ifstream & file)
345 int i,j;
char buffer[4*4096];
347 double swap = 0.0, noswap = 0.0;
348 for(i=0; i<4096 ; i++){
350 file.read(buffer,4*4096);
354 "Cannot read correctly the file");
358 for (j=0; j<4096; j++) {
368 file.seekg(0,ios::beg);
370 h.SetBins(4096,0,4096,4096,0,4096);
371 for(i=0; i<4096 ; i++){
372 printf(
"Reading row #%d \r",i);
373 file.read(buffer,4*4096);
374 for (j=0; j<4096; j++){
375 h.Fill(i,j,
Int(buffer+4*j));
388 if ( !h.InheritsFrom(
"TH2S") && !h.InheritsFrom(
"TH2I") ) {
391 "Only TH2S or TH2I can be exported as Radware matrix");
395 TString hname, filename, ext;
398 hname = h.GetName(); hname.ReplaceAll(
" ",
"");
401 if ( hname.EndsWith(
".mat") ) { hname.Remove(hname.Last(
'.'));}
402 if ( hname.EndsWith(
".m4b") ) { hname.Remove(hname.Last(
'.'));}
403 if ( hname.EndsWith(
".spn") ) { hname.Remove(hname.Last(
'.'));}
406 if ( h.InheritsFrom(
"TH2S") )
407 { filename +=
".mat"; ext =
"mat"; }
409 { filename +=
".m4b"; ext =
"m4b"; }
414 TSystemDirectory dir;
415 dir.SetDirectory(
fName.Data()); TList *list = dir.GetListOfFiles();
420 for (
int i = 0; i <=
MAXCYCLE; i++ ) {
423 if ( i == 0 ) tmp.Form(
"%s.%s",hname.Data(),ext.Data());
424 else tmp.Form(
"%s%c%d.%s",hname.Data(),
fCycle,i,ext.Data());
427 if ( !list->FindObject(tmp.Data()) ) {
break; }
430 filename.Form(
"%s.%s",hname.Data(),ext.Data());
433 filename.Form(
"%s%c%d.%s",hname.Data(),
fCycle,
MAXCYCLE,ext.Data());
435 if ( 0 < cycle && cycle <
MAXCYCLE ) {
436 if (
fMode == HistoConverter::kVersion )
437 filename.Form(
"%s%c%d.%s",hname.Data(),
fCycle,cycle,ext.Data());
441 filename.Form(
"%s.%s",hname.Data(),ext.Data());
443 filename.Form(
"%s%c%d.%s",hname.Data(),
fCycle,cycle,ext.Data());
451 filename.Prepend(
fName.Data());
453 fLog <<
error <<
"File to store the matrix "
454 << filename <<
nline;
463 file.open(filename.Data());
464 if ( ! file.is_open() ) {
466 Form(
"Cannot open file %s",filename.Data()));
471 for(
int i=0; i<4096 ; i++) {
472 for (
int j=0; j<4096; j++) {
473 if ( h.InheritsFrom(
"TH2S") ) {
475 if ( i < h.GetNbinsX() && j < h.GetNbinsY() )
476 value = (
short)h.GetBinContent(i+1,j+1);
477 file.write((
char *)&value,2);
481 if ( i < h.GetNbinsX() && j < h.GetNbinsY() )
482 value = (
int)h.GetBinContent(i+1,j+1);
483 file.write((
char *)&value,4);
487 ok = !file.fail(); file.close();
return ok;
493 TString hsname = hs.GetName();
496 hsname.ReplaceAll(
" ",
"");
501 TString reg =
"*" +
fFileExt; Bool_t wildcard =
false;
502 if ( hsname.Contains(
"*") )
504 TRegexp all(reg,kTRUE), pattern(hsname.Data(),wildcard);
506 if ( pattern.Status() != TRegexp::kOK ) {
507 if ( gDebug > 0 ) cerr <<
"pattern [*] intead of " << hsname <<
endl ;
510 else {
if ( gDebug > 0 ) cerr <<
"pattern " << hsname <<
endl ; }
513 TSystemDirectory dir; dir.SetDirectory(
fName.Data()); TList *list = dir.GetListOfFiles();
515 TIter iter(list); TObject *entry;
516 while ( (entry = iter()) ) {
518 TString
tmp = entry->GetName();
520 if ( tmp.Contains(pattern) ) {
523 fLog <<
info <<
"Reading spectrum " << entry->GetName() <<
nline;
525 TH1 *h =
Get(entry->GetName());
if ( h ) { hs.Add(h); }
528 if ( list )
delete list;
537 SetError(HistoConverter::kFail,
"NO IMPLEMENTATION !!");
void SetError(HistoConverter::EStatus status=kGood, const char *msgerror="")
to set/reset the status of this converter
printf("******************************************************************** \n")
virtual TH1 * Get(int)
to get histo number i in the list
LogMessage & error(LogMessage &)
static void AddPrototype(const char *, HistoConverter *)
to add a new type of database service
LogMessage & nline(LogMessage &)
virtual void ls(Option_t *) const
to show the spectra in this DB system on the standard output
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
virtual bool Write(const TH1 &)
to write an histogram
#define MAXCYCLE
Base class that interfaces all other histograms DB systems.
virtual bool Read(TH1 &)
to read an histogram
LogMessage & debug(LogMessage &)
virtual std::string & GetProcessName()
To get the Process name.
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual HistoConverter * NewDB(const char *) const
to creat a new DB system
virtual void CheckDirectory(const char *name)
check if the given directory can be read/written
virtual std::string & GetProcessMethod()
To get the current method.
TString CheckFileVersion(TString filename)
virtual const char * GetType() const
to know what type of HistoConverter system it is