source: trunk/MagicSoft/Mars/manalysis/MApplyPadding.cc@ 1775

Last change on this file since 1775 was 1713, checked in by rwagner, 22 years ago
*** empty log message ***
File size: 10.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): Robert Wagner <magicsoft@rwagner.de> 10/2002
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26// //
27// MApplyPadding //
28// //
29// This task applies padding to a given Sigmabar target value. //
30// The task checks whether the data stream it is applied to has to be //
31// padded or not and if so, Gaussian noise with the difference in Sigma //
32// is produced and added to the particular event. Number of photons and //
33// error on this number is altered. //
34// //
35// There are three ways to define the sigmabar value to which the events //
36// are padded: //
37// //
38// 1) Set a fixed level with SetTargetLevel //
39// //
40// 2) Give a TH1D* which defines the Sigmabar as function of Theta //
41// with SetDefiningHistogram //
42// The given histogram should have the same binning in Theta as //
43// is used in the analysis //
44// //
45// 3) Do nothing, then PreProcess will try to read in a (workaround!) //
46// propriety format ASCII database for the CT1 test. //
47// the name of this file is set by SetDatabaseFile //
48// Better know what you are doing or use methods 1 or 2. //
49// //
50// This implementation is still PRELIMINARY and requires some workarounds //
51// put in SPECIFICALLY FOR THE CT1 TESTS, since a database to access is //
52// missing. It is not the FINAL MAGIC VERSION. //
53// //
54/////////////////////////////////////////////////////////////////////////////
55#include "MApplyPadding.h"
56
57#include <stdio.h>
58
59#include "TH1.h"
60#include "TH2.h"
61#include "TRandom.h"
62
63#include "MBinning.h"
64#include "MSigmabar.h"
65#include "MMcEvt.hxx"
66#include "MLog.h"
67#include "MLogManip.h"
68#include "MParList.h"
69#include "MGeomCam.h"
70#include "MCerPhotPix.h"
71#include "MCerPhotEvt.h"
72#include "MPedestalPix.h"
73
74ClassImp(MApplyPadding);
75
76// --------------------------------------------------------------------------
77//
78// Default constructor.
79//
80MApplyPadding::MApplyPadding(const char *name, const char *title) : fRunType(0), fGroup(0), fUseHistogram(kTRUE), fFixedSigmabar(0.0)
81{
82 fName = name ? name : "MApplyPadding";
83 fTitle = title ? title : "Task to apply padding";
84 Print();
85}
86
87// --------------------------------------------------------------------------
88//
89// Destructor.
90//
91MApplyPadding::~MApplyPadding()
92{
93 //nothing yet
94}
95
96// --------------------------------------------------------------------------
97//
98// You can provide a TH1D* histogram containing the target Sigmabar in
99// bins of theta. Be sure to use the same binning as for the analysis
100//
101Bool_t MApplyPadding::SetDefiningHistogram(TH1D *histo)
102{
103 fHSigmabarMax = histo;
104 return kTRUE;
105}
106
107
108// --------------------------------------------------------------------------
109//
110// check if MEvtHeader exists in the Parameter list already.
111// if not create one and add them to the list
112//
113Bool_t MApplyPadding::PreProcess(MParList *pList)
114{
115 fRnd = new TRandom3(0);
116
117 fMcEvt = (MMcEvt*)pList->FindObject("MMcEvt");
118 if (!fMcEvt)
119 {
120 *fLog << err << dbginf << "MMcEvt not found... aborting." << endl;
121 return kFALSE;
122 }
123
124 fPed = (MPedestalCam*)pList->FindObject("MPedestalCam");
125 if (!fPed)
126 {
127 *fLog << dbginf << "MPedestalCam not found... aborting." << endl;
128 return kFALSE;
129 }
130
131 fCam = (MGeomCam*)pList->FindObject("MGeomCam");
132 if (!fCam)
133 {
134 *fLog << dbginf << "MGeomCam not found (no geometry information available)... aborting." << endl;
135 return kFALSE;
136 }
137
138 fEvt = (MCerPhotEvt*)pList->FindObject("MCerPhotEvt");
139 if (!fEvt)
140 {
141 *fLog << dbginf << "MCerPhotEvt not found... aborting." << endl;
142 return kFALSE;
143 }
144
145 fSigmabar = (MSigmabar*)pList->FindCreateObj("MSigmabar");
146 if (!fSigmabar)
147 {
148 *fLog << dbginf << "MSigmabar not found... aborting." << endl;
149 return kFALSE;
150 }
151
152 // Get Theta Binning
153 MBinning* binstheta = (MBinning*)pList->FindObject("BinningTheta");
154 if (!binstheta)
155 {
156 *fLog << err << dbginf << "BinningTheta not found... aborting." << endl;
157 return kFALSE;
158 }
159
160 // Create fSigmabarMax histogram
161 // (only if no fixed Sigmabar target value or a histogram have already been
162 // provided)
163 if ((!fUseHistogram) && (fHSigmabarMax==NULL)) {
164
165 fHSigmabarMax = new TH1D();
166 fHSigmabarMax->SetNameTitle("fHSigmabarMax","Sigmabarmax for this analysis");
167 TAxis &x = *fHSigmabarMax->GetXaxis();
168#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
169 TString xtitle = x.GetTitle();
170#endif
171 fHSigmabarMax->SetBins(binstheta->GetNumBins(), 0, 1);
172 // Set the binning of the current histogram to the binning
173 // in one of the two given histograms
174 x.Set(binstheta->GetNumBins(), binstheta->GetEdges());
175#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
176 x.SetTitle(xtitle);
177#endif
178
179 // -------------------------------------------------
180 // read in SigmabarParams
181 // workaround--proprietary file format--CT1test only BEGIN
182 // -------------------------------------------------
183
184 FILE *f;
185 if( !(f =fopen(fDatabaseFilename, "r")) ) {
186 *fLog << err << dbginf << "Database file " << fDatabaseFilename << "was not found... (specify with MApplyPadding::SetDatabaseFile) aborting." << endl;
187 return kFALSE;
188 }
189 char line[80];
190 Float_t sigmabarMin, sigmabarMax, thetaMin, thetaMax, ra, dec2;
191 Int_t type, group, mjd, nr;
192 while ( fgets(line, sizeof(line), f) != NULL) {
193 if ((line[0]!='#')) {
194 sscanf(line,"%d %d %f %f %d %d %f %f %f %f",&type, &group, &ra, &dec2, &mjd, &nr, &sigmabarMin,&sigmabarMax,&thetaMin,&thetaMax);
195 if ((group==fGroup)||(type==1)) //selected ON group or OFF
196 {
197 // find out which bin(s) we have to look at
198 for (Int_t i=fHSigmabarMax->GetXaxis()->FindBin(thetaMin);
199 i<fHSigmabarMax->GetXaxis()->FindBin(thetaMax)+1; i++)
200 if (sigmabarMax > fHSigmabarMax->GetBinContent(i))
201 fHSigmabarMax->SetBinContent(i, sigmabarMax);
202 }
203 }
204 }//while
205
206 // workaround--proprietary file format--CT1test only END
207
208 // fHSigmabarMax->DrawClone();
209 // fTest = new TH2D("fTest", "Test if padding works", 201, -0.05, 2.05, 201, -0.05, 2.05);
210
211 } //!fUseHistogram
212 return kTRUE;
213}
214
215// --------------------------------------------------------------------------
216//
217// Calculate Sigmabar for current event
218// Then apply padding
219//
220// 1) have current event
221// 2) get sigmabar(theta)
222// 3) pad event
223//
224Bool_t MApplyPadding::Process()
225{
226 // Calculate sigmabar of event
227 fSigmabar->Calc(*fCam, *fPed);
228 Double_t mySig = fSigmabar->GetSigmabar();
229
230 // Get sigmabar which we have to pad to
231 Double_t otherSig;
232 if (fUseHistogram) {
233 Int_t binNumber = fHSigmabarMax->GetXaxis()->FindBin(fMcEvt->GetTheta()*kRad2Deg);
234 otherSig = fHSigmabarMax->GetBinContent(binNumber);
235 } else {
236 otherSig = fFixedSigmabar;
237 }
238
239 // Determine quadratic difference other-mine
240 Double_t quadraticDiff = otherSig*otherSig - mySig*mySig;
241
242 if (quadraticDiff < 0) {
243 *fLog << err << dbginf << "Event has higher Sigmabar="<<mySig<<" than Sigmabarmax="<<otherSig << " ...Skipping this event" <<endl;
244 return kCONTINUE; //skip
245 }
246
247 if (quadraticDiff == 0) return kTRUE; //no padding necessary.
248
249 // Pad if quadratic difference > 0
250 if (quadraticDiff > 0) {
251
252 MPedestalCam newPed;
253 newPed.InitSize(fPed->GetSize());
254
255 // const UInt_t npix = fPed->GetSize();
256 const UInt_t npix = fEvt->GetNumPixels(); // Total number of pixels
257
258 for (UInt_t i=0; i<npix; i++) {
259 MCerPhotPix pix = fEvt->operator[](i);
260 if (!pix.IsPixelUsed())
261 continue;
262 pix.SetNumPhotons(pix.GetNumPhotons() +
263 sqrt(quadraticDiff)*
264 fRnd->Gaus(0.0, 1.0)/
265 fCam->GetPixRatio(pix.GetPixId())
266 );
267 // error: add sigma of padded noise quadratically
268 Double_t error = pix.GetErrorPhot();
269 pix.SetErrorPhot(sqrt(error*error + quadraticDiff));
270 MPedestalPix ppix = fPed->operator[](i);
271 MPedestalPix npix;
272 npix.SetSigma(sqrt(ppix.GetSigma()*ppix.GetSigma() + quadraticDiff));
273 newPed[i]=npix;
274 } //for
275 // Calculate Sigmabar again and crosscheck
276 fSigmabar->Calc(*fCam, newPed);
277 //mySig = fSigmabar->GetSigmabar();
278 // fTest->Fill(otherSig,mySig);
279 return kTRUE;
280 } //if
281 return kFALSE;
282}
283
284Bool_t MApplyPadding::PostProcess()
285{
286 // fTest->DrawClone();
287 return kTRUE;
288}
Note: See TracBrowser for help on using the repository browser.