| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 8 | ! *
|
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 12 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 14 | ! * or implied warranty.
|
|---|
| 15 | ! *
|
|---|
| 16 | !
|
|---|
| 17 | !
|
|---|
| 18 | ! Author(s): Josep Flix 04/2001 <mailto:jflix@ifae.es>
|
|---|
| 19 | ! Author(s): Thomas Bretz 05/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 20 | ! Author(s): Sebastian Commichau 12/2003
|
|---|
| 21 | ! Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es>
|
|---|
| 22 | ! Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es>
|
|---|
| 23 | !
|
|---|
| 24 | ! Copyright: MAGIC Software Development, 2000-2004
|
|---|
| 25 | !
|
|---|
| 26 | !
|
|---|
| 27 | \* ======================================================================== */
|
|---|
| 28 |
|
|---|
| 29 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 30 | //
|
|---|
| 31 | // MPedCalcPedRun
|
|---|
| 32 | //
|
|---|
| 33 | // This task takes a pedestal run file and fills MPedestalCam during
|
|---|
| 34 | // the Process() with the pedestal and rms computed in an event basis.
|
|---|
| 35 | // In the PostProcess() MPedestalCam is finally filled with the pedestal
|
|---|
| 36 | // mean and rms computed in a run basis.
|
|---|
| 37 | // More than one run (file) can be merged
|
|---|
| 38 | //
|
|---|
| 39 | //
|
|---|
| 40 | // Actually, MPedCalcPedRun applies the following formula (1):
|
|---|
| 41 | //
|
|---|
| 42 | // PedRMS = Sqrt( (sum(x_i^2) - sum(x_i)^2/n) / n-1 / 14 )
|
|---|
| 43 | //
|
|---|
| 44 | // where x_i is the sum of 14 FADC slices and sum means the sum over all
|
|---|
| 45 | // events, n is the number of events.
|
|---|
| 46 | //
|
|---|
| 47 | // For a high number of events, this formula is equivalent to formula (2):
|
|---|
| 48 | //
|
|---|
| 49 | // PedRMS = Sqrt( (<x_i*x_i> - <x_i>*<x_i>) / 14 )
|
|---|
| 50 | //
|
|---|
| 51 | // where <> is the mean over all events and x_i again the sum over the 14
|
|---|
| 52 | // slices.
|
|---|
| 53 | //
|
|---|
| 54 | // If you assume statistical equivalence of all slices (say, all have equal
|
|---|
| 55 | // offset and are not correlated and fluctuate Gaussian), it should also be
|
|---|
| 56 | // equivalent to (old formula) (3):
|
|---|
| 57 | //
|
|---|
| 58 | // PedRMS = Sqrt( (<p_i*p_i> - <p_i>*<p_i>) )
|
|---|
| 59 | //
|
|---|
| 60 | // which is the RMS per slice of a single slice (p_i) and
|
|---|
| 61 | // <> the mean over the total number of measurements, i.e. n*14.
|
|---|
| 62 | //
|
|---|
| 63 | // If we assume that at least our pairs fluctuate independently and Gaussian,
|
|---|
| 64 | // then we can use the actual formula (1) in order to get
|
|---|
| 65 | // fluctuations of pairs by the transformation:
|
|---|
| 66 | //
|
|---|
| 67 | // PedRMS/pair = PedRMS (form. (3)) * Sqrt(2)
|
|---|
| 68 | //
|
|---|
| 69 | // (However, we know that our slice-to-slice fluctuations are not Gaussian
|
|---|
| 70 | // (and moreover asymmetric) and that they are also correlated.)
|
|---|
| 71 | //
|
|---|
| 72 | //
|
|---|
| 73 | // Input Containers:
|
|---|
| 74 | // MRawEvtData
|
|---|
| 75 | //
|
|---|
| 76 | // Output Containers:
|
|---|
| 77 | // MPedestalCam
|
|---|
| 78 | //
|
|---|
| 79 | //
|
|---|
| 80 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 81 | #include "MPedCalcPedRun.h"
|
|---|
| 82 |
|
|---|
| 83 | #include "MParList.h"
|
|---|
| 84 |
|
|---|
| 85 | #include "MLog.h"
|
|---|
| 86 | #include "MLogManip.h"
|
|---|
| 87 |
|
|---|
| 88 | #include "MRawRunHeader.h"
|
|---|
| 89 | #include "MRawEvtPixelIter.h"
|
|---|
| 90 | #include "MRawEvtData.h"
|
|---|
| 91 |
|
|---|
| 92 | #include "MPedestalPix.h"
|
|---|
| 93 | #include "MPedestalCam.h"
|
|---|
| 94 |
|
|---|
| 95 | #include "MExtractedSignalPix.h"
|
|---|
| 96 | #include "MExtractedSignalCam.h"
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | #include "MGeomCamMagic.h"
|
|---|
| 100 |
|
|---|
| 101 | ClassImp(MPedCalcPedRun);
|
|---|
| 102 |
|
|---|
| 103 | using namespace std;
|
|---|
| 104 |
|
|---|
| 105 | // --------------------------------------------------------------------------
|
|---|
| 106 | //
|
|---|
| 107 | // default constructor
|
|---|
| 108 | //
|
|---|
| 109 | MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
|
|---|
| 110 | : fRawEvt(NULL), fPedestals(NULL)
|
|---|
| 111 | {
|
|---|
| 112 | fName = name ? name : "MPedCalcPedRun";
|
|---|
| 113 | fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
|
|---|
| 114 |
|
|---|
| 115 | AddToBranchList("fHiGainPixId");
|
|---|
| 116 | AddToBranchList("fHiGainFadcSamples");
|
|---|
| 117 |
|
|---|
| 118 | Clear();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void MPedCalcPedRun::Clear(const Option_t *o)
|
|---|
| 122 | {
|
|---|
| 123 | fNumHiGainSamples = 0;
|
|---|
| 124 | fNumSamplesTot = 0;
|
|---|
| 125 |
|
|---|
| 126 | fRawEvt = NULL;
|
|---|
| 127 | fPedestals = NULL;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | // --------------------------------------------------------------------------
|
|---|
| 132 | //
|
|---|
| 133 | // Look for the following input containers:
|
|---|
| 134 | //
|
|---|
| 135 | // - MRawEvtData
|
|---|
| 136 | //
|
|---|
| 137 | // The following output containers are also searched and created if
|
|---|
| 138 | // they were not found:
|
|---|
| 139 | //
|
|---|
| 140 | // - MPedestalCam
|
|---|
| 141 | //
|
|---|
| 142 | Int_t MPedCalcPedRun::PreProcess( MParList *pList )
|
|---|
| 143 | {
|
|---|
| 144 | Clear();
|
|---|
| 145 |
|
|---|
| 146 | fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData");
|
|---|
| 147 | if (!fRawEvt)
|
|---|
| 148 | {
|
|---|
| 149 | *fLog << err << "MRawEvtData not found... aborting." << endl;
|
|---|
| 150 | return kFALSE;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
|
|---|
| 154 | if (!fPedestals)
|
|---|
| 155 | return kFALSE;
|
|---|
| 156 |
|
|---|
| 157 | return kTRUE;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | // --------------------------------------------------------------------------
|
|---|
| 161 | //
|
|---|
| 162 | // The ReInit searches for the following input containers:
|
|---|
| 163 | // - MRawRunHeader
|
|---|
| 164 | //
|
|---|
| 165 | // It also initializes the data arrays fSumx and fSumx2
|
|---|
| 166 | // (only for the first read file)
|
|---|
| 167 | //
|
|---|
| 168 | Bool_t MPedCalcPedRun::ReInit(MParList *pList)
|
|---|
| 169 | {
|
|---|
| 170 | const MRawRunHeader *runheader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
|
|---|
| 171 | if (!runheader)
|
|---|
| 172 | {
|
|---|
| 173 | *fLog << warn << dbginf;
|
|---|
| 174 | *fLog << "Warning - cannot check file type, MRawRunHeader not found." << endl;
|
|---|
| 175 | }
|
|---|
| 176 | else
|
|---|
| 177 | if (runheader->IsMonteCarloRun())
|
|---|
| 178 | return kTRUE;
|
|---|
| 179 |
|
|---|
| 180 | Int_t n = fPedestals->GetSize();
|
|---|
| 181 |
|
|---|
| 182 | if (fSumx.GetSize()==0)
|
|---|
| 183 | {
|
|---|
| 184 | fSumx.Set(n);
|
|---|
| 185 | fSumx2.Set(n);
|
|---|
| 186 |
|
|---|
| 187 | fSumx.Reset();
|
|---|
| 188 | fSumx2.Reset();
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | // Calculate an even number for the hi gain samples to avoid
|
|---|
| 192 | // biases due to the fluctuation in pedestal from one slice to
|
|---|
| 193 | // the other one
|
|---|
| 194 | fNumHiGainSamples = runheader->GetNumSamplesHiGain() & ~1;
|
|---|
| 195 |
|
|---|
| 196 | return kTRUE;
|
|---|
| 197 | }
|
|---|
| 198 | // --------------------------------------------------------------------------
|
|---|
| 199 | //
|
|---|
| 200 | // Fill the MPedestalCam container with the signal mean and rms for the event.
|
|---|
| 201 | // Store the measured signal in arrays fSumx and fSumx2 so that we can
|
|---|
| 202 | // calculate the overall mean and rms in the PostProcess()
|
|---|
| 203 | //
|
|---|
| 204 | Int_t MPedCalcPedRun::Process()
|
|---|
| 205 | {
|
|---|
| 206 | MRawEvtPixelIter pixel(fRawEvt);
|
|---|
| 207 |
|
|---|
| 208 | while (pixel.Next())
|
|---|
| 209 | {
|
|---|
| 210 | Byte_t *ptr = pixel.GetHiGainSamples();
|
|---|
| 211 | const Byte_t *end = ptr + fNumHiGainSamples;
|
|---|
| 212 |
|
|---|
| 213 | UInt_t sum = 0;
|
|---|
| 214 | UInt_t sqr = 0;
|
|---|
| 215 |
|
|---|
| 216 | do
|
|---|
| 217 | {
|
|---|
| 218 | sum += *ptr;
|
|---|
| 219 | sqr += *ptr * *ptr;
|
|---|
| 220 | }
|
|---|
| 221 | while (++ptr != end);
|
|---|
| 222 |
|
|---|
| 223 | const Float_t msum = (Float_t)sum;
|
|---|
| 224 |
|
|---|
| 225 | const UInt_t idx = pixel.GetPixelId();
|
|---|
| 226 | //
|
|---|
| 227 | // These three lines have been uncommented by Markus Gaug
|
|---|
| 228 | // If anybody needs them, please contact me!!
|
|---|
| 229 | //
|
|---|
| 230 | // const Float_t higainped = msum/fNumHiGainSamples;
|
|---|
| 231 | // const Float_t higainrms = TMath::Sqrt((msqr-msum*msum/fNumHiGainSamples)/(fNumHiGainSamples-1.));
|
|---|
| 232 | // (*fPedestals)[idx].Set(higainped, higainrms);
|
|---|
| 233 |
|
|---|
| 234 | fSumx[idx] += msum;
|
|---|
| 235 | //
|
|---|
| 236 | // The old version:
|
|---|
| 237 | //
|
|---|
| 238 | // const Float_t msqr = (Float_t)sqr;
|
|---|
| 239 | // fSumx2[idx] += msqr;
|
|---|
| 240 | //
|
|---|
| 241 | // The new version:
|
|---|
| 242 | //
|
|---|
| 243 | fSumx2[idx] += msum*msum;
|
|---|
| 244 |
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | fPedestals->SetReadyToSave();
|
|---|
| 248 | fNumSamplesTot += fNumHiGainSamples;
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 | return kTRUE;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | // --------------------------------------------------------------------------
|
|---|
| 255 | //
|
|---|
| 256 | // Compute signal mean and rms in the whole run and store it in MPedestalCam
|
|---|
| 257 | //
|
|---|
| 258 | Int_t MPedCalcPedRun::PostProcess()
|
|---|
| 259 | {
|
|---|
| 260 |
|
|---|
| 261 | // Compute pedestals and rms from the whole run
|
|---|
| 262 | const ULong_t n = fNumSamplesTot;
|
|---|
| 263 | const ULong_t nevts = GetNumExecutions();
|
|---|
| 264 |
|
|---|
| 265 | MRawEvtPixelIter pixel(fRawEvt);
|
|---|
| 266 |
|
|---|
| 267 | while (pixel.Next())
|
|---|
| 268 | {
|
|---|
| 269 | const Int_t pixid = pixel.GetPixelId();
|
|---|
| 270 |
|
|---|
| 271 | const Float_t sum = fSumx.At(pixid);
|
|---|
| 272 | const Float_t sum2 = fSumx2.At(pixid);
|
|---|
| 273 |
|
|---|
| 274 | const Float_t higainped = sum/n;
|
|---|
| 275 | //
|
|---|
| 276 | // The old version:
|
|---|
| 277 | //
|
|---|
| 278 | // const Float_t higainrms = TMath::Sqrt((sum2-sum*sum/n)/(n-1.));
|
|---|
| 279 | //
|
|---|
| 280 | // The new version:
|
|---|
| 281 | //
|
|---|
| 282 | // 1. Calculate the Variance of the sums:
|
|---|
| 283 | Float_t higainVar = (sum2-sum*sum/nevts)/(nevts-1.);
|
|---|
| 284 | // 2. Scale the variance to the number of slices:
|
|---|
| 285 | higainVar /= (Float_t)fNumHiGainSamples;
|
|---|
| 286 | // 3. Calculate the RMS from the Variance:
|
|---|
| 287 | const Float_t higainrms = TMath::Sqrt(higainVar);
|
|---|
| 288 |
|
|---|
| 289 | (*fPedestals)[pixid].Set(higainped, higainrms);
|
|---|
| 290 |
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | fPedestals->SetTotalEntries(fNumSamplesTot);
|
|---|
| 294 |
|
|---|
| 295 | return kTRUE;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|