source: trunk/Mars/mdrs/MDrsCalibApply.cc@ 19129

Last change on this file since 19129 was 19129, checked in by tbretz, 6 years ago
Secure the algorithm for the case when the output container (64) is smaller than the input container (1440).
File size: 8.5 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 return kTRUE;
132}
133
134// --------------------------------------------------------------------------
135//
136//
137Int_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) // Do a fake (none) calibration
174 {
175 /*
176 const int16_t *val = reinterpret_cast<int16_t*>(fRawEvt->GetSamples());
177 const uint32_t num = fRawEvt->GetNumPixels()*roi;
178
179 std::copy(val, val+num, fSignal->GetSamples(0));
180 */
181 /*
182 const int16_t *val = reinterpret_cast<int16_t*>(fRawEvt->GetSamples());
183 Float_t *vec = fSignal->GetSamples(0);
184
185 const UShort_t *idx = fRawEvt->GetPixelIds();
186 for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
187 {
188 const size_t hw = ch*roi;
189 const size_t sw = ch*roi;//idx[ch]*roi;
190 std::copy(val+hw, val+hw+roi, vec+sw);
191 }
192 */
193 return kTRUE;
194 }
195
196 const int16_t *val = reinterpret_cast<int16_t*>(fRawEvt->GetSamples());
197
198 const int32_t *offset = fDrsCalib->fOffset.data();
199 const int64_t *gain = fDrsCalib->fGain.data();
200 const int64_t *trgoff = fDrsCalib->fTrgOff.data();
201
202 const uint64_t scaleabs = fDrsCalib->fNumOffset;
203 const uint64_t scalegain = fDrsCalib->fNumGain;
204 const uint64_t scalerel = fDrsCalib->fNumTrgOff;
205
206 const int16_t *start = reinterpret_cast<int16_t*>(fRawEvt->GetStartCells());
207
208 Float_t *vec = fSignal->GetSamples(0);
209
210 const UShort_t *idx = fRawEvt->GetPixelIds();
211 for (size_t ch=0; ch<fRawEvt->GetNumPixels(); ch++)
212 {
213 if (idx[ch]>=fSignal->GetNumPixels())
214 continue;
215
216 const size_t drs = ch*1024;
217 const size_t hw = ch*roi;
218 const size_t sw = idx[ch]*roi;
219
220 DrsCalibrate::ApplyCh(vec+sw, val+hw, start[ch], roi,
221 offset+drs, scaleabs,
222 gain +drs, scalegain,
223 trgoff+hw, scalerel);
224 }
225
226 if (fResult)
227 {
228 fResult->fData.resize(fPrevStart.size()*2);
229
230 int i=0;
231 for (auto it=fPrevStart.begin(); it!=fPrevStart.end(); it++)
232 {
233 fResult->fData[i++] = DrsCalibrate::CorrectStep(vec, fSignal->GetNumPixels(), roi, it->data(), start, roi+10, idx);
234 fResult->fData[i++] = DrsCalibrate::CorrectStep(vec, fSignal->GetNumPixels(), roi, it->data(), start, 3, idx);
235 }
236
237 fPrevStart.push_front(vector<Short_t>(start, start+fRawEvt->GetNumPixels()));
238 if (fPrevStart.size()>fMaxNumPrevEvents)
239 fPrevStart.pop_back();
240 }
241
242 switch (fRemoveSpikes)
243 {
244 case 1:
245 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
246 DrsCalibrate::RemoveSpikes(vec+ch*roi, roi);
247 break;
248 case 2:
249 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
250 DrsCalibrate::RemoveSpikes2(vec+ch*roi, roi);
251 break;
252 case 3:
253 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
254 DrsCalibrate::RemoveSpikes3(vec+ch*roi, roi);
255 break;
256 case 4:
257 for (size_t ch=0; ch<fSignal->GetNumPixels(); ch++)
258 DrsCalibrate::RemoveSpikes4(vec+ch*roi, roi);
259 break;
260 }
261
262 if (fSlidingAverage)
263 DrsCalibrate::SlidingAverage(vec, roi, fSlidingAverage);
264
265 fSignal->SetReadyToSave();
266
267 return kTRUE;
268}
Note: See TracBrowser for help on using the repository browser.