Changes between Version 18 and Version 19 of DatabaseBasedAnalysis/Examples


Ignore:
Timestamp:
08/15/18 15:46:04 (6 years ago)
Author:
tbretz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DatabaseBasedAnalysis/Examples

    v18 v19  
    573573
    574574Note that this query can easily run 30min or more!
     575
     576== Energy Optimization ==
     577
     578{{{#!cpp
     579#include <iostream>
     580#include <iomanip>
     581
     582#include <TMath.h>
     583#include <TF1.h>
     584#include <TH1.h>
     585#include <TH2.h>
     586#include <TProfile.h>
     587#include <TChain.h>
     588#include <TGraph.h>
     589#include <TCanvas.h>
     590#include <TStopwatch.h>
     591
     592void optimenergy()
     593{
     594    // Create chain for the tree Result
     595    // This is just easier than using TFile/TTree
     596    TChain c("Result");
     597
     598    // Add the input file to the
     599    c.AddFile("simulation.root");
     600
     601    // Define variables for all leaves to be accessed
     602    // By definition rootifysql writes only doubles
     603    double X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta,
     604        M3Long, SlopeLong, Leakage1, SlopeSpreadWeighted, Size,
     605        ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd, Energy;
     606
     607    // Connect the variables to the cordesponding leaves
     608    //c.SetBranchAddress("FileId", &FileId);
     609    //c.SetBranchAddress("EvtNumber", &EvtNumber);
     610    c.SetBranchAddress("X", &X);
     611    c.SetBranchAddress("Y", &Y);
     612    c.SetBranchAddress("MeanX", &MeanX);
     613    c.SetBranchAddress("MeanY", &MeanY);
     614    c.SetBranchAddress("Width", &Width);
     615    c.SetBranchAddress("Length", &Length);
     616    c.SetBranchAddress("CosDelta", &CosDelta);
     617    c.SetBranchAddress("SinDelta", &SinDelta);
     618    c.SetBranchAddress("M3Long", &M3Long);
     619    c.SetBranchAddress("SlopeLong", &SlopeLong);
     620    c.SetBranchAddress("Leakage1", &Leakage1);
     621    c.SetBranchAddress("NumIslands", &NumIslands);
     622    c.SetBranchAddress("NumUsedPixels", &NumUsedPixels);
     623    c.SetBranchAddress("Size", &Size);
     624    c.SetBranchAddress("Zd", &Zd);
     625    c.SetBranchAddress("Energy", &Energy);
     626
     627    // Set some constants (they could be included in the database
     628    // in the future)
     629    double mm2deg = +0.0117193246260285378;
     630    //double abberation = 1.02;
     631
     632    // -------------------- Source dependent parameter calculation -------------------
     633
     634    TH2F h2svse ("H_SvsE",      "", 26*3, 2.2, 4.8, 3*30, 1.3, 4.3);
     635    TH2F h2est  ("H_EstVsMC",   "", 26*3, 2.2, 4.8, 26*3, 2.2, 4.8);
     636    TH2F h2bias ("H_BiasLog",   "", 26*3, 2.2, 4.8, 100, -1, 1);
     637    TH2F h2lin  ("H_BiasLin",   "", 26*3, 2.2, 4.8, 100, -1, 2);
     638
     639    TH2F h2size ("H_ResSize",   "", 26*3, 2.2, 4.8, 100, -1, 1);
     640    TH2F h2dist ("H_ResDist",   "", 50,  0, 2.5,    100, -1, 1);
     641    TH2F h2slope("H_ResSlope",  "", 50, -10, 10,    100, -1, 1);
     642    TH2F h2zd   ("H_ResZd",     "", 90, 0,   90,    100, -1, 1);
     643
     644    h2svse.SetStats(kFALSE);
     645    h2bias.SetStats(kFALSE);
     646    h2lin.SetStats(kFALSE);
     647    h2est.SetStats(kFALSE);
     648    h2size.SetStats(kFALSE);
     649    h2dist.SetStats(kFALSE);
     650    h2slope.SetStats(kFALSE);
     651    h2zd.SetStats(kFALSE);
     652
     653    // Loop over all events
     654    TStopwatch clock;
     655        // Loop over all wobble positions in the camera
     656    for (int i=0; i<c.GetEntries(); i++)
     657    {
     658        // read the i-th event from the file
     659        c.GetEntry(i);
     660
     661        // First calculate all cuts to speedup the analysis
     662        double area = TMath::Pi()*Width*Length;
     663
     664        // The abberation correction does increase also Width and Length by 1.02
     665
     666        int angle = 0;
     667
     668        // -------------------- Source dependent parameter calculation -------------------
     669
     670        double cr = cos(angle*TMath::DegToRad());
     671        double sr = sin(angle*TMath::DegToRad());
     672
     673        double px = cr*X-sr*Y;
     674        double py = cr*Y+sr*X;
     675
     676        double dx = MeanX - px*1.022;
     677        double dy = MeanY - py*1.022;
     678
     679        double norm = sqrt(dx*dx + dy*dy);
     680        double dist = norm*mm2deg;
     681
     682        double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.);
     683        double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.);
     684
     685        double alpha = asin(lx);
     686        double sgn   = TMath::Sign(1., ly);
     687
     688        // ------------------------------- Application ----------------------------------
     689
     690        double m3l   = M3Long*sgn*mm2deg;
     691        double slope = SlopeLong*sgn/mm2deg;
     692
     693        // --------------------------------- Analysis -----------------------------------
     694
     695        //double xi = 1.34723 + 0.15214 *slope + 0.970704*(1-1/(1+8.89826*Leakage1));
     696        double xi = 1.340   + 0.0755  *slope + 1.67972 *(1-1/(1+4.86232*Leakage1));
     697
     698        double sign1 = m3l+0.07;
     699        double sign2 = (dist-0.5)*7.2-slope;
     700
     701        double disp  = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length);
     702
     703        double thetasq = disp*disp + dist*dist - 2*disp*dist*sqrt(1-lx*lx);
     704
     705        if (thetasq<0.024)
     706            continue;
     707
     708        bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1<0.1;
     709        if (!cutq)
     710            continue;
     711
     712        bool cut0 = area < log10(Size)*898-1535;
     713        if (!cut0)
     714            continue;
     715
     716        h2svse.Fill(log10(Energy), log10(Size));
     717
     718        double step = 0.8;
     719        double dd = dist<step ? -(dist-step)*0.4 : (dist-step)*0.65;
     720
     721        double energy = pow(10, 0.33 + log10(Size)/1.1 + Zd*0.02 + dd + slope*0.01);
     722
     723        h2est.Fill(log10(Energy),   log10(energy));
     724        h2bias.Fill(log10(energy), log10(energy)-log10(Energy));
     725        h2lin.Fill(log10(energy),  (energy-Energy)/Energy);
     726
     727        h2size.Fill (log10(Size), log10(energy)-log10(Energy));
     728        h2dist.Fill (dist,        log10(energy)-log10(Energy));
     729        h2slope.Fill(slope,       log10(energy)-log10(Energy));
     730        h2zd.Fill(   Zd,          log10(energy)-log10(Energy));
     731    }
     732
     733    clock.Print();
     734
     735    TF1 fx("f", "x", -100, 100);
     736    TF1 f0("f", "0", -100, 100);
     737
     738    TCanvas *canv = new TCanvas("Canvas", "Energy estimation", 800, 900);
     739    canv->Divide(2,4);
     740
     741    canv->cd(1);
     742    gPad->SetGridy();
     743    h2size.DrawCopy("colz");
     744    f0.DrawCopy("same");
     745
     746    canv->cd(3);
     747    gPad->SetGridy();
     748    h2dist.DrawCopy("colz");
     749    f0.DrawCopy("same");
     750
     751    canv->cd(5);
     752    gPad->SetGridy();
     753    h2slope.DrawCopy("colz");
     754    f0.DrawCopy("same");
     755
     756    canv->cd(7);
     757    gPad->SetGridy();
     758    h2zd.DrawCopy("colz");
     759    f0.DrawCopy("same");
     760
     761    canv->cd(2);
     762    gPad->SetGridy();
     763    h2svse.DrawCopy("colz");
     764    fx.DrawCopy("same");
     765
     766    canv->cd(4);
     767    gPad->SetGridy();
     768    h2est.DrawCopy("colz");
     769    fx.DrawCopy("same");
     770
     771    canv->cd(6);
     772    gPad->SetGridy();
     773    h2bias.DrawCopy("colz");
     774    f0.DrawCopy("same");
     775
     776
     777    canv->cd(8);
     778
     779    TGraph grlin;
     780    TGraph grlog;
     781
     782    for (int x=1; x<h2bias.GetNbinsX(); x++)
     783    {
     784        TH1D *p = h2bias.ProjectionY("_py", x, x);
     785        if (p->GetRMS()>0)
     786            grlog.SetPoint(grlog.GetN(), h2bias.GetXaxis()->GetBinCenter(x), pow(10, p->GetRMS())-1);
     787        delete p;
     788
     789        p = h2lin.ProjectionY("_py", x, x);
     790        if (p->GetRMS()>0)
     791            grlin.SetPoint(grlin.GetN(), h2lin.GetXaxis()->GetBinCenter(x), p->GetRMS());
     792        delete p;
     793    }
     794
     795    grlog.SetMarkerColor(kBlue);
     796    grlin.SetMinimum(0);
     797    grlin.DrawClone("A*");
     798    grlog.DrawClone("*");
     799}
     800}}}