| 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): Thomas Bretz, 2013<mailto:thomas.bretz@epfl.ch>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2013
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 26 | //
|
|---|
| 27 | // MDrsCalibApply
|
|---|
| 28 | //
|
|---|
| 29 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 30 | #include "MDrsCalibApply.h"
|
|---|
| 31 |
|
|---|
| 32 | #include "MLog.h"
|
|---|
| 33 | #include "MLogManip.h"
|
|---|
| 34 |
|
|---|
| 35 | #include "MParList.h"
|
|---|
| 36 |
|
|---|
| 37 | #include "MArrayB.h"
|
|---|
| 38 |
|
|---|
| 39 | #include "MRawRunHeader.h"
|
|---|
| 40 | #include "MRawEvtData.h"
|
|---|
| 41 | #include "MRawEvtPixelIter.h"
|
|---|
| 42 |
|
|---|
| 43 | #include "MPedestalCam.h"
|
|---|
| 44 | #include "MPedestalPix.h"
|
|---|
| 45 |
|
|---|
| 46 | #include "MDrsCalibration.h"
|
|---|
| 47 | #include "MPedestalSubtractedEvt.h"
|
|---|
| 48 |
|
|---|
| 49 | #include "MExtractedSignalCam.h"
|
|---|
| 50 | #include "MExtractedSignalPix.h"
|
|---|
| 51 |
|
|---|
| 52 | ClassImp(MDrsCalibApply);
|
|---|
| 53 |
|
|---|
| 54 | using namespace std;
|
|---|
| 55 |
|
|---|
| 56 | // --------------------------------------------------------------------------
|
|---|
| 57 | //
|
|---|
| 58 | // Default constructor.
|
|---|
| 59 | //
|
|---|
| 60 | MDrsCalibApply::MDrsCalibApply(const char *name, const char *title)
|
|---|
| 61 | : fRawEvt(NULL), fDrsCalib(NULL), fSignal(NULL), fResult(0),
|
|---|
| 62 | fMaxNumPrevEvents(5), fRemoveSpikes(3), fSlidingAverage(0)
|
|---|
| 63 | {
|
|---|
| 64 | fName = name ? name : "MDrsCalibApply";
|
|---|
| 65 | fTitle = title ? title : "Class to subtract pedestal";
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // --------------------------------------------------------------------------
|
|---|
| 69 | //
|
|---|
| 70 | // The PreProcess searches for the following input containers:
|
|---|
| 71 | // - MRawEvtData
|
|---|
| 72 | // - MRawRunHeader
|
|---|
| 73 | // - MPedestalCam
|
|---|
| 74 | //
|
|---|
| 75 | // The following output containers are also searched and created if
|
|---|
| 76 | // they were not found:
|
|---|
| 77 | //
|
|---|
| 78 | // - MDrsCalibApplyedEvt
|
|---|
| 79 | //
|
|---|
| 80 | Int_t MDrsCalibApply::PreProcess(MParList *pList)
|
|---|
| 81 | {
|
|---|
| 82 | fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
|
|---|
| 83 | if (!fRawEvt)
|
|---|
| 84 | {
|
|---|
| 85 | *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
|
|---|
| 86 | return kFALSE;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | fDrsCalib = (MDrsCalibration*)pList->FindObject("MDrsCalibration");
|
|---|
| 90 | if (!fDrsCalib)
|
|---|
| 91 | *fLog << warn << "No DRS calibration container (MDrsCalibration) found... skipping." << endl;
|
|---|
| 92 |
|
|---|
| 93 | else
|
|---|
| 94 | *fLog << inf << "DRS calibration will be applied." << endl;
|
|---|
| 95 |
|
|---|
| 96 | fResult = 0;
|
|---|
| 97 | if (fMaxNumPrevEvents>0)
|
|---|
| 98 | {
|
|---|
| 99 | fPrevStart.clear();
|
|---|
| 100 | fResult = (MDrsCalibResult*)pList->FindCreateObj("MDrsCalibResult");
|
|---|
| 101 | if (!fResult)
|
|---|
| 102 | return kFALSE;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | fSignal = (MPedestalSubtractedEvt*)pList->FindCreateObj("MPedestalSubtractedEvt");//, AddSerialNumber(fNamePedestalSubtractedEvt));
|
|---|
| 106 | if (!fSignal)
|
|---|
| 107 | return kFALSE;
|
|---|
| 108 |
|
|---|
| 109 | return kTRUE;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | Bool_t MDrsCalibApply::ReInit(MParList *pList)
|
|---|
| 113 | {
|
|---|
| 114 | fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
|
|---|
| 115 | if (!fRunHeader)
|
|---|
| 116 | {
|
|---|
| 117 | *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
|
|---|
| 118 | return kFALSE;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | if (!fDrsCalib)
|
|---|
| 122 | return kTRUE;
|
|---|
| 123 |
|
|---|
| 124 | if (fDrsCalib->fRoi>0 && fDrsCalib->fRoi != fRunHeader->GetNumSamplesHiGain())
|
|---|
| 125 | {
|
|---|
| 126 | *fLog << warn << "Slice number of DRS calibration " << fDrsCalib->fRoi << " and run-header " << fRunHeader->GetNumSamplesHiGain() << " don't match...\n";
|
|---|
| 127 | *fLog << "secondary baseline correction will not be applied." << endl;
|
|---|
| 128 | return kFALSE;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | return kTRUE;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | // --------------------------------------------------------------------------
|
|---|
| 135 | //
|
|---|
| 136 | //
|
|---|
| 137 | Int_t MDrsCalibApply::Process()
|
|---|
| 138 | {
|
|---|
| 139 | // Please note:
|
|---|
| 140 | // - for data with only hi-gain samples numl is 0
|
|---|
| 141 | // - for data with hi- and lo-gain samples
|
|---|
| 142 | // numl is 0 if read from a raw-data file or a new MC root-file(?)
|
|---|
| 143 | // numl is not 0 if read from an old MC root-file
|
|---|
| 144 |
|
|---|
| 145 | // Total number of samples
|
|---|
| 146 | const Int_t roi = fRawEvt->GetNumHiGainSamples();
|
|---|
| 147 |
|
|---|
| 148 | // Check if event is empty (presumably MC event -- sanity check)
|
|---|
| 149 | if (roi==0)
|
|---|
| 150 | return kCONTINUE;
|
|---|
| 151 |
|
|---|
| 152 | // Check for consistency (our simulation can do weird things!)
|
|---|
| 153 | if (roi!=fRunHeader->GetNumSamplesHiGain())
|
|---|
| 154 | {
|
|---|
| 155 | *fLog << err << "MDrsCalibApply::Process: ERROR - Number of samples in event ";
|
|---|
| 156 | *fLog << "(hi=" << roi << ")" << endl << " doesn't match number in run-header ";
|
|---|
| 157 | *fLog << "(" << fRunHeader->GetNumSamplesHiGain() << ")" << endl;
|
|---|
| 158 | *fLog << " In case you are processing real data you have just found a bug." << endl;
|
|---|
| 159 | *fLog << " If you process Monte Carlo data, it means probably that the length" << endl;
|
|---|
| 160 | *fLog << " of the arrays in MRawEvtData are inconsistent with the run-header." << endl;
|
|---|
| 161 | return kERROR;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (fRawEvt->GetNumLoGainSamples()+fRunHeader->GetNumSamplesLoGain()>0)
|
|---|
| 165 | {
|
|---|
| 166 | *fLog << err << "MDrsCalibApply::Process: ERROR - Data must not contain lo gain slices." << endl;
|
|---|
| 167 | return kERROR;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | // initialize fSignal
|
|---|
| 171 | fSignal->InitSamples(roi);
|
|---|
| 172 |
|
|---|
| 173 | if (!fDrsCalib) // FIXME: Do a fake (none) calibration
|
|---|
| 174 | return kTRUE;
|
|---|
| 175 |
|
|---|
| 176 | const int16_t *val = reinterpret_cast<int16_t*>(fRawEvt->GetSamples());
|
|---|
| 177 |
|
|---|
| 178 | const int32_t *offset = fDrsCalib->fOffset.data();
|
|---|
| 179 | const int64_t *gain = fDrsCalib->fGain.data();
|
|---|
| 180 | const int64_t *trgoff = fDrsCalib->fTrgOff.data();
|
|---|
| 181 |
|
|---|
| 182 | const uint64_t scaleabs = fDrsCalib->fNumOffset;
|
|---|
| 183 | const uint64_t scalegain = fDrsCalib->fNumGain;
|
|---|
| 184 | const uint64_t scalerel = fDrsCalib->fNumTrgOff;
|
|---|
| 185 |
|
|---|
| 186 | const int16_t *start = reinterpret_cast<int16_t*>(fRawEvt->GetStartCells());
|
|---|
| 187 |
|
|---|
| 188 | Float_t *vec = fSignal->GetSamples(0);
|
|---|
| 189 |
|
|---|
| 190 | const UShort_t *idx = fRawEvt->GetPixelIds();
|
|---|
| 191 | for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
|
|---|
| 192 | {
|
|---|
| 193 | const size_t drs = ch*1024;
|
|---|
| 194 | const size_t hw = ch*roi;
|
|---|
| 195 | const size_t sw = idx[ch]*roi;
|
|---|
| 196 |
|
|---|
| 197 | DrsCalibrate::ApplyCh(vec+sw, val+hw, start[ch], roi,
|
|---|
| 198 | offset+drs, scaleabs,
|
|---|
| 199 | gain +drs, scalegain,
|
|---|
| 200 | trgoff+hw, scalerel);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | if (fResult)
|
|---|
| 204 | {
|
|---|
| 205 | fResult->fData.resize(fPrevStart.size()*2);
|
|---|
| 206 |
|
|---|
| 207 | int i=0;
|
|---|
| 208 | for (auto it=fPrevStart.begin(); it!=fPrevStart.end(); it++)
|
|---|
| 209 | {
|
|---|
| 210 | fResult->fData[i++] = DrsCalibrate::CorrectStep(vec, fRawEvt->GetNumPixels(), roi, it->data(), start, roi+10, idx);
|
|---|
| 211 | fResult->fData[i++] = DrsCalibrate::CorrectStep(vec, fRawEvt->GetNumPixels(), roi, it->data(), start, 3, idx);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | fPrevStart.push_front(vector<Short_t>(start, start+fRawEvt->GetNumPixels()));
|
|---|
| 215 | if (fPrevStart.size()>fMaxNumPrevEvents)
|
|---|
| 216 | fPrevStart.pop_back();
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | switch (fRemoveSpikes)
|
|---|
| 220 | {
|
|---|
| 221 | case 1:
|
|---|
| 222 | for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
|
|---|
| 223 | DrsCalibrate::RemoveSpikes(vec+ch*roi, roi);
|
|---|
| 224 | break;
|
|---|
| 225 | case 2:
|
|---|
| 226 | for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
|
|---|
| 227 | DrsCalibrate::RemoveSpikes2(vec+ch*roi, roi);
|
|---|
| 228 | break;
|
|---|
| 229 | case 3:
|
|---|
| 230 | for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
|
|---|
| 231 | DrsCalibrate::RemoveSpikes3(vec+ch*roi, roi);
|
|---|
| 232 | break;
|
|---|
| 233 | case 4:
|
|---|
| 234 | for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
|
|---|
| 235 | DrsCalibrate::RemoveSpikes4(vec+ch*roi, roi);
|
|---|
| 236 | break;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 | if (fSlidingAverage)
|
|---|
| 241 | DrsCalibrate::SlidingAverage(vec, roi, fSlidingAverage);
|
|---|
| 242 |
|
|---|
| 243 | fSignal->SetReadyToSave();
|
|---|
| 244 |
|
|---|
| 245 | return kTRUE;
|
|---|
| 246 | }
|
|---|