| 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): Abelardo Moralejo 7/2002  <mailto:moralejo@pd.infn.it> | 
|---|
| 19 | !   Author(s): Thomas Bretz 2002  <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 20 | ! | 
|---|
| 21 | !   Copyright: MAGIC Software Development, 2002-2003 | 
|---|
| 22 | ! | 
|---|
| 23 | ! | 
|---|
| 24 | \* ======================================================================== */ | 
|---|
| 25 |  | 
|---|
| 26 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 27 | // | 
|---|
| 28 | //   MCerPhotCalc | 
|---|
| 29 | // | 
|---|
| 30 | //   This is a task which calculates the number of photons from the FADC | 
|---|
| 31 | //   time slices. It weights the each slice according to the numbers in | 
|---|
| 32 | //   the array fWeight (default: all slices added up with weight 1). | 
|---|
| 33 | // | 
|---|
| 34 | //   The weights are rescaled, such that sum(weigths)=num slices | 
|---|
| 35 | // | 
|---|
| 36 | //  Input Containers: | 
|---|
| 37 | //   MRawRunHeader, MRawEvtData, MPedestalCam | 
|---|
| 38 | // | 
|---|
| 39 | //  Output Containers: | 
|---|
| 40 | //   MCerPhotEvt | 
|---|
| 41 | // | 
|---|
| 42 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 43 | #include "MCerPhotCalc.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include "MParList.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include "MLog.h" | 
|---|
| 48 | #include "MLogManip.h" | 
|---|
| 49 |  | 
|---|
| 50 | #include "MMcRunHeader.hxx" | 
|---|
| 51 |  | 
|---|
| 52 | #include "MRawRunHeader.h" | 
|---|
| 53 | #include "MRawEvtData.h"       // MRawEvtData::GetNumPixels | 
|---|
| 54 | #include "MCerPhotEvt.h" | 
|---|
| 55 | #include "MPedestalPix.h" | 
|---|
| 56 | #include "MPedestalCam.h" | 
|---|
| 57 | #include "MRawEvtPixelIter.h" | 
|---|
| 58 |  | 
|---|
| 59 | ClassImp(MCerPhotCalc); | 
|---|
| 60 |  | 
|---|
| 61 | using namespace std; | 
|---|
| 62 |  | 
|---|
| 63 | // -------------------------------------------------------------------------- | 
|---|
| 64 | // | 
|---|
| 65 | // Default constructor. | 
|---|
| 66 | // | 
|---|
| 67 | MCerPhotCalc::MCerPhotCalc(const char *name, const char *title) | 
|---|
| 68 | { | 
|---|
| 69 | fName  = name  ? name  : "MCerPhotCalc"; | 
|---|
| 70 | fTitle = title ? title : "Calculate pixel signal from FADC data"; | 
|---|
| 71 |  | 
|---|
| 72 | AddToBranchList("MRawEvtData.fHiGainPixId"); | 
|---|
| 73 | AddToBranchList("MRawEvtData.fLoGainPixId"); | 
|---|
| 74 | AddToBranchList("MRawEvtData.fHiGainFadcSamples"); | 
|---|
| 75 | AddToBranchList("MRawEvtData.fLoGainFadcSamples"); | 
|---|
| 76 |  | 
|---|
| 77 | SetDefaultWeights(); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | // -------------------------------------------------------------------------- | 
|---|
| 81 | // | 
|---|
| 82 | // The PreProcess searches for the following input containers: | 
|---|
| 83 | //  - MRawRunHeader | 
|---|
| 84 | //  - MRawEvtData | 
|---|
| 85 | //  - MPedestalCam | 
|---|
| 86 | // | 
|---|
| 87 | // The following output containers are also searched and created if | 
|---|
| 88 | // they were not found: | 
|---|
| 89 | //  - MCerPhotEvt | 
|---|
| 90 | // | 
|---|
| 91 | Int_t MCerPhotCalc::PreProcess(MParList *pList) | 
|---|
| 92 | { | 
|---|
| 93 | fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader"); | 
|---|
| 94 | if (!fRunHeader) | 
|---|
| 95 | { | 
|---|
| 96 | *fLog << dbginf << "MRawRunHeader not found... aborting." << endl; | 
|---|
| 97 | return kFALSE; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData"); | 
|---|
| 101 | if (!fRawEvt) | 
|---|
| 102 | { | 
|---|
| 103 | *fLog << dbginf << "MRawEvtData not found... aborting." << endl; | 
|---|
| 104 | return kFALSE; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam"); | 
|---|
| 108 | if (!fPedestals) | 
|---|
| 109 | { | 
|---|
| 110 | *fLog << dbginf << "MPedestalCam not found... aborting." << endl; | 
|---|
| 111 | return kFALSE; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | fCerPhotEvt = (MCerPhotEvt*)pList->FindCreateObj("MCerPhotEvt"); | 
|---|
| 115 | if (!fCerPhotEvt) | 
|---|
| 116 | return kFALSE; | 
|---|
| 117 |  | 
|---|
| 118 | // Calculate sum and quadratic sum of weights: | 
|---|
| 119 | fSumWeights = 0; | 
|---|
| 120 | fSumQuadWeights = 0; | 
|---|
| 121 | for (Int_t i=0; i<fWeight.GetSize(); i++) | 
|---|
| 122 | { | 
|---|
| 123 | fSumWeights += fWeight[i]; | 
|---|
| 124 | fSumQuadWeights += fWeight[i]*fWeight[i]; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | fSumQuadWeights = sqrt(fSumQuadWeights); | 
|---|
| 128 |  | 
|---|
| 129 | return kTRUE; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | // -------------------------------------------------------------------------- | 
|---|
| 133 | // | 
|---|
| 134 | // Check for the run type and camera version. | 
|---|
| 135 | // If the file is a MC file and the used camera version is <= 40 | 
|---|
| 136 | // we enable a fix for truncated pedestal means in this version. | 
|---|
| 137 | // | 
|---|
| 138 | Bool_t MCerPhotCalc::ReInit(MParList *pList) | 
|---|
| 139 | { | 
|---|
| 140 | const MRawRunHeader *runheader = (MRawRunHeader*)pList->FindObject("MRawRunHeader"); | 
|---|
| 141 | if (!runheader) | 
|---|
| 142 | { | 
|---|
| 143 | *fLog << warn << dbginf << "Warning - cannot check file type, MRawRunHeader not found." << endl; | 
|---|
| 144 | return kTRUE; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | if (fRunHeader->GetNumSamplesHiGain() != fWeight.GetSize()) | 
|---|
| 148 | { | 
|---|
| 149 | *fLog << dbginf << "Number of FADC slices (" << fRunHeader->GetNumSamplesHiGain() <<") is different from assumed one (" << fWeight.GetSize() << ")... aborting." << endl; | 
|---|
| 150 | return kFALSE; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | Bool_t fIsMcFile = runheader->GetRunType() == kRTMonteCarlo; | 
|---|
| 154 | if (!fIsMcFile) | 
|---|
| 155 | return kTRUE; | 
|---|
| 156 |  | 
|---|
| 157 | MMcRunHeader *mcrunheader = (MMcRunHeader*)pList->FindObject("MMcRunHeader"); | 
|---|
| 158 | if (!mcrunheader) | 
|---|
| 159 | { | 
|---|
| 160 | *fLog << warn << dbginf << "Warning - cannot check for camera version, MC run header not found." << endl; | 
|---|
| 161 | return kTRUE; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | fEnableFix = kFALSE; | 
|---|
| 165 |  | 
|---|
| 166 | if (mcrunheader->GetCamVersion() <= 40) | 
|---|
| 167 | fEnableFix = kTRUE; | 
|---|
| 168 |  | 
|---|
| 169 | ScalePedestals(); | 
|---|
| 170 |  | 
|---|
| 171 | return kTRUE; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | void MCerPhotCalc::ScalePedestals() | 
|---|
| 175 | { | 
|---|
| 176 | Int_t n = 577; //FIXME: fPedestals->GetNumPixel(); | 
|---|
| 177 |  | 
|---|
| 178 | for (int idx=0; idx<n; idx++) | 
|---|
| 179 | { | 
|---|
| 180 | MPedestalPix &ped = (*fPedestals)[idx]; | 
|---|
| 181 |  | 
|---|
| 182 | // This converts the pedestal info contained in ped  from the pedestal | 
|---|
| 183 | // of each FADC slice to the pedesal of the pixel signal (depends on | 
|---|
| 184 | // how many FADC slices are added up). | 
|---|
| 185 |  | 
|---|
| 186 | const Double_t offset = fEnableFix ? ped.GetPedestal()-0.5 : ped.GetPedestal(); | 
|---|
| 187 |  | 
|---|
| 188 | ped.Set(offset*fSumWeights, | 
|---|
| 189 | ped.GetPedestalRms()*fSumQuadWeights); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | fPedestals->SetReadyToSave(); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | // -------------------------------------------------------------------------- | 
|---|
| 196 | // | 
|---|
| 197 | // Calculate the integral of the FADC time slices and store them as a new | 
|---|
| 198 | // pixel in the MCerPhotEvt container. | 
|---|
| 199 | // | 
|---|
| 200 | Int_t MCerPhotCalc::Process() | 
|---|
| 201 | { | 
|---|
| 202 | //fCerPhotEvt->InitSize(fRawEvt->GetNumPixels()); | 
|---|
| 203 |  | 
|---|
| 204 | //    if (fIsMcFile) | 
|---|
| 205 | //        ScalePedestals(); | 
|---|
| 206 |  | 
|---|
| 207 | MRawEvtPixelIter pixel(fRawEvt); | 
|---|
| 208 |  | 
|---|
| 209 | Int_t SaturatedPixels = 0; | 
|---|
| 210 |  | 
|---|
| 211 | while (pixel.Next()) | 
|---|
| 212 | { | 
|---|
| 213 | const UInt_t idx = pixel.GetPixelId(); | 
|---|
| 214 | MPedestalPix &ped = (*fPedestals)[idx]; | 
|---|
| 215 |  | 
|---|
| 216 | // | 
|---|
| 217 | // sanity check (old MC files sometimes have pixids>577) | 
|---|
| 218 | // | 
|---|
| 219 | if (!fPedestals->CheckBounds(idx)) | 
|---|
| 220 | { | 
|---|
| 221 | *fLog << inf << "Pixel Index larger than camera... skipping event." << endl; | 
|---|
| 222 | return kCONTINUE; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | // | 
|---|
| 226 | // Calculate pixel signal unless it has all FADC slices empty: | 
|---|
| 227 | // | 
|---|
| 228 | Byte_t *ptr = pixel.GetHiGainSamples(); | 
|---|
| 229 |  | 
|---|
| 230 | Int_t i; | 
|---|
| 231 | Double_t nphot = 0; | 
|---|
| 232 | for (i=0; i<fWeight.GetSize(); i++) | 
|---|
| 233 | { | 
|---|
| 234 | if (ptr[i]==0xff) | 
|---|
| 235 | break; | 
|---|
| 236 | nphot += ptr[i]*fWeight[i]; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | Bool_t SaturatedLG = kFALSE; | 
|---|
| 240 |  | 
|---|
| 241 | if (i<fWeight.GetSize()) | 
|---|
| 242 | { | 
|---|
| 243 | nphot = 0; | 
|---|
| 244 |  | 
|---|
| 245 | ptr = pixel.GetLoGainSamples(); | 
|---|
| 246 | if (ptr==NULL) | 
|---|
| 247 | { | 
|---|
| 248 | *fLog << warn << "WARNING - Pixel #" << idx << " saturated but has no lo gains.. skipping!" << endl; | 
|---|
| 249 | return kCONTINUE; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | for (i=0; i<fWeight.GetSize(); i++) | 
|---|
| 253 | { | 
|---|
| 254 | if (ptr[i]==0xff) | 
|---|
| 255 | SaturatedLG = kTRUE; | 
|---|
| 256 |  | 
|---|
| 257 | nphot += ptr[i]*fWeight[i]; | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | nphot -= ped.GetPedestal(); | 
|---|
| 261 | nphot *= 10; | 
|---|
| 262 | } | 
|---|
| 263 | else | 
|---|
| 264 | nphot -= ped.GetPedestal(); | 
|---|
| 265 |  | 
|---|
| 266 | fCerPhotEvt->AddPixel(idx, nphot, 0); | 
|---|
| 267 |  | 
|---|
| 268 | if (SaturatedLG) | 
|---|
| 269 | SaturatedPixels ++; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | switch(SaturatedPixels) | 
|---|
| 273 | { | 
|---|
| 274 | case 0: | 
|---|
| 275 | break; | 
|---|
| 276 | case 1: | 
|---|
| 277 | *fLog << warn << "WARNING: 1 pixel had saturating low gains..." << endl; | 
|---|
| 278 | break; | 
|---|
| 279 | default: | 
|---|
| 280 | *fLog << warn << "WARNING: " << SaturatedPixels << " pixels had saturating low gains..." << endl; | 
|---|
| 281 | break; | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 |  | 
|---|
| 285 | fCerPhotEvt->FixSize(); | 
|---|
| 286 | fCerPhotEvt->SetReadyToSave(); | 
|---|
| 287 |  | 
|---|
| 288 | return kTRUE; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | // -------------------------------------------------------------------------- | 
|---|
| 292 | // | 
|---|
| 293 | // Set default values for the number of slices and weights: | 
|---|
| 294 | // | 
|---|
| 295 | void MCerPhotCalc::SetDefaultWeights() | 
|---|
| 296 | { | 
|---|
| 297 | const Float_t dummy[15] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; | 
|---|
| 298 | fWeight.Set(15, dummy); | 
|---|
| 299 | } | 
|---|