#include #include #include "TGraph2DErrors.h" #include "TFile.h" #include "TString.h" void PrintGraphInfo(TGraph2DErrors* gr) { std::cout << std::hex << std::showbase << "\ngr @ " << gr << "\ngr->GetX()=" << gr->GetX() << "\ngr->GetY()=" << gr->GetY() << "\ngr->GetZ()=" << gr->GetZ() << std::endl; std::cout << std::dec << std::noshowbase; } // Open the file, // --> get the graph, // --> copy it, // --> close the file, // --> return the copy TGraph2DErrors* GetGraph(TString filename) { TGraph2DErrors* gr = NULL; TFile *f = new TFile(filename.Data(),"UPDATE"); if (f==NULL) return gr; else if (!f->IsOpen()) return gr; TString my_graph_name = "mygraph"; gr = dynamic_cast( f->Get(my_graph_name.Data()) ); if (gr!=NULL) { std::cout << "\n\nInformation about TGraph2DErrors" << "\nB/4 Clone..."; PrintGraphInfo(gr); // ... copy the graph TString name(gr->GetName()); TGraph2DErrors* temp_gr = static_cast(gr->Clone("temp_gr")); gr = temp_gr; gr->SetName(name.Data()); } std::cout << "\n\n...AFTER Copy"; PrintGraphInfo(gr); f->Close(); std::cout << "\n\n...AFTER File Closed"; PrintGraphInfo(gr); return gr; } Int_t Main() { // TString root_fname; // std::cout << "Enter location of graph : "; // root_fname.ReadLine(std::cin); TGraph2DErrors *gr = GetGraph("myfile.root"); /* do something with the graph */ // ** the TFILE thinks it owns the graph ** // // delete gr; return 0; } #ifndef __CINT__ Int_t main() { return Main(); } #endif