Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:12:16

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 <TSystem.h>
0019 #include <TPad.h>
0020 #include <TString.h>
0021 #include <TDirectory.h>
0022 #include <TFile.h>
0023 #include <TStyle.h>
0024 #include <TObject.h>
0025 #include <TH1F.h>
0026 
0027 #include <iostream>
0028 
0029 using namespace std;
0030 
0031 //! Service function to SaveCanvas()
0032 void
0033 SavePad(TPad * p)
0034 {
0035   if (!p)
0036     return;
0037 
0038   TList * l = p->GetListOfPrimitives();
0039 //  l->Print();
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     }
0067 }
0068 
0069 //! Save canvas to multiple formats
0070 /*!
0071  *  @param[in] c    pointer to the canvas
0072  *  @param[in] name Base of the file name. The default is the name of the cavas
0073  *  @param[in] bEPS true = save .eps and .pdf format too.
0074  */
0075 void
0076 SaveCanvas(TCanvas * c, TString name = "", Bool_t bEPS = kTRUE)
0077 {
0078   if (name.Length() == 0)
0079     name = c->GetName();
0080 
0081   c->Print(name + ".png");
0082 
0083   TDirectory * oldd = gDirectory;
0084 
0085   TString rootfilename;
0086 
0087   c->Print(rootfilename = name + ".root");
0088 
0089   TFile f(rootfilename, "update");
0090 
0091   SavePad(c);
0092 
0093   f.Close();
0094 
0095   oldd->cd();
0096 
0097   if (bEPS)
0098     {
0099 //      c->Print(name + ".pdf");
0100 
0101       float x = 20;
0102       float y = 20;
0103       gStyle->GetPaperSize(x, y);
0104 
0105       gStyle->SetPaperSize(c->GetWindowWidth() / 72 * 2.54,
0106           c->GetWindowHeight() / 72 * 2.54);
0107 //      c->Print(name + ".eps");
0108       c->Print(name + ".svg");
0109       gSystem->Exec("rsvg-convert -f pdf -o "+name + ".pdf " + name + ".svg");
0110       gSystem->Exec("rm -fv " +  name + ".svg");
0111 
0112       gStyle->SetPaperSize(x, y);
0113     }
0114   //      c->Print(name+".C");
0115 }
0116 
0117 //! example to use this SaveCanvas()
0118 /*!
0119  *  Output:
0120  *  The canvas data will be saved to RootFileName.root, as well as
0121  *  RootFileName.png for presentation and RootFileName.eps for Latex
0122  *
0123  *  How to use RootFileName.root
0124  *  open RootFileName.root with root.
0125  *  It contains the canvas object "CanvasTest", which can be redraw with CanvasTest -> Draw()
0126  *  It also contains a copy of the histograms and graphs data for use in root again, e.g.
0127  *  root [3]  h1->GetBinContent(30)
0128  *
0129  */
0130 void
0131 example_save_canvas()
0132 {
0133 
0134   TCanvas *c1 = new TCanvas("CanvasTest", "CanvasTest", 800, 900);
0135 
0136   TH1F * h1 = new TH1F("h1", "histo from a gaussian", 100, -3, 3);
0137   h1->FillRandom("gaus", 10000);
0138 
0139   h1->Draw();
0140 
0141   // single call to save c1 to file RootFileName.*
0142   SaveCanvas(c1, "RootFileName");
0143 
0144 }
0145 
0146 #endif
0147