| 1 | #include <TH1.h> | 
|---|
| 2 | #include <TCanvas.h> | 
|---|
| 3 | #include <TVector.h> | 
|---|
| 4 | #include <TMatrix.h> | 
|---|
| 5 | #include <iomanip.h> | 
|---|
| 6 |  | 
|---|
| 7 | void optPad() | 
|---|
| 8 | { | 
|---|
| 9 | Int_t N = 10; | 
|---|
| 10 | Double_t eps = 1.e-10; | 
|---|
| 11 |  | 
|---|
| 12 | TMatrix g(N, N); | 
|---|
| 13 | g.Zero(); | 
|---|
| 14 |  | 
|---|
| 15 | // hista is the normalized 1D histogram of sigmabar for ON data | 
|---|
| 16 | // histb is the normalized 1D histogram of sigmabar for OFF data | 
|---|
| 17 | // histc is the difference ON-OFF | 
|---|
| 18 |  | 
|---|
| 19 | TH1D *hista = new TH1D("ON",   "ON  data before padding", N, 0., 5.); | 
|---|
| 20 | TH1D *histb = new TH1D("OFF",  "OFF data before padding", N, 0., 5.); | 
|---|
| 21 | TH1D *histc = new TH1D("ON-OFF", "ON-OFF before padding", N, 0., 5.); | 
|---|
| 22 | hista->SetMaximum( 5.0/(Double_t)N ); | 
|---|
| 23 | hista->SetMinimum(-1.0/(Double_t)N ); | 
|---|
| 24 | histb->SetMaximum( 5.0/(Double_t)N ); | 
|---|
| 25 | histb->SetMinimum(-1.0/(Double_t)N ); | 
|---|
| 26 | histc->SetMaximum( 5.0/(Double_t)N ); | 
|---|
| 27 | histc->SetMinimum(-1.0/(Double_t)N ); | 
|---|
| 28 |  | 
|---|
| 29 | // at the beginning, histap is a copy of hista | 
|---|
| 30 | // at the end, it will be the 1D histogram for ON data after padding | 
|---|
| 31 |  | 
|---|
| 32 | // at the beginning, histbp is a copy of histb | 
|---|
| 33 | // at the end, it will be the 1D histogram for OFF data after padding | 
|---|
| 34 |  | 
|---|
| 35 | // at the beginning, histcp is a copy of histc | 
|---|
| 36 | // at the end, it should be zero | 
|---|
| 37 |  | 
|---|
| 38 | TH1D *histap = new TH1D("ONp",   "ON  data after padding", N, 0., 5.); | 
|---|
| 39 | TH1D *histbp = new TH1D("OFFp",  "OFF data after padding", N, 0., 5.); | 
|---|
| 40 | TH1D *histcp = new TH1D("ON-OFFp", "ON-OFF after padding", N, 0., 5.); | 
|---|
| 41 | histap->SetMaximum( 5.0/(Double_t)N ); | 
|---|
| 42 | histap->SetMinimum(-1.0/(Double_t)N ); | 
|---|
| 43 | histbp->SetMaximum( 5.0/(Double_t)N ); | 
|---|
| 44 | histbp->SetMinimum(-1.0/(Double_t)N ); | 
|---|
| 45 | histcp->SetMaximum( 1.0/(Double_t)N); | 
|---|
| 46 | histcp->SetMinimum(-1.0/(Double_t)N); | 
|---|
| 47 |  | 
|---|
| 48 | Double_t shoulda[] = {1.0, 2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 1.0}; | 
|---|
| 49 | Double_t shouldb[] = {0.0, 0.0, 1.0, 2.0, 3.0, 2.0, 1.0, 0.0, 1.0, 2.0}; | 
|---|
| 50 | for (Int_t i=1; i<=N; i++) | 
|---|
| 51 | { | 
|---|
| 52 | hista->SetBinContent(i, shoulda[i-1]); | 
|---|
| 53 | histb->SetBinContent(i, shouldb[i-1]); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | // normalize histograms | 
|---|
| 57 | Double_t suma, sumb, conta, contb; | 
|---|
| 58 | suma = 0.0; | 
|---|
| 59 | sumb = 0.0; | 
|---|
| 60 | for (Int_t i=1; i<=N; i++) | 
|---|
| 61 | { | 
|---|
| 62 | conta = hista->GetBinContent(i); | 
|---|
| 63 | contb = histb->GetBinContent(i); | 
|---|
| 64 | suma += conta; | 
|---|
| 65 | sumb += contb; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | for (Int_t i=1; i<=N; i++) | 
|---|
| 69 | { | 
|---|
| 70 | conta = hista->GetBinContent(i); | 
|---|
| 71 | contb = histb->GetBinContent(i); | 
|---|
| 72 |  | 
|---|
| 73 | hista->SetBinContent(i,conta/suma); | 
|---|
| 74 | histb->SetBinContent(i,contb/sumb); | 
|---|
| 75 | histc->SetBinContent(i, conta/suma - contb/sumb); | 
|---|
| 76 |  | 
|---|
| 77 | histap->SetBinContent(i,conta/suma); | 
|---|
| 78 | histbp->SetBinContent(i,contb/sumb); | 
|---|
| 79 | histcp->SetBinContent(i, conta/suma - contb/sumb); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | // g[k-1][j-1] (if <0.0) tells how many ON events in bin k should be padded | 
|---|
| 84 | //              from sigma_k to sigma_j | 
|---|
| 85 |  | 
|---|
| 86 | // g[k-1][j-1] (if >0.0) tells how many OFF events in bin k should be padded | 
|---|
| 87 | //              from sigma_k to sigma_j | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 | //--------   start j loop   ------------------------------------------------ | 
|---|
| 91 | // loop over bins in histc, starting from the end | 
|---|
| 92 | Double_t v, s, w, t, x, u, a, b, arg; | 
|---|
| 93 |  | 
|---|
| 94 | for (Int_t j=N; j >= 1; j--) | 
|---|
| 95 | { | 
|---|
| 96 | v = histcp->GetBinContent(j); | 
|---|
| 97 | if ( fabs(v) < eps ) continue; | 
|---|
| 98 | if (v >= 0.0) | 
|---|
| 99 | s = 1.0; | 
|---|
| 100 | else | 
|---|
| 101 | s = -1.0; | 
|---|
| 102 |  | 
|---|
| 103 | //................   start k loop   ...................................... | 
|---|
| 104 | // look for a bin k which may compensate the content of bin j | 
|---|
| 105 | for (Int_t k=j-1; k >= 1; k--) | 
|---|
| 106 | { | 
|---|
| 107 | w = histcp->GetBinContent(k); | 
|---|
| 108 | if ( fabs(w) < eps ) continue; | 
|---|
| 109 | if (w >= 0.0) | 
|---|
| 110 | t = 1.0; | 
|---|
| 111 | else | 
|---|
| 112 | t = -1.0; | 
|---|
| 113 |  | 
|---|
| 114 | if (s==t) continue; | 
|---|
| 115 |  | 
|---|
| 116 | x = v + w; | 
|---|
| 117 | if (x >= 0.0) | 
|---|
| 118 | u = 1.0; | 
|---|
| 119 | else | 
|---|
| 120 | u = -1.0; | 
|---|
| 121 |  | 
|---|
| 122 | if (u == s) | 
|---|
| 123 | { | 
|---|
| 124 | arg = -w; | 
|---|
| 125 | g(k-1, j-1)   = arg; | 
|---|
| 126 | cout << "k-1, j-1, arg = " << k-1 << ",  " << j-1 << ",  " | 
|---|
| 127 | << arg << endl; | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 | //...................................... | 
|---|
| 131 | // this is for checking the procedure | 
|---|
| 132 | if (arg < 0.0) | 
|---|
| 133 | { | 
|---|
| 134 | a = histap->GetBinContent(k); | 
|---|
| 135 | histap->SetBinContent(k, a+arg); | 
|---|
| 136 | a = histap->GetBinContent(j); | 
|---|
| 137 | histap->SetBinContent(j, a-arg); | 
|---|
| 138 | } | 
|---|
| 139 | else | 
|---|
| 140 | { | 
|---|
| 141 | b = histbp->GetBinContent(k); | 
|---|
| 142 | histbp->SetBinContent(k, b-arg); | 
|---|
| 143 | b = histbp->GetBinContent(j); | 
|---|
| 144 | histbp->SetBinContent(j, b+arg); | 
|---|
| 145 | } | 
|---|
| 146 | //...................................... | 
|---|
| 147 |  | 
|---|
| 148 | histcp->SetBinContent(k, 0.0); | 
|---|
| 149 | histcp->SetBinContent(j,   x); | 
|---|
| 150 |  | 
|---|
| 151 | //...................................... | 
|---|
| 152 | // redefine v | 
|---|
| 153 | v = histcp->GetBinContent(j); | 
|---|
| 154 | if ( fabs(v) < eps ) break; | 
|---|
| 155 | if (v >= 0.0) | 
|---|
| 156 | s = 1.0; | 
|---|
| 157 | else | 
|---|
| 158 | s = -1.0; | 
|---|
| 159 | //...................................... | 
|---|
| 160 |  | 
|---|
| 161 | continue; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | arg = v; | 
|---|
| 165 | g(k-1, j-1) = arg; | 
|---|
| 166 | cout << "k-1, j-1, arg = " << k-1 << ",  " << j-1 << ",  " | 
|---|
| 167 | << arg << endl; | 
|---|
| 168 |  | 
|---|
| 169 | //...................................... | 
|---|
| 170 | // this is for checking the procedure | 
|---|
| 171 | if (arg < 0.0) | 
|---|
| 172 | { | 
|---|
| 173 | a = histap->GetBinContent(k); | 
|---|
| 174 | histap->SetBinContent(k, a+arg); | 
|---|
| 175 | a = histap->GetBinContent(j); | 
|---|
| 176 | histap->SetBinContent(j, a-arg); | 
|---|
| 177 | } | 
|---|
| 178 | else | 
|---|
| 179 | { | 
|---|
| 180 | b = histbp->GetBinContent(k); | 
|---|
| 181 | histbp->SetBinContent(k, b-arg); | 
|---|
| 182 | b = histbp->GetBinContent(j); | 
|---|
| 183 | histbp->SetBinContent(j, b+arg); | 
|---|
| 184 | } | 
|---|
| 185 | //...................................... | 
|---|
| 186 |  | 
|---|
| 187 | histcp->SetBinContent(k,   x); | 
|---|
| 188 | histcp->SetBinContent(j, 0.0); | 
|---|
| 189 |  | 
|---|
| 190 | break; | 
|---|
| 191 | } | 
|---|
| 192 | //................   end k loop   ...................................... | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 | } | 
|---|
| 196 | //--------   end j loop   ------------------------------------------------ | 
|---|
| 197 | TVector index(N); | 
|---|
| 198 | index.Zero(); | 
|---|
| 199 |  | 
|---|
| 200 | TVector  map(N); | 
|---|
| 201 | Int_t indexold = 0; | 
|---|
| 202 |  | 
|---|
| 203 | cout << "=========================================================" << endl; | 
|---|
| 204 | for (Int_t k=0; k<N-1; k++) | 
|---|
| 205 | { | 
|---|
| 206 | index(k) = (Double_t)indexold; | 
|---|
| 207 | Int_t add = 0; | 
|---|
| 208 | for (Int_t j=k+1; j<N; j++) | 
|---|
| 209 | { | 
|---|
| 210 | if (fabs(g(k,j)) > eps) | 
|---|
| 211 | { | 
|---|
| 212 | map(indexold) = g(k, j); | 
|---|
| 213 |  | 
|---|
| 214 | cout << "k, j, indexold, map = " << k << ",  " << j << ",  " | 
|---|
| 215 | << indexold << ",  " << map(indexold) << endl;; | 
|---|
| 216 |  | 
|---|
| 217 | indexold += 1; | 
|---|
| 218 | add += 1; | 
|---|
| 219 | } | 
|---|
| 220 | } | 
|---|
| 221 | if (add == 0) index(k) = 0; | 
|---|
| 222 | cout << endl; | 
|---|
| 223 | cout << "index(k), add = " << index(k) << ",  " << add << endl; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | cout << "=========================================================" << endl; | 
|---|
| 227 | cout << " " << endl; | 
|---|
| 228 |  | 
|---|
| 229 | //------------------------------------------------------------------------ | 
|---|
| 230 |  | 
|---|
| 231 |  | 
|---|
| 232 | TCanvas *c1 = new TCanvas("c1","", 600, 900); | 
|---|
| 233 | c1->Divide(2,3); | 
|---|
| 234 |  | 
|---|
| 235 | c1->cd(1); | 
|---|
| 236 | hista->Draw(); | 
|---|
| 237 |  | 
|---|
| 238 | c1->cd(2); | 
|---|
| 239 | histap->Draw(); | 
|---|
| 240 |  | 
|---|
| 241 | c1->cd(3); | 
|---|
| 242 | histb->Draw(); | 
|---|
| 243 |  | 
|---|
| 244 | c1->cd(4); | 
|---|
| 245 | histbp->Draw(); | 
|---|
| 246 |  | 
|---|
| 247 | c1->cd(5); | 
|---|
| 248 | histc->Draw(); | 
|---|
| 249 |  | 
|---|
| 250 | c1->cd(6); | 
|---|
| 251 | histcp->Draw(); | 
|---|
| 252 |  | 
|---|
| 253 | } | 
|---|
| 254 | //========================================================================= | 
|---|
| 255 |  | 
|---|
| 256 |  | 
|---|
| 257 |  | 
|---|
| 258 |  | 
|---|
| 259 |  | 
|---|
| 260 |  | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 |  | 
|---|
| 264 |  | 
|---|
| 265 |  | 
|---|
| 266 |  | 
|---|
| 267 |  | 
|---|
| 268 |  | 
|---|