21#include "TDirectory.h"
34#define GO4_EXMAN_CREATE_HISTOGRAM(X,Y) \
37 theHisto = new TH1##Y(name.Data(), title.Data(), bins[0], axmin[0], axmax[0]);\
40 theHisto = new TH2##Y(name.Data(), title.Data(), bins[0], axmin[0], axmax[0], bins[1], axmin[1], axmax[1]);\
43 theHisto = new TH3##Y(name.Data(), title.Data(), bins[0], axmin[0], axmax[0], bins[1], axmin[1], axmax[1], bins[2], axmin[2], axmax[2]);\
97 if (myobject->InheritsFrom(TDirectory::Class())) {
98 TDirectory *subdir =
dynamic_cast<TDirectory *
>(myobject);
100 }
else if (myobject->InheritsFrom(TFolder::Class())) {
101 TFolder *subfold =
dynamic_cast<TFolder *
>(myobject);
103 }
else if (myobject->InheritsFrom(TCollection::Class())) {
104 TCollection *col =
dynamic_cast<TCollection *
>(myobject);
106 }
else if (myobject->InheritsFrom(TH1::Class())) {
107 TH1 *histo =
dynamic_cast<TH1 *
>(myobject);
109 }
else if (myobject->InheritsFrom(TGraph::Class())) {
110 TGraph *graph =
dynamic_cast<TGraph *
>(myobject);
125 "ExportManager: NOT Converting object %s of class %s",
126 myobject->GetName(), myobject->ClassName());
145 TString dirname = fold->GetName();
147 TString com =
"mkdir " + dirname;
149 gSystem->cd(dirname.Data());
151 TCollection *folderlist = fold->GetListOfFolders();
163 TGo4Log::Message(0,
"ExportManager: Converting contents of directory %s", source->GetName());
171 TString dirname = source->GetName();
172 if (!dirname.Contains(
".root")) {
174 TString com =
"mkdir " + dirname;
176 gSystem->cd(dirname.Data());
181 TIter iter(source->GetListOfKeys());
182 while (
auto mykey = (TKey *)iter()) {
183 auto myobject = mykey->ReadObj();
187 TGo4Log::Message(2,
"ExportManager: Could not read key %s", mykey->GetName());
190 if (!dirname.Contains(
".root")) {
201 TGo4Log::Message(0,
"ExportManager: Converting contents of collection %s",col->GetName());
210 while(
auto ob = iter())
266 TString objectname=histo->GetName();
267 TString outname=objectname;
268 outname.Append(
".hdat");
271 std::ofstream outfile(outname.Data());
273 TGo4Log::Message(3,
"ExportManager: Error opening outputfile %s",outname.Data());
276 TGo4Log::Message(0,
"ExportManager: Converting histogram %s to ASCII",histo->GetName());
277 Int_t maxbinX = histo->GetNbinsX();
278 Int_t maxbinY = histo->GetNbinsY();
279 Int_t maxbinZ = histo->GetNbinsZ();
282 outfile <<
"# Histogram "<<histo->ClassName() <<
": "<<histo->GetName()<< std::endl;
284 outfile <<
"# Xbin \tYbin \tZbin \tContent"<< std::endl;
286 outfile <<
"# X \tY \tZ \tContent"<< std::endl;
287 for(Int_t x=1; x<=maxbinX; ++x)
289 for(Int_t y=1; y<=maxbinY; ++y)
291 for(Int_t z=1; z<=maxbinZ; ++z)
293 globalbin=histo->GetBin(x,y,z);
294 cont=histo->GetBinContent(globalbin);
297 outfile <<x<<
" \t"<<y<<
" \t"<<z<<
" \t"<<cont<< std::endl;
302 TAxis *xax = histo->GetXaxis();
304 if(xax) xval=xax->GetBinLowEdge(x);
306 TAxis *yax = histo->GetYaxis();
308 if(yax) yval = yax->GetBinLowEdge(y);
310 TAxis *zax = histo->GetZaxis();
312 if(zax) zval = zax->GetBinLowEdge(z);
313 outfile <<xval<<
" \t"<<yval<<
" \t"<<zval<<
" \t"<<cont<< std::endl;
322catch(std::exception& ex)
324 TGo4Log::Message(3,
"standard exception %s in TGo4ExportManager::ExportASCII(TH1 *)",
329 TGo4Log::Message(3,
"!!! Unexpected exception in TGo4ExportManager::ExportASCII(TH1 *)!!!");
339 TString objectname = graph->GetName();
340 TString outname = objectname;
341 outname.Append(
".gdat");
343 std::ofstream outfile(outname.Data());
345 TGo4Log::Message(3,
"ExportManager: Error opening outputfile %s", outname.Data());
348 TGo4Log::Message(0,
"ExportManager: Converting graph %s to ASCII", graph->GetName());
349 Int_t maxpoints = graph->GetN();
350 outfile <<
"# Graph " << graph->ClassName() <<
": " << graph->GetName() << std::endl;
351 outfile <<
"# Point \tX \tY" << std::endl;
352 for (Int_t point = 0; point < maxpoints; ++point) {
353 Double_t xg = 0, yg = 0;
354 graph->GetPoint(point, xg, yg);
355 outfile << point <<
" \t\t" << xg <<
" \t" << yg << std::endl;
361 catch (std::exception &ex)
363 TGo4Log::Message(3,
"standard exception %s in TGo4ExportManager::ExportASCII(TGraph *)", ex.what());
365 TGo4Log::Message(3,
"!!! Unexpected exception in TGo4ExportManager::ExportASCII(TGraph *)!!!");
371 if (histo->InheritsFrom(TH2::Class())) {
372 TH2 *map =
dynamic_cast<TH2 *
>(histo);
374 }
else if (histo->InheritsFrom(TH3::Class())) {
375 TGo4Log::Message(2,
"ExportManager: Converting 3d histogram %s to radware not supported yet!", histo->GetName());
379 TString objectname = histo->GetName();
380 TString outname = objectname;
381 outname.Append(
".spe");
382 TString hname = objectname;
384 Int_t maxbinX = histo->GetNbinsX();
385 Int_t l_chan = maxbinX;
392 l_head[5] = l_chan * 4;
394 std::ofstream outfile(outname.Data());
396 TGo4Log::Message(3,
"ExportManager: Error opening outputfile %s", outname.Data());
399 TGo4Log::Message(0,
"ExportManager: Converting 1d histogram %s to RADWARE", histo->GetName());
402 outfile.write((
char *)&first,
sizeof(Int_t));
404 outfile.write(hname.Data(), 8);
406 outfile.write((
char *)l_head, 6 *
sizeof(Int_t));
409 for (Int_t xbin = 0; xbin < maxbinX; ++xbin) {
410 cont = (Float_t)histo->GetBinContent(xbin);
411 outfile.write((
char *)&cont,
sizeof(Float_t));
414 Int_t charnum = l_chan *
sizeof(Float_t);
415 outfile.write((
char *)&charnum,
sizeof(Int_t));
416 Int_t hislen = l_chan * 4 + 40;
417 Int_t byteswritten = outfile.tellp();
419 if (byteswritten == hislen) {
420 TGo4Log::Message(1,
"Histogram %s: %d bytes (data %d) written to %s", hname.Data(), byteswritten, hislen,
423 TGo4Log::Message(3,
"Histogram %s: Size mismatch: %d bytes written to %s, datalength is %d", hname.Data(),
424 byteswritten, outname.Data(), hislen);
430 catch (std::exception &ex)
432 TGo4Log::Message(3,
"standard exception %s in TGo4ExportManager::ExportRadware(TH1 *)", ex.what());
434 TGo4Log::Message(3,
"!!! Unexpected exception in TGo4ExportManager::ExportRadware(TH1 *)!!!");
442 TGo4Log::Message(2,
"ExportManager: Converting 2d histo %s to radware not supported yet!",histo->GetName());
446 TGo4Log::Message(2,
"ExportManager: Converting graph %s to radware not supported yet!",graph->GetName());
452 TString fname=ob->GetName();
455 TFile *f = TFile::Open(fname.Data(),
"recreate");
460 TGo4Log::Message(0,
"ExportManager: Wrote object %s of class %s to root file %s",
461 ob->GetName(), ob->ClassName(),fname.Data() );
470 if(!fname.Contains(
".root")) fname.Append(
".root");
471 TFile *f = TFile::Open(fname.Data(),
"recreate");
473 f->SetTitle(ftitle.Data());
477 TGo4Log::Message(0,
"ExportManager: Wrote object %s of class %s to root file %s",
478 ob->GetName(), ob->ClassName(),fname.Data() );
499 "ExportManager: Can not import histogram from file %s with unknown import type %d ",
510 if (!path.Contains(
".Spe")) {
514 in.open(path.Data());
517 "ExportManager: could not open file %s for Ortec MCA histogram import",
523 Int_t numbins = 1024;
529 std::string date =
"unknown date";
530 std::string desc =
"no id";
531 Int_t headerlines = 12;
532 for (Int_t i = 0; i < headerlines; ++i) {
535 if (header.find(
"$DATA:") != std::string::npos) {
538 numbins = xmax - xmin;
542 }
else if (header.find(
"$DATE_MEA:") != std::string::npos) {
546 }
else if (header.find(
"$SPEC_ID:") != std::string::npos) {
556 Ssiz_t lastslash = name.Last(
'/');
557 name = name(lastslash + 1, name.Length());
558 Ssiz_t lastdot = name.Last(
'.');
559 name = name(0, lastdot);
561 TString title = TString::Format(
"%s of MCA from %s, %s", name.Data(),
562 date.c_str(), desc.c_str());
563 TH1D *theHisto =
new TH1D(name.Data(), title.Data(), numbins, xmin, xmax);
565 for (Int_t b = 0; b < numbins; ++b) {
570 theHisto->SetBinContent(b + 1, value);
573 theHisto->ResetStats();
578 Int_t footerlines = 14;
579 for (Int_t i = 0; i < footerlines; ++i) {
591 TH1 *theHisto =
nullptr;
593 if (!path.Contains(
".hdat")) {
594 path.Append(
".hdat");
597 in.open(path.Data());
600 "ExportManager: could not open file %s for go4 ASCII histogram import",
606 Ssiz_t lastslash = name.Last(
'/');
607 name = name(lastslash + 1, name.Length());
608 Ssiz_t lastdot = name.Last(
'.');
609 name = name(0, lastdot);
610 TString title = TString::Format(
"%s imported from ASCII %s.hdat",
611 name.Data(), name.Data());
614 std::string header, dummy;
622 std::size_t pos1=header.find(
"TH1");
623 std::size_t pos2=header.find(
"TH2");
624 std::size_t pos3=header.find(
"TH3");
627 if (pos1 != std::string::npos) {
629 type=header.at(pos1+3);
630 }
else if (pos2 != std::string::npos) {
632 type=header.at(pos2+3);
633 }
else if (pos3!= std::string::npos) {
635 type=header.at(pos3+3);
645 Double_t axval[3]={0.};
646 Double_t axmin[3]={0.};
647 Double_t axmax[3]={0.};
649 Bool_t firstline=kTRUE;
652 in >> axval[0]>> axval[1]>> axval[2] >> val;
653 for (Int_t i = 0; i < 3; ++i) {
656 axmin[i] = axmax[i] = axval[i];
658 if (axval[i] <= axmin[i]) {
661 if (axval[i] > axmax[i]) {
671 for (Int_t i = 0; i < 3; ++i) {
672 if(bins[i]==0)
continue;
674 Double_t binsize = (axmax[i] - axmin[i]) / (bins[i]);
714 in >> axval[0]>> axval[1] >> axval[2] >> val;
715 Int_t globalbin=theHisto->FindBin(axval[0], axval[1], axval[2]);
717 theHisto->SetBinContent(globalbin, val);
720 theHisto->ResetStats();
#define GO4_EXMAN_CREATE_HISTOGRAM(X, Y)
TString fxOutFileComment
Comment (title) of the output file.
TH1 * ImportHistogramGo4Ascii(const char *nom)
Provide reading back ascii histograms exported by another go4.
virtual ~TGo4ExportManager()
void SetFilter(Go4Export_t format)
TH1 * ImportHistogramOrtec(const char *filename)
Convert from Ortec MCA (request U.Spillmann)
TString fxOutFile
Name of the output file.
void SetStartDir(const char *dir=nullptr)
Go4Export_t fiFilter
Active filter format.
void ExportXML(TObject *ob)
Store object into root xml file.
void ExportRoot(TObject *ob)
Store object into root file.
void Export(TObject *ob, Go4Export_t format)
Conversion of object into selected file format in working dir.
void ExportRadware(TH1 *histo)
Conversion of histogram into radware file.
TString fxStartDir
name of the start (top level) directory.
void SetCurrentDir(const char *dir=nullptr)
void ExportASCII(TH1 *histo, Bool_t channels=kTRUE)
Conversion of histogram into ascii file.
TString fxCurrentDir
name of the current working directory.
TH1 * ImportHistogram(const char *filename, Go4Import_t format)
create imported histogram from file of given name
static const char * Message(Int_t prio, const char *text,...) GO4_PRINTF2_ARGS
Display a message.