Logo ROOT  
Reference Guide
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_io
3/// \notebook -nodraw
4/// Example script to loop over all the objects of a ROOT file directory and print in
5/// Postscript all the TH1 derived objects.
6/// This script uses the file generated by tutorial hsimple.C
8/// \macro_code
10/// \author Rene Brun
12void loopdir() {
13 TFile *f1 = TFile::Open("hsimple.root");
14 TIter keyList(f1->GetListOfKeys());
15 TKey *key;
16 TCanvas c1;
17 c1.Print("hsimple.ps[");
18 while ((key = (TKey*)keyList())) {
19 TClass *cl = gROOT->GetClass(key->GetClassName());
20 if (!cl->InheritsFrom("TH1")) continue;
21 TH1 *h = (TH1*)key->ReadObj();
22 h->Draw();
23 c1.Print("hsimple.ps");
24 }
25 c1.Print("hsimple.ps]");
#define h(i)
Definition: RSha256.hxx:106
#define gROOT
Definition: TROOT.h:406
The Canvas class.
Definition: TCanvas.h:23
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:81
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4863
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:4053
TH1 is the base class of all histogram classes in ROOT.
Definition: TH1.h:58
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:28
virtual const char * GetClassName() const
Definition: TKey.h:75
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:750
return c1
Definition: legend1.C:41
TF1 * f1
Definition: legend1.C:11