| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of CheObs, the Modular 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 appears 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, 1/2009 <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: CheObs Software Development, 2000-2009
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 26 | //
|
|---|
| 27 | // MSimAbsorption
|
|---|
| 28 | //
|
|---|
| 29 | // Task to calculate wavelength or incident angle dependent absorption
|
|---|
| 30 | //
|
|---|
| 31 | // Input Containers:
|
|---|
| 32 | // MPhotonEvent
|
|---|
| 33 | // MCorsikaEvtHeader
|
|---|
| 34 | // MCorsikaRunHeader
|
|---|
| 35 | //
|
|---|
| 36 | // Output Containers:
|
|---|
| 37 | // MPhotonEvent
|
|---|
| 38 | //
|
|---|
| 39 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 40 | #include "MSimAbsorption.h"
|
|---|
| 41 |
|
|---|
| 42 | #include <fstream>
|
|---|
| 43 |
|
|---|
| 44 | #include <TRandom.h>
|
|---|
| 45 | #include <TSpline.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "MLog.h"
|
|---|
| 48 | #include "MLogManip.h"
|
|---|
| 49 |
|
|---|
| 50 | #include "MParList.h"
|
|---|
| 51 |
|
|---|
| 52 | #include "MBinning.h"
|
|---|
| 53 | #include "MArrayD.h"
|
|---|
| 54 |
|
|---|
| 55 | #include "MSpline3.h"
|
|---|
| 56 |
|
|---|
| 57 | #include "MCorsikaEvtHeader.h"
|
|---|
| 58 | #include "MCorsikaRunHeader.h"
|
|---|
| 59 | #include "MPhotonEvent.h"
|
|---|
| 60 | #include "MPhotonData.h"
|
|---|
| 61 |
|
|---|
| 62 | ClassImp(MSimAbsorption);
|
|---|
| 63 |
|
|---|
| 64 | using namespace std;
|
|---|
| 65 |
|
|---|
| 66 | // --------------------------------------------------------------------------
|
|---|
| 67 | //
|
|---|
| 68 | // Default Constructor.
|
|---|
| 69 | //
|
|---|
| 70 | MSimAbsorption::MSimAbsorption(const char* name, const char *title)
|
|---|
| 71 | : fEvt(0), fHeader(0), fSpline(0), fUseTheta(kFALSE)
|
|---|
| 72 | {
|
|---|
| 73 | fName = name ? name : "MSimAbsorption";
|
|---|
| 74 | fTitle = title ? title : "Task to calculate wavelength dependent absorption";
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | // --------------------------------------------------------------------------
|
|---|
| 78 | //
|
|---|
| 79 | // Calls Clear()
|
|---|
| 80 | //
|
|---|
| 81 | MSimAbsorption::~MSimAbsorption()
|
|---|
| 82 | {
|
|---|
| 83 | Clear();
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | // --------------------------------------------------------------------------
|
|---|
| 87 | //
|
|---|
| 88 | // Delete fSpline and set to 0.
|
|---|
| 89 | //
|
|---|
| 90 | void MSimAbsorption::Clear(Option_t *)
|
|---|
| 91 | {
|
|---|
| 92 | if (fSpline)
|
|---|
| 93 | delete fSpline;
|
|---|
| 94 | fSpline=0;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | // --------------------------------------------------------------------------
|
|---|
| 98 | //
|
|---|
| 99 | // Read a TGraph from a file and return a newly allocated MSpline3.
|
|---|
| 100 | //
|
|---|
| 101 | MSpline3 *MSimAbsorption::ReadSpline(const char *fname)
|
|---|
| 102 | {
|
|---|
| 103 | *fLog << inf << "Reading spline from " << fname << endl;
|
|---|
| 104 |
|
|---|
| 105 | TGraph g(fname);
|
|---|
| 106 | if (g.GetN()==0)
|
|---|
| 107 | {
|
|---|
| 108 | *fLog << err << "ERROR - No data points from " << fname << "." << endl;
|
|---|
| 109 | return 0;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | // option: b1/e1 b2/e2 (first second derivative?)
|
|---|
| 113 | // option: valbeg/valend (first second derivative?)
|
|---|
| 114 |
|
|---|
| 115 | return new MSpline3(g);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // --------------------------------------------------------------------------
|
|---|
| 119 | //
|
|---|
| 120 | // Initializes a spline from min to max with n points with 1.
|
|---|
| 121 | //
|
|---|
| 122 | void MSimAbsorption::InitUnity(UInt_t n, Float_t min, Float_t max)
|
|---|
| 123 | {
|
|---|
| 124 | // Delete the existing spline
|
|---|
| 125 | Clear();
|
|---|
| 126 |
|
|---|
| 127 | // We need at least two points (the edges)
|
|---|
| 128 | if (n<2)
|
|---|
| 129 | return;
|
|---|
| 130 |
|
|---|
| 131 | // Initialize an array with the x-values
|
|---|
| 132 | const MBinning bins(n-1, min, max);
|
|---|
| 133 |
|
|---|
| 134 | // Initialize an array with all one
|
|---|
| 135 | MArrayD y(n);
|
|---|
| 136 | y.Reset(1);
|
|---|
| 137 |
|
|---|
| 138 | // Set the spline
|
|---|
| 139 | fSpline = new MSpline3(bins.GetEdges(), y.GetArray(), n);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | // --------------------------------------------------------------------------
|
|---|
| 143 | //
|
|---|
| 144 | // Takes the existing fSpline and multiplies it with the given spline.
|
|---|
| 145 | // As x-points the values from fSpline are used.
|
|---|
| 146 | //
|
|---|
| 147 | void MSimAbsorption::Multiply(const TSpline3 &spline)
|
|---|
| 148 | {
|
|---|
| 149 | if (!fSpline)
|
|---|
| 150 | {
|
|---|
| 151 | Clear();
|
|---|
| 152 | // WARNING WARNING WARNING: This is a very dangerous cast!
|
|---|
| 153 | fSpline = (MSpline3*)spline.Clone();
|
|---|
| 154 | return;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | // Initialize a TGraph with the number of knots from a TSpline
|
|---|
| 158 | TGraph g(fSpline->GetNp());
|
|---|
| 159 |
|
|---|
| 160 | // Loop over all knots
|
|---|
| 161 | for (int i=0; i<fSpline->GetNp(); i++)
|
|---|
| 162 | {
|
|---|
| 163 | // Get th i-th knot
|
|---|
| 164 | Double_t x, y;
|
|---|
| 165 | fSpline->GetKnot(i, x, y);
|
|---|
| 166 |
|
|---|
| 167 | // Multiply y by the value from the given spline
|
|---|
| 168 | y *= spline.Eval(x);
|
|---|
| 169 |
|
|---|
| 170 | // push the point "back"
|
|---|
| 171 | g.SetPoint(i, x, y);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | // Clear the spline and initialize a new one from the updated TGraph
|
|---|
| 175 | Clear();
|
|---|
| 176 | fSpline = new MSpline3(g);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | // --------------------------------------------------------------------------
|
|---|
| 180 | //
|
|---|
| 181 | // Initializes a TSpline3 with n knots x and y. Call Multiply for it.
|
|---|
| 182 | //
|
|---|
| 183 | void MSimAbsorption::Multiply(UInt_t n, const Double_t *x, const Double_t *y)
|
|---|
| 184 | {
|
|---|
| 185 | const TSpline3 spline("Spline", const_cast<Double_t*>(x), const_cast<Double_t*>(y), n);
|
|---|
| 186 | Multiply(spline);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | // --------------------------------------------------------------------------
|
|---|
| 190 | //
|
|---|
| 191 | // Read a Spline from a file using ReadSpline.
|
|---|
| 192 | // Call Multiply for it.
|
|---|
| 193 | //
|
|---|
| 194 | void MSimAbsorption::Multiply(const char *fname)
|
|---|
| 195 | {
|
|---|
| 196 | const MSpline3 *spline = ReadSpline(fname);
|
|---|
| 197 | if (!spline)
|
|---|
| 198 | return;
|
|---|
| 199 |
|
|---|
| 200 | fFileName = "";
|
|---|
| 201 |
|
|---|
| 202 | Multiply(*spline);
|
|---|
| 203 |
|
|---|
| 204 | delete spline;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | // --------------------------------------------------------------------------
|
|---|
| 208 | //
|
|---|
| 209 | // Read a spline from a file and set fSpline accfordingly.
|
|---|
| 210 | //
|
|---|
| 211 | Bool_t MSimAbsorption::ReadFile(const char *fname)
|
|---|
| 212 | {
|
|---|
| 213 | if (fname)
|
|---|
| 214 | fFileName = fname;
|
|---|
| 215 |
|
|---|
| 216 | MSpline3 *spline = ReadSpline(fFileName);
|
|---|
| 217 | if (!spline)
|
|---|
| 218 | return kFALSE;
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 | // option: b1/e1 b2/e2 (first second derivative?)
|
|---|
| 222 | // option: valbeg/valend (first second derivative?)
|
|---|
| 223 |
|
|---|
| 224 | Clear();
|
|---|
| 225 | fSpline = spline;
|
|---|
| 226 |
|
|---|
| 227 | return kTRUE;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | // --------------------------------------------------------------------------
|
|---|
| 231 | //
|
|---|
| 232 | // Search for the needed parameter containers. Read spline from file
|
|---|
| 233 | // calling ReadFile();
|
|---|
| 234 | //
|
|---|
| 235 | Int_t MSimAbsorption::PreProcess(MParList *pList)
|
|---|
| 236 | {
|
|---|
| 237 | fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent");
|
|---|
| 238 | if (!fEvt)
|
|---|
| 239 | {
|
|---|
| 240 | *fLog << err << "MPhotonEvent not found... aborting." << endl;
|
|---|
| 241 | return kFALSE;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | fHeader = (MCorsikaEvtHeader*)pList->FindObject("MCorsikaEvtHeader");
|
|---|
| 245 | if (!fHeader)
|
|---|
| 246 | {
|
|---|
| 247 | *fLog << err << "MCorsikaEvtHeader not found... aborting." << endl;
|
|---|
| 248 | return kFALSE;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | return ReadFile();
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | // --------------------------------------------------------------------------
|
|---|
| 255 | //
|
|---|
| 256 | Bool_t MSimAbsorption::ReInit(MParList *pList)
|
|---|
| 257 | {
|
|---|
| 258 | MCorsikaRunHeader *h = (MCorsikaRunHeader*)pList->FindObject("MCorsikaRunHeader");
|
|---|
| 259 | if (!h)
|
|---|
| 260 | {
|
|---|
| 261 | *fLog << err << "MCorsikaRunHeader not found... aborting." << endl;
|
|---|
| 262 | return kFALSE;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | if (h->GetWavelengthMin()<fSpline->GetXmin())
|
|---|
| 266 | *fLog << warn << "WARNING - Lower bound of wavelength bandwidth exceeds lower bound of spline." << endl;
|
|---|
| 267 |
|
|---|
| 268 | if (h->GetWavelengthMax()>fSpline->GetXmax())
|
|---|
| 269 | *fLog << warn << "WARNING - Upper bound of wavelength bandwidth exceeds upper bound of spline." << endl;
|
|---|
| 270 |
|
|---|
| 271 | return kTRUE;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | // --------------------------------------------------------------------------
|
|---|
| 275 | //
|
|---|
| 276 | // Throw all events out of the MPhotonEvent which don't survive.
|
|---|
| 277 | // Depending on fUseTheta either the wavelength or the incident angle
|
|---|
| 278 | // is used.
|
|---|
| 279 | //
|
|---|
| 280 | Int_t MSimAbsorption::Process()
|
|---|
| 281 | {
|
|---|
| 282 | // Get the number of photons in the list
|
|---|
| 283 | const Int_t num = fEvt->GetNumPhotons();
|
|---|
| 284 |
|
|---|
| 285 | // Counter for number of total and final events
|
|---|
| 286 | Int_t cnt = 0;
|
|---|
| 287 | for (Int_t i=0; i<num; i++)
|
|---|
| 288 | {
|
|---|
| 289 | // Get i-th photon from the list
|
|---|
| 290 | MPhotonData &ph = (*fEvt)[i];
|
|---|
| 291 |
|
|---|
| 292 | // Depending on fUseTheta get the incident angle of the wavelength
|
|---|
| 293 | const Double_t wl = fUseTheta ? ph.GetTheta()*TMath::RadToDeg() : ph.GetWavelength();
|
|---|
| 294 |
|
|---|
| 295 | // Get the efficiency (the probability that this photon will survive)
|
|---|
| 296 | // from the spline.
|
|---|
| 297 | const Double_t eff = fSpline->Eval(wl);
|
|---|
| 298 |
|
|---|
| 299 | // Get a random value between 0 and 1 to determine whether the photn will survive
|
|---|
| 300 | // gRandom->Rndm() = [0;1[
|
|---|
| 301 | if (gRandom->Rndm()>=eff)
|
|---|
| 302 | continue;
|
|---|
| 303 |
|
|---|
| 304 | // Copy the surviving events bakc in the list
|
|---|
| 305 | (*fEvt)[cnt++] = ph;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | // Now we shrink the array to the number of new entries.
|
|---|
| 309 | fEvt->Shrink(cnt);
|
|---|
| 310 |
|
|---|
| 311 | return kTRUE;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | // --------------------------------------------------------------------------
|
|---|
| 315 | //
|
|---|
| 316 | // FileName: reflectivity.txt
|
|---|
| 317 | // UseTheta: No
|
|---|
| 318 | //
|
|---|
| 319 | Int_t MSimAbsorption::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
|---|
| 320 | {
|
|---|
| 321 | Bool_t rc = kFALSE;
|
|---|
| 322 | if (IsEnvDefined(env, prefix, "FileName", print))
|
|---|
| 323 | {
|
|---|
| 324 | rc = kTRUE;
|
|---|
| 325 | SetFileName(GetEnvValue(env, prefix, "FileName", fFileName));
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | if (IsEnvDefined(env, prefix, "UseTheta", print))
|
|---|
| 329 | {
|
|---|
| 330 | rc = kTRUE;
|
|---|
| 331 | SetUseTheta(GetEnvValue(env, prefix, "UseTheta", fUseTheta));
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | return rc;
|
|---|
| 335 | }
|
|---|