Changes between Version 23 and Version 24 of DatabaseBasedAnalysis/Examples


Ignore:
Timestamp:
08/16/18 20:33:58 (6 years ago)
Author:
tbretz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DatabaseBasedAnalysis/Examples

    v23 v24  
    423423    hnew.DrawCopy();
    424424    hold.DrawCopy("same");
     425}
     426}}}
     427
     428
     429== Optim Disp (Leakage==0) ==
     430
     431{{{#!Spoiler
     432{{{#!cpp
     433#include <iostream>
     434#include <iomanip>
     435
     436#include <TMath.h>
     437#include <TH2.h>
     438#include <TChain.h>
     439#include <TCanvas.h>
     440
     441void optimdisp2()
     442{
     443    // Create chain for the tree Result
     444    // This is just easier than using TFile/TTree
     445    TChain c("Result");
     446
     447    // Add the input file to the
     448    c.AddFile("simulation.root");
     449
     450    // Define variables for all leaves to be accessed
     451    // By definition rootifysql writes only doubles
     452    double FileId, EvtNumber, X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta,
     453        M3Long, SlopeLong, Leakage1, SlopeSpreadWeighted, Size,
     454        ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd;
     455
     456    // Connect the variables to the cordesponding leaves
     457    c.SetBranchAddress("FileId", &FileId);
     458    c.SetBranchAddress("EvtNumber", &EvtNumber);
     459    c.SetBranchAddress("X", &X);
     460    c.SetBranchAddress("Y", &Y);
     461    c.SetBranchAddress("MeanX", &MeanX);
     462    c.SetBranchAddress("MeanY", &MeanY);
     463    c.SetBranchAddress("Width", &Width);
     464    c.SetBranchAddress("Length", &Length);
     465    c.SetBranchAddress("CosDelta", &CosDelta);
     466    c.SetBranchAddress("SinDelta", &SinDelta);
     467    c.SetBranchAddress("M3Long", &M3Long);
     468    c.SetBranchAddress("SlopeLong", &SlopeLong);
     469    c.SetBranchAddress("Leakage1", &Leakage1);
     470    c.SetBranchAddress("NumIslands", &NumIslands);
     471    c.SetBranchAddress("NumUsedPixels", &NumUsedPixels);
     472    c.SetBranchAddress("Size", &Size);
     473
     474    // Set some constants (they could be included in the database
     475    // in the future)
     476    double mm2deg = +0.0117193246260285378;
     477
     478    // -------------------- Source dependent parameter calculation -------------------
     479
     480    TH2F hist("", "",
     481              26, 1.300-0.0025, 1.430-0.0025,
     482              20, 0.040-0.0025, 0.140-0.0025);
     483
     484    for (float xi0=1.30; xi0<1.43; xi0+=0.005)
     485        for (float slope0=0.04; slope0<0.14; slope0+=0.005)
     486        {
     487            int cnt_on = 0;
     488
     489            for (int i=0; i<c.GetEntries(); i++)
     490            {
     491                // read the i-th event from the file
     492                c.GetEntry(i);
     493
     494                // First calculate all cuts to speedup the analysis
     495                double area = TMath::Pi()*Width*Length;
     496
     497                // The abberation correction does increase also Width and Length by 1.02
     498
     499                bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1==0;
     500                if (!cutq)
     501                    continue;
     502
     503                bool cut0 = area < log10(Size)*898-1535;
     504                if (!cut0)
     505                    continue;
     506
     507                int angle = 0;
     508
     509                // -------------------- Source dependent parameter calculation -------------------
     510
     511                double cr = cos(angle*TMath::DegToRad());
     512                double sr = sin(angle*TMath::DegToRad());
     513
     514                double px = cr*X-sr*Y;
     515                double py = cr*Y+sr*X;
     516
     517                double dx = MeanX - px*1.02;
     518                double dy = MeanY - py*1.02;
     519
     520                double norm = sqrt(dx*dx + dy*dy);
     521                double dist = norm*mm2deg;
     522
     523                double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.);
     524                double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.);
     525
     526                double alpha = asin(lx);
     527                double sgn   = TMath::Sign(1., ly);
     528
     529                // ------------------------------- Application ----------------------------------
     530
     531                double m3l   = M3Long*sgn*mm2deg;
     532                double slope = SlopeLong*sgn/mm2deg;
     533
     534                // --------------------------------- Analysis -----------------------------------
     535
     536                double xi = xi0 + slope0 *slope;
     537
     538                double sign1 = m3l+0.07;
     539                double sign2 = (dist-0.5)*7.2-slope;
     540
     541                double disp  = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length);
     542
     543                double thetasq = disp*disp + dist*dist - 2*disp*dist*sqrt(1-lx*lx);
     544
     545                if (thetasq<0.024)
     546                    cnt_on++;
     547            }
     548
     549            hist.Fill(xi0, slope0, cnt_on);
     550
     551            cout << xi0 << " " << slope0 << " " << cnt_on << endl;
     552        }
     553
     554    TCanvas *canv = new TCanvas;
     555    canv->SetTopMargin(0.01);
     556
     557    hist.SetStats(kFALSE);
     558    hist.SetContour(100);
     559    hist.SetXTitle("Xi_{0} / deg");
     560    hist.SetYTitle("Slope_{0} #dot ns/deg^{2} ");
     561    hist.GetYaxis()->SetTitleOffset(1.2);
     562    hist.DrawCopy("colz cont2");
     563
     564}
     565}}}
     566}}}
     567
     568Results in
     569
     570[[Image(Result.png)]]
     571
     572== Optim Disp (Leakage>0) ==
     573
     574{{{#!Spoiler
     575{{{#!cpp
     576#include <iostream>
     577#include <iomanip>
     578
     579#include <TMath.h>
     580#include <TH2.h>
     581#include <TChain.h>
     582#include <TCanvas.h>
     583
     584void optimdisp3()
     585{
     586    // Create chain for the tree Result
     587    // This is just easier than using TFile/TTree
     588    TChain c("Result");
     589
     590    // Add the input file to the
     591    c.AddFile("simulation.root");
     592
     593    // Define variables for all leaves to be accessed
     594    // By definition rootifysql writes only doubles
     595    double FileId, EvtNumber, X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta,
     596        M3Long, SlopeLong, Leakage1, SlopeSpreadWeighted, Size,
     597        ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd;
     598
     599    // Connect the variables to the cordesponding leaves
     600    c.SetBranchAddress("FileId", &FileId);
     601    c.SetBranchAddress("EvtNumber", &EvtNumber);
     602    c.SetBranchAddress("X", &X);
     603    c.SetBranchAddress("Y", &Y);
     604    c.SetBranchAddress("MeanX", &MeanX);
     605    c.SetBranchAddress("MeanY", &MeanY);
     606    c.SetBranchAddress("Width", &Width);
     607    c.SetBranchAddress("Length", &Length);
     608    c.SetBranchAddress("CosDelta", &CosDelta);
     609    c.SetBranchAddress("SinDelta", &SinDelta);
     610    c.SetBranchAddress("M3Long", &M3Long);
     611    c.SetBranchAddress("SlopeLong", &SlopeLong);
     612    c.SetBranchAddress("Leakage1", &Leakage1);
     613    c.SetBranchAddress("NumIslands", &NumIslands);
     614    c.SetBranchAddress("NumUsedPixels", &NumUsedPixels);
     615    //c.SetBranchAddress("SlopeSpreadWeighted", &SlopeSpreadWeighted);
     616    c.SetBranchAddress("Size", &Size);
     617    c.SetBranchAddress("Zd", &Zd);
     618    //c.SetBranchAddress("ConcCOG", &ConcCOG);
     619    //c.SetBranchAddress("ConcCore", &ConcCore);
     620
     621    // Set some constants (they could be included in the database
     622    // in the future)
     623    double mm2deg = +0.0117193246260285378;
     624    //double abberation = 1.02;
     625
     626    // -------------------- Source dependent parameter calculation -------------------
     627
     628    TH2F hist("", "",
     629              32, 0.800-0.0250, 2.400-0.0250,
     630              60, 3.000-0.0250, 6.000-0.0250);
     631
     632
     633    for (float l0=0.8; l0<2.4; l0+=0.05)
     634        for (float l1=3; l1<6; l1+=0.05)
     635        {
     636            int cnt_on = 0;
     637
     638            for (int i=0; i<c.GetEntries(); i++)
     639            {
     640                // read the i-th event from the file
     641                c.GetEntry(i);
     642
     643                // First calculate all cuts to speedup the analysis
     644                double area = TMath::Pi()*Width*Length;
     645
     646                // The abberation correction does increase also Width and Length by 1.02
     647
     648                bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1>0;
     649                if (!cutq)
     650                    continue;
     651
     652                bool cut0 = area < log10(Size)*898-1535;
     653                if (!cut0)
     654                    continue;
     655
     656                int angle = 0;
     657
     658                // -------------------- Source dependent parameter calculation -------------------
     659
     660                double cr = cos(angle*TMath::DegToRad());
     661                double sr = sin(angle*TMath::DegToRad());
     662
     663                double px = cr*X-sr*Y;
     664                double py = cr*Y+sr*X;
     665
     666                double dx = MeanX - px*1.02;
     667                double dy = MeanY - py*1.02;
     668
     669                double norm = sqrt(dx*dx + dy*dy);
     670                double dist = norm*mm2deg;
     671
     672                double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.);
     673                double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.);
     674
     675                double alpha = asin(lx);
     676                double sgn   = TMath::Sign(1., ly);
     677
     678                // ------------------------------- Application ----------------------------------
     679
     680                double m3l   = M3Long*sgn*mm2deg;
     681                double slope = SlopeLong*sgn/mm2deg;
     682
     683                // --------------------------------- Analysis -----------------------------------
     684
     685                double xi = 1.36 + 0.085*slope + l0 *(1-1/(1+l1*Leakage1));
     686
     687                // Optim: 1.36 + 0.085
     688
     689                double sign1 = m3l+0.07;
     690                double sign2 = (dist-0.5)*7.2-slope;
     691
     692                double disp  = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length);
     693
     694                double thetasq = disp*disp + dist*dist - 2*disp*dist*sqrt(1-lx*lx);
     695
     696                if (thetasq<0.024)
     697                    cnt_on++;
     698            }
     699
     700            hist.Fill(l0, l1, cnt_on);
     701
     702            cout << l0 << " " << l1 << " " << cnt_on << endl;
     703        }
     704
     705    TCanvas *canv = new TCanvas;
     706    canv->SetTopMargin(0.01);
     707
     708    hist.SetStats(kFALSE);
     709    hist.SetContour(100);
     710    hist.SetXTitle("L_{0} / deg");
     711    hist.SetYTitle("L_{1} / deg");
     712    hist.GetYaxis()->SetTitleOffset(1.2);
     713    hist.DrawCopy("colz cont2");
     714
     715}
     716}}}
     717}}}
     718
     719Results in
     720
     721[[Image(Result2.png)]]
     722
     723== Energy Optimization ==
     724
     725{{{#!cpp
     726#include <iostream>
     727#include <iomanip>
     728
     729#include <TMath.h>
     730#include <TF1.h>
     731#include <TH1.h>
     732#include <TH2.h>
     733#include <TProfile.h>
     734#include <TChain.h>
     735#include <TGraph.h>
     736#include <TCanvas.h>
     737#include <TStopwatch.h>
     738
     739void optimenergy()
     740{
     741    // Create chain for the tree Result
     742    // This is just easier than using TFile/TTree
     743    TChain c("Result");
     744
     745    // Add the input file to the
     746    c.AddFile("simulation.root");
     747
     748    // Define variables for all leaves to be accessed
     749    // By definition rootifysql writes only doubles
     750    double X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta,
     751        M3Long, SlopeLong, Leakage1, SlopeSpreadWeighted, Size,
     752        ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd, Energy;
     753
     754    // Connect the variables to the cordesponding leaves
     755    //c.SetBranchAddress("FileId", &FileId);
     756    //c.SetBranchAddress("EvtNumber", &EvtNumber);
     757    c.SetBranchAddress("X", &X);
     758    c.SetBranchAddress("Y", &Y);
     759    c.SetBranchAddress("MeanX", &MeanX);
     760    c.SetBranchAddress("MeanY", &MeanY);
     761    c.SetBranchAddress("Width", &Width);
     762    c.SetBranchAddress("Length", &Length);
     763    c.SetBranchAddress("CosDelta", &CosDelta);
     764    c.SetBranchAddress("SinDelta", &SinDelta);
     765    c.SetBranchAddress("M3Long", &M3Long);
     766    c.SetBranchAddress("SlopeLong", &SlopeLong);
     767    c.SetBranchAddress("Leakage1", &Leakage1);
     768    c.SetBranchAddress("NumIslands", &NumIslands);
     769    c.SetBranchAddress("NumUsedPixels", &NumUsedPixels);
     770    c.SetBranchAddress("Size", &Size);
     771    c.SetBranchAddress("Zd", &Zd);
     772    c.SetBranchAddress("Energy", &Energy);
     773
     774    // Set some constants (they could be included in the database
     775    // in the future)
     776    double mm2deg = +0.0117193246260285378;
     777    //double abberation = 1.02;
     778
     779    // -------------------- Source dependent parameter calculation -------------------
     780
     781    TH2F h2svse ("H_SvsE",      "", 26*3, 2.2, 4.8, 3*30, 1.3, 4.3);
     782    TH2F h2est  ("H_EstVsMC",   "", 26*3, 2.2, 4.8, 26*3, 2.2, 4.8);
     783    TH2F h2bias ("H_BiasLog",   "", 26*3, 2.2, 4.8, 100, -1, 1);
     784    TH2F h2lin  ("H_BiasLin",   "", 26*3, 2.2, 4.8, 100, -1, 2);
     785
     786    TH2F h2size ("H_ResSize",   "", 26*3, 2.2, 4.8, 100, -1, 1);
     787    TH2F h2dist ("H_ResDist",   "", 50,  0, 2.5,    100, -1, 1);
     788    TH2F h2slope("H_ResSlope",  "", 50, -10, 10,    100, -1, 1);
     789    TH2F h2zd   ("H_ResZd",     "", 90, 0,   90,    100, -1, 1);
     790
     791    h2svse.SetStats(kFALSE);
     792    h2bias.SetStats(kFALSE);
     793    h2lin.SetStats(kFALSE);
     794    h2est.SetStats(kFALSE);
     795    h2size.SetStats(kFALSE);
     796    h2dist.SetStats(kFALSE);
     797    h2slope.SetStats(kFALSE);
     798    h2zd.SetStats(kFALSE);
     799
     800    // Loop over all events
     801    TStopwatch clock;
     802        // Loop over all wobble positions in the camera
     803    for (int i=0; i<c.GetEntries(); i++)
     804    {
     805        // read the i-th event from the file
     806        c.GetEntry(i);
     807
     808        // First calculate all cuts to speedup the analysis
     809        double area = TMath::Pi()*Width*Length;
     810
     811        // The abberation correction does increase also Width and Length by 1.02
     812
     813        int angle = 0;
     814
     815        // -------------------- Source dependent parameter calculation -------------------
     816
     817        double cr = cos(angle*TMath::DegToRad());
     818        double sr = sin(angle*TMath::DegToRad());
     819
     820        double px = cr*X-sr*Y;
     821        double py = cr*Y+sr*X;
     822
     823        double dx = MeanX - px*1.022;
     824        double dy = MeanY - py*1.022;
     825
     826        double norm = sqrt(dx*dx + dy*dy);
     827        double dist = norm*mm2deg;
     828
     829        double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.);
     830        double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.);
     831
     832        double alpha = asin(lx);
     833        double sgn   = TMath::Sign(1., ly);
     834
     835        // ------------------------------- Application ----------------------------------
     836
     837        double m3l   = M3Long*sgn*mm2deg;
     838        double slope = SlopeLong*sgn/mm2deg;
     839
     840        // --------------------------------- Analysis -----------------------------------
     841
     842        //double xi = 1.34723 + 0.15214 *slope + 0.970704*(1-1/(1+8.89826*Leakage1));
     843        double xi = 1.340   + 0.0755  *slope + 1.67972 *(1-1/(1+4.86232*Leakage1));
     844
     845        double sign1 = m3l+0.07;
     846        double sign2 = (dist-0.5)*7.2-slope;
     847
     848        double disp  = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length);
     849
     850        double thetasq = disp*disp + dist*dist - 2*disp*dist*sqrt(1-lx*lx);
     851
     852        if (thetasq<0.024)
     853            continue;
     854
     855        bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1<0.1;
     856        if (!cutq)
     857            continue;
     858
     859        bool cut0 = area < log10(Size)*898-1535;
     860        if (!cut0)
     861            continue;
     862
     863        h2svse.Fill(log10(Energy), log10(Size));
     864
     865        double step = 0.8;
     866        double dd = dist<step ? -(dist-step)*0.4 : (dist-step)*0.65;
     867
     868        double energy = pow(10, 0.33 + log10(Size)/1.1 + Zd*0.02 + dd + slope*0.01);
     869
     870        h2est.Fill(log10(Energy),   log10(energy));
     871        h2bias.Fill(log10(energy), log10(energy)-log10(Energy));
     872        h2lin.Fill(log10(energy),  (energy-Energy)/Energy);
     873
     874        h2size.Fill (log10(Size), log10(energy)-log10(Energy));
     875        h2dist.Fill (dist,        log10(energy)-log10(Energy));
     876        h2slope.Fill(slope,       log10(energy)-log10(Energy));
     877        h2zd.Fill(   Zd,          log10(energy)-log10(Energy));
     878    }
     879
     880    clock.Print();
     881
     882    TF1 fx("f", "x", -100, 100);
     883    TF1 f0("f", "0", -100, 100);
     884
     885    TCanvas *canv = new TCanvas("Canvas", "Energy estimation", 800, 900);
     886    canv->Divide(2,4);
     887
     888    canv->cd(1);
     889    gPad->SetGridy();
     890    h2size.DrawCopy("colz");
     891    f0.DrawCopy("same");
     892
     893    canv->cd(3);
     894    gPad->SetGridy();
     895    h2dist.DrawCopy("colz");
     896    f0.DrawCopy("same");
     897
     898    canv->cd(5);
     899    gPad->SetGridy();
     900    h2slope.DrawCopy("colz");
     901    f0.DrawCopy("same");
     902
     903    canv->cd(7);
     904    gPad->SetGridy();
     905    h2zd.DrawCopy("colz");
     906    f0.DrawCopy("same");
     907
     908    canv->cd(2);
     909    gPad->SetGridy();
     910    h2svse.DrawCopy("colz");
     911    fx.DrawCopy("same");
     912
     913    canv->cd(4);
     914    gPad->SetGridy();
     915    h2est.DrawCopy("colz");
     916    fx.DrawCopy("same");
     917
     918    canv->cd(6);
     919    gPad->SetGridy();
     920    h2bias.DrawCopy("colz");
     921    f0.DrawCopy("same");
     922
     923
     924    canv->cd(8);
     925
     926    TGraph grlin;
     927    TGraph grlog;
     928
     929    for (int x=1; x<h2bias.GetNbinsX(); x++)
     930    {
     931        TH1D *p = h2bias.ProjectionY("_py", x, x);
     932        if (p->GetRMS()>0)
     933            grlog.SetPoint(grlog.GetN(), h2bias.GetXaxis()->GetBinCenter(x), pow(10, p->GetRMS())-1);
     934        delete p;
     935
     936        p = h2lin.ProjectionY("_py", x, x);
     937        if (p->GetRMS()>0)
     938            grlin.SetPoint(grlin.GetN(), h2lin.GetXaxis()->GetBinCenter(x), p->GetRMS());
     939        delete p;
     940    }
     941
     942    grlog.SetMarkerColor(kBlue);
     943    grlin.SetMinimum(0);
     944    grlin.DrawClone("A*");
     945    grlog.DrawClone("*");
    425946}
    426947}}}
     
    5731094
    5741095Note that this query can easily run 30min or more!
    575 
    576 == Optim Disp (Leakage==0) ==
    577 
    578 {{{#!Spoiler
    579 {{{#!cpp
    580 #include <iostream>
    581 #include <iomanip>
    582 
    583 #include <TMath.h>
    584 #include <TH2.h>
    585 #include <TChain.h>
    586 #include <TCanvas.h>
    587 
    588 void optimdisp2()
    589 {
    590     // Create chain for the tree Result
    591     // This is just easier than using TFile/TTree
    592     TChain c("Result");
    593 
    594     // Add the input file to the
    595     c.AddFile("simulation.root");
    596 
    597     // Define variables for all leaves to be accessed
    598     // By definition rootifysql writes only doubles
    599     double FileId, EvtNumber, X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta,
    600         M3Long, SlopeLong, Leakage1, SlopeSpreadWeighted, Size,
    601         ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd;
    602 
    603     // Connect the variables to the cordesponding leaves
    604     c.SetBranchAddress("FileId", &FileId);
    605     c.SetBranchAddress("EvtNumber", &EvtNumber);
    606     c.SetBranchAddress("X", &X);
    607     c.SetBranchAddress("Y", &Y);
    608     c.SetBranchAddress("MeanX", &MeanX);
    609     c.SetBranchAddress("MeanY", &MeanY);
    610     c.SetBranchAddress("Width", &Width);
    611     c.SetBranchAddress("Length", &Length);
    612     c.SetBranchAddress("CosDelta", &CosDelta);
    613     c.SetBranchAddress("SinDelta", &SinDelta);
    614     c.SetBranchAddress("M3Long", &M3Long);
    615     c.SetBranchAddress("SlopeLong", &SlopeLong);
    616     c.SetBranchAddress("Leakage1", &Leakage1);
    617     c.SetBranchAddress("NumIslands", &NumIslands);
    618     c.SetBranchAddress("NumUsedPixels", &NumUsedPixels);
    619     c.SetBranchAddress("Size", &Size);
    620 
    621     // Set some constants (they could be included in the database
    622     // in the future)
    623     double mm2deg = +0.0117193246260285378;
    624 
    625     // -------------------- Source dependent parameter calculation -------------------
    626 
    627     TH2F hist("", "",
    628               26, 1.300-0.0025, 1.430-0.0025,
    629               20, 0.040-0.0025, 0.140-0.0025);
    630 
    631     for (float xi0=1.30; xi0<1.43; xi0+=0.005)
    632         for (float slope0=0.04; slope0<0.14; slope0+=0.005)
    633         {
    634             int cnt_on = 0;
    635 
    636             for (int i=0; i<c.GetEntries(); i++)
    637             {
    638                 // read the i-th event from the file
    639                 c.GetEntry(i);
    640 
    641                 // First calculate all cuts to speedup the analysis
    642                 double area = TMath::Pi()*Width*Length;
    643 
    644                 // The abberation correction does increase also Width and Length by 1.02
    645 
    646                 bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1==0;
    647                 if (!cutq)
    648                     continue;
    649 
    650                 bool cut0 = area < log10(Size)*898-1535;
    651                 if (!cut0)
    652                     continue;
    653 
    654                 int angle = 0;
    655 
    656                 // -------------------- Source dependent parameter calculation -------------------
    657 
    658                 double cr = cos(angle*TMath::DegToRad());
    659                 double sr = sin(angle*TMath::DegToRad());
    660 
    661                 double px = cr*X-sr*Y;
    662                 double py = cr*Y+sr*X;
    663 
    664                 double dx = MeanX - px*1.02;
    665                 double dy = MeanY - py*1.02;
    666 
    667                 double norm = sqrt(dx*dx + dy*dy);
    668                 double dist = norm*mm2deg;
    669 
    670                 double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.);
    671                 double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.);
    672 
    673                 double alpha = asin(lx);
    674                 double sgn   = TMath::Sign(1., ly);
    675 
    676                 // ------------------------------- Application ----------------------------------
    677 
    678                 double m3l   = M3Long*sgn*mm2deg;
    679                 double slope = SlopeLong*sgn/mm2deg;
    680 
    681                 // --------------------------------- Analysis -----------------------------------
    682 
    683                 double xi = xi0 + slope0 *slope;
    684 
    685                 double sign1 = m3l+0.07;
    686                 double sign2 = (dist-0.5)*7.2-slope;
    687 
    688                 double disp  = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length);
    689 
    690                 double thetasq = disp*disp + dist*dist - 2*disp*dist*sqrt(1-lx*lx);
    691 
    692                 if (thetasq<0.024)
    693                     cnt_on++;
    694             }
    695 
    696             hist.Fill(xi0, slope0, cnt_on);
    697 
    698             cout << xi0 << " " << slope0 << " " << cnt_on << endl;
    699         }
    700 
    701     TCanvas *canv = new TCanvas;
    702     canv->SetTopMargin(0.01);
    703 
    704     hist.SetStats(kFALSE);
    705     hist.SetContour(100);
    706     hist.SetXTitle("Xi_{0} / deg");
    707     hist.SetYTitle("Slope_{0} #dot ns/deg^{2} ");
    708     hist.GetYaxis()->SetTitleOffset(1.2);
    709     hist.DrawCopy("colz cont2");
    710 
    711 }
    712 }}}
    713 }}}
    714 
    715 Results in
    716 
    717 [[Image(Result.png)]]
    718 
    719 == Optim Disp (Leakage>0) ==
    720 
    721 {{{#!Spoiler
    722 {{{#!cpp
    723 #include <iostream>
    724 #include <iomanip>
    725 
    726 #include <TMath.h>
    727 #include <TH2.h>
    728 #include <TChain.h>
    729 #include <TCanvas.h>
    730 
    731 void optimdisp3()
    732 {
    733     // Create chain for the tree Result
    734     // This is just easier than using TFile/TTree
    735     TChain c("Result");
    736 
    737     // Add the input file to the
    738     c.AddFile("simulation.root");
    739 
    740     // Define variables for all leaves to be accessed
    741     // By definition rootifysql writes only doubles
    742     double FileId, EvtNumber, X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta,
    743         M3Long, SlopeLong, Leakage1, SlopeSpreadWeighted, Size,
    744         ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd;
    745 
    746     // Connect the variables to the cordesponding leaves
    747     c.SetBranchAddress("FileId", &FileId);
    748     c.SetBranchAddress("EvtNumber", &EvtNumber);
    749     c.SetBranchAddress("X", &X);
    750     c.SetBranchAddress("Y", &Y);
    751     c.SetBranchAddress("MeanX", &MeanX);
    752     c.SetBranchAddress("MeanY", &MeanY);
    753     c.SetBranchAddress("Width", &Width);
    754     c.SetBranchAddress("Length", &Length);
    755     c.SetBranchAddress("CosDelta", &CosDelta);
    756     c.SetBranchAddress("SinDelta", &SinDelta);
    757     c.SetBranchAddress("M3Long", &M3Long);
    758     c.SetBranchAddress("SlopeLong", &SlopeLong);
    759     c.SetBranchAddress("Leakage1", &Leakage1);
    760     c.SetBranchAddress("NumIslands", &NumIslands);
    761     c.SetBranchAddress("NumUsedPixels", &NumUsedPixels);
    762     //c.SetBranchAddress("SlopeSpreadWeighted", &SlopeSpreadWeighted);
    763     c.SetBranchAddress("Size", &Size);
    764     c.SetBranchAddress("Zd", &Zd);
    765     //c.SetBranchAddress("ConcCOG", &ConcCOG);
    766     //c.SetBranchAddress("ConcCore", &ConcCore);
    767 
    768     // Set some constants (they could be included in the database
    769     // in the future)
    770     double mm2deg = +0.0117193246260285378;
    771     //double abberation = 1.02;
    772 
    773     // -------------------- Source dependent parameter calculation -------------------
    774 
    775     TH2F hist("", "",
    776               32, 0.800-0.0250, 2.400-0.0250,
    777               60, 3.000-0.0250, 6.000-0.0250);
    778 
    779 
    780     for (float l0=0.8; l0<2.4; l0+=0.05)
    781         for (float l1=3; l1<6; l1+=0.05)
    782         {
    783             int cnt_on = 0;
    784 
    785             for (int i=0; i<c.GetEntries(); i++)
    786             {
    787                 // read the i-th event from the file
    788                 c.GetEntry(i);
    789 
    790                 // First calculate all cuts to speedup the analysis
    791                 double area = TMath::Pi()*Width*Length;
    792 
    793                 // The abberation correction does increase also Width and Length by 1.02
    794 
    795                 bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1>0;
    796                 if (!cutq)
    797                     continue;
    798 
    799                 bool cut0 = area < log10(Size)*898-1535;
    800                 if (!cut0)
    801                     continue;
    802 
    803                 int angle = 0;
    804 
    805                 // -------------------- Source dependent parameter calculation -------------------
    806 
    807                 double cr = cos(angle*TMath::DegToRad());
    808                 double sr = sin(angle*TMath::DegToRad());
    809 
    810                 double px = cr*X-sr*Y;
    811                 double py = cr*Y+sr*X;
    812 
    813                 double dx = MeanX - px*1.02;
    814                 double dy = MeanY - py*1.02;
    815 
    816                 double norm = sqrt(dx*dx + dy*dy);
    817                 double dist = norm*mm2deg;
    818 
    819                 double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.);
    820                 double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.);
    821 
    822                 double alpha = asin(lx);
    823                 double sgn   = TMath::Sign(1., ly);
    824 
    825                 // ------------------------------- Application ----------------------------------
    826 
    827                 double m3l   = M3Long*sgn*mm2deg;
    828                 double slope = SlopeLong*sgn/mm2deg;
    829 
    830                 // --------------------------------- Analysis -----------------------------------
    831 
    832                 double xi = 1.36 + 0.085*slope + l0 *(1-1/(1+l1*Leakage1));
    833 
    834                 // Optim: 1.36 + 0.085
    835 
    836                 double sign1 = m3l+0.07;
    837                 double sign2 = (dist-0.5)*7.2-slope;
    838 
    839                 double disp  = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length);
    840 
    841                 double thetasq = disp*disp + dist*dist - 2*disp*dist*sqrt(1-lx*lx);
    842 
    843                 if (thetasq<0.024)
    844                     cnt_on++;
    845             }
    846 
    847             hist.Fill(l0, l1, cnt_on);
    848 
    849             cout << l0 << " " << l1 << " " << cnt_on << endl;
    850         }
    851 
    852     TCanvas *canv = new TCanvas;
    853     canv->SetTopMargin(0.01);
    854 
    855     hist.SetStats(kFALSE);
    856     hist.SetContour(100);
    857     hist.SetXTitle("L_{0} / deg");
    858     hist.SetYTitle("L_{1} / deg");
    859     hist.GetYaxis()->SetTitleOffset(1.2);
    860     hist.DrawCopy("colz cont2");
    861 
    862 }
    863 }}}
    864 }}}
    865 
    866 Results in
    867 
    868 [[Image(Result2.png)]]
    869 
    870 == Energy Optimization ==
    871 
    872 {{{#!cpp
    873 #include <iostream>
    874 #include <iomanip>
    875 
    876 #include <TMath.h>
    877 #include <TF1.h>
    878 #include <TH1.h>
    879 #include <TH2.h>
    880 #include <TProfile.h>
    881 #include <TChain.h>
    882 #include <TGraph.h>
    883 #include <TCanvas.h>
    884 #include <TStopwatch.h>
    885 
    886 void optimenergy()
    887 {
    888     // Create chain for the tree Result
    889     // This is just easier than using TFile/TTree
    890     TChain c("Result");
    891 
    892     // Add the input file to the
    893     c.AddFile("simulation.root");
    894 
    895     // Define variables for all leaves to be accessed
    896     // By definition rootifysql writes only doubles
    897     double X, Y, MeanX, MeanY, Width, Length, CosDelta, SinDelta,
    898         M3Long, SlopeLong, Leakage1, SlopeSpreadWeighted, Size,
    899         ConcCore, ConcCOG, NumIslands, NumUsedPixels, Zd, Energy;
    900 
    901     // Connect the variables to the cordesponding leaves
    902     //c.SetBranchAddress("FileId", &FileId);
    903     //c.SetBranchAddress("EvtNumber", &EvtNumber);
    904     c.SetBranchAddress("X", &X);
    905     c.SetBranchAddress("Y", &Y);
    906     c.SetBranchAddress("MeanX", &MeanX);
    907     c.SetBranchAddress("MeanY", &MeanY);
    908     c.SetBranchAddress("Width", &Width);
    909     c.SetBranchAddress("Length", &Length);
    910     c.SetBranchAddress("CosDelta", &CosDelta);
    911     c.SetBranchAddress("SinDelta", &SinDelta);
    912     c.SetBranchAddress("M3Long", &M3Long);
    913     c.SetBranchAddress("SlopeLong", &SlopeLong);
    914     c.SetBranchAddress("Leakage1", &Leakage1);
    915     c.SetBranchAddress("NumIslands", &NumIslands);
    916     c.SetBranchAddress("NumUsedPixels", &NumUsedPixels);
    917     c.SetBranchAddress("Size", &Size);
    918     c.SetBranchAddress("Zd", &Zd);
    919     c.SetBranchAddress("Energy", &Energy);
    920 
    921     // Set some constants (they could be included in the database
    922     // in the future)
    923     double mm2deg = +0.0117193246260285378;
    924     //double abberation = 1.02;
    925 
    926     // -------------------- Source dependent parameter calculation -------------------
    927 
    928     TH2F h2svse ("H_SvsE",      "", 26*3, 2.2, 4.8, 3*30, 1.3, 4.3);
    929     TH2F h2est  ("H_EstVsMC",   "", 26*3, 2.2, 4.8, 26*3, 2.2, 4.8);
    930     TH2F h2bias ("H_BiasLog",   "", 26*3, 2.2, 4.8, 100, -1, 1);
    931     TH2F h2lin  ("H_BiasLin",   "", 26*3, 2.2, 4.8, 100, -1, 2);
    932 
    933     TH2F h2size ("H_ResSize",   "", 26*3, 2.2, 4.8, 100, -1, 1);
    934     TH2F h2dist ("H_ResDist",   "", 50,  0, 2.5,    100, -1, 1);
    935     TH2F h2slope("H_ResSlope",  "", 50, -10, 10,    100, -1, 1);
    936     TH2F h2zd   ("H_ResZd",     "", 90, 0,   90,    100, -1, 1);
    937 
    938     h2svse.SetStats(kFALSE);
    939     h2bias.SetStats(kFALSE);
    940     h2lin.SetStats(kFALSE);
    941     h2est.SetStats(kFALSE);
    942     h2size.SetStats(kFALSE);
    943     h2dist.SetStats(kFALSE);
    944     h2slope.SetStats(kFALSE);
    945     h2zd.SetStats(kFALSE);
    946 
    947     // Loop over all events
    948     TStopwatch clock;
    949         // Loop over all wobble positions in the camera
    950     for (int i=0; i<c.GetEntries(); i++)
    951     {
    952         // read the i-th event from the file
    953         c.GetEntry(i);
    954 
    955         // First calculate all cuts to speedup the analysis
    956         double area = TMath::Pi()*Width*Length;
    957 
    958         // The abberation correction does increase also Width and Length by 1.02
    959 
    960         int angle = 0;
    961 
    962         // -------------------- Source dependent parameter calculation -------------------
    963 
    964         double cr = cos(angle*TMath::DegToRad());
    965         double sr = sin(angle*TMath::DegToRad());
    966 
    967         double px = cr*X-sr*Y;
    968         double py = cr*Y+sr*X;
    969 
    970         double dx = MeanX - px*1.022;
    971         double dy = MeanY - py*1.022;
    972 
    973         double norm = sqrt(dx*dx + dy*dy);
    974         double dist = norm*mm2deg;
    975 
    976         double lx = min(max((CosDelta*dy - SinDelta*dx)/norm, -1.), 1.);
    977         double ly = min(max((CosDelta*dx + SinDelta*dy)/norm, -1.), 1.);
    978 
    979         double alpha = asin(lx);
    980         double sgn   = TMath::Sign(1., ly);
    981 
    982         // ------------------------------- Application ----------------------------------
    983 
    984         double m3l   = M3Long*sgn*mm2deg;
    985         double slope = SlopeLong*sgn/mm2deg;
    986 
    987         // --------------------------------- Analysis -----------------------------------
    988 
    989         //double xi = 1.34723 + 0.15214 *slope + 0.970704*(1-1/(1+8.89826*Leakage1));
    990         double xi = 1.340   + 0.0755  *slope + 1.67972 *(1-1/(1+4.86232*Leakage1));
    991 
    992         double sign1 = m3l+0.07;
    993         double sign2 = (dist-0.5)*7.2-slope;
    994 
    995         double disp  = (sign1<0 || sign2<0 ? -xi : xi)*(1-Width/Length);
    996 
    997         double thetasq = disp*disp + dist*dist - 2*disp*dist*sqrt(1-lx*lx);
    998 
    999         if (thetasq<0.024)
    1000             continue;
    1001 
    1002         bool cutq = NumIslands<3.5 && NumUsedPixels>5.5 && Leakage1<0.1;
    1003         if (!cutq)
    1004             continue;
    1005 
    1006         bool cut0 = area < log10(Size)*898-1535;
    1007         if (!cut0)
    1008             continue;
    1009 
    1010         h2svse.Fill(log10(Energy), log10(Size));
    1011 
    1012         double step = 0.8;
    1013         double dd = dist<step ? -(dist-step)*0.4 : (dist-step)*0.65;
    1014 
    1015         double energy = pow(10, 0.33 + log10(Size)/1.1 + Zd*0.02 + dd + slope*0.01);
    1016 
    1017         h2est.Fill(log10(Energy),   log10(energy));
    1018         h2bias.Fill(log10(energy), log10(energy)-log10(Energy));
    1019         h2lin.Fill(log10(energy),  (energy-Energy)/Energy);
    1020 
    1021         h2size.Fill (log10(Size), log10(energy)-log10(Energy));
    1022         h2dist.Fill (dist,        log10(energy)-log10(Energy));
    1023         h2slope.Fill(slope,       log10(energy)-log10(Energy));
    1024         h2zd.Fill(   Zd,          log10(energy)-log10(Energy));
    1025     }
    1026 
    1027     clock.Print();
    1028 
    1029     TF1 fx("f", "x", -100, 100);
    1030     TF1 f0("f", "0", -100, 100);
    1031 
    1032     TCanvas *canv = new TCanvas("Canvas", "Energy estimation", 800, 900);
    1033     canv->Divide(2,4);
    1034 
    1035     canv->cd(1);
    1036     gPad->SetGridy();
    1037     h2size.DrawCopy("colz");
    1038     f0.DrawCopy("same");
    1039 
    1040     canv->cd(3);
    1041     gPad->SetGridy();
    1042     h2dist.DrawCopy("colz");
    1043     f0.DrawCopy("same");
    1044 
    1045     canv->cd(5);
    1046     gPad->SetGridy();
    1047     h2slope.DrawCopy("colz");
    1048     f0.DrawCopy("same");
    1049 
    1050     canv->cd(7);
    1051     gPad->SetGridy();
    1052     h2zd.DrawCopy("colz");
    1053     f0.DrawCopy("same");
    1054 
    1055     canv->cd(2);
    1056     gPad->SetGridy();
    1057     h2svse.DrawCopy("colz");
    1058     fx.DrawCopy("same");
    1059 
    1060     canv->cd(4);
    1061     gPad->SetGridy();
    1062     h2est.DrawCopy("colz");
    1063     fx.DrawCopy("same");
    1064 
    1065     canv->cd(6);
    1066     gPad->SetGridy();
    1067     h2bias.DrawCopy("colz");
    1068     f0.DrawCopy("same");
    1069 
    1070 
    1071     canv->cd(8);
    1072 
    1073     TGraph grlin;
    1074     TGraph grlog;
    1075 
    1076     for (int x=1; x<h2bias.GetNbinsX(); x++)
    1077     {
    1078         TH1D *p = h2bias.ProjectionY("_py", x, x);
    1079         if (p->GetRMS()>0)
    1080             grlog.SetPoint(grlog.GetN(), h2bias.GetXaxis()->GetBinCenter(x), pow(10, p->GetRMS())-1);
    1081         delete p;
    1082 
    1083         p = h2lin.ProjectionY("_py", x, x);
    1084         if (p->GetRMS()>0)
    1085             grlin.SetPoint(grlin.GetN(), h2lin.GetXaxis()->GetBinCenter(x), p->GetRMS());
    1086         delete p;
    1087     }
    1088 
    1089     grlog.SetMarkerColor(kBlue);
    1090     grlin.SetMinimum(0);
    1091     grlin.DrawClone("A*");
    1092     grlog.DrawClone("*");
    1093 }
    1094 }}}