source: trunk/Mars/mdrs/MDrsCalibApply.cc

Last change on this file was 19858, checked in by tbretz, 5 years ago
Do not apply step correction when MPedestalSubtractedEvt is not a multiple of 9.
File size: 9.1 KB
Line 
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
52ClassImp(MDrsCalibApply);
53
54using namespace std;
55
56// --------------------------------------------------------------------------
57//
58// Default constructor.
59//
60MDrsCalibApply::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//
80Int_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
112Bool_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 if (fSignal->GetNumPixels()%9)
132 {
133 *fLog << warn << "MPedestalSubtractedEvt size [" << fSignal->GetNumPixels() << "] not a multiple of 9...";
134 *fLog << "step correction will not be applied." << endl;
135 }
136 else
137 {
138 if (fSignal->GetNumPixels()!=fRawEvt->GetNumPixels())
139 {
140 *fLog << warn << "MPedestalSubtractedEvt size [" << fSignal->GetNumPixels() << "] does not match raw data [" << fRawEvt->GetNumPixels() << "]...";
141 *fLog << "step correction might not work properly if mapping table exceeds " << fSignal->GetNumPixels() << endl;
142 }
143 }
144
145 return kTRUE;
146}
147
148// --------------------------------------------------------------------------
149//
150//
151Int_t MDrsCalibApply::Process()
152{
153 // Please note:
154 // - for data with only hi-gain samples numl is 0
155 // - for data with hi- and lo-gain samples
156 // numl is 0 if read from a raw-data file or a new MC root-file(?)
157 // numl is not 0 if read from an old MC root-file
158
159 // Total number of samples
160 const Int_t roi = fRawEvt->GetNumHiGainSamples();
161
162 // Check if event is empty (presumably MC event -- sanity check)
163 if (roi==0)
164 return kCONTINUE;
165
166 // Check for consistency (our simulation can do weird things!)
167 if (roi!=fRunHeader->GetNumSamplesHiGain())
168 {
169 *fLog << err << "MDrsCalibApply::Process: ERROR - Number of samples in event ";
170 *fLog << "(hi=" << roi << ")" << endl << " doesn't match number in run-header ";
171 *fLog << "(" << fRunHeader->GetNumSamplesHiGain() << ")" << endl;
172 *fLog << " In case you are processing real data you have just found a bug." << endl;
173 *fLog << " If you process Monte Carlo data, it means probably that the length" << endl;
174 *fLog << " of the arrays in MRawEvtData are inconsistent with the run-header." << endl;
175 return kERROR;
176 }
177
178 if (fRawEvt->GetNumLoGainSamples()+fRunHeader->GetNumSamplesLoGain()>0)
179 {
180 *fLog << err << "MDrsCalibApply::Process: ERROR - Data must not contain lo gain slices." << endl;
181 return kERROR;
182 }
183
184 // initialize fSignal
185 fSignal->InitSamples(roi);
186
187 if (!fDrsCalib) // Do a fake (none) calibration
188 {
189 /*
190 const int16_t *val = reinterpret_cast<int16_t*>(fRawEvt->GetSamples());
191 const uint32_t num = fRawEvt->GetNumPixels()*roi;
192
193 std::copy(val, val+num, fSignal->GetSamples(0));
194 */
195 /*
196 const int16_t *val = reinterpret_cast<int16_t*>(fRawEvt->GetSamples());
197 Float_t *vec = fSignal->GetSamples(0);
198
199 const UShort_t *idx = fRawEvt->GetPixelIds();
200 for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
201 {
202 const size_t hw = ch*roi;
203 const size_t sw = ch*roi;//idx[ch]*roi;
204 std::copy(val+hw, val+hw+roi, vec+sw);
205 }
206 */
207 return kTRUE;
208 }
209
210 const int16_t *val = reinterpret_cast<int16_t*>(fRawEvt->GetSamples());
211
212 const int32_t *offset = fDrsCalib->fOffset.data();
213 const int64_t *gain = fDrsCalib->fGain.data();
214 const int64_t *trgoff = fDrsCalib->fTrgOff.data();
215
216 const uint64_t scaleabs = fDrsCalib->fNumOffset;
217 const uint64_t scalegain = fDrsCalib->fNumGain;
218 const uint64_t scalerel = fDrsCalib->fNumTrgOff;
219
220 const int16_t *start = reinterpret_cast<int16_t*>(fRawEvt->GetStartCells());
221
222 Float_t *vec = fSignal->GetSamples(0);
223
224 const UShort_t *idx = fRawEvt->GetPixelIds();
225 for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
226 {
227 if (idx[ch]>=fSignal->GetNumPixels())
228 continue;
229
230 const size_t drs = ch*1024;
231 const size_t hw = ch*roi;
232 const size_t sw = idx[ch]*roi;
233
234 DrsCalibrate::ApplyCh(vec+sw, val+hw, start[ch], roi,
235 offset+drs, scaleabs,
236 gain +drs, scalegain,
237 trgoff+hw, scalerel);
238 }
239
240 if (fResult && fSignal->GetNumPixels()%9==0)
241 {
242 fResult->fData.resize(fPrevStart.size()*2);
243
244 int i=0;
245 for (auto it=fPrevStart.begin(); it!=fPrevStart.end(); it++)
246 {
247 fResult->fData[i++] = DrsCalibrate::CorrectStep(vec, fSignal->GetNumPixels(), roi, it->data(), start, roi+10, idx);
248 fResult->fData[i++] = DrsCalibrate::CorrectStep(vec, fSignal->GetNumPixels(), roi, it->data(), start, 3, idx);
249 }
250
251 fPrevStart.push_front(vector<Short_t>(start, start+fRawEvt->GetNumPixels()));
252 if (fPrevStart.size()>fMaxNumPrevEvents)
253 fPrevStart.pop_back();
254 }
255
256 switch (fRemoveSpikes)
257 {
258 case 1:
259 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
260 DrsCalibrate::RemoveSpikes(vec+ch*roi, roi);
261 break;
262 case 2:
263 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
264 DrsCalibrate::RemoveSpikes2(vec+ch*roi, roi);
265 break;
266 case 3:
267 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
268 DrsCalibrate::RemoveSpikes3(vec+ch*roi, roi);
269 break;
270 case 4:
271 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
272 DrsCalibrate::RemoveSpikes4(vec+ch*roi, roi);
273 break;
274 }
275
276 if (fSlidingAverage)
277 DrsCalibrate::SlidingAverage(vec, roi, fSlidingAverage);
278
279 fSignal->SetReadyToSave();
280
281 return kTRUE;
282}
Note: See TracBrowser for help on using the repository browser.