From 53c48e5034d46be481f9deeae29a19e018f55cf5 Mon Sep 17 00:00:00 2001 From: nemu Date: Fri, 31 Oct 2008 07:08:08 +0000 Subject: [PATCH] added save data in ascii format --- src/classes/PMusrCanvas.cpp | 351 +++++++++++++++++++++++++++++++----- src/include/PMusrCanvas.h | 32 +++- 2 files changed, 334 insertions(+), 49 deletions(-) diff --git a/src/classes/PMusrCanvas.cpp b/src/classes/PMusrCanvas.cpp index 844d5b8d..517f24ba 100644 --- a/src/classes/PMusrCanvas.cpp +++ b/src/classes/PMusrCanvas.cpp @@ -30,11 +30,13 @@ ***************************************************************************/ #include +#include using namespace std; #include #include #include +#include #include "PMusrCanvas.h" @@ -49,14 +51,16 @@ ClassImpQ(PMusrCanvas) PMusrCanvas::PMusrCanvas() { fValid = false; - fDifferencePlot = false; + fDifferenceView = false; + fCurrentPlotView = PV_DATA; fPlotType = -1; fPlotNumber = -1; fImp = 0; fBar = 0; - fPopupMain = 0; - fPopupSave = 0; + fPopupMain = 0; + fPopupSave = 0; + fPopupFourier = 0; fStyle = 0; fMainCanvas = 0; @@ -105,10 +109,6 @@ PMusrCanvas::~PMusrCanvas() { cout << "~PMusrCanvas() called. fMainCanvas name=" << fMainCanvas->GetName() << endl; // cleanup - if (fPopupSave) { - delete fPopupSave; - fPopupSave = 0; - } if (fStyle) { delete fStyle; fStyle = 0; @@ -177,13 +177,15 @@ void PMusrCanvas::CreateStyle() void PMusrCanvas::InitMusrCanvas(const char* title, Int_t wtopx, Int_t wtopy, Int_t ww, Int_t wh) { fValid = false; - fDifferencePlot = false; + fDifferenceView = false; + fCurrentPlotView = PV_DATA; fPlotType = -1; fImp = 0; fBar = 0; - fPopupMain = 0; - fPopupSave = 0; + fPopupMain = 0; + fPopupSave = 0; + fPopupFourier = 0; fMainCanvas = 0; fTitlePad = 0; @@ -205,13 +207,21 @@ void PMusrCanvas::InitMusrCanvas(const char* title, Int_t wtopx, Int_t wtopy, In fImp = (TRootCanvas*)fMainCanvas->GetCanvasImp(); fBar = fImp->GetMenuBar(); fPopupMain = fBar->AddPopup("&Musrfit"); - fPopupMain->AddEntry("&Fourier", P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber); + + fPopupFourier = new TGPopupMenu(); + fPopupMain->AddPopup("&Fourier", fPopupFourier); + fPopupFourier->AddEntry("Real", P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_REAL); + fPopupFourier->AddEntry("Imag", P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_IMAG); + fPopupFourier->AddEntry("Power", P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_PWR); + fPopupFourier->AddEntry("Phase", P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_PHASE); + fPopupMain->AddEntry("&Difference", P_MENU_ID_DIFFERENCE+P_MENU_PLOT_OFFSET*fPlotNumber); fPopupMain->AddSeparator(); -// fPopupMain->AddEntry("&Save Data", P_MENU_ID_SAVE_DATA+P_MENU_PLOT_OFFSET*fPlotNumber); + fPopupSave = new TGPopupMenu(); fPopupSave->AddEntry("Save ascii", P_MENU_ID_SAVE_DATA+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_SAVE_ASCII); fPopupSave->AddEntry("Save db", P_MENU_ID_SAVE_DATA+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_SAVE_DB); + fPopupMain->AddPopup("&Save Data", fPopupSave); fBar->MapSubwindows(); fBar->Layout(); @@ -331,8 +341,14 @@ void PMusrCanvas::HandleCmdKey(Int_t event, Int_t x, Int_t y, TObject *selected) */ void PMusrCanvas::HandleMenuPopup(Int_t id) { - if (id == P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber) { - cout << endl << ">> will handle Fourier ..." << endl; + if (id == P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_REAL) { + cout << endl << ">> will handle Real Part Fourier ..." << endl; + } else if (id == P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_IMAG) { + cout << endl << ">> will handle Imaginary Part Fourier ..." << endl; + } else if (id == P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_PWR) { + cout << endl << ">> will handle Power Fourier ..." << endl; + } else if (id == P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_PHASE) { + cout << endl << ">> will handle Phase Fourier ..." << endl; } else if (id == P_MENU_ID_DIFFERENCE+P_MENU_PLOT_OFFSET*fPlotNumber) { HandleDifference(); } else if (id == P_MENU_ID_SAVE_DATA+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_SAVE_ASCII) { @@ -1056,13 +1072,11 @@ void PMusrCanvas::HandleDifference() cout << endl << ">> will show the difference between the theory and the signal, to be implemented yet. fMainCanvas name = " << fMainCanvas->GetName(); cout << endl; - if (fDifferencePlot) - fDifferencePlot = kFALSE; - else - fDifferencePlot = kTRUE; + // toggle difference view flag + fDifferenceView = !fDifferenceView; // difference plot wished hence feed difference data and plot them - if (fDifferencePlot) { + if (fDifferenceView && (fCurrentPlotView == PV_DATA)) { // check if it is necessary to calculate diff data if ((fPlotType != MSR_PLOT_NON_MUSR) && (fData[0].diff == 0)) { TH1F *diffHisto; @@ -1163,19 +1177,22 @@ cout << endl << ">> diff-name = " << name.Data() << endl; } // switch back to the "normal" view - if (!fDifferencePlot) { + if (!fDifferenceView) { // set current x-axis range - PlotData(); + if (fCurrentPlotView == PV_DATA) + PlotData(); } else { - if (fPlotType != MSR_PLOT_NON_MUSR) { - // set current x-axis range - Int_t xminBin = fData[0].data->GetXaxis()->GetFirst(); // first bin of the zoomed range - Int_t xmaxBin = fData[0].data->GetXaxis()->GetLast(); // last bin of the zoomed range - Double_t xmin = fData[0].data->GetXaxis()->GetBinCenter(xminBin); - Double_t xmax = fData[0].data->GetXaxis()->GetBinCenter(xmaxBin); - fData[0].diff->GetXaxis()->SetRangeUser(xmin, xmax); + if (fCurrentPlotView == PV_DATA) { + if (fPlotType != MSR_PLOT_NON_MUSR) { + // set current x-axis range + Int_t xminBin = fData[0].data->GetXaxis()->GetFirst(); // first bin of the zoomed range + Int_t xmaxBin = fData[0].data->GetXaxis()->GetLast(); // last bin of the zoomed range + Double_t xmin = fData[0].data->GetXaxis()->GetBinCenter(xminBin); + Double_t xmax = fData[0].data->GetXaxis()->GetBinCenter(xmaxBin); + fData[0].diff->GetXaxis()->SetRangeUser(xmin, xmax); + } + PlotDifference(); } - PlotDifference(); } } @@ -1208,27 +1225,45 @@ double PMusrCanvas::CalculateDiff(const double x, const double y, TH1F *theo) */ double PMusrCanvas::CalculateDiff(const double x, const double y, TGraphErrors *theo) { - Int_t i, bin = 0; - Double_t *xTheo = theo->GetX(); + Int_t bin = 0; Double_t xVal, yVal; - // find proper bin of the theory - for (i=0; iGetN(); i++) { + bin = FindBin(x, theo); + + theo->GetPoint(bin, xVal, yVal); + +//cout << endl << ">> bin=" << bin << ", x=" << xVal << " (xData=" << x << "), y=" << yVal; + + return y - yVal; +} + +//-------------------------------------------------------------------------- +// FindBin +//-------------------------------------------------------------------------- +/** + *

Analog to FindBin for histograms (TH1F) but here for TGraphErrors. + * + * \param x x-value of the data + * \param graph TGraphErrors which should be seaarched + */ +Int_t PMusrCanvas::FindBin(const double x, TGraphErrors *graph) +{ + Int_t i, bin = 0; + Double_t *xTheo = graph->GetX(); + + // find proper bin of the graph + for (i=0; iGetN(); i++) { if (*(xTheo+i) >= x) { bin = i; break; } } // in case it is the last point - if (i == theo->GetN()) { + if (i == graph->GetN()) { bin = i; } - theo->GetPoint(bin, xVal, yVal); - -cout << endl << ">> bin=" << bin << ", x=" << xVal << " (xData=" << x << "), y=" << yVal; - - return y - yVal; + return bin; } //-------------------------------------------------------------------------- @@ -1398,7 +1433,241 @@ void PMusrCanvas::PlotFourier(int fourierType) */ void PMusrCanvas::SaveDataAscii() { - cout << endl << ">> will handle Save Data in ascii format ..." << endl; + // generate output filename + + // in order to handle names with "." correctly this slightly odd data-filename generation + TObjArray *tokens = fMsrHandler->GetFileName().Tokenize("."); + TObjString *ostr; + TString str = TString(""); + for (int i=0; iGetEntries()-1; i++) { + ostr = dynamic_cast(tokens->At(i)); + str += ostr->GetString() + TString("."); + } + str += "data"; + + if (tokens) { + delete tokens; + tokens = 0; + } + + // open file + ofstream fout; + + // open mlog-file + fout.open(str.Data(), iostream::out); + if (!fout.is_open()) { + cout << endl << ">> PMusrCanvas::SaveDataAscii: **ERROR** couldn't open file " << str.Data() << " for writing." << endl; + return; + } + + // extract data + Double_t time, xval, yval; + Int_t xminBin; + Int_t xmaxBin; + Int_t theoBin; + Double_t xmin; + Double_t xmax; + switch (fPlotType) { + case MSR_PLOT_SINGLE_HISTO: + case MSR_PLOT_ASYM: + case MSR_PLOT_ASYM_RRF: + if (fDifferenceView) { // difference view plot + switch (fCurrentPlotView) { + case PV_DATA: + // write header + fout << endl << "% time (us), "; + for (unsigned int j=0; jGetXaxis()->GetFirst(); // first bin of the zoomed range + xmaxBin = fData[0].diff->GetXaxis()->GetLast(); // last bin of the zoomed range + xmin = fData[0].diff->GetXaxis()->GetBinCenter(xminBin); + xmax = fData[0].diff->GetXaxis()->GetBinCenter(xmaxBin); + // get difference data + for (int i=1; iGetNbinsX()-1; i++) { + time = fData[0].diff->GetBinCenter(i); // get time + if ((time < xmin) || (time > xmax)) + continue; + fout << time << ", "; + for (unsigned int j=0; jGetBinContent(i) << ", "; + fout << fData[j].diff->GetBinError(i) << ", "; + } + // write last data set + fout << fData[fData.size()-1].diff->GetBinContent(i) << ", "; + fout << fData[fData.size()-1].diff->GetBinError(i); + fout << endl; + } + break; + case PV_FOURIER_REAL: + break; + case PV_FOURIER_IMAG: + break; + case PV_FOURIER_PWR: + break; + case PV_FOURIER_PHASE: + break; + default: + break; + } + } else { // not a difference view plot + switch (fCurrentPlotView) { + case PV_DATA: + // write header + fout << endl << "% time (us), "; + for (unsigned int j=0; jGetXaxis()->GetFirst(); // first bin of the zoomed range + xmaxBin = fData[0].data->GetXaxis()->GetLast(); // last bin of the zoomed range + xmin = fData[0].data->GetXaxis()->GetBinCenter(xminBin); + xmax = fData[0].data->GetXaxis()->GetBinCenter(xmaxBin); + // get data + for (int i=1; iGetNbinsX()-1; i++) { + time = fData[0].data->GetBinCenter(i); // get time + if ((time < xmin) || (time > xmax)) + continue; + fout << time << ", "; + for (unsigned int j=0; jGetBinContent(i) << ", "; + fout << fData[j].data->GetBinError(i) << ", "; + theoBin = fData[j].theory->FindBin(time); + fout << fData[j].theory->GetBinContent(theoBin) << ", "; + } + // write last data set + fout << fData[fData.size()-1].data->GetBinContent(i) << ", "; + fout << fData[fData.size()-1].data->GetBinError(i) << ", "; + theoBin = fData[fData.size()-1].theory->FindBin(time); + fout << fData[fData.size()-1].theory->GetBinContent(theoBin); + fout << endl; + } + break; + case PV_FOURIER_REAL: + break; + case PV_FOURIER_IMAG: + break; + case PV_FOURIER_PWR: + break; + case PV_FOURIER_PHASE: + break; + default: + break; + } + } + break; + case MSR_PLOT_NON_MUSR: + if (fDifferenceView) { // difference view plot + switch (fCurrentPlotView) { + case PV_DATA: + // write header + fout << "% " << fNonMusrData[0].diff->GetXaxis()->GetTitle() << ", "; + for (unsigned int j=0; jGetXaxis()->GetFirst(); // first bin of the zoomed range + xmaxBin = fNonMusrData[0].diff->GetXaxis()->GetLast(); // last bin of the zoomed range + xmin = fNonMusrData[0].diff->GetXaxis()->GetBinCenter(xminBin); + xmax = fNonMusrData[0].diff->GetXaxis()->GetBinCenter(xmaxBin); + // get data + for (int i=0; iGetN(); i++) { + fNonMusrData[0].diff->GetPoint(i,xval,yval); // get values + if ((xval < xmin) || (xval > xmax)) + continue; + fout << xval << ", "; + for (unsigned int j=0; jGetPoint(i,xval,yval); // get values + fout << yval << ", "; + fout << fNonMusrData[j].diff->GetErrorY(i) << ", "; + } + // write last data set + fNonMusrData[fNonMusrData.size()-1].diff->GetPoint(i,xval,yval); // get values + fout << yval << ", "; + fout << fNonMusrData[fNonMusrData.size()-1].diff->GetErrorY(i); + fout << endl; + } + break; + case PV_FOURIER_REAL: + break; + case PV_FOURIER_IMAG: + break; + case PV_FOURIER_PWR: + break; + case PV_FOURIER_PHASE: + break; + default: + break; + } + } else { // not a difference view plot + switch (fCurrentPlotView) { + case PV_DATA: + // write header + fout << "% " << fNonMusrData[0].data->GetXaxis()->GetTitle() << ", "; + for (unsigned int j=0; jGetXaxis()->GetFirst(); // first bin of the zoomed range + xmaxBin = fNonMusrData[0].data->GetXaxis()->GetLast(); // last bin of the zoomed range + xmin = fNonMusrData[0].data->GetXaxis()->GetBinCenter(xminBin); + xmax = fNonMusrData[0].data->GetXaxis()->GetBinCenter(xmaxBin); + // get data + for (int i=0; iGetN(); i++) { + fNonMusrData[0].data->GetPoint(i,xval,yval); // get values + if ((xval < xmin) || (xval > xmax)) + continue; + fout << xval << ", "; + for (unsigned int j=0; jGetPoint(i,xval,yval); // get values + fout << yval << ", "; + fout << fNonMusrData[j].data->GetErrorY(i) << ", "; + theoBin = FindBin(xval, fNonMusrData[j].theory); + fNonMusrData[j].theory->GetPoint(theoBin,xval,yval); // get values + fout << yval << ", "; + } + // write last data set + fNonMusrData[fNonMusrData.size()-1].data->GetPoint(i,xval,yval); // get values + fout << yval << ", "; + fout << fNonMusrData[fNonMusrData.size()-1].data->GetErrorY(i) << ", "; + theoBin = FindBin(xval, fNonMusrData[fNonMusrData.size()-1].theory); + fNonMusrData[fNonMusrData.size()-1].theory->GetPoint(theoBin,xval,yval); // get values + fout << yval; + fout << endl; + } + break; + case PV_FOURIER_REAL: + break; + case PV_FOURIER_IMAG: + break; + case PV_FOURIER_PWR: + break; + case PV_FOURIER_PHASE: + break; + default: + break; + } + } + break; + default: + break; + } + + // close file + fout.close(); + + cout << endl << ">> Data windows saved in ascii format ..." << endl; } //-------------------------------------------------------------------------- @@ -1410,6 +1679,6 @@ void PMusrCanvas::SaveDataAscii() */ void PMusrCanvas::SaveDataDb() { - cout << endl << ">> will handle Save Data in db format ..." << endl; + cout << endl << ">> will eventually handle Save Data in db format ..." << endl; } diff --git a/src/include/PMusrCanvas.h b/src/include/PMusrCanvas.h index bf2a9196..f5cde4f2 100644 --- a/src/include/PMusrCanvas.h +++ b/src/include/PMusrCanvas.h @@ -54,14 +54,27 @@ #define YTITLE 0.95 #define XTHEO 0.75 -// Canvas menu id's -#define P_MENU_ID_FOURIER 10001 -#define P_MENU_ID_DIFFERENCE 10002 -#define P_MENU_ID_SAVE_DATA 10003 +// Current Plot Views +#define PV_DATA 1 +#define PV_FOURIER_REAL 2 +#define PV_FOURIER_IMAG 3 +#define PV_FOURIER_PWR 4 +#define PV_FOURIER_PHASE 5 -#define P_MENU_PLOT_OFFSET 1000 -#define P_MENU_ID_SAVE_ASCII 100 -#define P_MENU_ID_SAVE_DB 101 +// Canvas menu id's +#define P_MENU_ID_FOURIER 10001 +#define P_MENU_ID_DIFFERENCE 10002 +#define P_MENU_ID_SAVE_DATA 10003 + +#define P_MENU_PLOT_OFFSET 1000 + +#define P_MENU_ID_FOURIER_REAL 100 +#define P_MENU_ID_FOURIER_IMAG 101 +#define P_MENU_ID_FOURIER_PWR 102 +#define P_MENU_ID_FOURIER_PHASE 103 + +#define P_MENU_ID_SAVE_ASCII 200 +#define P_MENU_ID_SAVE_DB 201 //------------------------------------------------------------------------ /** @@ -153,7 +166,8 @@ class PMusrCanvas : public TObject, public TQObject private: Bool_t fValid; - Bool_t fDifferencePlot; /// flag showing is a difference plot is shown + Bool_t fDifferenceView; /// tag showing that the shown data, fourier, are the difference between data and theory + Int_t fCurrentPlotView; /// tag showing what the current plot view is: data, fourier, ... Int_t fPlotType; Int_t fPlotNumber; @@ -164,6 +178,7 @@ class PMusrCanvas : public TObject, public TQObject TGMenuBar *fBar; TGPopupMenu *fPopupMain; TGPopupMenu *fPopupSave; + TGPopupMenu *fPopupFourier; // canvas related variables TCanvas *fMainCanvas; @@ -195,6 +210,7 @@ class PMusrCanvas : public TObject, public TQObject virtual double CalculateDiff(const double x, const double y, TH1F *theo); virtual double CalculateDiff(const double x, const double y, TGraphErrors *theo); + virtual Int_t FindBin(const double x, TGraphErrors *graph); virtual void PlotData(); virtual void PlotDifference();