Skip to content

Instantly share code, notes, and snippets.

@alejolp
Last active November 15, 2016 15:43
Show Gist options
  • Save alejolp/c385fde5717120a3ad614ce18c9a2abd to your computer and use it in GitHub Desktop.
Save alejolp/c385fde5717120a3ad614ce18c9a2abd to your computer and use it in GitHub Desktop.
List all the files inside a ROOT file and dump an Histogram or object
/*
* List the files inside a ROOT file and print the specific histogram or object.
*
* By Alejandro Santos
*
* Build with: g++ dump_root_hist.cpp -Wall -g $(root-config --cflags --glibs)
*
*/
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <TROOT.h>
#include <TSystem.h>
#include <TStyle.h>
#include <TFile.h>
#include <TCanvas.h>
#include <TGraph.h>
#include <TGraph2D.h>
#include <TH2F.h>
#include <TTree.h>
#include <TApplication.h>
#include <TKey.h>
using namespace std;
void loopdir(TDirectoryFile* f1, string indent = "") {
TIter next(f1->GetListOfKeys());
TKey *key;
while ((key = (TKey*)next())) {
cout << indent << key->GetName() << endl;
TClass *cl = gROOT->GetClass(key->GetClassName());
if (!cl->InheritsFrom("TDirectoryFile")) continue;
loopdir(dynamic_cast<TDirectoryFile*>(key->ReadObj()), indent + key->GetName() + "/");
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Need to specify the filename" << endl;
return 1;
}
TFile* f = new TFile(argv[1]);
if (argc < 3) {
cout << "Need to speficy the histogram path. The files are:" << endl;
f->ls();
loopdir(f);
return 1;
}
TTree* tr = (TTree*)f->Get(argv[2]);
tr->Print("all");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment