Skip to content

Instantly share code, notes, and snippets.

@kratsg
Created February 13, 2015 17:40
Show Gist options
  • Save kratsg/11ab8356229088d19d09 to your computer and use it in GitHub Desktop.
Save kratsg/11ab8356229088d19d09 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <fstream>
#include "TFile.h"
#include "TLine.h"
#include "TEllipse.h"
#include "TGraphErrors.h"
#include "TGaxis.h"
#include "TGraphAsymmErrors.h"
#include "TCanvas.h"
#include "TFrame.h"
#include "TPad.h"
#include "TF1.h"
#include "TH1.h"
#include "TH2.h"
#include "TPolyLine.h"
#include "TProfile.h"
#include "TLegend.h"
#include "TLegendEntry.h"
#include "TLatex.h"
#include "TPaveStats.h"
#include "TStyle.h"
#include "TROOT.h"
#include "TFile.h"
#include "TMath.h"
#include "THStack.h"
#include "TTree.h"
#include "TBranch.h"
#include "TSystem.h"
void drawAllBranches(TString name = "tree.root", TString tree = "CollectionTree") {
TFile *f = new TFile(name);
TTree *t = (TTree*)f->Get(tree);
cout << t->GetEntries() << endl;
// -- Get list of branches for the tree
TObjArray *o = t->GetListOfBranches();
int m = o->GetEntries();
cout << "Number of branches: " << m << endl;
TCanvas *canvas = (TCanvas*)gROOT->FindObject("c1");
if (canvas) {canvas->Clear();}
else {
canvas = new TCanvas("c1","c1");
}
canvas->Print(TString(tree + ".ps["));
// -- Loop over all, and draw their variables into TCanvas c1
for (int i = 0; i < m; ++i) {
t->Draw(((TBranch*)(*o)[i])->GetName());
TH1F *h1 = (TH1F*)gPad->GetPrimitive("htemp");
h1->SetStats(kTRUE);
if (h1->GetEntries() == 0) canvas->SetFillColor(kRed);
else if (h1->GetMean() != 0 && h1->GetRMS() == 0) canvas->SetFillColor(kYellow);
else canvas->SetFillColor(0);
canvas->Update();
canvas->Print(TString(tree + ".ps"));
}
canvas->Print(TString(tree + ".ps]"));
return;
}
#include <vector>
#include <iostream>
#include <fstream>
#include "TFile.h"
#include "TLine.h"
#include "TEllipse.h"
#include "TGraphErrors.h"
#include "TGaxis.h"
#include "TGraphAsymmErrors.h"
#include "TCanvas.h"
#include "TFrame.h"
#include "TPad.h"
#include "TF1.h"
#include "TKey.h"
#include "TH1.h"
#include "TH2.h"
#include "TPolyLine.h"
#include "TProfile.h"
#include "TLegend.h"
#include "TLegendEntry.h"
#include "TLatex.h"
#include "TPaveStats.h"
#include "TStyle.h"
#include "TROOT.h"
#include "TFile.h"
#include "TMath.h"
#include "THStack.h"
#include "TTree.h"
#include "TBranch.h"
#include "TSystem.h"
#include "TIterator.h"
#include "Util.cpp"
using namespace Utils;
void drawAllHistograms(TString name = "JetTrackAnalysis_subStructure_pythia_v08.root",
TString out = "JetTrackAnalysis_subStructure_pythia_v08_TEST",
TString tag = ""
)
{
TFile *f = new TFile(name,"READ");
f->cd();
cout << "Keys in file: N = " << f->GetListOfKeys()->GetSize() << endl;
//f->GetListOfKeys()->Print();
TH1 *h1;
TH2 *h2;
TCanvas *canvas = (TCanvas*)gROOT->FindObject("c1");
if (canvas) {canvas->Clear();}
else canvas = new TCanvas("c1","c1");
canvas->Print(TString(out + ".ps["));
TKey *key(0);
TIter nextkey(f->GetListOfKeys());
while ((key = (TKey*)nextkey())) {
TObject *obj = key->ReadObj();
if (tag != "" && !(TString(obj->GetName()).Contains(tag))) continue;
//cout << "Found: " << obj->GetName() << " (" << obj->IsA() << ") = " << obj->ClassName() << endl;
// 1-D Histograms
if (obj->InheritsFrom(TH2::Class())) {
h2 = (TH2*)obj;
//cout << h2->GetName() << endl;
//h2->Draw("colz");
Plot2DPlusProfile(h2,"","",0,0,0);
gPad->SetLogy(kFALSE);
if (h2->GetMaximum()/h2->GetMinimum() > 5E2) gPad->SetLogz(kTRUE);
else gPad->SetLogz(kFALSE);
canvas->SetRightMargin(0.15);
myText(0.2,0.95,tag.Data());
canvas->Modified();
canvas->Update();
canvas->Print(TString(out + ".ps"));
}
// 2-D Histograms
else if (obj->InheritsFrom(TH1::Class())) {
h1 = (TH1*)obj;
//cout << h1->GetName() << endl;
h1->Draw();
if (h1->GetMaximum()/h1->GetMinimum() > 5E2) gPad->SetLogy(kTRUE);
else gPad->SetLogy(kFALSE);
canvas->SetRightMargin(0.08);
myText(0.2,0.95,tag.Data());
canvas->Modified();
canvas->Update();
canvas->Print(TString(out + ".ps"));
}
}
canvas->Print(TString(out + ".ps]"));
return;
}
#include "TROOT.h"
#include "TChain.h"
#include "TFile.h"
#include "TBranch.h"
#include "TObject.h"
#include "TObjArray.h"
#include "TCanvas.h"
#include "TString.h"
#include "TH1.h"
#include "TH2.h"
#include "TF1.h"
#include "TArrow.h"
#include "TLine.h"
#include "TLatex.h"
#include "TStyle.h"
#include "TMarker.h"
#include "TFormula.h"
#include "TGaxis.h"
#include "TGraphErrors.h"
#include "TMath.h"
#include "TDatime.h"
#include "TLegend.h"
#include "TKey.h"
#include "TObjArray.h"
#include "TPie.h"
#include <vector>
#include <string>
#include <iostream>
#include <map>
#ifdef __MAKECINT__
#pragma link C++ class map<int,string>;
#pragma link C++ class vector< vector<float> >;
#pragma link C++ class vector<TString>;
#endif
void dumpTree(TString fileName, TString treeName = "CollectionTree", TString branchType = "", bool print = false)
{
/// Get tree and branches
TFile *f = new TFile(fileName,"READ");
TTree *t = (TTree*)f->Get(treeName);
TObjArray *branches = (TObjArray*)t->GetListOfBranches()->Clone();
branches->SetOwner(kFALSE);
branches->Sort();
// branches->Print();
/// Print information
printf("\n");
if (branchType != "") {
printf("Only looking at branches of type: %s \n",branchType.Data());
}
printf("File: %s \n",t->GetCurrentFile()->GetName());
printf(" (%i entries) \n",(int) t->GetEntries());
printf("\n");
printf(" Container File Size (kB) Size/evt \n");
printf(" --------------- ------------------ ------------\n");
/// Save branch groups
int nBytes = 0;
vector<TString> branchGroups;
vector<int> branchFraction;
TBranch *branch(0),*branch2(0),*branch3(0);
TObjArray *array2(0),*array3(0);
TString name = "";
int index = 0;
int oldindex = 0;
double totalSize = 0.0;
for (int i = 0; i < branches->GetEntries(); ++i) {
TString thisBranchName = TString(branches->At(i)->GetName());
if (branchType != "" && !thisBranchName.Contains(branchType)) continue;
branch = (TBranch*)branches->At(i);
totalSize += (double)branch->GetZipBytes("*");
}
for (int i = 0; i < branches->GetEntries(); ++i) {
/// DEBUG
TString thisBranchName = TString(branches->At(i)->GetName());
if (print) cout << "Branch name: " << thisBranchName << endl;
if (branchType != "" && !thisBranchName.Contains(branchType)) { if (print) cout << "Skipping!" << endl; continue; }
/// Take Group Name
TString oldName = name;
name = TString(branches->At(i)->GetName());
if (branchType != "" && branchType.Contains(branchType)) name.Replace(0,branchType.Length(),"");
if (name.Contains("_")) name.Remove(name.First("_"));
/// Look at list
bool saved = false;
for (UInt_t j=0; j<branchGroups.size(); j++) {
if (name == branchGroups.at(j)) {
saved = true;
oldindex = index;
index = j;
}
}
/// If we have not saved it, save it
if (!saved) {
if (print) cout << "Branch has not been saved" << endl;
if (branchFraction.size() > 0) {
if (print) cout << "But others have been! Printing out the info for those branches" << endl;
if(branchFraction.at(oldindex)) printf(" %20s %11f (%4.1f%%) %6.1f kB\n",
oldName.Data(),branchFraction.at(oldindex)/1000.,
branchFraction.at(oldindex)*100./totalSize,
(float)branchFraction.at(oldindex)/(float)branch->GetEntries()/1000. );
}
branch = (TBranch*)branches->At(i);
branchGroups.push_back(name);
int nBytesBranch2 = 0;
nBytesBranch2 += branch->GetZipBytes("*");
/*
/// Loop over inner branches
array2 = branch->GetListOfBranches();
if (array2->GetEntries() ==0) nBytesBranch2 = (int) branch->GetZipBytes();
else {
for(Int_t j=0;j<array2->GetEntries();j++) {
branch2 = (TBranch*) array2->At(j);
array3 = branch2->GetListOfBranches();
Int_t nBranch2 = array3->GetEntries();
if (nBranch2==0) nBytesBranch2 += (int) branch2->GetZipBytes();
else {
for(Int_t k=0;k<nBranch2;k++) {
branch3 = (TBranch*) array3->At(k);
nBytesBranch2 += (int) branch3->GetZipBytes();
}
}
}
}
*/
branchFraction.push_back(nBytesBranch2);
}
/// Otherwise increase the branch size
else {
if (print) cout << "Already saved: " << name << " = " << branch->GetZipBytes() << endl;
int nBytesBranchInner = branchFraction[index];
nBytesBranchInner += branch->GetZipBytes("*");
/*
/// Loop over inner branches
array2 = branch->GetListOfBranches();
if (array2->GetEntries() ==0) nBytesBranchInner += (int) branch->GetZipBytes();
else {
for(Int_t j=0;j<array2->GetEntries();j++) {
branch2 = (TBranch*) array2->At(j);
array3 = branch2->GetListOfBranches();
Int_t nBranch2 = array3->GetEntries();
if (nBranch2==0) nBytesBranchInner += (int) branch2->GetZipBytes();
else {
for(Int_t k=0;k<nBranch2;k++) {
branch3 = (TBranch*) array3->At(k);
nBytesBranchInner += (int) branch3->GetZipBytes();
}
}
}
}
*/
branchFraction[index] = nBytesBranchInner;
if (print) {
if(branchFraction.at(index)) printf(" %20s %11f (%4.1f%%) %6.1f bytes\n",
name.Data(),branchFraction.at(index)/1000.,
branchFraction.at(index)*100./totalSize,
(float)branchFraction.at(index)/(float)branch->GetEntries() );
}
}
}
const int npoints = (int)branchFraction.size();
const char *lbls[npoints];
double* values = new double[npoints];
for (UInt_t l=0; l<branchFraction.size(); l++) {
nBytes += branchFraction.at(l);
values[l] = branchFraction.at(l);
//if (values[l]/nBytes < 0.1) lbls[l] = "";
//else lbls[l] = branchGroups.at(l).Data();
lbls[l] = branchGroups.at(l).Data();
}
printf("------------------- ------------------ ------------\n");
printf(" Tot %6.3f kB/event %11f (%4.1f%%) %6.3f kB/event\n",
totalSize/t->GetEntries()/1000., nBytes/1000.,nBytes*100./totalSize, nBytes/t->GetEntries()/1000. );
printf("\n");
TPie *pie = new TPie("TTreeProfile","TTreeProfile",npoints,values,0,lbls);
pie->SetLabelFormat("#splitline{%txt}{(%perc)}");
pie->SetLabelFormat("%txt");
pie->SetRadius(0.2);
pie->Draw("3d");
pie->SetTextSize(0.03);
pie->SetCircle(0.4808696, 0.7940109, 0.2);
pie->SetValueFormat("%4.2f");
pie->SetLabelFormat("%txt");
pie->SetPercentFormat("%3.1f");
pie->SetLabelsOffset(0.005);
pie->SetAngularOffset(265.2655);
TLegend *leg = new TLegend(0.05580866,0.06554878,0.785877,0.4512195,NULL,"brNDC");
for (Int_t iSlice=0; iSlice < npoints; iSlice++) {
//cout << "values[iSlice]/nBytes = " << values[iSlice]/nBytes << endl;
if (values[iSlice]/nBytes > 0.05) {
pie->SetEntryRadiusOffset(iSlice,.07);
leg->AddEntry((TObject*)pie->GetSlice(iSlice),Form(TString(pie->GetEntryLabel(iSlice)) + ": %1.1lf%%",100.*(double)values[iSlice]/(double)nBytes),"f");
}
else pie->SetEntryLabel(iSlice,"");
}
leg->SetFillColor(0);
leg->SetFillStyle(0);
leg->Draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment