Ignore:
Timestamp:
08/10/10 12:27:00 (14 years ago)
Author:
tbretz
Message:
Added the possibility to set a conversion function which is applied
before the histogram is displayed.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mhbase/MH3.cc

    r9576 r9821  
    1818!   Author(s): Thomas Bretz  2002 <mailto:tbretz@astro.uni-wuerzburg.de>
    1919!
    20 !   Copyright: MAGIC Software Development, 2000-2008
     20!   Copyright: MAGIC Software Development, 2000-2010
    2121!
    2222!
     
    143143//   + MBinning fBins[3]
    144144//
     145// Class Version 5:
     146// ----------------
     147//   + TFormula *fConversion
     148//
    145149/////////////////////////////////////////////////////////////////////////////
    146150#include "MH3.h"
     
    151155
    152156#include <TMath.h>
     157#include <TFormula.h>
    153158
    154159#include <THashList.h>
     
    200205    fScale[2] = 1;
    201206
     207    fConversion = NULL;
     208
    202209    fName  = gsDefName;
    203210    fTitle = gsDefTitle;
     
    239246        fHist = new TProfile;
    240247        fHist->SetYTitle("Average");
     248        static_cast<TProfile*>(fHist)->SetErrorOption("s");
    241249        break;
    242250    case 2:
     
    247255        fHist = new TProfile2D;
    248256        fHist->SetZTitle("Average");
     257        static_cast<TProfile2D*>(fHist)->SetErrorOption("s");
    249258        break;
    250259    case 3:
     
    331340    case -1:
    332341        fHist = static_cast<TH1*>(new TProfile);
     342        static_cast<TProfile*>(fHist)->SetErrorOption("s");
     343
    333344        break;
    334345    }
     
    352363    : fDimension(type==kHistogram?3:-2)
    353364{
    354     fHist = type&kProfile ? static_cast<TH1*>(new TProfile2D) : static_cast<TH1*>(new TH3D);
     365    if (type&kProfile)
     366    {
     367        fHist = static_cast<TH1*>(new TProfile2D);
     368        static_cast<TProfile2D*>(fHist)->SetErrorOption("s");
     369    }
     370    else
     371        fHist = static_cast<TH1*>(new TH3D);
     372
    355373
    356374    fData[0] = new MDataPhrase(memberx);
     
    370388        delete fHist;
    371389
     390    if (fConversion)
     391        delete fConversion;
     392
    372393    for (int i=0; i<4; i++)
    373394        if (fData[i])
     
    390411        delete fData[3];
    391412    fData[3] = new MDataPhrase(phrase);
     413}
     414
     415// --------------------------------------------------------------------------
     416//
     417// Set a function which is applied to the histogram before it is displayed.
     418// Note, that it only effects the displayed histogram.
     419//
     420//   e.g. SetConversion("sqrt(x)");
     421//
     422Bool_t MH3::SetConversion(const char *func)
     423{
     424    if (TString(func).IsNull())
     425    {
     426        delete fConversion;
     427        fConversion = 0;
     428        return kTRUE;
     429    }
     430
     431    fConversion = new TFormula;
     432
     433    // Must have a name otherwise all axis labels disappear like a miracle
     434    fConversion->SetName("ConversionFunction");
     435    if (fConversion->Compile(func))
     436    {
     437        *fLog << err << dbginf << "Syntax Error: TFormula::Compile failed for " << func << endl;
     438        delete fConversion;
     439        fConversion = 0;
     440        return kFALSE;
     441    }
     442
     443    gROOT->GetListOfFunctions()->Remove(fConversion);
     444
     445    return kTRUE;
    392446}
    393447
     
    845899// --------------------------------------------------------------------------
    846900//
     901// Apply the conversion function to the contents stored in fHist and
     902// store the result in h.
     903//
     904void MH3::Convert(TH1 &h) const
     905{
     906    for (Int_t z=0; z<=h.GetNbinsZ()+1; z++)
     907        for (Int_t y=0; y<=h.GetNbinsY()+1; y++)
     908            for (Int_t x=0; x<=h.GetNbinsX()+1; x++)
     909            {
     910                h.SetBinContent(x, y, z, fConversion->Eval(fHist->GetBinContent(x, y, z)));
     911                h.SetBinError(  x, y, z, fConversion->Eval(fHist->GetBinError(  x, y, z)));
     912            }
     913
     914    if (h.InheritsFrom(TProfile::Class()))
     915        for (Int_t x=0; x<=h.GetNbinsX()+1; x++)
     916            static_cast<TProfile&>(h).SetBinEntries(x, 1);
     917
     918    if (h.InheritsFrom(TProfile2D::Class()))
     919        for (Int_t x=0; x<=h.GetNbinsX()+1; x++)
     920            static_cast<TProfile2D&>(h).SetBinEntries(x, 1);
     921}
     922
     923// --------------------------------------------------------------------------
     924//
    847925// FIXME
    848926//
     
    853931    if (TMath::Abs(fDimension)==2)
    854932        MH::SetPalette("pretty");
     933
     934    if (fConversion)
     935    {
     936        TH1 *h = 0;
     937        if ((h=dynamic_cast<TH1*>(gPad->FindObject(fHist->GetName()))))
     938            Convert(*h);
     939    }
    855940
    856941    const TString pfx(MString::Format("%sProfX", fHist->GetName()));
     
    9631048    }
    9641049
     1050
     1051    TH1 *h = fHist;
     1052
     1053    if (fConversion)
     1054    {
     1055        h = static_cast<TH1*>(fHist->Clone());
     1056        h->SetDirectory(0);
     1057        h->SetBit(kCanDelete);
     1058
     1059        Convert(*h);
     1060    }
     1061
    9651062    // FIXME: We may have to remove all our own options from str!
    9661063    if (!only)
    967         fHist->Draw(str);
     1064        h->Draw(str);
    9681065
    9691066    AppendPad();
     
    9721069    if (profx)
    9731070    {
    974         const TString pfx(MString::Format("%sProfX", fHist->GetName()));
     1071        const TString pfx(MString::Format("%sProfX", h->GetName()));
    9751072
    9761073        if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
    9771074            *fLog << warn << "TProfile " << pfx << " already in pad." << endl;
    9781075
    979         p = ((TH2*)fHist)->ProfileX(pfx, -1, -1, "s");
     1076        p = ((TH2*)h)->ProfileX(pfx, -1, -1, "s");
    9801077        p->UseCurrentStyle();
    981         p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
     1078        p->SetLineColor(blue ? kBlue : h->GetLineColor());
    9821079        p->SetBit(kCanDelete);
    9831080        p->SetDirectory(NULL);
    984         p->SetXTitle(fHist->GetXaxis()->GetTitle());
    985         p->SetYTitle(fHist->GetYaxis()->GetTitle());
     1081        p->SetXTitle(h->GetXaxis()->GetTitle());
     1082        p->SetYTitle(h->GetYaxis()->GetTitle());
    9861083        p->Draw(only&&!same?"":"same");
    9871084    }
    9881085    if (profy)
    9891086    {
    990         const TString pfy(MString::Format("%sProfY", fHist->GetName()));
     1087        const TString pfy(MString::Format("%sProfY", h->GetName()));
    9911088
    9921089        if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
    9931090            *fLog << warn << "TProfile " << pfy << " already in pad." << endl;
    9941091
    995         p = ((TH2*)fHist)->ProfileY(pfy, -1, -1, "s");
     1092        p = ((TH2*)h)->ProfileY(pfy, -1, -1, "s");
    9961093        p->UseCurrentStyle();
    997         p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
     1094        p->SetLineColor(blue ? kBlue : h->GetLineColor());
    9981095        p->SetBit(kCanDelete);
    9991096        p->SetDirectory(NULL);
    1000         p->SetYTitle(fHist->GetXaxis()->GetTitle());
    1001         p->SetXTitle(fHist->GetYaxis()->GetTitle());
     1097        p->SetYTitle(h->GetXaxis()->GetTitle());
     1098        p->SetXTitle(h->GetYaxis()->GetTitle());
    10021099        p->Draw(only&&!same?"":"same");
    10031100    }
Note: See TracChangeset for help on using the changeset viewer.