Ignore:
Timestamp:
08/20/07 11:04:58 (17 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mhflux
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mhflux/MMcSpectrumWeight.cc

    r8674 r8680  
    1919!   Author(s): Marcos Lopez 10/2003 <mailto:marcos@gae.ucm.es>
    2020!
    21 !   Copyright: MAGIC Software Development, 2000-2006
     21!   Copyright: MAGIC Software Development, 2000-2007
    2222!
    2323!
     
    4040//  Method:
    4141//  -------
    42 //                                 
     42//
    4343//   - Corsika spectrun: dN/dE = A * E^(a)
    4444//     with a = fOldSlope, and A = N/integral{E*de} from ELowLim to EUppLim
     
    7272//    MMcEvt
    7373//    MMcCorsikaRunHeader
     74//    [MPointingPos]
     75//    [MHillas]
    7476//
    7577//  Output Container:
     
    8183#include <TF1.h>
    8284#include <TH1.h>
     85#include <TH2.h>
     86#include <TH3.h>
    8387#include <TSpline.h>
    8488
     
    109113    fNameMcEvt   = "MMcEvt";
    110114
    111     fNewSlope    = -9;
    112     fOldSlope    = -9;
     115    fNewSlope    = -99;
     116    fOldSlope    = -99;
    113117
    114118    fEnergyMin   = -1;
     
    210214// The slope is returned as %.3f
    211215//
    212 TString MMcSpectrumWeight::GetFormulaSpecOld() const
    213 {
    214     return Form("pow(%s.fEnergy, %.3f)", fNameMcEvt.Data(), fOldSlope);
     216TString MMcSpectrumWeight::GetFormulaSpecOld(const char *name) const
     217{
     218    return Form("pow(%s.fEnergy, %.3f)", name, fOldSlope);
    215219}
    216220
     
    225229// unchanged.
    226230//
    227 TString MMcSpectrumWeight::GetFormulaSpecNew() const
    228 {
    229     TString str = fFormula.IsNull() ? Form("pow(%s.fEnergy, %.3f)", fNameMcEvt.Data(), fNewSlope) : fFormula.Data();
     231TString MMcSpectrumWeight::GetFormulaSpecNew(const char *name) const
     232{
     233    TString str = fFormula.IsNull() ? Form("pow(%s.fEnergy, %.3f)", name, fNewSlope) : fFormula.Data();
    230234    if (!fFormula.IsNull())
    231         str.ReplaceAll("X", Form("(%s.fEnergy)", fNameMcEvt.Data()));
     235        str.ReplaceAll("X", Form("(%s.fEnergy)", name));
    232236
    233237    return str;
     
    258262// Example: 0.3712780019*(pow(MMcEvt.fEnergy,-2.270))/(pow(MMcEvt.fEnergy,-2.600))
    259263//
    260 TString MMcSpectrumWeight::GetFormulaWeights() const
     264TString MMcSpectrumWeight::GetFormulaWeights(const char *name) const
    261265{
    262266    if (GetFormulaSpecOld()==GetFormulaSpecNew())
     
    268272    const Double_t norm = fNorm*iold/inew;
    269273
    270     return Form("%.16e*(%s)/(%s)", norm, GetFormulaSpecNew().Data(), GetFormulaSpecOld().Data());
     274    return Form("%.16e*(%s)/(%s)", norm, GetFormulaSpecNew(name).Data(), GetFormulaSpecOld(name).Data());
    271275}
    272276
     
    276280// GetFormulaSpecNewX() describing the destination spectrum
    277281//
    278 Double_t MMcSpectrumWeight::GetSpecNewIntegral() const
     282Double_t MMcSpectrumWeight::GetSpecNewIntegral(Double_t emin, Double_t emax) const
    279283{
    280284    TF1 funcnew("Dummy", GetFormulaSpecNewX());
    281     return funcnew.Integral(fEnergyMin, fEnergyMax);
     285    return funcnew.Integral(emin, emax);
    282286}
    283287
     
    287291// GetFormulaSpecOldX() describing the simulated spectrum
    288292//
    289 Double_t MMcSpectrumWeight::GetSpecOldIntegral() const
     293Double_t MMcSpectrumWeight::GetSpecOldIntegral(Double_t emin, Double_t emax) const
    290294{
    291295    TF1 funcold("Dummy", GetFormulaSpecOldX());
    292     return funcold.Integral(fEnergyMin, fEnergyMax);
     296    return funcold.Integral(emin, emax);
    293297}
    294298
     
    357361    if (fEnergyMax>fEnergyMin && !fAllowChange)
    358362    {
     363        if (TMath::Abs(fEnergyMax-rh.GetEUppLim())>1e-10)
     364        {
     365            *fLog << err;
     366            *fLog << "ERROR - The maximum simulated Monte Carlo energy is not allowed to change (";
     367            *fLog << "(" << fEnergyMax << " --> " << rh.GetEUppLim() << ")... abort." << endl;
     368            return kFALSE;
     369        }
     370
    359371        if (TMath::Abs(fOldSlope-rh.GetSlopeSpec())>1e-10)
    360372        {
     
    364376            return kFALSE;
    365377        }
    366         if (TMath::Abs(fEnergyMin-rh.GetELowLim())>1e-10)
    367         {
    368             *fLog << err;
    369             *fLog << "ERROR - The minimum simulated Monte Carlo energy is not allowed to change ";
    370             *fLog << "(" << fEnergyMin << " --> " << rh.GetELowLim() << ")... abort." << endl;
    371             return kFALSE;
    372         }
    373         if (TMath::Abs(fEnergyMax-rh.GetEUppLim())>1e-10)
    374         {
    375             *fLog << err;
    376             *fLog << "ERROR - The maximum simulated Monte Carlo energy is not allowed to change (";
    377             *fLog << "(" << fEnergyMax << " --> " << rh.GetEUppLim() << ")... abort." << endl;
    378             return kFALSE;
    379         }
    380         return kTRUE;
     378
     379        // No change happened
     380        if (TMath::Abs(fEnergyMin-rh.GetELowLim())<=1e-10)
     381            return kTRUE;
     382
     383        // The lower energy limit has changed
     384        *fLog << warn;
     385        *fLog << "The minimum simulated Monte Carlo energy has changed from ";
     386        *fLog << fEnergyMin << "GeV to " << rh.GetELowLim() << "GeV." << endl;
     387        fEnergyMin = rh.GetELowLim();
    381388    }
    382389
     
    385392    fEnergyMax = rh.GetEUppLim();
    386393
    387     if (fNewSlope==-9)
    388     {
    389         *fLog << inf << "The new slope of the power law is undefined (-9)... no weighting applied." << endl;
     394    if (fNewSlope==-99)
     395    {
     396        *fLog << inf << "A new slope for the power law has not been defined... no weighting applied." << endl;
    390397        fNewSlope = fOldSlope;
    391398    }
     
    403410// ---------------------------------------------------------------------------
    404411//
     412// completes a simulated spectrum starting at an energy fEnergyMin down to
     413// an energy emin.
     414//
     415// It is assumed that the contents of MMcSpectrumWeight for the new spectrum
     416// correctly describe the spectrum within the histogram, and fEnergyMin
     417// and fEnergyMax correctly describe the range.
     418//
     419// In the 1D case it is assumed that the x-axis is a zenith angle binning.
     420// In the 2D case the x-axis is assumed to be zenith angle, the y-axis
     421// to be energy.
     422//
     423void MMcSpectrumWeight::CompleteEnergySpectrum(TH1 &h, Double_t emin) const
     424{
     425    if (h.InheritsFrom(TH3::Class()))
     426    {
     427        *fLog << "ERROR - MMcSpctrumWeight::CompleteEnergySpectrum doesn't support TH3." << endl;
     428        return;
     429    }
     430
     431    if (fEnergyMin <= emin)
     432        return;
     433
     434    const Double_t norm = GetSpecNewIntegral();
     435
     436    if (!h.InheritsFrom(TH2::Class()))
     437    {
     438        h.Scale(GetSpecNewIntegral(emin, fEnergyMax)/norm);
     439        return;
     440    }
     441
     442    const TAxis &axey = *h.GetYaxis();
     443
     444    const Int_t first = axey.FindFixBin(emin);
     445    const Int_t last  = axey.FindFixBin(fEnergyMin);
     446
     447    for (int x=1; x<=h.GetNbinsX(); x++)
     448    {
     449         const Double_t f = h.Integral(x, x, -1, 9999)/norm;
     450
     451        for (int y=first; y<=last; y++)
     452        {
     453            const Double_t lo = axey.GetBinLowEdge(y)  <emin       ? emin       : axey.GetBinLowEdge(y);
     454            const Double_t hi = axey.GetBinLowEdge(y+1)>fEnergyMin ? fEnergyMin : axey.GetBinLowEdge(y+1);
     455
     456            h.AddBinContent(h.GetBin(x, y), f*GetSpecNewIntegral(lo, hi));
     457        }
     458    }
     459}
     460
     461// ---------------------------------------------------------------------------
     462//
    405463// The current contants are printed
    406464//
    407465void MMcSpectrumWeight::Print(Option_t *o) const
    408466{
     467    const TString opt(o);
     468
     469    const Bool_t hasnew = opt.Contains("new") || opt.IsNull();
     470    const Bool_t hasold = opt.Contains("old") || opt.IsNull();
     471
    409472    *fLog << all << GetDescriptor() << endl;
    410     *fLog << " Simulated energy range:   " << fEnergyMin << "GeV - " << fEnergyMax << "GeV" << endl;
    411     *fLog << " Simulated spectral slope: " << fOldSlope << endl;
    412     *fLog << " New spectral slope:       " << fNewSlope << endl;
     473
     474    if (hasold)
     475    {
     476        *fLog << " Simulated energy range:   " << fEnergyMin << "GeV - " << fEnergyMax << "GeV" << endl;
     477        *fLog << " Simulated spectral slope: ";
     478        if (fOldSlope==-99)
     479            *fLog << "undefined" << endl;
     480        else
     481            *fLog << fOldSlope << endl;
     482    }
     483    if (hasnew)
     484    {
     485        *fLog << " New spectral slope:       ";
     486        if (fNewSlope==-99)
     487            *fLog << "undefined" << endl;
     488        else
     489            *fLog << fNewSlope << endl;
     490    }
    413491    *fLog << " Additional user norm.:    " << fNorm << endl;
    414492    *fLog << " Spectra are normalized:   " << (fNormEnergy<0?"by integral":Form("at %.1fGeV", fNormEnergy)) << endl;
    415     *fLog << " Old Spectrum:     " << GetFormulaSpecOldX() << "   (I=" << GetSpecOldIntegral() << ")" << endl;
    416     *fLog << " New Spectrum:     " << GetFormulaSpecNewX() << "   (I=" << GetSpecNewIntegral() << ")" << endl;
     493    if (hasold)
     494    {
     495        *fLog << " Old Spectrum: " << GetFormulaSpecOldX();
     496        if (fEnergyMin>=0 && fEnergyMax>0)
     497            *fLog << "   (I=" << GetSpecOldIntegral() << ")";
     498        *fLog << endl;
     499    }
     500    if (hasnew)
     501    {
     502        *fLog << " New Spectrum: " << GetFormulaSpecNewX();
     503        if (fEnergyMin>=0 && fEnergyMax>0)
     504            *fLog << "   (I=" << GetSpecNewIntegral() << ")";
     505        *fLog << endl;
     506    }
    417507    if (fFunc)
    418         *fLog << " Weight function:  " << fFunc->GetTitle()   << endl;
     508        *fLog << " Weight func:  " << fFunc->GetTitle()   << endl;
    419509}
    420510
     
    436526}
    437527
     528/*
     529 * This could be used to improve the Zd-weighting within a bin.
     530 * Another option is to use more bins, or both.
     531 * Note that it seems unnecessary, because the shape within the
     532 * theta-bins should be similar in data and Monte Carlo... hopefully.
     533 *
     534void MMcSpectrumWeight::InitZdWeights()
     535{
     536    TH2D w(*fWeightsZd);
     537
     538    for (int i=1; i<=w.GetNbinsX(); i++)
     539    {
     540         const Double_t tmin = w.GetBinLowEdge(i)  *TMath::DegToRad();
     541         const Double_t tmax = w.GetBinLowEdge(i+1)*TMath::DegToRad();
     542
     543         const Double_t wdth  = tmax-tmin;
     544         const Double_t integ = cos(tmin)-cos(tmax);
     545
     546         w.SetBinContent(i, w.GetBinContent(i)*wdth/integ);
     547    }
     548
     549    //  const Int_t i = fWeightsZd->GetXaxis()->FindFixBin(fPointing->GetZd());
     550    //  const Double_t theta = fPointing->GetZd()*TMath::DegToRad();
     551    //  w = sin(theta)*w.GetBinContent(i);
     552}
     553*/
     554
    438555// ----------------------------------------------------------------------------
    439556//
     
    447564    if (fWeightsZd)
    448565    {
    449         /*
    450          const Int_t i = fWeightsZd->GetXaxis()->FindFixBin(fPointing->GetZd());
    451 
    452          Double_t tmin = fWeightsZd->GetXaxis()->GetBinLowEdge(i)  *TMath::DegToRad();
    453          Double_t tmax = fWeightsZd->GetXaxis()->GetBinLowEdge(i+1)*TMath::DegToRad();
    454          Double_t wdth = fWeightsZd->GetXaxis()->GetBinWidth(i)    *TMath::DegToRad();
    455 
    456          Double_t cont = fWeightsZd->GetBinContent(i);
    457 
    458          Double_t integ = cos(tmin)-cos(tmax);
    459 
    460          w = sin(tmax)*cont/integ*wdth;
    461          */
    462 
    463566        const Int_t i = fWeightsZd->GetXaxis()->FindFixBin(fPointing->GetZd());
    464567        w = fWeightsZd->GetBinContent(i);
     
    472575
    473576    const Double_t e = fMcEvt->GetEnergy();
     577
    474578    fWeight->SetVal(fFunc->Eval(e)*w);
    475579
  • trunk/MagicSoft/Mars/mhflux/MMcSpectrumWeight.h

    r7688 r8680  
    7777
    7878    // Getter
    79     TString GetFormulaSpecOld() const;
    80     TString GetFormulaSpecNew() const;
    81     TString GetFormulaWeights() const;
     79    TString GetFormulaSpecOld(const char *name) const;
     80    TString GetFormulaSpecNew(const char *name) const;
     81    TString GetFormulaWeights(const char *name) const;
     82    TString GetFormulaSpecOld() const { return GetFormulaSpecOld(fNameMcEvt); }
     83    TString GetFormulaSpecNew() const { return GetFormulaSpecNew(fNameMcEvt); }
     84    TString GetFormulaWeights() const { return GetFormulaWeights(fNameMcEvt); }
    8285
    8386    TString GetFormulaSpecOldX() const { return ReplaceX(GetFormulaSpecOld()); }
     
    8588    TString GetFormulaWeightsX() const { return ReplaceX(GetFormulaWeights()); }
    8689
    87     Double_t GetSpecNewIntegral() const;
    88     Double_t GetSpecOldIntegral() const;
     90    Double_t GetSpecNewIntegral(Double_t emin, Double_t emax) const;
     91    Double_t GetSpecOldIntegral(Double_t emin, Double_t emax) const;
     92
     93    Double_t GetSpecNewIntegral() const { return GetSpecNewIntegral(fEnergyMin, fEnergyMax); }
     94    Double_t GetSpecOldIntegral() const { return GetSpecOldIntegral(fEnergyMin, fEnergyMax); }
    8995
    9096    Double_t CalcSpecNew(Double_t e) const;
     
    9399    Double_t GetEnergyMin() const { return fEnergyMin; }
    94100    Double_t GetEnergyMax() const { return fEnergyMax; }
     101
     102    // Functions
     103    void CompleteEnergySpectrum(TH1 &h, Double_t emin) const;
    95104
    96105    // TObject
Note: See TracChangeset for help on using the changeset viewer.