Ignore:
Timestamp:
10/24/06 09:26:10 (18 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mextralgo
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mextralgo/MExtralgoDigitalFilter.cc

    r8072 r8154  
    3838}
    3939
     40// -----------------------------------------------------------------------------
     41//
     42// Calculates the chi2 of the fit, once the weights have been iterated.
     43// Argument: time, obtained after a call to EvalDigitalFilterHiGain
     44//
     45Float_t MExtralgoDigitalFilter::GetChisq(const Int_t maxp, const Int_t frac, const Float_t sum) const
     46{
     47    /*
     48    TMatrix g (windowh,1);
     49    TMatrix gt(windowh,1);
     50    TMatrix y (windowh,1);
     51
     52    const Float_t etau   = fFineAdjustHi*sumhi;
     53    const Float_t t_fine = TMath::Abs(fFineAdjustHi)< 1./fBinningResolutionHiGain ? -fFineAdjustHi : 0.;
     54
     55    //   if (t_fine==0.)
     56    //     return -1.;
     57
     58    if (fDebug)
     59        gLog << all << " fMaxPHi: " << fMaxPHi << " fIterPHi " << fIterPHi << " t_fine: " << t_fine << endl;
     60
     61    //
     62    // Slide with a window of size windowh over the sample
     63    // and calculate the arrays by interpolating the pulse shape using the
     64    // fine-tuned time information.
     65    //
     66    for (Int_t sample=0; sample &lt; windowh; sample++)
     67    {
     68        const Int_t idx = fArrBinningResHalfHiGain[sample] + fIterPHi;
     69        const Int_t ids = fMaxPHi + sample;
     70
     71        y [sample][0]     = fHiGainSignalDF[ids];
     72        g [sample][0]     = t_fine >= 0
     73            ? (fPulseShapeHiGain[idx]    + t_fine*(fPulseShapeHiGain[idx+1]   -fPulseShapeHiGain[idx])  )*sumhi
     74            : (fPulseShapeHiGain[idx]    + t_fine*(fPulseShapeHiGain[idx]     -fPulseShapeHiGain[idx-1]))*sumhi;
     75        gt[sample][0]     = t_fine >= 0
     76            ? (fPulseShapeDotHiGain[idx] + t_fine*(fPulseShapeDotHiGain[idx+1]-fPulseShapeDotHiGain[idx])   )*etau
     77            : (fPulseShapeDotHiGain[idx] + t_fine*(fPulseShapeDotHiGain[idx]  -fPulseShapeDotHiGain[idx-1]) )*etau;
     78    }
     79
     80    TMatrix second = y - g - gt;
     81    TMatrix first(TMatrix::kTransposed,second);
     82    TMatrix chisq = first * ((*fBHiInv)*second);
     83
     84    return chisq[0][0]/(windowh-2);
     85    */
     86/*
     87
     88    TMatrix S(fWindowSize, 1); // Signal (start==start of window)
     89    for (int i=0; i<fWindowSize; i++)
     90        S[i][0] = fVal[i+maxp];
     91
     92    TMatrix g(fWindowSize, 1);
     93    //TMatrix gT(fWindowSize, 1);
     94
     95    for (int i=0; i<fWindowSize; i++)
     96    {
     97        Int_t idx = fWeightsPerBin*i + frac;
     98
     99        // FIXME: Maybe we could do an interpolation on time-fineadj?
     100        //Float_t slope  = fPulseShapeHiGain[idx+1]   -fPulseShapeHiGain[idx];
     101        //Float_t slopet = fPulseShapeDotHiGain[idx+1]-fPulseShapeDotHiGain[idx];
     102
     103        g[i][0] = fPulseShapeHiGain[idx]   *sumhi;
     104        //gT[i][0] = fPulseShapeHiGainDot[idx]*tau;
     105    }
     106
     107    TMatrix Ainv; // Autocorrelation Matrix (inverted)
     108
     109    TMatrix m = S - g;// - gT;
     110    TMatrix mT(TMatrix::kTransposed, m);
     111
     112    TMatrix chisq = mT * (Ainv*m);
     113
     114    return chisq[0][0]/(fWindowSize-2);
     115  */
     116
     117    Double_t sumc = 0;
     118
     119    TMatrix d(fWindowSize, 1); // Signal (start==start of window)
     120    for (int i=0; i<fWindowSize; i++)
     121    {
     122        d[i][0] = fVal[i+maxp]/sum - fPulseShape[fWeightsPerBin*i + frac];
     123        sumc += d[i][0]*d[i][0];
     124    }
     125
     126/*
     127    TMatrix Ainv; // Autocorrelation Matrix (inverted)
     128
     129    TMatrix dT(TMatrix::kTransposed, d);
     130
     131    TMatrix chisq = dT * (*fAinv*d);
     132  */
     133    return sumc;//chisq[0][0]/(fWindowSize-2);
     134}
     135
    40136#include <iostream>
    41 void MExtralgoDigitalFilter::Extract()
     137void MExtralgoDigitalFilter::Extract(Int_t maxpos)
    42138{
    43139    fSignal    =  0; // default is: no pulse found
     
    54150    // Calculate the sum of the first fWindowSize slices
    55151    //
    56 
    57     // For the case of an even numberof weights/bin there is
     152    // For the case of an even number of weights/bin there is
    58153    // no central bin.So we create an artificial central bin.
    59154    for (Int_t i=0; i<fNum-fWindowSize+1; i++)
     
    66161        }
    67162    }
     163
     164    /*
     165     // This could be for a fast but less accurate extraction....
     166     maxamp = Eval(fWeightsAmp, maxpos-fWindowSize/2);
     167     maxp   = maxpos-fWindowSize/2;
     168     */
     169
    68170    // The number of available slices were smaller than the
    69171    // extraction window size of the extractor
     
    136238        fTime -= timefineadjust;
    137239}
     240
    138241
    139242#include <TH1.h>
     
    145248#include <TProfile.h>
    146249
    147 Bool_t MExtralgoDigitalFilter::CalculateWeights(TH1 &shape, const TH2 &autocorr, TArrayF &weightsamp, TArrayF &weightstime, Int_t wpb)
     250Int_t MExtralgoDigitalFilter::CalculateWeights(TH1 &shape, const TH2 &autocorr, TArrayF &weightsamp, TArrayF &weightstime, Int_t wpb)
    148251{
    149252    const Int_t weightsperbin = wpb<=0?shape.GetNbinsX()/autocorr.GetNbinsX():wpb;
     
    154257        cout << "        Shape: " << shape.GetNbinsX() << endl;
    155258        cout << "        ACorr: " << autocorr.GetNbinsX() << endl;
    156         return kFALSE;
     259        return -1;
    157260    }
    158261
     
    180283    // FIXME: DELETE!!!
    181284    TH1 &derivative = *static_cast<TH1*>(shape.Clone());
     285    derivative.SetDirectory(0);
    182286    derivative.Reset();
    183287
     
    233337        {
    234338            cout << "ERROR - Division by zero: denom[0][0]==0 for i=" << i << "." << endl;
    235             return kFALSE;
     339            return -1;
    236340        }
    237341
     
    248352    }
    249353
    250     return kTRUE;
     354    return first*weightsperbin;
    251355}
    252356
    253 void MExtralgoDigitalFilter::CalculateWeights2(TH1F &shape, const TH2F &autocorr, TArrayF &weightsamp, TArrayF &weightstime, Int_t wpb)
     357Int_t MExtralgoDigitalFilter::CalculateWeights2(TH1 &shape, const TH2 &autocorr, TArrayF &weightsamp, TArrayF &weightstime, Int_t wpb)
    254358{
    255359    const Int_t weightsperbin = wpb<=0?shape.GetNbinsX()/autocorr.GetNbinsX():wpb;
     
    260364        cout << "        Shape: " << shape.GetNbinsX() << endl;
    261365        cout << "        ACorr: " << autocorr.GetNbinsX() << endl;
    262         return;
     366        return -1;
    263367    }
    264368
     
    282386    TSpline5 val("Signal", &gr);
    283387
    284     TH1F derivative(shape);
     388    // FIXME: DELETE!!!
     389    TH1 &derivative = *static_cast<TH1*>(shape.Clone());
     390    derivative.SetDirectory(0);
    285391    derivative.Reset();
    286392
     
    331437        const TMatrixD denom  = (gT*(B*g))*(dT*(B*d)) - (dT*(B*g))*(dT*(B*g));
    332438
     439        if (denom[0][0]==0)
     440        {
     441            cout << "ERROR - Division by zero: denom[0][0]==0 for i=" << i << "." << endl;
     442            return -1;
     443        }
     444
    333445        const TMatrixD w_amp  = (dT*(B*d))*(gT*B) - (gT*(B*d))*(dT*B);
    334446        const TMatrixD w_time = (gT*(B*g))*(dT*B) - (gT*(B*d))*(gT*B);
     
    342454        }
    343455    }
     456    return first*weightsperbin;
    344457}
    345 /*
    346 Int_t start = fBinningResolutionHiGain*(fSignalStartBinHiGain + 1);
    347 for (Int_t i = -fBinningResolutionHiGain/2+1; i<=fBinningResolutionHiGain/2; i++)
    348 {
    349     // i = -4...5
    350     for (Int_t count=0; count < fWindowSizeHiGain; count++)
    351     {
    352         // count=0: pos=10*(start+0) + 10 + i
    353         // count=1: pos=10*(start+1) + 10 + i
    354         // count=2: pos=10*(start+2) + 10 + i
    355         // count=3: pos=10*(start+3) + 10 + i
    356     }
    357 
    358     for (Int_t count=0; count < fWindowSizeHiGain; count++)
    359     {
    360         // count=0: pos = 10*0 + 5 +i-1
    361         // count=1: pos = 10*1 + 5 +i-1
    362         // count=2: pos = 10*2 + 5 +i-1
    363         // count=3: pos = 10*3 + 5 +i-1
    364     }
    365 }
    366 
    367 Int_t start = fBinningResolutionLoGain*fSignalStartBinLoGain + fBinningResolutionLoGain/2;
    368 for (Int_t i = -fBinningResolutionLoGain/2+1; i<=fBinningResolutionLoGain/2; i++)
    369 {
    370     // i=-4..5
    371     for (Int_t count=0; count < fWindowSizeLoGain; count++)
    372     {
    373         // count=0: pos = 10*(start+0) + 5 + i
    374         // count=1: pos = 10*(start+1) + 5 + i
    375         // count=2: pos = 10*(start+2) + 5 + i
    376         // count=3: pos = 10*(start+3) + 5 + i
    377     }
    378 
    379     for (Int_t count=0; count < fWindowSizeLoGain; count++)
    380     {
    381         // count=0: pos = 10*0 + 5 +i-1
    382         // count=1: pos = 10*1 + 5 +i-1
    383         // count=2: pos = 10*2 + 5 +i-1
    384         // count=3: pos = 10*3 + 5 +i-1
    385     }
    386 }
    387 */
  • trunk/MagicSoft/Mars/mextralgo/MExtralgoDigitalFilter.h

    r8072 r8154  
    22#define MARS_MExtralgoDigitalFilter
    33
    4 #ifndef ROOT_TROOT
    5 #include <TROOT.h>
     4#ifndef ROOT_TMatrix
     5#include <TMatrix.h>
    66#endif
    77
     
    1212class TArrayF;
    1313
     14//#include <TMatrix.h>
     15
    1416class MExtralgoDigitalFilter
    1517{
    1618private:
    1719    // Input
    18     Float_t *fVal;
    19     Int_t    fNum;
     20    const Float_t *fVal;
     21    Int_t fNum;
    2022
    2123    Float_t const *fWeightsAmp;
    2224    Float_t const *fWeightsTime;
     25    Float_t const *fPulseShape;
     26
     27    const TMatrix *fAinv;
    2328
    2429    const Int_t fWeightsPerBin; // Number of weights per data bin
     
    3035    Float_t fSignal;
    3136    Float_t fSignalDev;
     37
     38    Float_t GetChisq(const Int_t maxp, const Int_t frac, const Float_t sum) const;
     39
     40    inline Double_t ChiSq(const Double_t sum, const Int_t startv, const Int_t startw=0) const
     41    {
     42        //
     43        // Slide with a window of size windowsize over the sample
     44        // and multiply the entries with the corresponding weights
     45        //
     46        Double_t chisq = 0;
     47
     48        // Shift the start of the weight to the center of sample 0
     49        Float_t const *w = fPulseShape + startw;
     50
     51        const Float_t *beg = fVal+startv;
     52        for (Float_t const *pex=beg; pex<beg+fWindowSize; pex++)
     53        {
     54            const Double_t c = *w - *pex/sum;
     55            chisq += c*c;
     56            w += fWeightsPerBin;
     57        }
     58        return chisq;
     59    }
    3260
    3361    // Weights: Weights to evaluate
     
    4573        Float_t const *w = weights + startw;
    4674
    47         Float_t *const beg = fVal+startv;
     75        const Float_t *beg = fVal+startv;
    4876        for (Float_t const *pex=beg; pex<beg+fWindowSize; pex++)
    4977        {
     
    115143
    116144public:
    117     MExtralgoDigitalFilter(Int_t res, Int_t windowsize, Float_t *wa, Float_t *wt)
     145    MExtralgoDigitalFilter(Int_t res, Int_t windowsize, Float_t *wa, Float_t *wt, Float_t *ps=0, TMatrix *ainv=0)
    118146        : fVal(0), fNum(0), fWeightsAmp(wa+res/2), fWeightsTime(wt+res/2),
    119         fWeightsPerBin(res), fWindowSize(windowsize),
     147        fPulseShape(ps), fAinv(ainv), fWeightsPerBin(res), fWindowSize(windowsize),
    120148        fTime(0), fTimeDev(-1), fSignal(0), fSignalDev(-1)
    121149    {
    122150    }
    123151
    124     void SetData(Int_t n, Float_t *val) { fNum=n; fVal=val; }
     152    void SetData(Int_t n, Float_t const *val) { fNum=n; fVal=val; }
    125153
    126154    Float_t GetTime() const          { return fTime; }
     
    134162
    135163    Float_t ExtractNoise(Int_t iter) const;
    136     void Extract();
     164    void Extract(Int_t maxpos=-1);
    137165
    138     static Bool_t CalculateWeights(TH1 &shape, const TH2 &autocorr, TArrayF &wa, TArrayF &wt, Int_t wpb=-1);
    139     static void CalculateWeights2(TH1F &shape, const TH2F &autocorr, TArrayF &wa, TArrayF &wt, Int_t wpb=-1);
     166    static Int_t CalculateWeights(TH1 &shape, const TH2 &autocorr, TArrayF &wa, TArrayF &wt, Int_t wpb=-1);
     167    static Int_t CalculateWeights2(TH1 &shape, const TH2 &autocorr, TArrayF &wa, TArrayF &wt, Int_t wpb=-1);
    140168};
    141169
  • trunk/MagicSoft/Mars/mextralgo/MExtralgoSpline.cc

    r7999 r8154  
    207207}
    208208
     209#include <TRandom.h>
    209210Float_t MExtralgoSpline::ExtractNoise(Int_t iter)
    210211{
    211     const Float_t nsx = iter * fResolution;
     212    const Float_t nsx = gRandom->Uniform(); //iter * fResolution;
    212213
    213214    if (fExtractionType == kAmplitude)
     
    223224    fSignalDev = -1;
    224225    fTimeDev   = -1;
    225 
     226/*
    226227    //
    227228    // Allow no saturated slice and
     
    229230    //
    230231
    231     /*
    232232    const Bool_t limlo = maxbin <      TMath::Ceil(fRiseTime);
    233233    const Bool_t limup = maxbin > fNum-TMath::Ceil(fFallTime)-1;
     
    248248        return;
    249249    }
    250     */
    251 
    252     fTimeDev = fResolution;
    253250
    254251    //
     
    256253    //
    257254
    258 
    259     /*
    260255    Float_t step = 0.2; // start with step size of 1ns and loop again with the smaller one
    261256
     
    346341        }
    347342    }
    348   */
    349 
     343
     344    fTime      = maxpos;
     345    fTimeDev   = fResolution;
     346    fSignal    = CalcIntegral(maxpos);
     347    fSignalDev = 0;  // means: is valid
     348
     349    return;
     350*/
    350351    // --- Start NEW ---
    351352
     
    369370
    370371    Float_t maxpos, maxval;
     372    // FIXME: Check the dfeault if no maximum found!!!
    371373    GetMaxAroundI(maxbin, maxpos, maxval);
    372374
     
    376378    {
    377379        fTime      = maxpos;
     380        fTimeDev   = fResolution;
    378381        fSignal    = maxval;
    379382        fSignalDev = 0;  // means: is valid
     
    381384    }
    382385
    383     //
    384     // Loop from the beginning of the slice upwards to reach the maxhalf:
    385     // With means of bisection:
    386     //
    387     /*
    388     static const Float_t sqrt2 = TMath::Sqrt(2.);
    389 
    390     step = sqrt2*3*0.061;//fRiseTime;
    391     Float_t x = maxpos-0.86-3*0.061;//fRiseTime*1.25;
    392 
    393 //    step = sqrt2*0.5;//fRiseTime;
    394 //    Float_t x = maxpos-1.25;//fRiseTime*1.25;
    395 
    396     Int_t  cnt  =0;
    397     while (cnt++<30)
    398     {
    399         const Float_t y=EvalAt(x);
    400 
    401         if (TMath::Abs(y-maxval/2)<fResolution)
    402             break;
    403 
    404         step /= sqrt2; // /2
    405         x += y>maxval/2 ? -step : +step;
    406     }
    407     */
    408 
    409386    // Search downwards for maxval/2
    410387    // By doing also a search upwards we could extract the pulse width
     
    412389
    413390    fTime      = x1;
     391    fTimeDev   = fResolution;
    414392    fSignal    = CalcIntegral(maxpos);
    415393    fSignalDev = 0;  // means: is valid
    416394
    417     //if (fSignal>100)
    418     //    cout << "V=" << maxpos-x1 << endl;
     395    //
     396    // Loop from the beginning of the slice upwards to reach the maxhalf:
     397    // With means of bisection:
     398    //
     399    /*
     400    static const Float_t sqrt2 = TMath::Sqrt(2.);
     401
     402    step = sqrt2*3*0.061;//fRiseTime;
     403    Float_t x = maxpos-0.86-3*0.061;//fRiseTime*1.25;
     404
     405//    step = sqrt2*0.5;//fRiseTime;
     406//    Float_t x = maxpos-1.25;//fRiseTime*1.25;
     407
     408    Int_t  cnt  =0;
     409    while (cnt++<30)
     410    {
     411        const Float_t y=EvalAt(x);
     412
     413        if (TMath::Abs(y-maxval/2)<fResolution)
     414            break;
     415
     416        step /= sqrt2; // /2
     417        x += y>maxval/2 ? -step : +step;
     418    }
     419    */
    419420
    420421    //
     
    422423    //
    423424    //   fTime      = cnt==31 ? -1 : x;
     425    //   fTimeDev   = fResolution;
    424426    //   fSignal    = cnt==31 ? CalcIntegral(x) : CalcIntegral(maxpos);
    425427    //   fSignalDev = 0;  // means: is valid
  • trunk/MagicSoft/Mars/mextralgo/MExtralgoSpline.h

    r7999 r8154  
    2121
    2222    // Input
    23     Float_t *fVal;
    24     Int_t    fNum;
     23    Float_t const *fVal;
     24    const Int_t    fNum;
    2525
    2626    Float_t *fDer1;
     
    238238
    239239        // Take a default in case no maximum is found
     240        // FIXME: Check THIS!!!
    240241        xmax=i;
    241242        ymax=fVal[i];
     
    438439
    439440public:
    440     MExtralgoSpline(Float_t *val, Int_t n, Float_t *der1, Float_t *der2)
     441    MExtralgoSpline(const Float_t *val, Int_t n, Float_t *der1, Float_t *der2)
    441442        : fExtractionType(kIntegral), fVal(val), fNum(n), fDer1(der1), fDer2(der2), fTime(0), fTimeDev(-1), fSignal(0), fSignalDev(-1)
    442443    {
Note: See TracChangeset for help on using the changeset viewer.