24 #ifndef GW_ASCCONVERTER_H
36 #ifndef ROOT_TSystemDirectory
37 #include <TSystemDirectory.h>
40 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
44 #ifndef ROOT_TObjArray
45 #include "TObjArray.h"
48 #ifndef ROOT_TObjString
49 #include "TObjString.h"
64 fStreamIN.clear();
fCycle =
'_';
97 HistoConverter::ls(o);
98 TSystemDirectory dir; dir.SetDirectory(
fName.Data());
99 TList *list = dir.GetListOfFiles();
104 while ( (entry = iter()) ) {
105 TString
tmp = entry->GetName();
107 cout <<
" [" << count++ <<
"] " << entry->GetName() ;
108 if ( (count+1) % 5 == 0 ) cout <<
endl;
113 if ( list )
delete list;
120 TSystemDirectory dir; dir.SetDirectory(
fName.Data()); TList *list = dir.GetListOfFiles();
121 TIter iter(list); TObject *entry;
int count = 0; TString result =
"_EMPTY_";
123 while ( (entry = iter()) ) {
124 TString
tmp = entry->GetName();
126 if ( which == count )
127 result = entry->GetName();
131 if ( list )
delete list;
133 if ( result !=
"_EMPTY_" )
return Get(result.Data());
136 Form(
"Cannot find entry # %d",which));
144 TH1F *h =
new TH1F();
157 TString filename =
fName.Data();
158 if ( filename.IsNull() ) filename =
".";
160 if ( ! filename.EndsWith(
"/") ) filename+=
"/";
161 filename += h.GetName();
166 ifstream in(filename, ios::in);
171 Form(
"Cannot open file %s",filename.Data()));
177 if ( !DecodeFormat(params) ) {
182 Int_t nbLines = params[0];
183 Int_t nbColumns = params[1];
184 Char_t
sep = (char)params[2];
185 Int_t nbParams = params[3];
186 Int_t posBin = params[4];
187 Int_t posBinContent = params[5];
189 vector<Int_t>
histo; histo.clear();
190 vector<Int_t> bin; bin.clear();
195 for (Int_t i = 0; i < nbLines; ++i)
196 in.getline(line,255);
204 while ( in.getline(line,255) ) {
207 TObjArray* stringList = tmp.Tokenize(TString(sep));
210 for (Int_t i = 0; i < nbColumns; ++i) {
212 idxBin = (posBin-1) + nbParams*i;
213 sBin = ((TObjString*)stringList->At(idxBin))->GetString();
214 bin.push_back(sBin.Atoi());
217 idxContent = (posBinContent-1) + nbParams*i;
218 sCnt = ((TObjString*)stringList->At(idxContent))->GetString();
219 histo.push_back(sCnt.Atoi());
226 h.SetBins(histo.size(),0,histo.size());
227 for (UInt_t i = 0; i < histo.size(); i++ )
229 h.SetBinContent(bin[i],histo[i]);
231 h.SetBinContent(i+1,histo[i]);
241 TString hname, filename;
245 filename.Prepend(
fName.Data());
248 SetError(HistoConverter::kFail, Form(
"file to store the spectrum %s", filename.Data()) );
254 ofstream out(filename.Data(), ios::out);
259 Form(
"Cannot open %s in writing mode",filename.Data()));
266 if ( !DecodeFormat(params) ) {
271 Int_t nbLines = params[0];
272 Int_t nbColumns = params[1];
273 Char_t
sep = (char)params[2];
274 Int_t nbParams = params[3];
275 Int_t posBin = params[4];
276 Int_t posBinContent = params[5];
278 Int_t idxContent = 0;
282 for (Int_t i = 0; i < nbLines; ++i)
283 out <<
"Header generated by [ASCConverter::Write(TH1 &h)]" <<
endl;
285 Int_t
size = h.GetNbinsX()/nbColumns;
287 for (Int_t j = 0; j <
size; ++j) {
289 for (Int_t i = 0; i < nbColumns; ++i) {
291 idxBin = (posBin-1) + nbParams*i;
292 idxContent = (posBinContent-1) + nbParams*i;
294 out << h.GetBin(bin);
295 else if (i == idxContent) {
296 out << h.GetBinContent(bin++);
312 SetError(HistoConverter::kFail,
"NO IMPLEMENTATION !!") ;
320 SetError(HistoConverter::kFail,
"NO IMPLEMENTATION !!");
328 SetError(HistoConverter::kFail,
"NO IMPLEMENTATION !!");
336 SetError(HistoConverter::kFail,
"NO IMPLEMENTATION !!");
342 Bool_t ASCConverter::DecodeFormat(TArrayI& params)
355 Int_t posBinContent = 0;
359 Int_t length = option.Length();
360 while (last < length) {
361 last = option.Index(type, idx);
362 if (last == -1)
break;
363 idx = last + type.Length();
365 TString format(option(idx, length -idx));
367 TObjArray* stringList = format.Tokenize(TString(
" "));
368 if (stringList->GetEntries() > 4) {
370 SetError(HistoConverter::kFail,
"Too many blanks in the format option !!");
374 for (Int_t i = 0; i< stringList->GetEntries(); ++i) {
375 keys = ((TObjString*)stringList->At(i))->GetString();
376 const Char_t* k = keys.Data();
380 sscanf(k,
"h:%d", &nbLines);
384 sscanf(k,
"c:%d", &nbColumns);
385 params[1] = nbColumns;
388 sscanf(k,
"s:%c", &sep);
389 params[2] = (Int_t)sep;
392 sscanf(k,
"p:%d.%d.%d", &nbParams, &posBin, &posBinContent);
393 params[3] = nbParams; params[4] = posBin; params[5] = posBinContent;
411 if ( rv.Length() <= 0 )
return TString();
413 while ( rv[0] ==
' ' )
417 while ( rv[rv.Length()-1] ==
' ' )
419 rv.Remove(rv.Length()-1,1);
423 for ( i = 0; i < rv.Length(); ++i )
void SetError(HistoConverter::EStatus status=kGood, const char *msgerror="")
to set/reset the status of this converter
virtual bool Read(TH1 &)
Read/write 1-D histogram.
virtual HistoConverter * NewDB(const char *) const
instanciate new HistoConverter
static void AddPrototype(const char *, HistoConverter *)
to add a new type of database service
LogMessage & nline(LogMessage &)
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
static TString Normalize(const char *line)
method to normalize string chains
virtual const char * GetType() const
get type
virtual void ls(Option_t *) const
to show the spectra in this DB system on the standard output
LogMessage & sep(LogMessage &)
virtual bool Write(const TH1 &)
to write an histogram
virtual std::string & GetProcessName()
To get the Process name.
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual TH1 * Get(int)
Get histogram from number Id.
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)