Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:15:47

0001 // $Id: $
0002 
0003 /*!
0004  * \file SaveCanvas.C
0005  * \brief Save canvas as png, eps, cxx, root, etc.
0006  * \author Jin Huang <jhuang@bnl.gov>
0007  * \version $Revision:   $
0008  * \date $Date: $
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 <TH1F.h>
0025 
0026 #include <iostream>
0027 
0028 using namespace std;
0029 
0030 //! Service function to SaveCanvas()
0031 void
0032 SavePad(TPad * p)
0033 {
0034   if (!p)
0035     return;
0036 
0037   TList * l = p->GetListOfPrimitives();
0038 //  l->Print();
0039 
0040   TIter next(l);
0041   TObject *obj = NULL;
0042   while ((obj = next()))
0043     {
0044 
0045       if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TPad")) >= 0)
0046         {
0047           if ((TPad *) obj != p)
0048             SavePad((TPad *) obj);
0049         }
0050       else if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TH1")) >= 0)
0051         {
0052           cout << "Save TH1 " << obj->GetName() << endl;
0053           obj->Clone()->Write(obj->GetName(), TObject::kOverwrite);
0054         }
0055       else if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TF1")) >= 0)
0056         {
0057           cout << "Save TF1 " << obj->GetName() << endl;
0058           obj->Clone()->Write(obj->GetName(), TObject::kOverwrite);
0059         }
0060       else if (obj->IsA()->GetBaseClassOffset(TClass::GetClass("TGraph")) >= 0)
0061         {
0062           cout << "Save TGraph " << obj->GetName() << endl;
0063           obj->Clone()->Write(obj->GetName(), TObject::kOverwrite);
0064         }
0065     }
0066 }
0067 
0068 //! Save canvas to multiple formats
0069 /*!
0070  *  @param[in] c    pointer to the canvas
0071  *  @param[in] name Base of the file name. The default is the name of the cavas
0072  *  @param[in] bEPS true = save .eps and .pdf format too.
0073  */
0074 void
0075 SaveCanvas(TCanvas * c, TString name = "", Bool_t bEPS = kTRUE)
0076 {
0077   if (name.Length() == 0)
0078     name = c->GetName();
0079 
0080   c->Print(name + ".png");
0081 
0082   TDirectory * oldd = gDirectory;
0083 
0084   TString rootfilename;
0085 
0086   c->Print(rootfilename = name + ".root");
0087 
0088   TFile f(rootfilename, "update");
0089 
0090   SavePad(c);
0091 
0092   f.Close();
0093 
0094   oldd->cd();
0095 
0096   if (bEPS)
0097     {
0098 //      c->Print(name + ".pdf");
0099 
0100       float x = 20;
0101       float y = 20;
0102       gStyle->GetPaperSize(x, y);
0103 
0104       gStyle->SetPaperSize(c->GetWindowWidth() / 72 * 2.54,
0105           c->GetWindowHeight() / 72 * 2.54);
0106 //      c->Print(name + ".eps");
0107       c->Print(name + ".svg");
0108       gSystem->Exec("rsvg-convert -f pdf -o "+name + ".pdf " + name + ".svg");
0109       gSystem->Exec("rm -fv " +  name + ".svg");
0110 
0111       gStyle->SetPaperSize(x, y);
0112     }
0113   // c->Print(name+".C");
0114 }
0115 
0116 //! example to use this SaveCanvas()
0117 /*!
0118  *  Output:
0119  *  The canvas data will be saved to RootFileName.root, as well as
0120  *  RootFileName.png for presentation and RootFileName.eps for Latex
0121  *
0122  *  How to use RootFileName.root
0123  *  open RootFileName.root with root.
0124  *  It contains the canvas object "CanvasTest", which can be redraw with CanvasTest -> Draw()
0125  *  It also contains a copy of the histograms and graphs data for use in root again, e.g.
0126  *  root [3]  h1->GetBinContent(30)
0127  *
0128  */
0129 void
0130 example_save_canvas()
0131 {
0132 
0133   TCanvas *c1 = new TCanvas("CanvasTest", "CanvasTest", 800, 900);
0134 
0135   TH1F * h1 = new TH1F("h1", "histo from a gaussian", 100, -3, 3);
0136   h1->FillRandom("gaus", 10000);
0137 
0138   h1->Draw();
0139 
0140   // single call to save c1 to file RootFileName.*
0141   SaveCanvas(c1, "RootFileName");
0142 
0143 }
0144 
0145 #endif
0146