43 fCurrentDB = 0x0; fStatus = HistoDB::kGood;
49 fCurrentDB = 0x0; fStatus = HistoDB::kGood;
51 map<const char *, HistoConverter *>::iterator im;
52 for (im = fConnections.begin() ; im != fConnections.end() ; im++ )
delete im->second;
56 void HistoDB::AddFileExt(
const char* type,
const char* ext)
58 TString dbtype(type),
tmp;
61 map<const char *, HistoConverter *>::iterator im;
63 for (im = fCurrentDB->GetMapPrototype()->begin() ; im != fCurrentDB->GetMapPrototype()->end() ; im++ ) {
66 im->second->SetFileExt(ext);
71 void HistoDB::AddOption(
const char* type, Option_t* opt,
const char* mode)
73 TString dbtype(type),
tmp;
76 map<const char *, HistoConverter *>::iterator im;
78 for (im = fCurrentDB->GetMapPrototype()->begin() ; im != fCurrentDB->GetMapPrototype()->end() ; im++ ) {
81 im->second->SetOption(opt,mode);
87 void HistoDB::SetCurrent(
const char *alias)
89 if ( fConnections.find(alias) != fConnections.end() ) { fCurrentDB = fConnections[alias]; }
98 dbtype = name; dbname = name;
99 if ( dbtype.Contains(
':') ) {
100 dbtype.Remove(dbtype.First(
':')); dbname.Remove(0,dbname.First(
':')+1);
103 cout << name <<
" is not a valid name " <<
endl;
108 map<const char *, HistoConverter *>::iterator im;
110 for (im = fCurrentDB->GetMapPrototype()->begin() ; im != fCurrentDB->GetMapPrototype()->end() ; im++ ) {
112 if ( dbtype == tmp ) {
113 newdb = im->second->
NewDB(dbname.Data());
114 newdb->
SetFileExt( im->second->GetFileExt() );
115 newdb->
SetOption( im->second->GetOption() );
120 if ( newdb == 0x0 ) {
121 cout <<
" Cannot allocate HistoDB service " << name <<
endl;
return 0x0;
124 if ( newdb->
GetStatus() == fCurrentDB->kZombie ) {
125 cout <<
" Cannot open a connection to "
128 delete newdb;
return 0x0;
133 if ( fConnections.find(alias) == fConnections.end() ) { isnew =
true; }
135 if (isnew) fConnections[alias] = newdb;
137 cout <<
" An HistoDB service already exists with the alias " << name <<
endl;
138 delete newdb; newdb = 0x0;
140 if ( newdb ) fCurrentDB = newdb;
146 void HistoDB::SetMode(
const char *alias, Option_t *mode)
150 if ( fConnections.find(alias) == fConnections.end() ) {
151 if ( opt.Contains(
"overwrite") )
152 fConnections[alias]->SetMode(HistoConverter::kOverwrite);
154 fConnections[alias]->
SetMode(HistoConverter::kVersion);
159 void HistoDB::SetMode(Option_t *mode)
164 if ( opt.Contains(
"overwrite") )
165 fCurrentDB->SetMode(HistoConverter::kOverwrite);
167 fCurrentDB->SetMode(HistoConverter::kVersion);
172 void HistoDB::ls(Option_t *o)
175 map<const char *, HistoConverter *>::iterator im;
177 if ( opt.Contains(
"proto") ) {
178 cout <<
"Known DB system to import/export ROOT histograms" <<
endl;
179 for (im = fCurrentDB->GetMapPrototype()->begin() ; im != fCurrentDB->GetMapPrototype()->end() ; im++ ) {
180 cout <<
" - " << im->first <<
endl ;
183 if ( opt.Contains(
"cdb") ) {
184 if ( fCurrentDB == 0x0 ) {
185 cout <<
"No current DB service defined !" <<
endl;
188 cout <<
"Current DB service is:" <<
endl; fCurrentDB->ls(o);
191 if ( opt.Contains(
"all") ) {
192 cout <<
"List of all DB services open:" <<
endl;
193 for (im = fConnections.begin() ; im != fConnections.end() ; im++ ) {
194 cout <<
"\t" << im->first <<
" "; im->second->ls(o); }
199 void HistoDB::SetVerboseLevel(
const int level){
201 HistoConverter::SetVerboseLevel(level);
206 int HistoDB::GetVerboseLevel(){
208 return HistoConverter::GetVerboseLevel();
213 TH1 *HistoDB::Get(
int i)
217 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
218 if ( (histo = fCurrentDB->Get(i)) == 0x0 ) {
219 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
226 TH1 *HistoDB::Get(
const char *name)
230 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
231 if ( (histo = fCurrentDB->Get(name)) == 0x0 ) {
232 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
239 bool HistoDB::Write(
const TH1 &h)
242 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
243 result = fCurrentDB->Write(h);
244 if ( result ==
false ) {
245 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
251 bool HistoDB::Write(
const TH2 &h)
254 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
255 result = fCurrentDB->Write(h);
256 if ( result ==
false ) {
257 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
266 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
267 if ( fCurrentDB->Write(h) == false ) {
268 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
278 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
279 if ( fCurrentDB->Read(h) == false ) {
280 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
289 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
290 if ( fCurrentDB->Write(h) == false ) {
291 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
300 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
301 if ( fCurrentDB->Read(h) == false ) {
302 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
311 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
312 if ( fCurrentDB->Write(hs) == false ) {
313 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
322 if ( fCurrentDB && fStatus == HistoDB::kGood ) {
323 if ( fCurrentDB->Read(hs) == false ) {
324 fStatus = HistoDB::kFail; cerr << fCurrentDB->GetError() <<
endl;
331 void HistoDB::MakeConverter(
const char *classname)
const
336 tmp = classname; tmp +=
".h"; ofstream header(tmp.Data());
337 if ( !header ) { cout <<
" Cannot open file " << tmp.Data() <<
endl;
return; }
340 tmp = classname; tmp +=
".C"; ofstream imp(tmp.Data());
341 if ( !imp ) { cout <<
" Cannot open file " << tmp.Data() <<
endl; header.close();
return; }
344 header <<
"#ifndef " << classname <<
"_H" <<
endl;
345 header <<
"#define " << classname <<
"_H 1" <<
endl;
347 header <<
"#include <HistoDB.h> " <<
endl;
349 header <<
"class "<< classname <<
": public HistoConverter " <<
endl;
350 header <<
"{ " <<
endl;
351 header <<
"public:" <<
endl;
352 header <<
" "<< classname <<
"();" <<
endl;
353 header <<
" "<< classname <<
"(const char *name);" <<
endl;
354 header <<
" virtual ~"<< classname <<
"();" <<
endl;
356 header <<
" virtual TH1 *Get(int); " <<
endl;
357 header <<
" virtual TH1 *Get(const char *); " <<
endl;
359 header <<
" virtual bool Read(TH1 &);" <<
endl;
360 header <<
" virtual bool Write(const TH1 &);" <<
endl;
362 header <<
" virtual bool Read(TH2 &);" <<
endl;
363 header <<
" virtual bool Write(const TH2 &);" <<
endl;
365 header <<
" virtual bool Read(THStack &);" <<
endl;
366 header <<
" virtual bool Write(const THStack &);" <<
endl;
368 header <<
" virtual HistoConverter *NewDB(const char *) const;" <<
endl;
370 header <<
" virtual const char *GetType() const { return \""
371 << classname <<
"\" ; }" <<
endl;
372 header <<
"};" <<
endl;
373 header <<
"#endif " <<
endl;
376 imp <<
"#include \"" << classname <<
".h\"" <<
endl;
378 imp <<
"namespace { " << classname <<
" g" << classname
379 <<
"; } // prototype ONLY use to creat a new DB system" <<
endl;
381 imp << classname <<
"::" << classname <<
"(const char *name) : HistoConverter(name)" <<
endl;
386 imp << classname <<
"::" << classname <<
"() : HistoConverter()" <<
endl;
389 imp <<
" // add this interface to the DB service " <<
endl;
390 imp <<
" AddPrototype(GetType(),this);" <<
endl;
392 imp << classname <<
"::~" << classname <<
"()" <<
endl;
397 imp <<
"HistoConverter *" << classname
398 <<
"::NewDB(const char *name) const { return new "<< classname <<
"(name) ;}" <<
endl;
400 imp <<
"TH1 *" << classname <<
"::Get(int which)" <<
endl;
402 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Get(int which)"
403 <<
"] NO IMPLEMENTATION !!\") ; return 0x0;" <<
endl;
406 imp <<
"TH1 *" << classname <<
"::Get(const char *name)" <<
endl;
408 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Get(const char *name)"
409 <<
"] NO IMPLEMENTATION !!\") ; return 0x0;" <<
endl;
412 imp <<
"bool " << classname <<
"::Read(TH1 &h)" <<
endl;
414 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Read(TH1 &h)"
415 <<
"] NO IMPLEMENTATION !!\") ; return false;" <<
endl;
418 imp <<
"bool " << classname <<
"::Write(const TH1 &h)" <<
endl;
420 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Write(const TH1 &h)"
421 <<
"] NO IMPLEMENTATION !!\") ; return false;" <<
endl;
424 imp <<
"bool " << classname <<
"::Read(TH2 &h)" <<
endl;
426 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Read(TH2 &h)"
427 <<
"] NO IMPLEMENTATION !!\") ; return false;" <<
endl;
430 imp <<
"bool " << classname <<
"::Write(const TH2 &h)" <<
endl;
432 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Write(const TH2 &h)"
433 <<
"] NO IMPLEMENTATION !!\") ; return false;" <<
endl;
436 imp <<
"bool " << classname <<
"::Read(THStack &hs)" <<
endl;
438 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Read(THStack &hs)"
439 <<
"] NO IMPLEMENTATION !!\") ; return false;" <<
endl;
442 imp <<
"bool " << classname <<
"::Write(const THStack &hs)" <<
endl;
444 imp <<
" SetError(HistoConverter::kFail,\"[" << classname <<
"::Write(const THStack &hs)"
445 <<
"] NO IMPLEMENTATION !!\") ; return false;" <<
endl;
448 header.close(); imp.close();
std::istream & operator>>(std::istream &is, ADF::FactoryItem &item)
void SetFileExt(TString ext)
to set/get file extension of the spectra/histo
void SetOption(Option_t *opt, const char *mode="reset")
set options
header file for the HistoDB facility
void SetMode(HistoConverter::EMode mode)
std::ostream & operator<<(std::ostream &out, const Parity &p)
to write a parity in a stream
ADF::LogMessage & endl(ADF::LogMessage &log)
Service to read/write ROOT histograms from/to different DB systems.
const char * GetError() const
virtual HistoConverter * NewDB(const char *) const
to creat a new DB system
void CheckMineTypes()
check mine types