added save data in ascii format

This commit is contained in:
nemu 2008-10-31 07:08:08 +00:00
parent 2ed303abb6
commit 53c48e5034
2 changed files with 334 additions and 49 deletions

View File

@ -30,11 +30,13 @@
***************************************************************************/ ***************************************************************************/
#include <iostream> #include <iostream>
#include <fstream>
using namespace std; using namespace std;
#include <TColor.h> #include <TColor.h>
#include <TRandom.h> #include <TRandom.h>
#include <TROOT.h> #include <TROOT.h>
#include <TObjString.h>
#include "PMusrCanvas.h" #include "PMusrCanvas.h"
@ -49,14 +51,16 @@ ClassImpQ(PMusrCanvas)
PMusrCanvas::PMusrCanvas() PMusrCanvas::PMusrCanvas()
{ {
fValid = false; fValid = false;
fDifferencePlot = false; fDifferenceView = false;
fCurrentPlotView = PV_DATA;
fPlotType = -1; fPlotType = -1;
fPlotNumber = -1; fPlotNumber = -1;
fImp = 0; fImp = 0;
fBar = 0; fBar = 0;
fPopupMain = 0; fPopupMain = 0;
fPopupSave = 0; fPopupSave = 0;
fPopupFourier = 0;
fStyle = 0; fStyle = 0;
fMainCanvas = 0; fMainCanvas = 0;
@ -105,10 +109,6 @@ PMusrCanvas::~PMusrCanvas()
{ {
cout << "~PMusrCanvas() called. fMainCanvas name=" << fMainCanvas->GetName() << endl; cout << "~PMusrCanvas() called. fMainCanvas name=" << fMainCanvas->GetName() << endl;
// cleanup // cleanup
if (fPopupSave) {
delete fPopupSave;
fPopupSave = 0;
}
if (fStyle) { if (fStyle) {
delete fStyle; delete fStyle;
fStyle = 0; 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) void PMusrCanvas::InitMusrCanvas(const char* title, Int_t wtopx, Int_t wtopy, Int_t ww, Int_t wh)
{ {
fValid = false; fValid = false;
fDifferencePlot = false; fDifferenceView = false;
fCurrentPlotView = PV_DATA;
fPlotType = -1; fPlotType = -1;
fImp = 0; fImp = 0;
fBar = 0; fBar = 0;
fPopupMain = 0; fPopupMain = 0;
fPopupSave = 0; fPopupSave = 0;
fPopupFourier = 0;
fMainCanvas = 0; fMainCanvas = 0;
fTitlePad = 0; fTitlePad = 0;
@ -205,13 +207,21 @@ void PMusrCanvas::InitMusrCanvas(const char* title, Int_t wtopx, Int_t wtopy, In
fImp = (TRootCanvas*)fMainCanvas->GetCanvasImp(); fImp = (TRootCanvas*)fMainCanvas->GetCanvasImp();
fBar = fImp->GetMenuBar(); fBar = fImp->GetMenuBar();
fPopupMain = fBar->AddPopup("&Musrfit"); 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->AddEntry("&Difference", P_MENU_ID_DIFFERENCE+P_MENU_PLOT_OFFSET*fPlotNumber);
fPopupMain->AddSeparator(); fPopupMain->AddSeparator();
// fPopupMain->AddEntry("&Save Data", P_MENU_ID_SAVE_DATA+P_MENU_PLOT_OFFSET*fPlotNumber);
fPopupSave = new TGPopupMenu(); 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 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); fPopupSave->AddEntry("Save db", P_MENU_ID_SAVE_DATA+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_SAVE_DB);
fPopupMain->AddPopup("&Save Data", fPopupSave); fPopupMain->AddPopup("&Save Data", fPopupSave);
fBar->MapSubwindows(); fBar->MapSubwindows();
fBar->Layout(); 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) void PMusrCanvas::HandleMenuPopup(Int_t id)
{ {
if (id == P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber) { if (id == P_MENU_ID_FOURIER+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_FOURIER_REAL) {
cout << endl << ">> will handle Fourier ..." << endl; 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) { } else if (id == P_MENU_ID_DIFFERENCE+P_MENU_PLOT_OFFSET*fPlotNumber) {
HandleDifference(); HandleDifference();
} else if (id == P_MENU_ID_SAVE_DATA+P_MENU_PLOT_OFFSET*fPlotNumber+P_MENU_ID_SAVE_ASCII) { } 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 << ">> will show the difference between the theory and the signal, to be implemented yet. fMainCanvas name = " << fMainCanvas->GetName();
cout << endl; cout << endl;
if (fDifferencePlot) // toggle difference view flag
fDifferencePlot = kFALSE; fDifferenceView = !fDifferenceView;
else
fDifferencePlot = kTRUE;
// difference plot wished hence feed difference data and plot them // 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 // check if it is necessary to calculate diff data
if ((fPlotType != MSR_PLOT_NON_MUSR) && (fData[0].diff == 0)) { if ((fPlotType != MSR_PLOT_NON_MUSR) && (fData[0].diff == 0)) {
TH1F *diffHisto; TH1F *diffHisto;
@ -1163,19 +1177,22 @@ cout << endl << ">> diff-name = " << name.Data() << endl;
} }
// switch back to the "normal" view // switch back to the "normal" view
if (!fDifferencePlot) { if (!fDifferenceView) {
// set current x-axis range // set current x-axis range
PlotData(); if (fCurrentPlotView == PV_DATA)
PlotData();
} else { } else {
if (fPlotType != MSR_PLOT_NON_MUSR) { if (fCurrentPlotView == PV_DATA) {
// set current x-axis range if (fPlotType != MSR_PLOT_NON_MUSR) {
Int_t xminBin = fData[0].data->GetXaxis()->GetFirst(); // first bin of the zoomed range // set current x-axis range
Int_t xmaxBin = fData[0].data->GetXaxis()->GetLast(); // last bin of the zoomed range Int_t xminBin = fData[0].data->GetXaxis()->GetFirst(); // first bin of the zoomed range
Double_t xmin = fData[0].data->GetXaxis()->GetBinCenter(xminBin); Int_t xmaxBin = fData[0].data->GetXaxis()->GetLast(); // last bin of the zoomed range
Double_t xmax = fData[0].data->GetXaxis()->GetBinCenter(xmaxBin); Double_t xmin = fData[0].data->GetXaxis()->GetBinCenter(xminBin);
fData[0].diff->GetXaxis()->SetRangeUser(xmin, xmax); 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) double PMusrCanvas::CalculateDiff(const double x, const double y, TGraphErrors *theo)
{ {
Int_t i, bin = 0; Int_t bin = 0;
Double_t *xTheo = theo->GetX();
Double_t xVal, yVal; Double_t xVal, yVal;
// find proper bin of the theory bin = FindBin(x, theo);
for (i=0; i<theo->GetN(); i++) {
theo->GetPoint(bin, xVal, yVal);
//cout << endl << ">> bin=" << bin << ", x=" << xVal << " (xData=" << x << "), y=" << yVal;
return y - yVal;
}
//--------------------------------------------------------------------------
// FindBin
//--------------------------------------------------------------------------
/**
* <p>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; i<graph->GetN(); i++) {
if (*(xTheo+i) >= x) { if (*(xTheo+i) >= x) {
bin = i; bin = i;
break; break;
} }
} }
// in case it is the last point // in case it is the last point
if (i == theo->GetN()) { if (i == graph->GetN()) {
bin = i; bin = i;
} }
theo->GetPoint(bin, xVal, yVal); return bin;
cout << endl << ">> bin=" << bin << ", x=" << xVal << " (xData=" << x << "), y=" << yVal;
return y - yVal;
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@ -1398,7 +1433,241 @@ void PMusrCanvas::PlotFourier(int fourierType)
*/ */
void PMusrCanvas::SaveDataAscii() 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; i<tokens->GetEntries()-1; i++) {
ostr = dynamic_cast<TObjString*>(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; j<fData.size()-1; j++) {
fout << "Diff" << j << ", eDiff" << j << ", ";
}
fout << "Diff" << fData.size()-1 << ", eDiff" << fData.size()-1;
fout << endl;
// get current x-range
xminBin = fData[0].diff->GetXaxis()->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; i<fData[0].diff->GetNbinsX()-1; i++) {
time = fData[0].diff->GetBinCenter(i); // get time
if ((time < xmin) || (time > xmax))
continue;
fout << time << ", ";
for (unsigned int j=0; j<fData.size()-1; j++) {
fout << fData[j].diff->GetBinContent(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; j<fData.size()-1; j++) {
fout << "Data" << j << ", eData" << j << ", Theo" << j << ", ";
}
fout << "Data" << fData.size()-1 << ", eData" << fData.size()-1 << ", Theo" << fData.size()-1;
fout << endl;
// get current x-range
xminBin = fData[0].data->GetXaxis()->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; i<fData[0].data->GetNbinsX()-1; i++) {
time = fData[0].data->GetBinCenter(i); // get time
if ((time < xmin) || (time > xmax))
continue;
fout << time << ", ";
for (unsigned int j=0; j<fData.size()-1; j++) {
fout << fData[j].data->GetBinContent(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; j<fNonMusrData.size()-1; j++) {
fout << "Diff" << j << ", eDiff" << j << ", ";
}
fout << "Diff" << fNonMusrData.size()-1 << ", eDiff" << fNonMusrData.size()-1;
fout << endl;
// write data
// get current x-range
xminBin = fNonMusrData[0].diff->GetXaxis()->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; i<fNonMusrData[0].diff->GetN(); i++) {
fNonMusrData[0].diff->GetPoint(i,xval,yval); // get values
if ((xval < xmin) || (xval > xmax))
continue;
fout << xval << ", ";
for (unsigned int j=0; j<fNonMusrData.size()-1; j++) {
fNonMusrData[j].diff->GetPoint(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; j<fNonMusrData.size()-1; j++) {
fout << "Data" << j << ", eData" << j << ", Theo" << j << ", ";
}
fout << "Data" << fNonMusrData.size()-1 << ", eData" << fNonMusrData.size()-1 << ", Theo" << fNonMusrData.size()-1;
fout << endl;
// write data
// get current x-range
xminBin = fNonMusrData[0].data->GetXaxis()->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; i<fNonMusrData[0].data->GetN(); i++) {
fNonMusrData[0].data->GetPoint(i,xval,yval); // get values
if ((xval < xmin) || (xval > xmax))
continue;
fout << xval << ", ";
for (unsigned int j=0; j<fNonMusrData.size()-1; j++) {
fNonMusrData[j].data->GetPoint(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() void PMusrCanvas::SaveDataDb()
{ {
cout << endl << ">> will handle Save Data in db format ..." << endl; cout << endl << ">> will eventually handle Save Data in db format ..." << endl;
} }

View File

@ -54,14 +54,27 @@
#define YTITLE 0.95 #define YTITLE 0.95
#define XTHEO 0.75 #define XTHEO 0.75
// Canvas menu id's // Current Plot Views
#define P_MENU_ID_FOURIER 10001 #define PV_DATA 1
#define P_MENU_ID_DIFFERENCE 10002 #define PV_FOURIER_REAL 2
#define P_MENU_ID_SAVE_DATA 10003 #define PV_FOURIER_IMAG 3
#define PV_FOURIER_PWR 4
#define PV_FOURIER_PHASE 5
#define P_MENU_PLOT_OFFSET 1000 // Canvas menu id's
#define P_MENU_ID_SAVE_ASCII 100 #define P_MENU_ID_FOURIER 10001
#define P_MENU_ID_SAVE_DB 101 #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: private:
Bool_t fValid; 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 fPlotType;
Int_t fPlotNumber; Int_t fPlotNumber;
@ -164,6 +178,7 @@ class PMusrCanvas : public TObject, public TQObject
TGMenuBar *fBar; TGMenuBar *fBar;
TGPopupMenu *fPopupMain; TGPopupMenu *fPopupMain;
TGPopupMenu *fPopupSave; TGPopupMenu *fPopupSave;
TGPopupMenu *fPopupFourier;
// canvas related variables // canvas related variables
TCanvas *fMainCanvas; 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, TH1F *theo);
virtual double CalculateDiff(const double x, const double y, TGraphErrors *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 PlotData();
virtual void PlotDifference(); virtual void PlotDifference();