| 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 "MPulseShape.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 = (MPulseShape*)pList->FindObject("MPulseShape"); | 
|---|
| 139 | if (!fPulse) | 
|---|
| 140 | { | 
|---|
| 141 | *fLog << err << "MPulsShape 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 | fRunHeader->SetReadyToSave(); | 
|---|
| 176 |  | 
|---|
| 177 | return tlist->ReInit(fParList); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | // -------------------------------------------------------------------------- | 
|---|
| 181 | // | 
|---|
| 182 | // Produce the events. | 
|---|
| 183 | // | 
|---|
| 184 | Int_t MSimCalibrationSignal::Process() | 
|---|
| 185 | { | 
|---|
| 186 | if (GetNumExecutions()>fNumEvents) | 
|---|
| 187 | return kFALSE; | 
|---|
| 188 |  | 
|---|
| 189 | if (!CallReInit()) | 
|---|
| 190 | return kERROR; | 
|---|
| 191 |  | 
|---|
| 192 | Int_t cnt = 0; | 
|---|
| 193 | if (fRunHeader->IsCalibrationRun()) | 
|---|
| 194 | { | 
|---|
| 195 | for (UInt_t idx=0; idx<fGeom->GetNumPixels(); idx++) | 
|---|
| 196 | { | 
|---|
| 197 | const Int_t num = TMath::Nint(gRandom->Gaus(fNumPhotons, fNumPhotons/10)); | 
|---|
| 198 |  | 
|---|
| 199 | // FIXME: How does the distribution look like? Poissonian? | 
|---|
| 200 | for (Int_t i=0; i<num; i++) | 
|---|
| 201 | { | 
|---|
| 202 | MPhotonData &ph = fEvt->Add(cnt++); | 
|---|
| 203 |  | 
|---|
| 204 | // FIMXE: Is this the correct distribution | 
|---|
| 205 | const Float_t tm = gRandom->Gaus(0, fTimeJitter); | 
|---|
| 206 |  | 
|---|
| 207 | ph.SetPrimary(MMcEvtBasic::kArtificial); | 
|---|
| 208 | ph.SetTag(idx); | 
|---|
| 209 | ph.SetWeight(); | 
|---|
| 210 | ph.SetTime(tm); | 
|---|
| 211 | } | 
|---|
| 212 | } | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | fEvt->Shrink(cnt); | 
|---|
| 216 | fEvt->Sort(kTRUE); | 
|---|
| 217 |  | 
|---|
| 218 | // ------------ FIMXE: Move somewhere else? ------------- | 
|---|
| 219 | // -------------------- MSimGeomCam --------------------- | 
|---|
| 220 |  | 
|---|
| 221 | // =====> Move to MSimReadoutWindow ? | 
|---|
| 222 |  | 
|---|
| 223 | const Double_t freq = fRunHeader->GetFreqSampling()/1000.; | 
|---|
| 224 |  | 
|---|
| 225 | // We add an additional sample at the end to support a possible shift | 
|---|
| 226 | // of the start time of the first event by 0 to 1 sample. | 
|---|
| 227 | const Int_t   ns = fRunHeader->GetNumSamplesHiGain()+1; | 
|---|
| 228 |  | 
|---|
| 229 | // Length (ns), Pulse position (Units ns) | 
|---|
| 230 | const Float_t pp = fPulsePos->GetVal(); | 
|---|
| 231 | const Float_t pw = fPulse->GetPulseWidth(); | 
|---|
| 232 |  | 
|---|
| 233 | const Float_t first = cnt>0 ? fEvt->GetFirst()->GetTime() : 0; | 
|---|
| 234 | const Float_t last  = cnt>0 ? fEvt->GetLast()->GetTime()  : ns*freq; | 
|---|
| 235 |  | 
|---|
| 236 | fStat->SetTime(first-pp-pw, last-pp+pw + ns*freq); | 
|---|
| 237 | fStat->SetMaxIndex(fGeom->GetNumPixels()-1); | 
|---|
| 238 | fStat->SetReadyToSave(); | 
|---|
| 239 |  | 
|---|
| 240 | // FIXME: Jitter! (Own class?) | 
|---|
| 241 | fTrigger->SetVal((pp+pw)*freq); | 
|---|
| 242 | fTrigger->SetReadyToSave(); | 
|---|
| 243 |  | 
|---|
| 244 | // Set trigger pattern according to the trigger type | 
|---|
| 245 | const UInt_t p = fRunHeader->IsCalibrationRun() ? MTriggerPattern::kCalibration : MTriggerPattern::kPedestal; | 
|---|
| 246 | fEvtHeader->SetTriggerPattern(~(p | (p<<8))); | 
|---|
| 247 | fEvtHeader->SetCalibrationPattern(0/*BIT(16)<<16*/); // CT1 Pulser, see MCalibrationPatternDecode | 
|---|
| 248 | fEvtHeader->SetReadyToSave(); | 
|---|
| 249 |  | 
|---|
| 250 | return kTRUE; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | // -------------------------------------------------------------------------- | 
|---|
| 254 | // | 
|---|
| 255 | // Read the parameters from the resource file. | 
|---|
| 256 | // | 
|---|
| 257 | //    NumEvents:   1000 | 
|---|
| 258 | //    NumPhotons:     5 | 
|---|
| 259 | //    TimeJitter:     1 | 
|---|
| 260 | // | 
|---|
| 261 | Int_t MSimCalibrationSignal::ReadEnv(const TEnv &env, TString prefix, Bool_t print) | 
|---|
| 262 | { | 
|---|
| 263 | Bool_t rc = kFALSE; | 
|---|
| 264 | if (IsEnvDefined(env, prefix, "NumEvents", print)) | 
|---|
| 265 | { | 
|---|
| 266 | rc = kTRUE; | 
|---|
| 267 | fNumEvents = GetEnvValue(env, prefix, "NumEvents", (Int_t)fNumEvents); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | if (IsEnvDefined(env, prefix, "NumPhotons", print)) | 
|---|
| 271 | { | 
|---|
| 272 | rc = kTRUE; | 
|---|
| 273 | fNumPhotons = GetEnvValue(env, prefix, "NumPhotons", (Int_t)fNumPhotons); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | if (IsEnvDefined(env, prefix, "TimeJitter", print)) | 
|---|
| 277 | { | 
|---|
| 278 | rc = kTRUE; | 
|---|
| 279 | fTimeJitter = GetEnvValue(env, prefix, "TimeJitter", fTimeJitter); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | return rc; | 
|---|
| 283 | } | 
|---|