| 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 | //  MSimCalibrationSignal
 | 
|---|
| 28 | //
 | 
|---|
| 29 | // This task is a MRead task which produces events instead of reading them
 | 
|---|
| 30 | // from a file. To be more precise, calibration events are produced.
 | 
|---|
| 31 | // (Note, that pedestal events are also a kind of calibration events).
 | 
|---|
| 32 | //
 | 
|---|
| 33 | // Whether pedestal or calibration events are produced is defined by
 | 
|---|
| 34 | // the RunType stored in MRawRunheader. The number of pixels which are
 | 
|---|
| 35 | // "enlightened" are determined from a MGeomCam.
 | 
|---|
| 36 | //
 | 
|---|
| 37 | //
 | 
|---|
| 38 | //  Input Containers:
 | 
|---|
| 39 | //   fNameGeomCam [MGeomCam]
 | 
|---|
| 40 | //   MRawRunHeader
 | 
|---|
| 41 | //   IntendedPulsePos [MParameterD]
 | 
|---|
| 42 | //
 | 
|---|
| 43 | //  Output Containers:
 | 
|---|
| 44 | //   [MPhotonEvent]
 | 
|---|
| 45 | //   [TriggerPos [MParameterD]]
 | 
|---|
| 46 | //   MPhotonStatistics
 | 
|---|
| 47 | //   MRawEvtHeader
 | 
|---|
| 48 | //
 | 
|---|
| 49 | //////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 50 | #include "MSimCalibrationSignal.h"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #include <TRandom.h>
 | 
|---|
| 53 | 
 | 
|---|
| 54 | #include "MParList.h"
 | 
|---|
| 55 | #include "MTaskList.h"
 | 
|---|
| 56 | 
 | 
|---|
| 57 | #include "MLog.h"
 | 
|---|
| 58 | #include "MLogManip.h"
 | 
|---|
| 59 | 
 | 
|---|
| 60 | #include "MParameters.h"
 | 
|---|
| 61 | 
 | 
|---|
| 62 | #include "MGeomCam.h"
 | 
|---|
| 63 | #include "MParSpline.h"
 | 
|---|
| 64 | #include "MTriggerPattern.h"
 | 
|---|
| 65 | 
 | 
|---|
| 66 | #include "MRawRunHeader.h"
 | 
|---|
| 67 | #include "MRawEvtHeader.h"
 | 
|---|
| 68 | 
 | 
|---|
| 69 | #include "MPhotonEvent.h"
 | 
|---|
| 70 | #include "MPhotonData.h"
 | 
|---|
| 71 | 
 | 
|---|
| 72 | ClassImp(MSimCalibrationSignal);
 | 
|---|
| 73 | 
 | 
|---|
| 74 | using namespace std;
 | 
|---|
| 75 | 
 | 
|---|
| 76 | // --------------------------------------------------------------------------
 | 
|---|
| 77 | //
 | 
|---|
| 78 | //  Default Constructor.
 | 
|---|
| 79 | //
 | 
|---|
| 80 | MSimCalibrationSignal::MSimCalibrationSignal(const char* name, const char *title)
 | 
|---|
| 81 |     : fParList(0), fGeom(0), fPulse(0), fPulsePos(0), fTrigger(0),
 | 
|---|
| 82 |     fRunHeader(0), fEvtHeader(0),  fEvt(0), fStat(0),
 | 
|---|
| 83 |     fNumEvents(1000), fNumPhotons(5), fTimeJitter(1)
 | 
|---|
| 84 | {
 | 
|---|
| 85 |     fName  = name  ? name  : "MRead";//"MSimCalibrationSignal";
 | 
|---|
| 86 |     fTitle = title ? title : "Task to create a fake signal (derives from MRead)";
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | // --------------------------------------------------------------------------
 | 
|---|
| 90 | //
 | 
|---|
| 91 | //  Check for the needed parameter containers.
 | 
|---|
| 92 | //
 | 
|---|
| 93 | Int_t MSimCalibrationSignal::PreProcess(MParList *pList)
 | 
|---|
| 94 | {
 | 
|---|
| 95 |     fGeom = (MGeomCam*)pList->FindObject(fNameGeomCam, "MGeomCam");
 | 
|---|
| 96 |     if (!fGeom)
 | 
|---|
| 97 |     {
 | 
|---|
| 98 |         *fLog << inf << fNameGeomCam << " [MGeomCam] not found..." << endl;
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
 | 
|---|
| 101 |         if (!fGeom)
 | 
|---|
| 102 |         {
 | 
|---|
| 103 |             *fLog << err << "MGeomCam not found... aborting." << endl;
 | 
|---|
| 104 |             return kFALSE;
 | 
|---|
| 105 |         }
 | 
|---|
| 106 |     }
 | 
|---|
| 107 | 
 | 
|---|
| 108 |     fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
 | 
|---|
| 109 |     if (!fRunHeader)
 | 
|---|
| 110 |     {
 | 
|---|
| 111 |         *fLog << err << "MRawRunHeader not found... aborting." << endl;
 | 
|---|
| 112 |         return kFALSE;
 | 
|---|
| 113 |     }
 | 
|---|
| 114 | 
 | 
|---|
| 115 |     fEvt = (MPhotonEvent*)pList->FindCreateObj("MPhotonEvent");
 | 
|---|
| 116 |     if (!fEvt)
 | 
|---|
| 117 |         return kFALSE;
 | 
|---|
| 118 | 
 | 
|---|
| 119 |     fTrigger = (MParameterD*)pList->FindCreateObj("MParameterD", "TriggerPos");
 | 
|---|
| 120 |     if (!fTrigger)
 | 
|---|
| 121 |         return kFALSE;
 | 
|---|
| 122 | 
 | 
|---|
| 123 |     fStat = (MPhotonStatistics*)pList->FindCreateObj("MPhotonStatistics");
 | 
|---|
| 124 |     if (!fStat)
 | 
|---|
| 125 |         return kFALSE;
 | 
|---|
| 126 | 
 | 
|---|
| 127 |     fEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
 | 
|---|
| 128 |     if (!fEvtHeader)
 | 
|---|
| 129 |         return kFALSE;
 | 
|---|
| 130 | 
 | 
|---|
| 131 |     fPulsePos = (MParameterD*)pList->FindObject("IntendedPulsePos", "MParameterD");
 | 
|---|
| 132 |     if (!fPulsePos)
 | 
|---|
| 133 |     {
 | 
|---|
| 134 |         *fLog << err << "IntendedPulsePos [MParameterD] not found... aborting." << endl;
 | 
|---|
| 135 |         return kFALSE;
 | 
|---|
| 136 |     }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |     fPulse = (MParSpline*)pList->FindObject("PulseShape", "MParSpline");
 | 
|---|
| 139 |     if (!fPulse)
 | 
|---|
| 140 |     {
 | 
|---|
| 141 |         *fLog << err << "PulsShape [MParSpline] not found... aborting." << endl;
 | 
|---|
| 142 |         return kFALSE;
 | 
|---|
| 143 |     }
 | 
|---|
| 144 | 
 | 
|---|
| 145 |     *fLog << all << "Number of Events: " << fNumEvents << endl;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |     //
 | 
|---|
| 148 |     // Search for MTaskList
 | 
|---|
| 149 |     //
 | 
|---|
| 150 |     fParList = pList;
 | 
|---|
| 151 | 
 | 
|---|
| 152 |     //
 | 
|---|
| 153 |     // A new file has been opened and new headers have been read.
 | 
|---|
| 154 |     //  --> ReInit tasklist
 | 
|---|
| 155 |     //
 | 
|---|
| 156 |     return kTRUE;
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | // --------------------------------------------------------------------------
 | 
|---|
| 160 | //
 | 
|---|
| 161 | // ReInit the task-list if this is the first "event from this fake file"
 | 
|---|
| 162 | //
 | 
|---|
| 163 | Bool_t MSimCalibrationSignal::CallReInit()
 | 
|---|
| 164 | {
 | 
|---|
| 165 |     if (GetNumExecutions()!=1)
 | 
|---|
| 166 |         return kTRUE;
 | 
|---|
| 167 | 
 | 
|---|
| 168 |     MTask *tlist = (MTask*)fParList->FindObject("MTaskList");
 | 
|---|
| 169 |     if (!tlist)
 | 
|---|
| 170 |     {
 | 
|---|
| 171 |         *fLog << err << dbginf << "MTaskList not found... abort." << endl;
 | 
|---|
| 172 |         return kFALSE;
 | 
|---|
| 173 |     }
 | 
|---|
| 174 | 
 | 
|---|
| 175 |     // FIXME: Is there a way to write them as LAST entry in the file?
 | 
|---|
| 176 |     fRunHeader->SetReadyToSave();
 | 
|---|
| 177 | 
 | 
|---|
| 178 |     return tlist->ReInit(fParList);
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | // --------------------------------------------------------------------------
 | 
|---|
| 182 | //
 | 
|---|
| 183 | // Produce the events.
 | 
|---|
| 184 | //
 | 
|---|
| 185 | Int_t MSimCalibrationSignal::Process()
 | 
|---|
| 186 | {
 | 
|---|
| 187 |     if (GetNumExecutions()>fNumEvents)
 | 
|---|
| 188 |         return kFALSE;
 | 
|---|
| 189 | 
 | 
|---|
| 190 |     if (!CallReInit())
 | 
|---|
| 191 |         return kERROR;
 | 
|---|
| 192 | 
 | 
|---|
| 193 |     Int_t cnt = 0;
 | 
|---|
| 194 |     if (fRunHeader->IsCalibrationRun())
 | 
|---|
| 195 |     {
 | 
|---|
| 196 |         for (UInt_t idx=0; idx<fGeom->GetNumPixels(); idx++)
 | 
|---|
| 197 |         {
 | 
|---|
| 198 |             // FIXME: Scale number of photons with the pixel size!
 | 
|---|
| 199 |             const Int_t num = TMath::Nint(gRandom->Gaus(fNumPhotons, fNumPhotons/10));
 | 
|---|
| 200 | 
 | 
|---|
| 201 |             // FIXME: How does the distribution look like? Poissonian?
 | 
|---|
| 202 |             for (Int_t i=0; i<num; i++)
 | 
|---|
| 203 |             {
 | 
|---|
| 204 |                 MPhotonData &ph = fEvt->Add(cnt++);
 | 
|---|
| 205 | 
 | 
|---|
| 206 |                 // FIMXE: Is this the correct distribution
 | 
|---|
| 207 |                 const Float_t tm = gRandom->Gaus(0, fTimeJitter);
 | 
|---|
| 208 | 
 | 
|---|
| 209 |                 ph.SetPrimary(MMcEvtBasic::kArtificial);
 | 
|---|
| 210 |                 ph.SetTag(idx);
 | 
|---|
| 211 |                 ph.SetWeight();
 | 
|---|
| 212 |                 ph.SetTime(tm);
 | 
|---|
| 213 |             }
 | 
|---|
| 214 |         }
 | 
|---|
| 215 |     }
 | 
|---|
| 216 | 
 | 
|---|
| 217 |     fEvt->Shrink(cnt);
 | 
|---|
| 218 |     fEvt->Sort(kTRUE);
 | 
|---|
| 219 | 
 | 
|---|
| 220 |     // ------------ FIMXE: Move somewhere else? -------------
 | 
|---|
| 221 |     // -------------------- MSimGeomCam ---------------------
 | 
|---|
| 222 | 
 | 
|---|
| 223 |     // =====> Move to MSimReadoutWindow ?
 | 
|---|
| 224 | 
 | 
|---|
| 225 |     const Double_t freq = fRunHeader->GetFreqSampling()/1000.;
 | 
|---|
| 226 | 
 | 
|---|
| 227 |     // We add an additional sample at the end to support a possible shift
 | 
|---|
| 228 |     // of the start time of the first event by 0 to 1 sample.
 | 
|---|
| 229 |     const Int_t   ns = fRunHeader->GetNumSamplesHiGain()+1;
 | 
|---|
| 230 | 
 | 
|---|
| 231 |     // Length (ns), Pulse position (Units ns)
 | 
|---|
| 232 |     const Float_t pp = fPulsePos->GetVal();
 | 
|---|
| 233 |     const Float_t pw = fPulse->GetWidth();
 | 
|---|
| 234 | 
 | 
|---|
| 235 |     const Float_t first = cnt>0 ? fEvt->GetFirst()->GetTime() : 0;
 | 
|---|
| 236 |     const Float_t last  = cnt>0 ? fEvt->GetLast()->GetTime()  : ns*freq;
 | 
|---|
| 237 | 
 | 
|---|
| 238 |     fStat->SetTime(first-pp-pw, last-pp+pw + ns*freq);
 | 
|---|
| 239 |     fStat->SetMaxIndex(fGeom->GetNumPixels()-1);
 | 
|---|
| 240 |     fStat->SetReadyToSave();
 | 
|---|
| 241 | 
 | 
|---|
| 242 |     // FIXME: Jitter! (Own class?)
 | 
|---|
| 243 |     fTrigger->SetVal((pp+pw)*freq);
 | 
|---|
| 244 |     fTrigger->SetReadyToSave();
 | 
|---|
| 245 | 
 | 
|---|
| 246 |     // Set trigger pattern according to the trigger type
 | 
|---|
| 247 |     const UInt_t p = fRunHeader->IsCalibrationRun() ? MTriggerPattern::kCalibration : MTriggerPattern::kPedestal;
 | 
|---|
| 248 |     fEvtHeader->SetTriggerPattern(~(p | (p<<8)));
 | 
|---|
| 249 |     fEvtHeader->SetCalibrationPattern(0/*BIT(16)<<16*/); // CT1 Pulser, see MCalibrationPatternDecode
 | 
|---|
| 250 |     fEvtHeader->SetReadyToSave();
 | 
|---|
| 251 | 
 | 
|---|
| 252 |     return kTRUE;
 | 
|---|
| 253 | }
 | 
|---|
| 254 | 
 | 
|---|
| 255 | // --------------------------------------------------------------------------
 | 
|---|
| 256 | //
 | 
|---|
| 257 | // Read the parameters from the resource file.
 | 
|---|
| 258 | //
 | 
|---|
| 259 | //    NumEvents:   1000
 | 
|---|
| 260 | //    NumPhotons:     5
 | 
|---|
| 261 | //    TimeJitter:     1
 | 
|---|
| 262 | //
 | 
|---|
| 263 | Int_t MSimCalibrationSignal::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
 | 
|---|
| 264 | {
 | 
|---|
| 265 |     Bool_t rc = kFALSE;
 | 
|---|
| 266 |     if (IsEnvDefined(env, prefix, "NumEvents", print))
 | 
|---|
| 267 |     {
 | 
|---|
| 268 |         rc = kTRUE;
 | 
|---|
| 269 |         fNumEvents = GetEnvValue(env, prefix, "NumEvents", (Int_t)fNumEvents);
 | 
|---|
| 270 |     }
 | 
|---|
| 271 | 
 | 
|---|
| 272 |     if (IsEnvDefined(env, prefix, "NumPhotons", print))
 | 
|---|
| 273 |     {
 | 
|---|
| 274 |         rc = kTRUE;
 | 
|---|
| 275 |         fNumPhotons = GetEnvValue(env, prefix, "NumPhotons", (Int_t)fNumPhotons);
 | 
|---|
| 276 |     }
 | 
|---|
| 277 | 
 | 
|---|
| 278 |     if (IsEnvDefined(env, prefix, "TimeJitter", print))
 | 
|---|
| 279 |     {
 | 
|---|
| 280 |         rc = kTRUE;
 | 
|---|
| 281 |         fTimeJitter = GetEnvValue(env, prefix, "TimeJitter", fTimeJitter);
 | 
|---|
| 282 |     }
 | 
|---|
| 283 | 
 | 
|---|
| 284 |     return rc;
 | 
|---|
| 285 | }
 | 
|---|