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

Last change on this file since 15840 was 14922, checked in by tbretz, 12 years ago
New classes for DRS calibration.
File size: 7.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) // 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 deque<vector<Short_t>>::const_iterator it = fPrevStart.begin();
208
209 for (size_t i=0; i<fPrevStart.size()*2; i++)
210 {
211 fResult->fData[i] = DrsCalibrate::Step();
212
213 const int32_t off = i%2 ? 3 : roi+10;
214
215 fResult->fData[i] =
216 DrsCalibrate::CorrectStep(vec, fRawEvt->GetNumPixels(), roi,
217 it->data(), start, off, idx);
218
219 if (i%2==1)
220 it++;
221 }
222
223 fPrevStart.push_front(vector<Short_t>(start, start+fRawEvt->GetNumPixels()));
224 if (fPrevStart.size()>fMaxNumPrevEvents)
225 fPrevStart.pop_back();
226 }
227
228 switch (fRemoveSpikes)
229 {
230 case 1:
231 DrsCalibrate::RemoveSpikes(vec, roi);
232 break;
233 case 2:
234 DrsCalibrate::RemoveSpikes2(vec, roi);
235 break;
236 case 3:
237 DrsCalibrate::RemoveSpikes3(vec, roi);
238 break;
239 }
240
241
242 if (fSlidingAverage)
243 DrsCalibrate::SlidingAverage(vec, roi, fSlidingAverage);
244
245
246 return kTRUE;
247}
Note: See TracBrowser for help on using the repository browser.