void RootProblem(){ int argc = 1; std::string filename = "/tmp/root2.root"; if (argc == 2){ std::cout << "Run with dummy canvas" << std::endl; // only one argument given // -> create dummy canvas to allow root to open file(!) TCanvas* dummyCan = new TCanvas("dummy","dummy",600,400); } else if (argc == 3){ std::cout << "Recreating root file with TH2D in canvas in draw mode \"COLZ\"" << std::endl; // two arguments // recreate root file, store a canvas with a TH2D in draw mode "COLZ" TFile* sourceFile = new TFile(filename.c_str(),"RECREATE"); TH2D *sourceHis = new TH2D("th2","th2",2,0,2,2,0,2); sourceHis->Fill(0.,0.,1.); sourceHis->Fill(0.,1.,2.); sourceHis->Fill(1.,0.,3.); sourceHis->Fill(1.,1.,4.); TCanvas* sourceCan = new TCanvas("can1","can1",600,400); sourceHis->Draw("COLZ"); sourceCan->Modified(); sourceCan->Update(); sourceCan->Write(); sourceFile->Close(); delete sourceFile; exit(1); } // read out previously created canvas std::cout << "Reading root file" << std::endl; TFile* rootFile = new TFile(filename.c_str()); TCanvas* readCan = 0; TH2D* readHis = 0; rootFile->GetObject("can1",readCan); TPad* pad5 = (TPad*) readCan->GetPad(0); readHis = (TH2D*) pad5->FindObject("th2"); rootFile->Close(); }