# How to calculate the standard deviation over a list of histograms existing in one root file?

Hello, I have a list of histograms in a single root file. I want to compute the standard deviation on a particular bin of these histograms. Could you please guide me how to proceed?

I guess I need to loop over all the histograms and then I can compute the standard deviation on that bin.

``````void anal_hist_result(string ttbarMC="ppepembbar_4FNS_LO_all_results"){
TFile *file        = TFile::Open("ppepembbar_4FNS_LO_all_results.root");

TH1F *h0 = (TH1F*)file->Get("AFB_Mll_0");
TH1F *h1 = (TH1F*)file->Get("AFB_Mll_1");
TH1F *h2 = (TH1F*)file->Get("AFB_Mll_2");
TH1F *h3 = (TH1F*)file->Get("AFB_Mll_3");
TH1F *h4 = (TH1F*)file->Get("AFB_Mll_4");
TH1F *h5 = (TH1F*)file->Get("AFB_Mll_5");
TH1F *h6 = (TH1F*)file->Get("AFB_Mll_6");
TH1F *h7 = (TH1F*)file->Get("AFB_Mll_7");
TH1F *h8 = (TH1F*)file->Get("AFB_Mll_8");
TH1F *h9 = (TH1F*)file->Get("AFB_Mll_9");
TH1F *h10 = (TH1F*)file->Get("AFB_Mll_10");
TH1F *h11 = (TH1F*)file->Get("AFB_Mll_11");
TH1F *h12 = (TH1F*)file->Get("AFB_Mll_12");
TH1F *h13 = (TH1F*)file->Get("AFB_Mll_13");
TH1F *h14 = (TH1F*)file->Get("AFB_Mll_14");
TH1F *h15 = (TH1F*)file->Get("AFB_Mll_15");
TH1F *h16 = (TH1F*)file->Get("AFB_Mll_16");
TH1F *h17 = (TH1F*)file->Get("AFB_Mll_17");
TH1F *h18 = (TH1F*)file->Get("AFB_Mll_18");
TH1F *h19 = (TH1F*)file->Get("AFB_Mll_19");
TH1F *h20 = (TH1F*)file->Get("AFB_Mll_20");
TH1F *h21 = (TH1F*)file->Get("AFB_Mll_21");
TH1F *h22 = (TH1F*)file->Get("AFB_Mll_22");
TH1F *h23 = (TH1F*)file->Get("AFB_Mll_23");
TH1F *h24 = (TH1F*)file->Get("AFB_Mll_24");
TH1F *h25 = (TH1F*)file->Get("AFB_Mll_25");
TH1F *h26 = (TH1F*)file->Get("AFB_Mll_26");
TH1F *h27 = (TH1F*)file->Get("AFB_Mll_27");
TH1F *h28 = (TH1F*)file->Get("AFB_Mll_28");
TH1F *h29 = (TH1F*)file->Get("AFB_Mll_29");
TH1F *h30 = (TH1F*)file->Get("AFB_Mll_30");
TH1F *h31 = (TH1F*)file->Get("AFB_Mll_31");
TH1F *h32 = (TH1F*)file->Get("AFB_Mll_32");
TH1F *h33 = (TH1F*)file->Get("AFB_Mll_33");
TH1F *h34 = (TH1F*)file->Get("AFB_Mll_34");
TH1F *h35 = (TH1F*)file->Get("AFB_Mll_35");
TH1F *h36 = (TH1F*)file->Get("AFB_Mll_36");
TH1F *h37 = (TH1F*)file->Get("AFB_Mll_37");
TH1F *h38 = (TH1F*)file->Get("AFB_Mll_38");
TH1F *h39 = (TH1F*)file->Get("AFB_Mll_39");
TH1F *h40 = (TH1F*)file->Get("AFB_Mll_40");
TH1F *h41 = (TH1F*)file->Get("AFB_Mll_41");
TH1F *h42 = (TH1F*)file->Get("AFB_Mll_42");
TH1F *h43 = (TH1F*)file->Get("AFB_Mll_43");
//TH1F *h44 = (TH1F*)file->Get("AFB_Mll_44");
TH1F *h45 = (TH1F*)file->Get("AFB_Mll_45");
TH1F *h46 = (TH1F*)file->Get("AFB_Mll_46");
TH1F *h47 = (TH1F*)file->Get("AFB_Mll_47");
TH1F *h48 = (TH1F*)file->Get("AFB_Mll_48");
TH1F *h49 = (TH1F*)file->Get("AFB_Mll_49");
TH1F *h50 = (TH1F*)file->Get("AFB_Mll_50");
TH1F *h51 = (TH1F*)file->Get("AFB_Mll_51");
TH1F *h52 = (TH1F*)file->Get("AFB_Mll_52");
TH1F *h53 = (TH1F*)file->Get("AFB_Mll_53");
TH1F *h54 = (TH1F*)file->Get("AFB_Mll_54");
TH1F *h55 = (TH1F*)file->Get("AFB_Mll_55");
TH1F *h56 = (TH1F*)file->Get("AFB_Mll_56");
TH1F *h57 = (TH1F*)file->Get("AFB_Mll_57");
TH1F *h58 = (TH1F*)file->Get("AFB_Mll_58");
TH1F *h59 = (TH1F*)file->Get("AFB_Mll_59");
TH1F *h60 = (TH1F*)file->Get("AFB_Mll_60");
TH1F *h61 = (TH1F*)file->Get("AFB_Mll_61");
TH1F *h62 = (TH1F*)file->Get("AFB_Mll_62");
TH1F *h63 = (TH1F*)file->Get("AFB_Mll_63");
TH1F *h64 = (TH1F*)file->Get("AFB_Mll_64");
TH1F *h65 = (TH1F*)file->Get("AFB_Mll_65");
TH1F *h66 = (TH1F*)file->Get("AFB_Mll_66");
TH1F *h67 = (TH1F*)file->Get("AFB_Mll_67");
TH1F *h68 = (TH1F*)file->Get("AFB_Mll_68");
TH1F *h69 = (TH1F*)file->Get("AFB_Mll_69");
TH1F *h70 = (TH1F*)file->Get("AFB_Mll_70");
TH1F *h71 = (TH1F*)file->Get("AFB_Mll_71");
TH1F *h72 = (TH1F*)file->Get("AFB_Mll_72");
TH1F *h73 = (TH1F*)file->Get("AFB_Mll_73");
TH1F *h74 = (TH1F*)file->Get("AFB_Mll_74");
TH1F *h75 = (TH1F*)file->Get("AFB_Mll_75");
TH1F *h76 = (TH1F*)file->Get("AFB_Mll_76");
TH1F *h77 = (TH1F*)file->Get("AFB_Mll_77");
TH1F *h78 = (TH1F*)file->Get("AFB_Mll_78");
TH1F *h79 = (TH1F*)file->Get("AFB_Mll_79");
TH1F *h80 = (TH1F*)file->Get("AFB_Mll_80");
TH1F *h81 = (TH1F*)file->Get("AFB_Mll_81");
TH1F *h82 = (TH1F*)file->Get("AFB_Mll_82");
TH1F *h83 = (TH1F*)file->Get("AFB_Mll_83");
TH1F *h84 = (TH1F*)file->Get("AFB_Mll_84");
TH1F *h85 = (TH1F*)file->Get("AFB_Mll_85");
TH1F *h86 = (TH1F*)file->Get("AFB_Mll_86");
TH1F *h87 = (TH1F*)file->Get("AFB_Mll_87");
TH1F *h88 = (TH1F*)file->Get("AFB_Mll_88");
TH1F *h89 = (TH1F*)file->Get("AFB_Mll_89");
TH1F *h90 = (TH1F*)file->Get("AFB_Mll_90");
TH1F *h91 = (TH1F*)file->Get("AFB_Mll_91");
TH1F *h92 = (TH1F*)file->Get("AFB_Mll_92");
TH1F *h93 = (TH1F*)file->Get("AFB_Mll_93");
TH1F *h94 = (TH1F*)file->Get("AFB_Mll_94");
TH1F *h95 = (TH1F*)file->Get("AFB_Mll_95");
TH1F *h96 = (TH1F*)file->Get("AFB_Mll_96");
TH1F *h97 = (TH1F*)file->Get("AFB_Mll_97");
TH1F *h98 = (TH1F*)file->Get("AFB_Mll_98");
TH1F *h99 = (TH1F*)file->Get("AFB_Mll_99");
TH1F *h100 = (TH1F*)file->Get("AFB_Mll_100");
TH1F *h101 = (TH1F*)file->Get("AFB_Mll_101");
TH1F *h102 = (TH1F*)file->Get("AFB_Mll_102");
TH1F *h103 = (TH1F*)file->Get("AFB_Mll_103");
TH1F *h104 = (TH1F*)file->Get("AFB_Mll_104");
TH1F *h105 = (TH1F*)file->Get("AFB_Mll_105");
TH1F *h106 = (TH1F*)file->Get("AFB_Mll_106");
TH1F *h107 = (TH1F*)file->Get("AFB_Mll_107");
TH1F *h108 = (TH1F*)file->Get("AFB_Mll_108");
TH1F *h109 = (TH1F*)file->Get("AFB_Mll_109");
TH1F *h110 = (TH1F*)file->Get("AFB_Mll_110");
TH1F *h111 = (TH1F*)file->Get("AFB_Mll_111");
TH1F *h112 = (TH1F*)file->Get("AFB_Mll_112");
TH1F *h113 = (TH1F*)file->Get("AFB_Mll_113");
TH1F *h114 = (TH1F*)file->Get("AFB_Mll_114");
TH1F *h115 = (TH1F*)file->Get("AFB_Mll_115");
TH1F *h116 = (TH1F*)file->Get("AFB_Mll_116");
TH1F *h117 = (TH1F*)file->Get("AFB_Mll_117");
TH1F *h118 = (TH1F*)file->Get("AFB_Mll_118");
TH1F *h119 = (TH1F*)file->Get("AFB_Mll_119");
TH1F *h120 = (TH1F*)file->Get("AFB_Mll_120");
TH1F *h121 = (TH1F*)file->Get("AFB_Mll_121");
TH1F *h122 = (TH1F*)file->Get("AFB_Mll_122");
TH1F *h123 = (TH1F*)file->Get("AFB_Mll_123");
TH1F *h124 = (TH1F*)file->Get("AFB_Mll_124");
TH1F *h125 = (TH1F*)file->Get("AFB_Mll_125");
TH1F *h126 = (TH1F*)file->Get("AFB_Mll_126");
TH1F *h127 = (TH1F*)file->Get("AFB_Mll_127");
TH1F *h128 = (TH1F*)file->Get("AFB_Mll_128");
TH1F *h129 = (TH1F*)file->Get("AFB_Mll_129");
TH1F *h130 = (TH1F*)file->Get("AFB_Mll_130");
TH1F *h131 = (TH1F*)file->Get("AFB_Mll_131");
TH1F *h132 = (TH1F*)file->Get("AFB_Mll_132");
TH1F *h133 = (TH1F*)file->Get("AFB_Mll_133");
TH1F *h134 = (TH1F*)file->Get("AFB_Mll_134");
TH1F *h135 = (TH1F*)file->Get("AFB_Mll_135");
TH1F *h136 = (TH1F*)file->Get("AFB_Mll_136");
TH1F *h137 = (TH1F*)file->Get("AFB_Mll_137");
TH1F *h138 = (TH1F*)file->Get("AFB_Mll_138");
TH1F *h139 = (TH1F*)file->Get("AFB_Mll_139");
TH1F *h140 = (TH1F*)file->Get("AFB_Mll_140");
TH1F *h141 = (TH1F*)file->Get("AFB_Mll_141");
TH1F *h142 = (TH1F*)file->Get("AFB_Mll_142");
TH1F *h143 = (TH1F*)file->Get("AFB_Mll_143");
TH1F *h144 = (TH1F*)file->Get("AFB_Mll_144");

for (auto&& keyAsObj : file->GetListOfKeys()){
auto key = (TKey*) keyAsObj;
cout << key->GetName() << " " << key->GetClassName() << endl;

}
}
``````

I get this error when I run this code:
error: invalid range expression of type ‘TList '; did you mean to dereference it with '’?
for (auto&& keyAsObj : file->GetListOfKeys()){
^
*

Attach the “ppepembbar_4FNS_LO_all_results.root” file.

The root file is on the farm and I do not have the permission to download it. But it contains all the histograms “AFB_Mll_num” I indicated in the code above. If you could guide me on what steps I should follow to obtain the standard deviation of all these histograms on that particular bin that would be a great help.

Thank you for you help.

I tried this, I got an empty histogram and this error: Error in TProfile::Add: Attempt to add a non-profile object.

void hist(){

TFile *f = TFile::Open(“ppepembbar_4FNS_LO_all_results.root”);
if ((!f) || f->IsZombie()) { delete f; return; }
gROOT->cd();
TProfile *hp = 0;
TH1 *h;
for (int i = 0; i < 145; i++) {
f->GetObject(TString::Format(“AFB_Mll_%d”, i), h);
if (h) {
if (!hp) {
TAxis *a = h->GetXaxis();
hp = new TProfile(“AFB_Mll_profile”, “AFB Mll profile;X;Y”,
a->GetNbins(), a->GetXmin(), a->GetXmax());
if (! hp->GetSumw2N()) hp->Sumw2(kTRUE);
}
delete h;
}
}
delete f;
if (hp) hp->Draw();
}

Since I have 144 histograms, I changed 199 to 145 in the argument of for loop.

Try:

``````{
TFile *f = TFile::Open("ppepembbar_4FNS_LO_all_results.root");
if ((!f) || f->IsZombie()) { delete f; return; }
gROOT->cd();
TProfile *hp = 0;
for (int i = 0; i < 199; i++) {
TH1 *h; f->GetObject(TString::Format("AFB_Mll_%d", i), h);
if (h) {
TAxis *a = h->GetXaxis();
if (!hp) {
// ... assuming all "AFB_Mll_*" are "akin" fix bin sizes histograms ...
hp = new TProfile("AFB_Mll_profile", "AFB Mll profile;X;Y",
a->GetNbins(), a->GetXmin(), a->GetXmax(),
""); // e.g., "", "s", "i" or "g"
if (! hp->GetSumw2N()) hp->Sumw2(kTRUE);
}
for (int j = 0; j <= a->GetNbins() + 1; j++)
hp->Fill(a->GetBinCenter(j), h->GetBinContent(j), h->GetBinError(j));
delete h;
}
}
delete f;
if (hp) hp->Draw();
}
``````
2 Likes

Thank you very much it worked fine. Now if I want to compute the standard deviation on bin 6 then I delete the for loop:

void hist(string ttbarMC=“ppepembbar_4FNS_LO_all_results”){
TFile *f = TFile::Open(“ppepembbar_4FNS_LO_all_results.root”);
if ((!f) || f->IsZombie()) { delete f; return; }
gROOT->cd();
TProfile *hp = 0;
TH1 *h;
for (int i = 0; i < 199; i++) {
f->GetObject(TString::Format(“AFB_Mll_%d”, i), h);
if (h) {
TAxis *a = h->GetXaxis();
if (!hp) {
hp = new TProfile(“AFB_Mll_profile”, “AFB Mll profile;X;Y”,
a->GetNbins(), a->GetXmin(), a->GetXmax());
if (! hp->GetSumw2N()) hp->Sumw2(kTRUE);
}

``````    hp->Fill(a->GetBinCenter(6), h->GetBinContent(6), h->GetBinError(6));
delete h;
}
``````

}
delete f;
if (hp) hp->Draw();
}

From the TProfile description, I assume you want to retrieve its bin 6 (e.g., contents and error).

Yes, I want to retrieve its bin 6. The way I made changes in the code you wrote is correct to compute the standard deviation on bin 6?

It makes no sense to create and fill a TProfile just to get a single bin. You can loop over all TH1 histograms and calculate this single value yourself (e.g., from “`h->GetBinContent(6)`” and “`h->GetBinError(6)`”).

Yeah but I could not loop over all these histograms. Can you show me how to loop over all histograms?

My “example” macro loops over all “`AFB_Mll_*`” histograms.

``````{
TFile *f = TFile::Open("ppepembbar_4FNS_LO_all_results.root");
if ((!f) || f->IsZombie()) { delete f; return; }
for (int i = 0; i < 199; i++) {
TH1 *h; f->GetObject(TString::Format("AFB_Mll_%d", i), h);
if (h) {
std::cout << i << " " << h->GetBinContent(6) << " " << h->GetBinError(6) << std::endl;
delete h;
}
}
delete f;
}
``````
1 Like

Thanks a lot for you help. In both cases, I obtained the same value for the standard deviation.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.