| 40 | {{{!#cpp |
| 41 | #include <iostream> |
| 42 | #include <iomanip> |
| 43 | |
| 44 | #include <TMath.h> |
| 45 | #include <TH1.h> |
| 46 | #include <TH2.h> |
| 47 | #include <TProfile.h> |
| 48 | #include <TChain.h> |
| 49 | #include <TGraph.h> |
| 50 | #include <TCanvas.h> |
| 51 | |
| 52 | Double_t LiMa(Double_t s, Double_t b, Double_t alpha=0.2) |
| 53 | { |
| 54 | const Double_t sum = s+b; |
| 55 | |
| 56 | if (s<0 || b<0 || alpha<=0) |
| 57 | return -1; |
| 58 | |
| 59 | const Double_t l = s==0 ? 0 : s*TMath::Log(s/sum*(alpha+1)/alpha); |
| 60 | const Double_t m = b==0 ? 0 : b*TMath::Log(b/sum*(alpha+1) ); |
| 61 | |
| 62 | return l+m<0 ? 0 : TMath::Sqrt((l+m)*2); |
| 63 | } |
| 64 | |
| 65 | void optimarea() |
| 66 | { |
| 67 | // Create chain for the tree Result |
| 68 | // This is just easier than using TFile/TTree |
| 69 | TChain c("Result"); |
| 70 | |
| 71 | // Add the input file to the |
| 72 | c.AddFile("crab2018.root"); |
| 73 | |
| 74 | // Define variables for all leaves to be accessed |
| 75 | // By definition rootifysql writes only doubles |
| 76 | double X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta, |
| 77 | M3Long, SlopeLong, Leakage1, Size, |
| 78 | ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd; |
| 79 | |
| 80 | // Connect the variables to the cordesponding leaves |
| 81 | c.SetBranchAddress("X", &X); |
| 82 | c.SetBranchAddress("Y", &Y); |
| 83 | c.SetBranchAddress("MeanX", &MeanX); |
| 84 | c.SetBranchAddress("MeanY", &MeanY); |
| 85 | c.SetBranchAddress("Width", &Width); |
| 86 | c.SetBranchAddress("Length", &Length); |
| 87 | c.SetBranchAddress("CosDelta", &CosDelta); |
| 88 | c.SetBranchAddress("SinDelta", &SinDelta); |
| 89 | c.SetBranchAddress("M3Long", &M3Long); |
| 90 | c.SetBranchAddress("SlopeLong", &SlopeLong); |
| 91 | c.SetBranchAddress("Leakage1", &Leakage1); |
| 92 | c.SetBranchAddress("NumIslands", &NumIslands); |
| 93 | c.SetBranchAddress("NumUsedPixels", &NumUsedPixels); |
| 94 | c.SetBranchAddress("Size", &Size); |
| 95 | |
| 96 | // Set some constants (they could be included in the database |
| 97 | // in the future) |
| 98 | double mm2deg = +0.0117193246260285378; |
| 99 | |
| 100 | // -------------------- Source dependent parameter calculation ------------------- |
| 101 | |
| 102 | // Create a histogram for on- and off-data |
| 103 | TH1F hold("old", "", 100, 0, 1); |
| 104 | TH1F hnew("onew", "", 100, 0, 1); |
| 105 | |
| 106 | TH2F h2area_on( "H_AvsS_on", "", 24, 1.5, 4.5, 200, -2, 0); |
| 107 | TH2F h2area_of( "H_AvsS_of", "", 24, 1.5, 4.5, 200, -2, 0); |
| 108 | TH2F h2area_sig("H_AvsS_sig", "", 24, 1.5, 4.5, 200, -2, 0); |
| 109 | |
| 110 | // Loop over all events |
| 111 | for (int i=0; i<c.GetEntries(); i++) |
| 112 | { |
| 113 | // read the i-th event from the file |
| 114 | c.GetEntry(i); |
| 115 | |
| 116 | // First calculate all cuts to speedup the analysis |
| 117 | double area = TMath::Pi()*Width*Length; |
| 118 | |
| 119 | bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1<0.1; |
| 120 | if (!cutq) |
| 121 | continue; |
| 122 | |
| 123 | // Loop over all wobble positions in the camera |
| 124 | for (int angle=0; angle<360; angle+=60) |
| 125 | { |
| 126 | |
| 127 | // -------------------- Source dependent parameter calculation ------------------- |
| 128 | |
| 129 | double cr = cos(angle*TMath::DegToRad()); |
| 130 | double sr = sin(angle*TMath::DegToRad()); |
| 131 | |
| 132 | double px = cr*X-sr*Y; |
| 133 | double py = cr*Y+sr*X; |
| 134 | |
| 135 | double dx = MeanX - px*1.022; |
| 136 | double dy = MeanY - py*1.022; |
| 137 | |
| 138 | double norm = sqrt(dx*dx + dy*dy); |
| 139 | double dist = norm*mm2deg; |
| 140 | |
| 141 | double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.); |
| 142 | double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.); |
| 143 | |
| 144 | double alpha = asin(lx); |
| 145 | double sgn = TMath::Sign(1., ly); |
| 146 | |
| 147 | // ------------------------------- Application ---------------------------------- |
| 148 | |
| 149 | double m3l = M3Long*sgn*mm2deg; |
| 150 | double slope = SlopeLong*sgn/mm2deg; |
| 151 | |
| 152 | // --------------------------------- Analysis ----------------------------------- |
| 153 | |
| 154 | double xi = 1.340 + 0.0755 *slope + 1.67972 *(1-1/(1+4.86232*Leakage1)); |
| 155 | |
| 156 | double sign1 = m3l+0.07; |
| 157 | double sign2 = (dist-0.5)*7.2-slope; |
| 158 | |
| 159 | double disp = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length); |
| 160 | |
| 161 | double thetasq = disp_old*disp_old + dist*dist - 2*disp_old*dist*sqrt(1-lx*lx); |
| 162 | |
| 163 | if (thetasq>0.024) |
| 164 | continue; |
| 165 | |
| 166 | if (angle==0) |
| 167 | h2area_on.Fill(log10(Size), log10(area*mm2deg*mm2deg)); |
| 168 | else |
| 169 | h2area_of.Fill(log10(Size), log10(area*mm2deg*mm2deg)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | for (int x=1; x<=h2area_sig.GetNbinsX(); x++) |
| 174 | for (int y=1; y<=h2area_sig.GetNbinsY(); y++) |
| 175 | { |
| 176 | double on = h2area_on.Integral(x, x, 1, y); |
| 177 | double of = h2area_of.Integral(x, x, 1, y); |
| 178 | |
| 179 | h2area_sig.SetBinContent(x, y, Error(on, of)); |
| 180 | } |
| 181 | |
| 182 | TGraph gmax; |
| 183 | for (int x=1; x<=h2area_sig.GetNbinsX(); x++) |
| 184 | { |
| 185 | TH1D *p = h2area_sig.ProjectionY("_py", x, x); |
| 186 | double max = p->GetXaxis()->GetBinCenter(p->GetMaximumBin()); |
| 187 | delete p; |
| 188 | |
| 189 | gmax.SetPoint(gmax.GetN(), |
| 190 | h2area_sig.GetXaxis()->GetBinCenter(x), |
| 191 | max); |
| 192 | } |
| 193 | |
| 194 | clock.Print(); |
| 195 | |
| 196 | h2area_on.SetStats(kFALSE); |
| 197 | h2area_of.SetStats(kFALSE); |
| 198 | h2area_sig.SetStats(kFALSE); |
| 199 | |
| 200 | TCanvas *canv = new TCanvas; |
| 201 | canv->Divide(2,2); |
| 202 | |
| 203 | canv->cd(1); |
| 204 | h2area_on.DrawCopy("colz"); |
| 205 | gmax.DrawClone("*"); |
| 206 | |
| 207 | canv->cd(2); |
| 208 | h2area_sig.DrawCopy("colz"); |
| 209 | gmax.DrawClone("*"); |
| 210 | |
| 211 | canv->cd(3); |
| 212 | h2area_of.DrawCopy("colz"); |
| 213 | gmax.DrawClone("*"); |
| 214 | |
| 215 | canv->cd(4); |
| 216 | } |
| 217 | }}} |
| 218 | |