File indexing completed on 2025-08-05 08:20:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef SaveCanvas_C
0012
0013 #define SaveCanvas_C
0014
0015 #include <TList.h>
0016 #include <TClass.h>
0017 #include <TCanvas.h>
0018 #include <TPad.h>
0019 #include <TString.h>
0020 #include <TDirectory.h>
0021 #include <TFile.h>
0022 #include <TStyle.h>
0023 #include <TObject.h>
0024 #include <TSystem.h>
0025 #include <TH1F.h>
0026
0027 #include <iostream>
0028
0029 using namespace std;
0030
0031
0032 void
0033 SavePad(TPad * p)
0034 {
0035 if (!p)
0036 return;
0037
0038 TList * l = p->GetListOfPrimitives();
0039
0040
0041 TIter next(l);
0042 TObject *obj = NULL;
0043 while ((obj = next()))
0044 {
0045
0046 if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TPad")) >= 0)
0047 {
0048 if ((TPad *) obj != p)
0049 SavePad((TPad *) obj);
0050 }
0051 else if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TH1")) >= 0)
0052 {
0053 cout << "Save TH1 " << obj->GetName() << endl;
0054 obj->Clone()->Write(obj->GetName(), TObject::kOverwrite);
0055 }
0056 else if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TF1")) >= 0)
0057 {
0058 cout << "Save TF1 " << obj->GetName() << endl;
0059 obj->Clone()->Write(obj->GetName(), TObject::kOverwrite);
0060 }
0061 else if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TGraph")) >= 0)
0062 {
0063 cout << "Save TGraph " << obj->GetName() << endl;
0064 obj->Clone()->Write(obj->GetName(), TObject::kOverwrite);
0065 }
0066 else if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TEfficiency")) >= 0)
0067 {
0068 cout << "Save TEfficiency " << obj->GetName() << endl;
0069 obj->Clone()->Write(obj->GetName(), TObject::kOverwrite);
0070 }
0071 }
0072 }
0073
0074
0075
0076
0077
0078
0079
0080 void
0081 SaveCanvas(TCanvas * c, TString name = "", Bool_t bEPS = kTRUE)
0082 {
0083 if (name.Length() == 0)
0084 name = c->GetName();
0085
0086 c->Print(name + ".png");
0087
0088 TDirectory * oldd = gDirectory;
0089
0090 TString rootfilename;
0091
0092 c->Print(rootfilename = name + ".root");
0093
0094 TFile f(rootfilename, "update");
0095
0096 SavePad(c);
0097
0098 f.Close();
0099
0100 oldd->cd();
0101
0102 if (bEPS)
0103 {
0104
0105
0106 float x = 20;
0107 float y = 20;
0108 gStyle->GetPaperSize(x, y);
0109
0110 gStyle->SetPaperSize(c->GetWindowWidth() / 72 * 2.54,
0111 c->GetWindowHeight() / 72 * 2.54);
0112 c->Print(name + ".eps");
0113 c->Print(name + ".svg");
0114 gSystem->Exec("rsvg-convert -f pdf -o "+name + ".pdf " + name + ".svg");
0115 gSystem->Exec("rm -fv " + name + ".svg");
0116
0117 gStyle->SetPaperSize(x, y);
0118 }
0119 c->Print(name+".C");
0120 }
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135 void
0136 example_save_canvas()
0137 {
0138
0139 TCanvas *c1 = new TCanvas("CanvasTest", "CanvasTest", 800, 900);
0140
0141 TH1F * h1 = new TH1F("h1", "histo from a gaussian", 100, -3, 3);
0142 h1->FillRandom("gaus", 10000);
0143
0144 h1->Draw();
0145
0146
0147 SaveCanvas(c1, "RootFileName");
0148
0149 }
0150
0151 #endif
0152