#include "zgraph.h" ClassImp(zora::zGraph); #include #include #include "../zdata/zdata.h" using namespace std; using namespace zora; zGraph::zGraph() : TGraph(), d_branch(0), d_reload(kFALSE), d_nExec(0) { this->SetName("noName"); this->SetBit(kCanDelete); } zGraph::zGraph(TBranch *branch, plot_mode pmode) : TGraph(), d_branch(branch), d_reload(kFALSE), d_nExec(0) { this->SetName("noName"); this->SetBit(kCanDelete); if (pmode == kImmediate) this->doGraph(); } #if 0 //actually not necessary: copy member to member is default zGraph::zGraph(zGraph const &zg) : TGraph(zg) { d_branch = zg.d_branch; d_whichX = zg.d_whichX; d_whichY = zg.d_whichY; d_f1 = zg.d_f1; d_f2 = zg.d_f1; d_reload = zg.d_reload; } #endif zGraph::~zGraph() { //cout << "destructor called for " << this->GetName() << endl; //doCleanup(); } void zGraph::doCleanup() { #if 0 //here is the code of ~TGraph, but I am not very sure of what it means. I'll leave it out for the moment delete [] fX; delete [] fY; if (fFunctions) { fFunctions->SetBit(kInvalidObject); //special logic to support the case where the same object is //added multiple times in fFunctions. //This case happens when the same object is added with different //drawing modes TObject *obj; while ((obj = fFunctions->First())) { while(fFunctions->Remove(obj)); delete obj; } delete fFunctions; } fFunctions = 0; delete fHistogram; fHistogram = 0; #endif } Int_t zGraph::doGraph() { //doCleanup()? //cout << "execute doGraph()\n"; //cout << "fX " << fX << " fY " << fY << endl; delete [] fX; delete [] fY; fNpoints = 0; if (!d_branch) { cerr << "branch = 0 in " << this->GetName() << endl; return -1; } /* the class zData is the base class for 3 zData2, zData3, zData6. They only contain 2, 3 or 6 numbers that refer to the same experimental point, and they can be one or two X variables (X1, X2) and 1 to 4 dependent variables (Y1 through Y4). Here I want to be able to plot in a TGraph one variable (X or Y) vs a second variable (X or Y). Given the name of the variable I want to plot, I have to call the right member function of zData. */ Float_t (zData::*getX)() = 0; Float_t (zData::*getY)() = 0; zData *data; if (strcmp(d_branch->GetClassName(), "zData2") == 0) { if (d_whichX == X1) getX = &zData::getXone; if (d_whichX == Y1) getX = &zData::getYone; if (d_whichY == X1) getY = &zData::getXone; if (d_whichY == Y1) getY = &zData::getYone; if (getX == 0 || getY == 0) { cerr << "function(s) not set in " << this->GetName() << endl; return -1; } data = new zData2(); d_branch->SetAddress(&data); } if (strcmp(d_branch->GetClassName(), "zData3") == 0) { if (d_whichX == X1) getX = &zData::getXone; if (d_whichX == Y1) getX = &zData::getYone; if (d_whichX == Y2) getX = &zData::getYtwo; if (d_whichY == X1) getY = &zData::getXone; if (d_whichY == Y1) getY = &zData::getYone; if (d_whichY == Y2) getY = &zData::getYtwo; if (getX == 0 || getY == 0) { cerr << "function(s) not set in " << this->GetName() << endl; return -1; } data = new zData3(); d_branch->SetAddress(&data); } if (strcmp(d_branch->GetClassName(), "zData6") == 0) { if (d_whichX == X1) getX = &zData::getXone; if (d_whichX == Y1) getX = &zData::getYone; if (d_whichX == Y2) getX = &zData::getYtwo; if (d_whichX == Y3) getX = &zData::getYthree; if (d_whichX == Y4) getX = &zData::getYfour; if (d_whichY == X1) getY = &zData::getXone; if (d_whichY == Y1) getY = &zData::getYone; if (d_whichY == Y2) getY = &zData::getYtwo; if (d_whichY == Y3) getY = &zData::getYthree; if (d_whichY == Y4) getY = &zData::getYfour; if (getX == 0 || getY == 0) { cerr << "function(s) not set in " << this->GetName() << endl; return -1; } data = new zData6(); d_branch->SetAddress(&data); } //----------------------------------------// fNpoints = (Int_t) d_branch->GetEntries(); if (fNpoints <= 0) { cerr << "fNpoints <= 0 in " << d_branch->GetName() << endl; return -1; } //this line I do not understand, but I have seen it in TGraph(int n) fFunctions = new TList; fX = new Double_t[fNpoints]; fY = new Double_t[fNpoints]; Int_t idx = 0; while (d_branch->GetEntry(idx) > 0) { fX[idx] = d_f1.Eval((data->*getX)()); fY[idx] = d_f2.Eval((data->*getY)()); idx++; //if (idx > 1100) //cout << idx << endl; } delete data; //this variable isn't in use yet d_nExec++; return 0; } //-------------------------------------------------- //This is what works but it isn't what I had in mind //-------------------------------------------------- TGraph *zGraph::getTGraph(axis whichX, axis whichY, TF1 *f1, TF1 *f2) { d_f1 = *f1; d_f2 = *f2; d_whichX = whichX; d_whichY = whichY; if (doGraph()) return new TGraph(); TGraph *gr = new TGraph(fNpoints, fX, fY); string str("fromBranch_"); str += d_branch->GetName(); gr->SetName(str.c_str()); return gr; } //-------------------------------------------------- //This is the function I would like to implement //-------------------------------------------------- zGraph *zGraph::getzGraph(axis whichX, axis whichY, TF1 *f1, TF1 *f2) { d_f1 = *f1; d_f2 = *f2; d_whichX = whichX; d_whichY = whichY; string str("fromBranch_"); str += d_branch->GetName(); this->SetName(str.c_str()); return new zGraph(*this); }