To fill a histogram by reading text file

I had tried that also, then the histogram is empty…

With this code:

{
   TH1F *h = new TH1F("h", "example histogram", 205, 0., 205.);
   ifstream inp; int x;

   inp.open("try.txt");
   for (int i=1; i<=205; i++) {
      char c;
      inp >> x >> c;
      std::cout << "i = " << i << " x = " << x << " c = " << c << "\n";
      h->SetBinContent(i,x);
   }
   h->Draw();
}

It gives:

C:\Users\bellenot\Downloads>root -l try_example.C
root [0]
Processing try_example.C...
i = 1 x = 0 c = ,
i = 2 x = 0 c = ,
i = 3 x = 0 c = ,
i = 4 x = 0 c = ,
i = 5 x = 0 c = ,
i = 6 x = 0 c = ,
i = 7 x = 0 c = ,
i = 8 x = 0 c = ,
i = 9 x = 0 c = ,
i = 10 x = 0 c = ,
i = 11 x = 0 c = ,
i = 12 x = 0 c = ,
i = 13 x = 0 c = ,
i = 14 x = 0 c = ,
i = 15 x = 0 c = ,
i = 16 x = 0 c = ,
i = 17 x = 0 c = ,
i = 18 x = 0 c = ,
i = 19 x = 0 c = ,
i = 20 x = 0 c = ,
i = 21 x = 0 c = ,
i = 22 x = 0 c = ,
i = 23 x = 0 c = ,
i = 24 x = 0 c = ,
i = 25 x = 0 c = ,
i = 26 x = 0 c = ,
i = 27 x = 0 c = ,
i = 28 x = 0 c = ,
i = 29 x = 0 c = ,
i = 30 x = 0 c = ,
i = 31 x = 0 c = ,
i = 32 x = 0 c = ,
i = 33 x = 0 c = ,
i = 34 x = 0 c = ,
i = 35 x = 0 c = ,
i = 36 x = 0 c = ,
i = 37 x = 0 c = ,
i = 38 x = 0 c = ,
i = 39 x = 0 c = ,
i = 40 x = 0 c = ,
i = 41 x = 0 c = ,
i = 42 x = 0 c = ,
i = 43 x = 0 c = ,
i = 44 x = 0 c = ,
i = 45 x = 0 c = ,
i = 46 x = 0 c = ,
i = 47 x = 0 c = ,
i = 48 x = 0 c = ,
i = 49 x = 0 c = ,
i = 50 x = 0 c = ,
i = 51 x = 0 c = ,
i = 52 x = 0 c = ,
i = 53 x = 0 c = ,
i = 54 x = 0 c = ,
i = 55 x = 0 c = ,
i = 56 x = 0 c = ,
i = 57 x = 0 c = ,
i = 58 x = 0 c = ,
i = 59 x = 0 c = ,
i = 60 x = 0 c = ,
i = 61 x = 0 c = ,
i = 62 x = 0 c = ,
i = 63 x = 0 c = ,
i = 64 x = 0 c = ,
i = 65 x = 0 c = ,
i = 66 x = 0 c = ,
i = 67 x = 0 c = ,
i = 68 x = 0 c = ,
i = 69 x = 0 c = ,
i = 70 x = 0 c = ,
i = 71 x = 0 c = ,
i = 72 x = 0 c = ,
i = 73 x = 0 c = ,
i = 74 x = 0 c = ,
i = 75 x = 0 c = ,
i = 76 x = 0 c = ,
i = 77 x = 0 c = ,
i = 78 x = 0 c = ,
i = 79 x = 0 c = ,
i = 80 x = 0 c = ,
i = 81 x = 0 c = ,
i = 82 x = 0 c = ,
i = 83 x = 0 c = ,
i = 84 x = 0 c = ,
i = 85 x = 0 c = ,
i = 86 x = 0 c = ,
i = 87 x = 0 c = ,
i = 88 x = 0 c = ,
i = 89 x = 0 c = ,
i = 90 x = 0 c = ,
i = 91 x = 0 c = ,
i = 92 x = 0 c = ,
i = 93 x = 0 c = ,
i = 94 x = 0 c = ,
i = 95 x = 0 c = ,
i = 96 x = 0 c = ,
i = 97 x = 0 c = ,
i = 98 x = 0 c = ,
i = 99 x = 0 c = ,
i = 100 x = 0 c = ,
i = 101 x = 0 c = ,
i = 102 x = 0 c = ,
i = 103 x = 0 c = ,
i = 104 x = 0 c = ,
i = 105 x = 0 c = ,
i = 106 x = 0 c = ,
i = 107 x = 0 c = ,
i = 108 x = 0 c = ,
i = 109 x = 0 c = ,
i = 110 x = 0 c = ,
i = 111 x = 0 c = ,
i = 112 x = 0 c = ,
i = 113 x = 0 c = ,
i = 114 x = 1 c = ,
i = 115 x = 0 c = ,
i = 116 x = 2 c = ,
i = 117 x = 2 c = ,
i = 118 x = 1 c = ,
i = 119 x = 2 c = ,
i = 120 x = 4 c = ,
i = 121 x = 3 c = ,
i = 122 x = 8 c = ,
i = 123 x = 5 c = ,
i = 124 x = 12 c = ,
i = 125 x = 9 c = ,
i = 126 x = 9 c = ,
i = 127 x = 13 c = ,
i = 128 x = 19 c = ,
i = 129 x = 15 c = ,
i = 130 x = 39 c = ,
i = 131 x = 35 c = ,
i = 132 x = 36 c = ,
i = 133 x = 41 c = ,
i = 134 x = 66 c = ,
i = 135 x = 65 c = ,
i = 136 x = 111 c = ,
i = 137 x = 111 c = ,
i = 138 x = 136 c = ,
i = 139 x = 169 c = ,
i = 140 x = 178 c = ,
i = 141 x = 203 c = ,
i = 142 x = 220 c = ,
i = 143 x = 313 c = ,
i = 144 x = 339 c = ,
i = 145 x = 407 c = ,
i = 146 x = 501 c = ,
i = 147 x = 498 c = ,
i = 148 x = 568 c = ,
i = 149 x = 659 c = ,
i = 150 x = 786 c = ,
i = 151 x = 800 c = ,
i = 152 x = 914 c = ,
i = 153 x = 1010 c = ,
i = 154 x = 1166 c = ,
i = 155 x = 1321 c = ,
i = 156 x = 1485 c = ,
i = 157 x = 1578 c = ,
i = 158 x = 1731 c = ,
i = 159 x = 1926 c = ,
i = 160 x = 2117 c = ,
i = 161 x = 2328 c = ,
i = 162 x = 2354 c = ,
i = 163 x = 2515 c = ,
i = 164 x = 2784 c = ,
i = 165 x = 2884 c = ,
i = 166 x = 3041 c = ,
i = 167 x = 3187 c = ,
i = 168 x = 3342 c = ,
i = 169 x = 3529 c = ,
i = 170 x = 3576 c = ,
i = 171 x = 3620 c = ,
i = 172 x = 3580 c = ,
i = 173 x = 3578 c = ,
i = 174 x = 3579 c = ,
i = 175 x = 3493 c = ,
i = 176 x = 3428 c = ,
i = 177 x = 3340 c = ,
i = 178 x = 3159 c = ,
i = 179 x = 2948 c = ,
i = 180 x = 2688 c = ,
i = 181 x = 2556 c = ,
i = 182 x = 2327 c = ,
i = 183 x = 2092 c = ,
i = 184 x = 1733 c = ,
i = 185 x = 1646 c = ,
i = 186 x = 1453 c = ,
i = 187 x = 1181 c = ,
i = 188 x = 991 c = ,
i = 189 x = 816 c = ,
i = 190 x = 700 c = ,
i = 191 x = 520 c = ,
i = 192 x = 433 c = ,
i = 193 x = 289 c = ,
i = 194 x = 211 c = ,
i = 195 x = 157 c = ,
i = 196 x = 87 c = ,
i = 197 x = 76 c = ,
i = 198 x = 57 c = ,
i = 199 x = 23 c = ,
i = 200 x = 27 c = ,
i = 201 x = 6 c = ,
i = 202 x = 5 c = ,
i = 203 x = 2 c = ,
i = 204 x = 0 c = ,
i = 205 x = 0 c = ,
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1]

and this histogram:

Thanks @bellenot and @Wile_E_Coyote .

It works now.

1 Like