Changeset 2170


Ignore:
Timestamp:
06/11/03 13:44:37 (22 years ago)
Author:
rwagner
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/Changelog

    r2168 r2170  
    11                                                 -*-*- END OF LINE -*-*-
     2
     3 2003/06/11: Robert Wagner
     4
     5   * mhist/MHOnSubtraction.[h,cc]
     6     - Some bugfixes, e.g. concerning binning of result histograms
     7     - Improvements in output
    28
    39
  • trunk/MagicSoft/Mars/mhist/MHOnSubtraction.cc

    r2164 r2170  
    2626//////////////////////////////////////////////////////////////////////////////
    2727//                                                                          //
    28 //  MHOnSubtraction                                                            //
     28//  MHOnSubtraction                                                         //
     29//                                                                          //
    2930//                                                                          //
    3031//  extracts the gamma signal from a pure ON-signal given in an             //
    3132//  ALPHA-Energy-Theta histogram. The class will take this histogram from   //
    32 //  the parameter list and will provide a MHArray of MH3 histograms in      //
    33 //  energy, one per theta bin.                                              //
     33//  the parameter list and will provide result histograms in the            //
     34//  parameter list.                                                         //
     35//                                                                          //
     36//  This class still is work in progress.                                   //
     37//                                                                          //
    3438//                                                                          //
    3539//////////////////////////////////////////////////////////////////////////////
    3640
    37 // This part of MARS is code still evolving. Please do not remove commentary
    38 // parts and clearly state where and how you changed the code, if you did so.
     41// This part of MARS is code still evolving. Please do not change the code
     42// without prior feedback by the author.
    3943
    4044#include "MHOnSubtraction.h"
     
    6468// Default Constructor.
    6569//
    66 MHOnSubtraction::MHOnSubtraction(const char *name, const char *title) : fMaxSignif(0),
    67                                                             fMaxRedChiSq(0),
    68                                                             fSlope(20.0)
     70MHOnSubtraction::MHOnSubtraction(const char *name, const char *title) : fMaxSignif(0),  fMaxRedChiSq(0), fSlope(20.0)
    6971{
    7072  fName  = name  ? name  : "MHOnSubtraction";
     
    9698// measurements have to be provided.
    9799//
     100// This function underestimates the true significance for it does not take
     101// into account errors on the event numbers. A more accurate variation wil
     102// be provided soon
     103//
    98104Double_t MHOnSubtraction::CalcSignificance(Double_t nOn, Double_t nOff, Double_t theta)
    99105{
    100 
    101   if (nOn<=0) {
     106  if (nOn<=0)
    102107    *fLog << warn << "Got " << nOn << " total events, " << flush;
    103   }
    104 
    105   if (nOff<=0) {
    106     *fLog << warn << "Got " << nOff << " background events, " << flush;   
    107   }
    108 
     108  if (nOff<=0)
     109    *fLog << warn << "Got " << nOff << " background events, " << flush;
    109110  if (nOn<=0 || nOff<=0.0) {
    110111    *fLog << warn << "returning significance of 0.0" << endl;
    111112    return 0.0;
    112113  }
    113 
    114114  return sqrt(2*(nOn*log((1+theta)*nOn/(theta*(nOn+nOff)))
    115115                 +nOff*log((1+theta)*(nOff/(nOn+nOff)))));
     
    117117
    118118// -----------------------------------------------------------------------
     119//
     120// This function takes a first look at a given ALPHA distribution
     121// and determines if a fit is applicable.
    119122//
    120123// Fits a given TH1 containing an ALPHA distribution with a combined
     
    177180
    178181   *fLog << dbg << "Plot contains " <<
    179      outerEvents << " outer ev (" << outerEvents/outerBins << "/bin), " << flush;
    180    *fLog << dbg <<
     182     outerEvents << " outer ev (" << outerEvents/outerBins << "/bin), " <<
    181183     innerEvents << " inner ev (" << innerEvents/innerBins << "/bin) " << endl;
    182184
    183185  if ((outerBinsZero!=0) || (innerBinsZero != 0))
    184186    *fLog << warn << "There are ";
    185 
    186187  if (outerBinsZero != 0)
    187188    *fLog << dbg << outerBinsZero << " empty outer bins ";
    188 
    189189  if (innerBinsZero != 0)
    190190    *fLog << dbg <<innerBinsZero << " empty inner bins ";
    191 
    192191  if ((outerBinsZero!=0) || (innerBinsZero != 0))
    193192    *fLog << endl;
    194193
    195194  if (outerEvents == 0.0) {
    196      *fLog << warn << "No events in OFF region. Assuming background = 0" << endl;
    197 
     195     *fLog << warn << "No events in OFF region. Assuming background = 0"
     196           << endl;
    198197     TF1 *po = new TF1("pol0"+funcName,"pol0(0)",-89.5,89.5);
    199198     po->SetLineWidth(2);
     
    203202     alphaHisto.GetListOfFunctions()->Add(po);
    204203     alphaHisto.SetLineColor(97);
    205      alphaHisto.DrawCopy(); //rwagner
    206      po->Draw("SAME");
    207 
     204     if (draw) {
     205       alphaHisto.DrawCopy(); //rwagner
     206       po->Draw("SAME");
     207     }
    208208     sigLiMa = 0;
    209209     lowerBin = 1;
     
    213213     return kFALSE; //No gaus fit applied
    214214  }
     215
    215216  if (outerEvents/outerBins < 2.5) {
    216217    *fLog << warn << "Only " << /*setprecision(2) <<*/ outerEvents/outerBins
     
    224225     po->SetParameter(0,outerEvents/outerBins);
    225226     alphaHisto.GetListOfFunctions()->Add(po);   
    226      alphaHisto.DrawCopy(); //rwagner
    227      po->Draw("SAME");
     227     if (draw) {
     228       alphaHisto.DrawCopy(); //rwagner
     229       po->Draw("SAME");
     230     }
    228231   
    229232     Int_t centerBin = alphaHisto.GetXaxis()->FindBin((Double_t)0.0);
     
    231234       alphaHisto.GetNbinsX()- centerBin : centerBin;
    232235     
    233 
    234236     Int_t lowerSignalEdge = centerBin;
    235237     for (Int_t i=3; i < maxBin; i++) {
    236        if ((double)alphaHisto.GetBinContent(centerBin-i) < outerEvents/outerBins) {
     238       if ((double)alphaHisto.GetBinContent(centerBin-i)
     239           < outerEvents/outerBins) {
    237240         lowerSignalEdge = centerBin-i+1;
    238241         break;
     
    243246     Int_t upperSignalEdge = centerBin;
    244247     for (Int_t i=3; i < maxBin; i++) {
    245        if ((double)alphaHisto.GetBinContent(centerBin+i) <= outerEvents/outerBins) {
     248       if ((double)alphaHisto.GetBinContent(centerBin+i)
     249           <= outerEvents/outerBins) {
    246250         upperSignalEdge=centerBin+i-1;
    247251         break;
     
    251255
    252256     double nOnInt = 0;
    253      for (Int_t i=1; i < alphaHisto.GetNbinsX(); i++) {
     257     for (Int_t i=1; i < alphaHisto.GetNbinsX(); i++)
    254258       nOnInt += alphaHisto.GetBinContent(i) - outerEvents/outerBins;
    255      }
    256259     
    257      double nOffInt = (upperSignalEdge - lowerSignalEdge + 1) * (outerEvents/outerBins);
     260     double nOffInt = (upperSignalEdge - lowerSignalEdge + 1)
     261       * (outerEvents/outerBins);
    258262     
    259263     sigLiMa = CalcSignificance(nOnInt, nOffInt, 1);
    260    
    261      *fLog << inf << "Estimated significance is " << sigLiMa << " sigma " << endl;   
     264
     265     if (sigLiMa < 3)
     266       alphaHisto.SetLineColor(2);   
     267
     268   
     269     *fLog << inf << "Estimated significance is " << sigLiMa
     270           << " sigma " << endl;   
    262271     *fLog << inf << "Signal region is "
    263            << lowerBin << " < ALPHA < " << upperBin << " (Most likely you wanna ignore this)" << endl;
     272           << lowerBin << " < ALPHA < " << upperBin
     273           << " (Most likely you wanna ignore this)" << endl;
    264274
    265275     return kFALSE; //No gaus fit applied   
    266 
    267276  }
    268277 
     
    325334 
    326335  sigLiMa = CalcSignificance(nOnInt, nOffInt, 1);
    327 
    328   *fLog << inf << "Fit estimates significance to be " << sigLiMa << " sigma " << endl;
     336//   if (sigLiMa < 3)
     337//     alphaHisto.SetLineColor(2);   
     338
     339
     340  *fLog << inf << "Fit estimates significance to be "
     341        << sigLiMa << " sigma " << endl;
    329342
    330343  *fLog << inf << "Fit yields signal region to be "
     
    336349}
    337350
     351
     352
     353
     354
    338355// -----------------------------------------------------------------------
    339356//
    340357// Does the actual extraction of the gamma signal. For performance
    341 // reasons, the fit from MHOnSubtraction::FitHistogram is used and not redone.
     358// reasons, fits already done by MHOnSubtraction::FitHistogram are used
     359// and not redone.
    342360// From it, a polynomial function for the background is evaluated and the
    343361// gamma signal is extracted in the region given by lowerBin < ALPHA <
     
    347365//
    348366Bool_t MHOnSubtraction::ExtractSignal(TH1 &alphaHisto, Double_t &sigLiMa,
    349                               Double_t &lowerBin, Double_t &upperBin,
    350                               Double_t &gammaSignal, Double_t &errorGammaSignal,
    351                               Double_t &off, Double_t &errorOff,
    352                               Float_t signalRegionFactor, const Bool_t draw, TString funcName)
     367   Double_t &lowerBin, Double_t &upperBin,
     368   Double_t &gammaSignal, Double_t &errorGammaSignal,
     369   Double_t &off, Double_t &errorOff,
     370   Float_t signalRegionFactor, const Bool_t draw, TString funcName,
     371   TPad *drawPad, Int_t drawBase)
    353372{
    354373  TF1 *gausPol = alphaHisto.GetFunction("gauspol3"+funcName);
     
    358377    *fLog << err << "Fatal: ALPHA histogram has no gauspol or pol " 
    359378          << " fit attached to it." << endl;
    360 //     if (draw) {
    361       TPaveLabel* lab = new TPaveLabel(0.1,0.2,0.9,0.8,"(no fit)");
    362       lab->SetFillStyle(3000);
    363       lab->SetBorderSize(0);
    364       lab->DrawClone(); 
    365       lab->SetBit(kCanDelete);
    366 
    367 //     }
     379    TPaveLabel* lab = new TPaveLabel(0.1,0.2,0.9,0.8,"(no fit)");
     380    lab->SetFillStyle(3000);
     381    lab->SetBorderSize(0);
     382    lab->DrawClone(); 
     383    lab->SetBit(kCanDelete);
    368384    return kFALSE;
    369385  }
     
    414430
    415431  if (pol) {
    416 
    417432    po = pol;
    418433   
     
    449464  } //for bin
    450465  eNOn = sqrt(eNOn);
    451  
    452  
     466     
    453467  // Evaluate background
    454468 
     
    463477  eNOff = sqrt(fabs(nOff));
    464478 
    465   if (nOn==0) { // there should not be a negative number of signal events
     479  if (nOn==0)   // there should not be a negative number of signal events
    466480    nOff=0;
    467   }
    468 
     481 
    469482  if (nOff<0) { // there should not be a negative number of off events
    470483    nOff=0;
     
    472485  }
    473486   
    474 
    475487  *fLog << inf << "nEvts = " << nOn << "+-" << eNOn << ", ";
    476488
     
    498510    pt->SetBorderSize(0);
    499511    pt->SetTextAlign(12);
    500     pt->Draw();
     512    pt->Draw("");
    501513  } 
    502514
    503   if (draw) {
     515  if (draw||drawPad) {
    504516    // Plot significance vs alpha_cut
    505517   
     
    507519    Int_t maxBin    = centerBin > alphaHisto.GetNbinsX()- centerBin ?
    508520      alphaHisto.GetNbinsX()- centerBin : centerBin;
    509    
    510    
     521         
    511522    const Int_t totalBins = centerBin;
    512523    Float_t alpha[totalBins];
    513524    Float_t signi[totalBins];
    514 
     525    Float_t maxSigni = 0;
     526   
    515527    for (Int_t i=0; i < maxBin; i++) {
    516528      double nOn = 0;  double eNOn = 0;
     
    529541         alphaHisto.GetXaxis()->GetBinLowEdge(centerBin-i))/2;
    530542      signi[i] = CalcSignificance(nOn, nOff, 1); 
     543      maxSigni = maxSigni > signi[i] ? maxSigni : signi[i];
    531544    }
    532        
    533     TCanvas *c3 = new TCanvas("c3"+funcName, "Significance vs ALPHA cut", 800,600);
    534     c3->cd();
     545
     546    if (!drawPad) {
     547      TCanvas *c3 = new TCanvas("c3"+funcName, "Significance vs ALPHA cut", 800,600);
     548      c3->cd();
     549    }
     550
     551    if (drawPad)
     552      drawPad->cd(drawBase-1);
     553   
    535554    TGraph* gr = new TGraph(totalBins-2,alpha,signi);
    536     gr->Draw("ACP");
    537     gr->GetXaxis()->SetTitle("|#alpha_{max}|");
    538     gr->GetYaxis()->SetTitle("Significance");
     555    TString drawOpt = "L";
     556   
     557    if (draw || (drawPad && fSigniPlotIndex == 0))
     558      drawOpt += "A";
     559   
     560    gr->Draw(drawOpt);
     561    if (drawPad && fSigniPlotIndex == 0) {
     562      gr->GetXaxis()->SetTitle("|#alpha_{max}|");
     563      gr->GetYaxis()->SetTitle("Significance");
     564    }
    539565    gr->SetMarkerStyle(2);
    540566    gr->SetMarkerSize(1);
    541     gr->SetMarkerColor(4);
    542     gr->SetLineColor(4);
    543     gr->SetLineWidth(2);
     567    gr->SetMarkerColor(4+fSigniPlotIndex);
     568    gr->SetLineColor(4+fSigniPlotIndex);
     569    gr->SetLineWidth(1);
    544570    gr->SetTitle("Significance vs ALPHA integration range");
    545571    gr->Draw("LP");
     572
     573    fSigniPlotIndex++;     
     574   
    546575  } //draw
    547576
     
    724753    // So now we can indeed extract the signal.
    725754         
    726     cout << "STARTIGN REAL CALC "<< endl;
     755//     cout << "STARTIGN REAL CALC "<< endl;
    727756
    728757    sumSigLiMa = 0;
     
    757786
    758787    //fit exponential function to data
    759     TF1 *e = new TF1("expF","expo",0,5);
     788    TF1* e = new TF1("expF","expo",0,5);
    760789    e->SetLineWidth(3);
    761790    e->SetLineColor(thetaBin);
     
    847876  // -------------------------------------
    848877  MBinning *binsmh3x = new MBinning("BinningMH3X");
     878  // dummy binning, real one follows some lines down
    849879  binsmh3x->SetEdgesLog(energyBins,aetHisto->GetYaxis()->GetBinLowEdge(1),
    850                      aetHisto->GetYaxis()->GetBinLowEdge(energyBins+1));
     880                        aetHisto->GetYaxis()->GetBinLowEdge(energyBins+1));
    851881  parlist->AddToList(binsmh3x); 
    852882
    853883  MBinning *binsmh3y = new MBinning("BinningMH3Y");
     884  // dummy binning, real one follows some lines down
    854885  binsmh3y->SetEdges(thetaBins,aetHisto->GetZaxis()->GetBinLowEdge(1),
    855886                     aetHisto->GetZaxis()->GetBinLowEdge(thetaBins+1));
     
    894925  signalTH2DHist.SetXTitle("E [GeV]");
    895926  signalTH2DHist.SetYTitle("theta [deg]");
     927  signalHisto->SetBinning(&signalTH2DHist, aetHisto->GetYaxis(), aetHisto->GetZaxis());
    896928  signalTH2DHist.Sumw2();
    897929 
     
    900932  offTH2DHist.SetXTitle("E [GeV]");
    901933  offTH2DHist.SetYTitle("theta [deg]");
     934  offHisto->SetBinning(&offTH2DHist, aetHisto->GetYaxis(), aetHisto->GetZaxis());
    902935  offTH2DHist.Sumw2(); 
    903936
     
    906939  signifTH2DHist.SetXTitle("E [GeV]");
    907940  signifTH2DHist.SetYTitle("theta [deg]");
     941  signifHisto->SetBinning(&signifTH2DHist, aetHisto->GetYaxis(), aetHisto->GetZaxis());
    908942  signifTH2DHist.Sumw2();
    909943 
     
    10051039 
    10061040    c4->cd((energyBins+3)*(thetaBin-1)+2);
    1007    
    10081041    sprintf(hname,"c4_%d",(energyBins+3)*(thetaBin-1)+2);     
    10091042    TPad* tp = (TPad*)(gROOT->FindObject(hname));
    1010     tp->SetLogx();
    1011    
     1043    tp->SetLogx();     
    10121044    signalTH1DHist.SetLineColor(2);
    1013     signalTH1DHist.DrawCopy();
    1014    
     1045    signalTH1DHist.DrawCopy();     
    10151046    offTH1DHist.SetLineColor(4);
    1016     offTH1DHist.DrawCopy("SAME");
    1017    
    1018    
     1047    offTH1DHist.DrawCopy("SAME");         
    10191048    c4->Update();
    1020    
    1021    
    1022    
     1049           
    10231050    signalTH2DHist.SetEntries(signalTH2DHist.GetEntries()+signalTH1DHist.GetEntries());
    10241051    offTH2DHist.SetEntries(offTH2DHist.GetEntries()+offTH1DHist.GetEntries());
     
    10321059  }
    10331060
     1061
    10341062  c4->Update();
    1035  
     1063
    10361064  return kTRUE;
    10371065}
     
    10841112Bool_t MHOnSubtraction::TH2Calc(TH2 *aeHisto, MParList *parlist, const Bool_t draw, TPad *drawPad, Int_t drawBase)
    10851113{
     1114
     1115  fSigniPlotColor = 0;
     1116
    10861117  // Analyze aeHisto
    10871118  // -------------------------------------
     
    11131144    signalHisto->SetTitle("Extracted Signal");
    11141145    parlist->AddToList(signalHisto);
    1115  
     1146    signalHisto->SetBinning(&((TH1D&)signalHisto->GetHist()),
     1147                            aeHisto->GetYaxis());
     1148
    11161149    offHisto = new MH3(1); // Off(E)
    11171150    offHisto->SetName("MHOnSubtractionOff");
    11181151    offHisto->SetTitle("Off Signal");
    11191152    parlist->AddToList(offHisto);
     1153    offHisto->SetBinning(&((TH1D&)offHisto->GetHist()),
     1154                            aeHisto->GetYaxis());
    11201155   
    11211156    signifHisto = new MH3(1); // Significance(E)
     
    11231158    signifHisto->SetTitle("Significance");
    11241159    parlist->AddToList(signifHisto);
     1160    signifHisto->SetBinning(&((TH1D&)signifHisto->GetHist()),
     1161                            aeHisto->GetYaxis());
     1162
    11251163  }
    11261164  if (fChiSquareHisto==0x0)
     
    11421180
    11431181  TH1F* alphaHisto[energyBins];
     1182
     1183  fSigniPlotIndex = 0; // cf. ExtractSignal
    11441184 
    11451185  for (Int_t energyBin = 1; energyBin < energyBins+1; energyBin++) {
     
    12381278                    sigLiMa, minLowerBin, maxUpperBin,
    12391279                    gammaSignal, errorGammaSignal, off, errorOff, (Float_t)3, drawFit,
    1240                     funcName);
     1280                    funcName, drawPad, drawBase);
    12411281
    12421282      sumSigLiMa += sigLiMa;     
     
    13041344//
    13051345// Extraction of Gamma signal is performed on a TH1 histogram in ALPHA
     1346//
    13061347//
    13071348Bool_t MHOnSubtraction::Calc(TH1 *aHisto, MParList *parlist,
    1308                              const Bool_t Draw, Float_t signalRegion)
     1349                             const Bool_t draw, Float_t signalRegion)
    13091350{
    13101351  // Find signal region and estimate significance
     
    13231364
    13241365  ExtractSignal(*aHisto, sigLiMa, lowerBin, upperBin,
    1325                 gammaSignal, errorGammaSignal, off, errorOff, (Float_t)3.0, kTRUE);
     1366                gammaSignal, errorGammaSignal, off, errorOff,
     1367                (Float_t)3.0, draw);
    13261368 
    13271369  *fLog << inf << "Signal is "
  • trunk/MagicSoft/Mars/mhist/MHOnSubtraction.h

    r2164 r2170  
    1717#include "TPaveLabel.h"
    1818#endif
     19#ifndef ROOT_TFile
     20#include "TFile.h"
     21#endif
     22
    1923
    2024
     
    4145  Double_t fSlope;             // slope for exponential fit
    4246  Int_t fThetaBin;
     47  Int_t fSigniPlotIndex;
     48  Int_t fSigniPlotColor;
     49
     50  Bool_t CalcAET(TH3D *histon, MParList *parlist, const Bool_t Draw);
     51
     52  Bool_t FitHistogram(TH1 &alphaHisto, Double_t &sigLiMa,
     53                      Double_t &lowerBin, Double_t &upperBin,
     54                      Float_t signalRegionFactor = 3, const Bool_t draw = kFALSE,
     55                      TString funcName = "");
     56
     57  Bool_t ExtractSignal(TH1 &alphaHisto, Double_t &sigLiMa,
     58                       Double_t &lowerBin, Double_t &upperBin,
     59                       Double_t &gammaSignal, Double_t &errorGammaSignal,
     60                       Double_t &off, Double_t &errorOff,
     61                       Float_t signalRegionFactor = 3, const Bool_t draw = kFALSE,
     62                       TString funcName = "", TPad *drawPad = 0, Int_t drawBase = 0);
     63
     64
    4365
    4466public:
     
    5577
    5678  void SetExpoSlope(Double_t slope) { fSlope = slope; }
    57 
    58   Bool_t FitHistogram(TH1 &alphaHisto, Double_t &sigLiMa,
    59                       Double_t &lowerBin, Double_t &upperBin,
    60                       Float_t signalRegionFactor = 3, const Bool_t draw = kFALSE,
    61                       TString funcName = "");
    62 
    63   Bool_t ExtractSignal(TH1 &alphaHisto, Double_t &sigLiMa,
    64                        Double_t &lowerBin, Double_t &upperBin,
    65                        Double_t &gammaSignal, Double_t &errorGammaSignal,
    66                        Double_t &off, Double_t &errorOff,
    67                        Float_t signalRegionFactor = 3, const Bool_t draw = kFALSE,
    68                        TString funcName = "");
    6979 
    7080  Bool_t Calc(MParList *parlist, const Bool_t Draw);
    71   Bool_t CalcAET(TH3D *histon, MParList *parlist, const Bool_t Draw);
    7281
    7382  Bool_t Calc(TH3 *histon, MParList *parlist, const Bool_t Draw);
    74   Bool_t TH2Calc(TH2 *aeHisto, MParList *parlist, const Bool_t Draw, TPad *drawPad = 0, Int_t drawBase = 0);
     83  Bool_t TH2Calc(TH2 *aeHisto, MParList *parlist, const Bool_t Draw,
     84                 TPad *drawPad = 0, Int_t drawBase = 0);
    7585  Bool_t Calc(TH1 *histon, MParList *parlist, const Bool_t Draw,
    7686              Float_t signalRegion = 0);
Note: See TracChangeset for help on using the changeset viewer.