GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HistoConverter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004-2006 by Olivier Stezowski & Christophe Theisen *
3  * stezow(AT)ipnl.in2p3.fr, christophe.theisen(AT)cea.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef GW_HISTOCONVERTER_H
24 #include "HistoConverter.h"
25 #endif
26 
27 #ifndef GW_ENV_H
28 #include "Env.h"
29 #endif
30 
31 #ifndef ROOT_TSystem
32 #include <TSystem.h>
33 #endif
34 
35 #ifndef ROOT_TROOT
36 #include <TROOT.h>
37 #endif
38 
39 #ifndef ROOT_TSystemDirectory
40 #include <TSystemDirectory.h>
41 #endif
42 
43 #ifndef ROOT_TGMimeTypes
44 #include <TGMimeTypes.h>
45 #endif
46 
47 #include <iostream>
48 
49 using namespace std;
50 using namespace Gw;
51 
52 
53 // ROOT dictionnary
55 
56 
57 namespace { HistoConverter gHistoConverter; } // prototype ONLY use to create a new DB system
58 
59 Int_t HistoConverter::fVerboseLevel = 1;
60 Int_t HistoConverter::gAllocated = 0;
61 
62 // static map that knows all the DB system
63 map<const char *, HistoConverter *>* HistoConverter::gPrototypes;
64 
65 //__________________________________________________________
66 HistoConverter::HistoConverter()
67 : fMode(HistoConverter::kVersion),
68  fName(""),
69  fStatus(HistoConverter::kGood),
70  fError(""),
71  fCycle(';'),
72  fType(""),
73  fFileExt(""),
74  fIsSwapping(false),
75  fOptions(""),
76  fLog("HistoConverter")
77 {
78  if (gPrototypes == 0)
79  gPrototypes = new map<const char *, HistoConverter *>;
80  AddPrototype(GetType(),this); // add this to the list of known prototypes
81  gAllocated++;
82 }
83 
84 //__________________________________________________________
86 : fMode(HistoConverter::kVersion),
87  fName(name),
88  fStatus(HistoConverter::kGood),
89  fError(""),
90  fCycle(';'),
91  fType(""),
92  fFileExt(""),
93  fIsSwapping(false),
94  fOptions(""),
95  fLog("HistoConverter")
96 {
97 
98 }
99 
100 //__________________________________________________________
102 {
103 
104  if ( gAllocated-- == 1 ) {
105  clog << "HistoConverter::~HistoConverter()\n[...\n" ;
106  clog << " Destroying the global prototype map ... " << endl;
107  delete gPrototypes;
108  clog << " done\n...]" << endl;
109  }
110 
111 }
112 
113 
114 //__________________________________________________________
115 void HistoConverter::SetOption(Option_t* opt, const char* mode)
116 {
117  TString inc(opt);
118 
119  if (!inc.EndsWith("]")) {
120  inc.Append("]");
121  inc.Prepend("[");
122  }
123  if ( strncmp(mode, "append", sizeof(opt)) == 0 )
124  fOptions += inc;
125  else if ( strncmp(mode, "reset", sizeof(opt)) == 0 )
126  fOptions = inc;
127  else {
128  fLog.GetProcessMethod() = "SetOption(Option_t*, const char* )";
129  SetError(HistoConverter::kFail, Form("Unknown %s mode ", mode));
130  }
131 }
132 
133 
134 //__________________________________________________________
135 void HistoConverter::SetError(HistoConverter::EStatus status, const char* msgerror)
136 {
137  fStatus = status;
138  fName = Form("[%s::%s] ", fLog.GetProcessName().c_str(), fLog.GetProcessMethod().c_str());
139  fName += msgerror;
140  fLog << error << msgerror << dolog;
141 }
142 
143 //__________________________________________________________
144 void HistoConverter::AddPrototype(const char *type, HistoConverter *proto)
145 {
146 
147  map<const char *, HistoConverter *>::iterator im; // iterator on the map prototype
148 
149  bool isnew_type = true;
150  for (im = gPrototypes->begin() ; im != gPrototypes->end() ; im++ ) { // look if it already exists
151  if ( im->first == type ) { isnew_type = false; break; }
152  }
153 
154  if (isnew_type) gPrototypes->insert(pair<const char *, HistoConverter *>(type, proto)); // gPrototypes[type] = proto if not add it to the map
155 }
156 
157 //__________________________________________________________
159 {
160  Char_t type[100];
161  TGMimeTypes* mineTypes = gClient->GetMimeTypeList();
162 
163  TString ext(fFileExt);
164  ext.Prepend("*");
165  Bool_t ok = mineTypes->GetType(ext.Data(), type);
166 
167  if (!ok)
168  AddMineTypes();
169 }
170 
171 //__________________________________________________________
173 {
174  TString application("[application/");
175  application += fType;
176  application.Append("]");
177 
178  TString ext(fFileExt);
179  ext.Prepend("*");
180 
181  TString icon(Env::GetPath("IconsPath"));
182  if (!icon.EndsWith("/")) icon.Append("/");
183  icon.Append("hdb_s.xpm");
184 
185 
186  TString command;
187  command = "Gw::HistoConverter::DrawSpectrum(\"%s\")";
188 
189  TGMimeTypes* mineTypes = gClient->GetMimeTypeList();
190 
191  mineTypes->AddType(application.Data(), ext.Data(), icon.Data(), icon.Data(), command.Data());
192 
193 }
194 
195 //__________________________________________________________
196 void HistoConverter::DrawSpectrum(const char* spectrum)
197 {
198  TString hname(spectrum);
199 
200  TH1* h;
201 
202  TString ext(spectrum);
203  ext = ext(ext.Last('.'), ext.Length()-ext.Last('.'));
204 
205  map<const char *, HistoConverter *>::iterator im; // iterator on the map prototype
206 
207  for (im = gPrototypes->begin(); im != gPrototypes->end(); ++im ) { // look if it already exists
208  if ( strncmp( im->second->GetFileExt(), ext.Data(), sizeof(im->second->GetFileExt())) == 0 ) {
209  if ( strcmp(im->first,"gpsi") == 0 ) { // special traitment for gpsi
210  im->second->CheckDirectory(hname);
211  im->second->ls("all");
212  break;
213  }
214  h = im->second->Get(hname);
215  h->SetDirectory(gROOT); // default wise
216  h->Draw();
217  }
218  }
219 }
220 
221 
222 //__________________________________________________________
223 TString HistoConverter::CheckFileVersion(TString hname)
224 {
225 
226  TString filename; // set file name as a full path name
227 
228  // check out the histogram name to avoid blank in a filename
229  hname.ReplaceAll(" ",""); // to avoid blank in a filename
230  if ( hname.EndsWith(fFileExt) )
231  hname.Remove(hname.Last('.'));
232 
233  filename = hname; filename += fFileExt; // local file name
234 
235  // determine the name of the file taking into account the versionning system
236  TSystemDirectory dir;
237  dir.SetDirectory(fName.Data()); TList *list = dir.GetListOfFiles();
238 
239  if ( list ) {
240  // look for the next cycle
241  int cycle;
242  for ( int i = 0; i <= MAXCYCLE; i++ ) { // up to MAXCYCLE files
243 
244  TString tmp;
245  if ( i == 0 ) tmp.Form("%s%s",hname.Data(), fFileExt.Data());
246  else tmp.Form("%s%c%d%s",hname.Data(),fCycle,i, fFileExt.Data());
247 
248  cycle = i;
249  if ( !list->FindObject(tmp.Data()) ) { break; }
250  }
251  if ( cycle == 0) { // the file doesn't exist yet
252  filename.Form("%s%s",hname.Data(), fFileExt.Data());
253  }
254  if ( cycle == MAXCYCLE ) { // force overwriting even if in versionning mode
255  filename.Form("%s%c%d%s",hname.Data(),fCycle,MAXCYCLE, fFileExt.Data());
256  }
257  if ( 0 < cycle && cycle < MAXCYCLE ) {
258  if ( fMode == HistoConverter::kVersion ) // ready for a new file
259  filename.Form("%s%c%d%s",hname.Data(),fCycle,cycle, fFileExt.Data());
260  else { // comes back to the previous one
261  cycle--;
262  if ( cycle == 0 )
263  filename.Form("%s%s",hname.Data(), fFileExt.Data());
264  else
265  filename.Form("%s%c%d%s",hname.Data(),fCycle,cycle, fFileExt.Data());
266  }
267  }
268  if ( gDebug > 0 ) {
269  fLog.GetProcessMethod() = "CheckFileVersion(TString )";
270  cerr << "Cycle is " << cycle << endl;
271  }
272  delete list;
273  }
274 
275  return TString(filename);
276 
277 }
278 
279 //__________________________________________________________
280 void HistoConverter::SetVerboseLevel(const int level){
281 
282  if ((level != 0) && (level != 1))
283  cout << "verbose level must be 0 or 1 " << endl;
284  else
286 
287 }
288 
289 //__________________________________________________________
291 
293 
294 }
295 
296 //__________________________________________________________
297 short HistoConverter::Short(char *buf)
298 {
299  if ( fIsSwapping ) {
300  char c;
301  c = buf[1]; buf[1] = buf[0]; buf[0] = c;
302  }
303  short *s = (short*)buf; return s[0];
304 }
305 
306 //__________________________________________________________
307 unsigned short HistoConverter::UShort(char *buf)
308 {
309  if ( fIsSwapping ) {
310  char c;
311  c = buf[1]; buf[1] = buf[0]; buf[0] = c;
312  }
313  short *s = (short*)buf; return s[0];
314 }
315 
316 //__________________________________________________________
317 long HistoConverter::Long(char *buf)
318 {
319  if ( fIsSwapping ) {
320  char c;
321  c = buf[3]; buf[3] = buf[0]; buf[0] = c;
322  c = buf[2]; buf[2] = buf[1]; buf[1] = c;
323  }
324  long *s = (long*)buf; return s[0];
325 }
326 
327 //__________________________________________________________
328 unsigned long HistoConverter::ULong(char *buf)
329 {
330  if ( fIsSwapping ) {
331  char c;
332  c = buf[3]; buf[3] = buf[0]; buf[0] = c;
333  c = buf[2]; buf[2] = buf[1]; buf[1] = c;
334  }
335  unsigned long *s = (unsigned long*)buf; return s[0];
336 }
337 
338 //__________________________________________________________
339 int HistoConverter::Int(char *buf)
340 {
341  if ( fIsSwapping ) {
342  char c;
343  c = buf[3]; buf[3] = buf[0]; buf[0] = c;
344  c = buf[2]; buf[2] = buf[1]; buf[1] = c;
345  }
346  int *s = (int*)buf; return s[0];
347 }
348 
349 //__________________________________________________________
350 unsigned int HistoConverter::UInt(char *buf)
351 {
352  if ( fIsSwapping ) {
353  char c;
354  c = buf[3]; buf[3] = buf[0]; buf[0] = c;
355  c = buf[2]; buf[2] = buf[1]; buf[1] = c;
356  }
357  unsigned int *s = (unsigned int*)buf; return s[0];
358 }
359 
360 //__________________________________________________________
361 float HistoConverter::Float(char *buf)
362 {
363  if ( fIsSwapping ) {
364  char c;
365  c = buf[3]; buf[3] = buf[0]; buf[0] = c;
366  c = buf[2]; buf[2] = buf[1]; buf[1] = c;
367  }
368  float *s = (float*)buf; return s[0];
369 }
370 
371 //__________________________________________________________
372 void HistoConverter::CheckDirectory(const char *name)
373 {
374  TString tmp, path;
375 
376  // Expand to set environment variables like $(HOME), and change the full path to
377  // the correct path depending on the operating system
378  tmp = name;
379  if ( tmp.Length() > 0 && tmp.EndsWith("/") == kFALSE ) tmp += "/";
380  path = gSystem->UnixPathName(tmp.Data()); gSystem->ExpandPathName(path);
381 
382  void *dir = gSystem->OpenDirectory(path.Data());
383  if ( dir == 0 ) {
384  fLog.GetProcessMethod() = "CheckDirectory(const char* )";
386  Form("the directory %s is not reachabled !",name));
387  } else {
388  gSystem->FreeDirectory(dir);
389  fName = path.Data();
390  }
391 }
392 
393 //__________________________________________________________
394 void HistoConverter::ls(Option_t *) const
395 {
396  cout << GetType() << ":" << fName << endl;
397 }
398 
399 //__________________________________________________________
400 HistoConverter *HistoConverter::NewDB(const char *name) const
401 {
402  return new HistoConverter(name);
403 }
404 
405 //__________________________________________________________
407 {
408  fLog.GetProcessMethod() = "Read(const TH1& )";
409  fLog << warning << Form("Reading ROOT histogram %s in a black hole ! \n",h.GetName()) << dolog;
410  h.Reset();
411  return true;
412 }
413 
414 //__________________________________________________________
415 bool HistoConverter::Write(const TH1 &h)
416 {
417  fLog.GetProcessMethod() = "Write(const TH1& )";
418  fLog << warning << Form("Writing ROOT histogram %s in a black hole ! \n",h.GetName()) << dolog;
419  return true;
420 }
421 
422 //__________________________________________________________
423 TH1* HistoConverter::Get(int /*i*/)
424 {
425  fLog.GetProcessMethod() = "Get(Int_t )";
426  fLog << error << "Not implemented" << dolog;
427  return NULL;
428 }
429 
430 //__________________________________________________________
431 TH1* HistoConverter::Get(const char* /*name*/)
432 {
433  fLog.GetProcessMethod() = "Get(const char* )";
434  fLog << error << "Not implemented" << dolog;
435  return NULL;
436 }
437 
438 
void SetError(HistoConverter::EStatus status=kGood, const char *msgerror="")
to set/reset the status of this converter
LogMessage & error(LogMessage &)
EStatus
current status of the converter
static void AddPrototype(const char *, HistoConverter *)
to add a new type of database service
LogMessage & warning(LogMessage &)
static void DrawSpectrum(const char *spectrum)
draw spectrum for mine types
float Float(char *buf)
void SetOption(Option_t *opt, const char *mode="reset")
set options
ClassImp(HistoConverter)
short Short(char *buf)
virtual const char * GetType() const
to know what type of HistoConverter system it is
header file for the HistoDB facility
long Long(char *buf)
virtual bool Read(TH1 &)
to read an histogram
virtual bool Write(const TH1 &)
to write an histogram
unsigned short UShort(char *buf)
#define MAXCYCLE
Base class that interfaces all other histograms DB systems.
LogMessage & dolog(LogMessage &)
virtual std::string & GetProcessName()
To get the Process name.
Definition: GwLogMessage.h:224
static void SetVerboseLevel(const int)
ADF::LogMessage & endl(ADF::LogMessage &log)
to get information about the gammaware configuration
virtual TH1 * Get(int)
to get histo number i in the list
static int fVerboseLevel
unsigned int UInt(char *buf)
virtual void ls(Option_t *) const
to show the spectra in this DB system on the standard output
virtual void CheckDirectory(const char *name)
check if the given directory can be read/written
virtual HistoConverter * NewDB(const char *) const
to creat a new DB system
virtual std::string & GetProcessMethod()
To get the current method.
Definition: GwLogMessage.h:227
int Int(char *buf)
static const char * GetPath(const char *)
to get a particular GW environment variable
Definition: Env.cpp:47
static int GetVerboseLevel()
TString CheckFileVersion(TString filename)
unsigned long ULong(char *buf)
void CheckMineTypes()
check mine types