Logo ROOT   6.12/07
Reference Guide
loopdir11.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// \notebook -nodraw
4 /// Example of script to loop on all the objects of a ROOT file directory
5 /// and print on Postscript all TH1 derived objects.
6 /// This script uses the file generated by tutorial hsimple.C
7 ///
8 /// \macro_image
9 /// \macro_code
10 ///
11 /// \author Rene Brun
12 
13 void loopdir11() {
14  TFile *f1 = TFile::Open("hsimple.root");
15  TCanvas c1;
16  c1.Print("hsimple.ps[");
17  for(auto k : *f1->GetListOfKeys()) {
18  TKey *key = static_cast<TKey*>(k);
19  TClass *cl = gROOT->GetClass(key->GetClassName());
20  if (!cl->InheritsFrom("TH1")) continue;
21  TH1 *h = key->ReadObject<TH1>();
22  h->Draw();
23  c1.Print("hsimple.ps");
24  }
25  c1.Print("hsimple.ps]");
26 }
T * ReadObject()
To read an object (non deriving from TObject) from the file.
Definition: TKey.h:98
return c1
Definition: legend1.C:41
virtual const char * GetClassName() const
Definition: TKey.h:71
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
#define gROOT
Definition: TROOT.h:402
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3950
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2969
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4688
virtual void Print(const char *filename="") const
Save Pad contents in a file in one of various formats.
Definition: TPad.cxx:4599
The Canvas class.
Definition: TCanvas.h:31
The TH1 histogram class.
Definition: TH1.h:56
virtual TList * GetListOfKeys() const
TF1 * f1
Definition: legend1.C:11