File indexing completed on 2023-03-17 11:09:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <string.h>
0018 #include "TChain.h"
0019 #include "TFile.h"
0020 #include "TH1.h"
0021 #include "TTree.h"
0022 #include "TKey.h"
0023 #include "Riostream.h"
0024
0025 TFile *sourceFile;
0026 TObject *obj;
0027 TString outputFolder;
0028 TH1 *h;
0029 TCanvas *canvasDefault;
0030
0031
0032
0033
0034 TString outputType;
0035 Int_t outputXsize;
0036 Int_t outputYsize;
0037
0038 TString drawOptions1D("");
0039
0040 TString drawOptions2D("box");
0041
0042
0043 int lineColor = 4;
0044 int lineWidth = 2;
0045
0046 Bool_t displayStatsBox = 1;
0047 Bool_t autoLabelXaxis = 1;
0048 Bool_t autoLogYaxis = 0;
0049 Bool_t printOutput = 1;
0050
0051
0052
0053
0054 void recurseOverKeys( TDirectory *target );
0055
0056 void saveHistograms(TString fileName,
0057 TString imageType = "gif",
0058 int outputWidth = 640,
0059 int outputHeight = 480) {
0060
0061 sourceFile = TFile::Open( fileName );
0062
0063 outputFolder = fileName+"/";
0064
0065
0066 outputFolder.ReplaceAll(".root","");
0067 gSystem->MakeDirectory(outputFolder);
0068
0069 outputType = "."+imageType;
0070 outputXsize = outputWidth;
0071 outputYsize = outputHeight;
0072 canvasDefault = new TCanvas("canvasDefault","testCanvas",outputWidth,outputHeight);
0073
0074
0075 gStyle->SetOptStat(111111);
0076 gStyle->SetHistLineWidth(2);
0077
0078
0079
0080 recurseOverKeys(sourceFile);
0081
0082 sourceFile->Close();
0083
0084 TString currentDir = gSystem->pwd();
0085 cout << "Done. See images in:" << endl << currentDir << "/" << outputFolder << endl;
0086 }
0087
0088 void recurseOverKeys( TDirectory *target ) {
0089
0090 TString path( (char*)strstr( target->GetPath(), ":" ) );
0091 path.Remove( 0, 2 );
0092
0093 cout << path << endl;
0094
0095 sourceFile->cd( path );
0096 TDirectory *current_sourcedir = gDirectory;
0097
0098 TKey *key;
0099 TIter nextkey(current_sourcedir->GetListOfKeys());
0100
0101 while (key = (TKey*)nextkey()) {
0102
0103 obj = key->ReadObj();
0104
0105 if (obj->IsA()->InheritsFrom("TH1")) {
0106
0107
0108
0109 h = (TH1*)obj;
0110 h->SetStats(displayStatsBox);
0111
0112 TString histName = h->GetName();
0113
0114
0115
0116
0117
0118
0119 if (autoLabelXaxis) {
0120 if ( histName.Contains("Phi") ) {
0121 h->GetXaxis()->SetTitle("#phi");
0122 } else if ( histName.Contains("eta") || histName.Contains("eta2") ) {
0123 h->GetXaxis()->SetTitle("#eta");
0124 } else if ( histName.Contains("Pt") ) {
0125 h->GetXaxis()->SetTitle("p_{T} (GeV)");
0126 } else if ( histName.Contains("et") || histName.Contains("et2") ) {
0127 h->GetXaxis()->SetTitle("E_{T} (GeV)");
0128 }
0129 }
0130
0131
0132
0133
0134 if (histName.Contains("total ") || histName.Contains("efficiency by step")) {
0135 canvasDefault->SetBottomMargin(0.24);
0136 canvasDefault->SetRightMargin(0.15);
0137 } else {
0138 canvasDefault->SetBottomMargin(0.1);
0139 canvasDefault->SetRightMargin(0.1);
0140 }
0141
0142 h->SetLineColor(lineColor);
0143 h->SetLineWidth(lineWidth);
0144
0145
0146
0147
0148
0149 if (autoLogYaxis) {
0150 Double_t testYvalue = h->GetMaximum();
0151
0152
0153 if (testYvalue > 1.0) {
0154 Double_t maxy = log10(testYvalue);
0155
0156
0157 Double_t miny = log10(h->GetMinimum(1.0));
0158
0159
0160 if ( (maxy-miny) > 2.0 ) {
0161 canvasDefault->SetLogy(1);
0162 }
0163 }
0164 }
0165
0166
0167
0168
0169
0170
0171 h->Draw(drawOptions1D);
0172 canvasDefault->Modified();
0173 canvasDefault->Update();
0174
0175 canvasDefault->Print(outputFolder+path+"/"+histName+outputType);
0176
0177
0178 if (printOutput) cout << outputFolder+path+"/"+histName+outputType << endl;
0179
0180 canvasDefault->SetLogy(0);
0181
0182
0183 } else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
0184
0185
0186 cout << "Found subdirectory " << obj->GetName() << endl;
0187 gSystem->MakeDirectory(outputFolder+path+"/"+obj->GetName());
0188
0189
0190
0191
0192 recurseOverKeys( (TDirectory*)obj );
0193
0194 }
0195 }
0196 }