| 1 | /* ======================================================================== *\ | 
|---|
| 2 | ! $Name: not supported by cvs2svn $:$Id: MPedestalSubtract.cc,v 1.12 2008-06-02 08:46:54 tbretz Exp $ | 
|---|
| 3 | ! -------------------------------------------------------------------------- | 
|---|
| 4 | ! | 
|---|
| 5 | ! * | 
|---|
| 6 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 7 | ! * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 8 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 9 | ! * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 10 | ! * | 
|---|
| 11 | ! * Permission to use, copy, modify and distribute this software and its | 
|---|
| 12 | ! * documentation for any purpose is hereby granted without fee, | 
|---|
| 13 | ! * provided that the above copyright notice appear in all copies and | 
|---|
| 14 | ! * that both that copyright notice and this permission notice appear | 
|---|
| 15 | ! * in supporting documentation. It is provided "as is" without express | 
|---|
| 16 | ! * or implied warranty. | 
|---|
| 17 | ! * | 
|---|
| 18 | ! | 
|---|
| 19 | ! | 
|---|
| 20 | !   Author(s): Thomas Bretz, 10/2006 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 21 | ! | 
|---|
| 22 | !   Copyright: MAGIC Software Development, 2000-2008 | 
|---|
| 23 | ! | 
|---|
| 24 | ! | 
|---|
| 25 | \* ======================================================================== */ | 
|---|
| 26 |  | 
|---|
| 27 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 28 | // | 
|---|
| 29 | //   MPedestalSubtract | 
|---|
| 30 | // | 
|---|
| 31 | //  This class merges hi- and lo-gain samples into one array and | 
|---|
| 32 | //  subtracts the pedestal (including the AB-offset) from the | 
|---|
| 33 | //  data and stores the result in MPedestalSubtractedEvt. | 
|---|
| 34 | // | 
|---|
| 35 | // Input Containers: | 
|---|
| 36 | //   MRawEvtData | 
|---|
| 37 | //   MRawRunHeader | 
|---|
| 38 | //   MPedestalCam | 
|---|
| 39 | // | 
|---|
| 40 | // Output Containers: | 
|---|
| 41 | //   MPedestalSubtractedEvt | 
|---|
| 42 | // | 
|---|
| 43 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 44 | #include "MPedestalSubtract.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include "MLog.h" | 
|---|
| 47 | #include "MLogManip.h" | 
|---|
| 48 |  | 
|---|
| 49 | #include "MParList.h" | 
|---|
| 50 |  | 
|---|
| 51 | #include "MArrayB.h" | 
|---|
| 52 |  | 
|---|
| 53 | #include "MRawRunHeader.h" | 
|---|
| 54 | #include "MRawEvtData.h" | 
|---|
| 55 | #include "MRawEvtPixelIter.h" | 
|---|
| 56 |  | 
|---|
| 57 | #include "MPedestalCam.h" | 
|---|
| 58 | #include "MPedestalPix.h" | 
|---|
| 59 |  | 
|---|
| 60 | #include "MPedestalSubtractedEvt.h" | 
|---|
| 61 |  | 
|---|
| 62 | #include "MExtractedSignalCam.h" | 
|---|
| 63 | #include "MExtractedSignalPix.h" | 
|---|
| 64 |  | 
|---|
| 65 | ClassImp(MPedestalSubtract); | 
|---|
| 66 |  | 
|---|
| 67 | using namespace std; | 
|---|
| 68 |  | 
|---|
| 69 | const char *MPedestalSubtract::fgNamePedestalCam           = "MPedestalCam"; | 
|---|
| 70 | const char *MPedestalSubtract::fgNamePedestalSubtractedEvt = "MPedestalSubtractedEvt"; | 
|---|
| 71 |  | 
|---|
| 72 | // -------------------------------------------------------------------------- | 
|---|
| 73 | // | 
|---|
| 74 | // Default constructor. | 
|---|
| 75 | // | 
|---|
| 76 | MPedestalSubtract::MPedestalSubtract(const char *name, const char *title) | 
|---|
| 77 | : fRawEvt(NULL), fPedestals(NULL), fSignal(NULL) | 
|---|
| 78 | { | 
|---|
| 79 | fName  = name  ? name  : "MPedestalSubtract"; | 
|---|
| 80 | fTitle = title ? title : "Class to subtract pedestal"; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | // -------------------------------------------------------------------------- | 
|---|
| 84 | // | 
|---|
| 85 | // The PreProcess searches for the following input containers: | 
|---|
| 86 | //  - MRawEvtData | 
|---|
| 87 | //  - MRawRunHeader | 
|---|
| 88 | //  - MPedestalCam | 
|---|
| 89 | // | 
|---|
| 90 | // The following output containers are also searched and created if | 
|---|
| 91 | // they were not found: | 
|---|
| 92 | // | 
|---|
| 93 | //  - MPedestalSubtractedEvt | 
|---|
| 94 | // | 
|---|
| 95 | Int_t MPedestalSubtract::PreProcess(MParList *pList) | 
|---|
| 96 | { | 
|---|
| 97 | fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData")); | 
|---|
| 98 | if (!fRawEvt) | 
|---|
| 99 | { | 
|---|
| 100 | *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl; | 
|---|
| 101 | return kFALSE; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | fSignal = (MPedestalSubtractedEvt*)pList->FindCreateObj("MPedestalSubtractedEvt");//, AddSerialNumber(fNamePedestalSubtractedEvt)); | 
|---|
| 105 | if (!fSignal) | 
|---|
| 106 | return kFALSE; | 
|---|
| 107 |  | 
|---|
| 108 | if (fPedestals) | 
|---|
| 109 | { | 
|---|
| 110 | *fLog << inf << "Pedestals given by pointer will be subtracted." << endl; | 
|---|
| 111 | return kTRUE; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | if (fNamePedestalCam.IsNull()) | 
|---|
| 115 | { | 
|---|
| 116 | *fLog << inf << "No name for MPedestalCam given, pedestal subtraction will be skipped." << endl; | 
|---|
| 117 | return kTRUE; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedestalCam), "MPedestalCam"); | 
|---|
| 121 | if (!fPedestals) | 
|---|
| 122 | { | 
|---|
| 123 | *fLog << err << AddSerialNumber(fNamePedestalCam) << " [MPedestalCam] not found... aborting" << endl; | 
|---|
| 124 | return kFALSE; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | *fLog << inf << "Pedestals " << fNamePedestalCam << " will be subtracted." << endl; | 
|---|
| 128 |  | 
|---|
| 129 | return kTRUE; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | void MPedestalSubtract::Memcpy(void *dest, void *src, Int_t cnt) const | 
|---|
| 133 | { | 
|---|
| 134 | if (fRawEvt->GetNumBytesPerSample()==2) | 
|---|
| 135 | memcpy(dest, src, cnt*2); | 
|---|
| 136 | else | 
|---|
| 137 | { | 
|---|
| 138 | const Byte_t *b = (Byte_t*)src; | 
|---|
| 139 | for (USample_t *ptr=(USample_t*)dest; ptr<(USample_t*)dest+cnt; ptr++) | 
|---|
| 140 | *ptr = *b++; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | Bool_t MPedestalSubtract::ReInit(MParList *pList) | 
|---|
| 145 | { | 
|---|
| 146 | fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader")); | 
|---|
| 147 | if (!fRunHeader) | 
|---|
| 148 | { | 
|---|
| 149 | *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl; | 
|---|
| 150 | return kFALSE; | 
|---|
| 151 | } | 
|---|
| 152 | return kTRUE; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | // -------------------------------------------------------------------------- | 
|---|
| 156 | // | 
|---|
| 157 | // | 
|---|
| 158 | Int_t MPedestalSubtract::Process() | 
|---|
| 159 | { | 
|---|
| 160 | // Please note: | 
|---|
| 161 | //   - for data with only hi-gain samples numl is 0 | 
|---|
| 162 | //   - for data with hi- and lo-gain samples | 
|---|
| 163 | //     numl is 0 if read from a raw-data file or a new MC root-file(?) | 
|---|
| 164 | //     numl is not 0 if read from an old MC root-file | 
|---|
| 165 |  | 
|---|
| 166 | // Total number of samples | 
|---|
| 167 | const Int_t numh = fRawEvt->GetNumHiGainSamples(); | 
|---|
| 168 | const Int_t numl = fRawEvt->GetNumLoGainSamples(); | 
|---|
| 169 |  | 
|---|
| 170 | // Check if event is empty (presumably MC event -- sanity check) | 
|---|
| 171 | if (numh+numl==0) | 
|---|
| 172 | return kCONTINUE; | 
|---|
| 173 |  | 
|---|
| 174 | // Check for consistency (our simulation can do weird things!) | 
|---|
| 175 | if (numh+numl!=fRunHeader->GetNumSamplesHiGain()+fRunHeader->GetNumSamplesLoGain()) | 
|---|
| 176 | { | 
|---|
| 177 | *fLog << err << "MPedestalSubtract::Process: ERROR - Number of samples in event "; | 
|---|
| 178 | *fLog << "(hi+lo=" << numh+numl << ")" << endl << " doesn't match number in run-header "; | 
|---|
| 179 | *fLog << "(" << fRunHeader->GetNumSamplesHiGain()+fRunHeader->GetNumSamplesLoGain() << ")" << endl; | 
|---|
| 180 | *fLog << " In case you are processing real data you have just found a bug." << endl; | 
|---|
| 181 | *fLog << " If you process Monte Carlo data, it means probably that the length" << endl; | 
|---|
| 182 | *fLog << " of the arrays in MRawEvtData are inconsistent with the run-header." << endl; | 
|---|
| 183 | return kERROR; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | // Get scale between FADC units and 256 ;-) | 
|---|
| 187 | const UInt_t scale = fRawEvt->GetScale(); | 
|---|
| 188 |  | 
|---|
| 189 | // initialize fSignal | 
|---|
| 190 | fSignal->InitSamples(numh+numl);//, fRawEvt->GetNumPixels(), numh+numl); | 
|---|
| 191 |  | 
|---|
| 192 | // iterate over all pixels | 
|---|
| 193 | MRawEvtPixelIter pixel(fRawEvt); | 
|---|
| 194 | while (pixel.Next()) | 
|---|
| 195 | { | 
|---|
| 196 | // Get index ofthis pixel | 
|---|
| 197 | const Int_t pixidx = pixel.GetPixelId(); | 
|---|
| 198 |  | 
|---|
| 199 | if (pixidx>=fSignal->GetNumPixels()) | 
|---|
| 200 | { | 
|---|
| 201 | *fLog << err << "ERROR - Pixel index " << pixidx << " out of bounds... abort." << endl; | 
|---|
| 202 | return kERROR; | 
|---|
| 203 | } | 
|---|
| 204 | // Get pointer were to store merged raw data | 
|---|
| 205 | USample_t *sample = fSignal->GetSamplesRaw(pixidx); | 
|---|
| 206 |  | 
|---|
| 207 | // copy hi- and lo-gains samples together | 
|---|
| 208 | Memcpy(sample,      pixel.GetHiGainSamples(), numh); | 
|---|
| 209 | Memcpy(sample+numh, pixel.GetLoGainSamples(), numl); | 
|---|
| 210 |  | 
|---|
| 211 | // start of destination array, end of hi-gain destination array | 
|---|
| 212 | // and start of hi-gain samples | 
|---|
| 213 | Float_t *beg = fSignal->GetSamples(pixidx); | 
|---|
| 214 | Float_t *end = beg + fSignal->GetNumSamples(); | 
|---|
| 215 |  | 
|---|
| 216 | const USample_t *src = sample; | 
|---|
| 217 |  | 
|---|
| 218 | // if no pedestals are given just convert the data into | 
|---|
| 219 | // floats and we are finished | 
|---|
| 220 | if (!fPedestals) | 
|---|
| 221 | { | 
|---|
| 222 | while (beg<end) | 
|---|
| 223 | *beg++ = *src++;//Float_t(*src++)/scale; | 
|---|
| 224 | continue; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | // get pedestal information for this pixel | 
|---|
| 228 | const MPedestalPix &pedpix = (*fPedestals)[pixidx]; | 
|---|
| 229 |  | 
|---|
| 230 | // pedestal information | 
|---|
| 231 | const Int_t   ab  = pixel.HasABFlag() ? 1 : 0; | 
|---|
| 232 | const Float_t ped = pedpix.GetPedestal(); | 
|---|
| 233 |  | 
|---|
| 234 | // determine with which pedestal (+/- AB offset) to start | 
|---|
| 235 | const Bool_t  swap    = (ab&1)==1; | 
|---|
| 236 | const Float_t offh    = swap ? -pedpix.GetPedestalABoffset() : pedpix.GetPedestalABoffset(); | 
|---|
| 237 | const Float_t mean[2] = { ped + offh, ped - offh }; | 
|---|
| 238 |  | 
|---|
| 239 | // Copy hi-gains into array and substract pedestal | 
|---|
| 240 | // FIXME: Shell we really subtract the pedestal from saturating slices??? | 
|---|
| 241 | for (Float_t *ptr=beg; ptr<end; ptr++) | 
|---|
| 242 | *ptr = Float_t(*src++)/scale - mean[(ptr-beg)&1]; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | return kTRUE; | 
|---|
| 246 | } | 
|---|