Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
loopdir11.C
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
7///
8/// \macro_code
9///
10/// \author Rene Brun
11
12void loopdir11() {
13 TFile *f1 = TFile::Open("hsimple.root");
14 TCanvas c1;
15 c1.Print("hsimple11.ps[");
16 for(auto k : *f1->GetListOfKeys()) {
17 TKey *key = static_cast<TKey*>(k);
18 TClass *cl = gROOT->GetClass(key->GetClassName());
19 if (!cl->InheritsFrom("TH1")) continue;
20 TH1 *h = key->ReadObject<TH1>();
21 h->Draw();
22 c1.Print("hsimple11.ps");
23 }
24 c1.Print("hsimple11.ps]");
25}
#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:4874
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2968
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4082
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
T * ReadObject()
To read an object (non deriving from TObject) from the file.
Definition TKey.h:103
virtual const char * GetClassName() const
Definition TKey.h:75
void Print(const char *filename="") const override
This method is equivalent to SaveAs("filename"). See TPad::SaveAs for details.
Definition TPad.cxx:4700
return c1
Definition legend1.C:41
TF1 * f1
Definition legend1.C:11
UInt_t GetListOfKeys(TList &keys, TString inherits, TDirectory *dir=nullptr)
Definition tmvaglob.cxx:382