| 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 1/2004 <mailto:moralejo@pd.infn.it> | 
|---|
| 19 | !              Thomas Bretz  5/2002 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 20 | ! | 
|---|
| 21 | !   Copyright: MAGIC Software Development, 2000-2004 | 
|---|
| 22 | ! | 
|---|
| 23 | ! | 
|---|
| 24 | \* ======================================================================== */ | 
|---|
| 25 |  | 
|---|
| 26 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 27 | // | 
|---|
| 28 | //  MMCALIBRATE - Calibration of MC data | 
|---|
| 29 | // | 
|---|
| 30 | //  This macro converts raw MC data into calibrated data (photons per pixel) | 
|---|
| 31 | //  It optionally divides the output into a train and a test sample | 
|---|
| 32 | // | 
|---|
| 33 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 34 |  | 
|---|
| 35 | #include "MImgCleanStd.h" | 
|---|
| 36 |  | 
|---|
| 37 | void mccalibrate() | 
|---|
| 38 | { | 
|---|
| 39 | // | 
|---|
| 40 | // This macro reads in MC camera files and produces and output with | 
|---|
| 41 | // calibrated events (signal in photons for all pixels, in MCerPhotEvt | 
|---|
| 42 | // containers). | 
|---|
| 43 | // | 
|---|
| 44 |  | 
|---|
| 45 | // ------------- user change ----------------- | 
|---|
| 46 | TString* CalibrationFilename; | 
|---|
| 47 |  | 
|---|
| 48 | CalibrationFilename = new TString("/users/emc/moralejo/mcdata/Period021_0.73_mirror/gammas_nonoise/Gamma_*root");  // File to be used for the calibration (must be a camera file without added noise) | 
|---|
| 49 |  | 
|---|
| 50 | Char_t* AnalysisFilename = "Gamma_zbin*w0.root";  // File to be analyzed | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | TString* OutFilename1; | 
|---|
| 54 | TString* OutFilename2; | 
|---|
| 55 |  | 
|---|
| 56 | // Output file names | 
|---|
| 57 | OutFilename1 = new TString("calibrated_gamma_train.root"); | 
|---|
| 58 | OutFilename2 = new TString("calibrated_gamma_test.root"); | 
|---|
| 59 |  | 
|---|
| 60 | // To get only one output file, just comment out the second | 
|---|
| 61 | // one of the above lines | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | // | 
|---|
| 65 | //  USER CHANGE: Set signal extractor | 
|---|
| 66 | // | 
|---|
| 67 | //    MExtractFixedWindowPeakSearch sigextract; | 
|---|
| 68 | //    sigextract.SetWindows(6, 6, 4); | 
|---|
| 69 | // | 
|---|
| 70 |  | 
|---|
| 71 | //    MExtractTimeAndChargeDigitalFilter sigextract; | 
|---|
| 72 | //    sigextract.SetNameWeightsFile("/users/emc/moralejo/Mars/msignal/MC_weights.dat"); | 
|---|
| 73 | //    sigextract.SetRange(1, 14, 3, 14); | 
|---|
| 74 |  | 
|---|
| 75 | MExtractTimeAndChargeSpline sigextract; | 
|---|
| 76 | sigextract.SetRiseTimeHiGain(0.5); | 
|---|
| 77 | sigextract.SetFallTimeHiGain(0.5); | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | // USER CHANGE: high to low gain ratio. DEPENDS on EXTRACTOR!! | 
|---|
| 81 | // One should calculate somewhere else what this factor is for each extractor! | 
|---|
| 82 | Float_t hi2low = 12.; // tentative value for spline with risetime 0.5, fall time 0.5 | 
|---|
| 83 |  | 
|---|
| 84 | MMcCalibrationUpdate  mccalibupdate; | 
|---|
| 85 | mccalibupdate.SetUserLow2HiGainFactor(hi2low); | 
|---|
| 86 | ///// User change: calibrate in photons or phe- : | 
|---|
| 87 | mccalibupdate.SetSignalType(MCalibrateData::kPhe); | 
|---|
| 88 | //  mccalibupdate.SetSignalType(MCalibrateData::kPhot); | 
|---|
| 89 |  | 
|---|
| 90 | // --------------------------------------------------------------------- | 
|---|
| 91 | // | 
|---|
| 92 | // Create a empty Parameter List and an empty Task List | 
|---|
| 93 | // The tasklist is identified in the eventloop by its name | 
|---|
| 94 | // | 
|---|
| 95 | MParList  plist; | 
|---|
| 96 |  | 
|---|
| 97 | MTaskList tlist; | 
|---|
| 98 |  | 
|---|
| 99 | plist.AddToList(&tlist); | 
|---|
| 100 |  | 
|---|
| 101 | MBadPixelsCam badpix; | 
|---|
| 102 | plist.AddToList(&badpix);  // Not used for now. | 
|---|
| 103 |  | 
|---|
| 104 | // | 
|---|
| 105 | // Now setup the tasks and tasklist: | 
|---|
| 106 | // --------------------------------- | 
|---|
| 107 | // | 
|---|
| 108 | MReadMarsFile read("Events"); | 
|---|
| 109 |  | 
|---|
| 110 | if (CalibrationFilename) | 
|---|
| 111 | read.AddFile(CalibrationFilename->Data()); | 
|---|
| 112 |  | 
|---|
| 113 | read.DisableAutoScheme(); | 
|---|
| 114 |  | 
|---|
| 115 | MGeomApply geom; | 
|---|
| 116 | // Reads in geometry from MC file and sets the right sizes for | 
|---|
| 117 | // several parameter containers. | 
|---|
| 118 |  | 
|---|
| 119 | MMcPedestalCopy   pcopy; | 
|---|
| 120 | // Copies pedestal data from the MC file run fadc header to the | 
|---|
| 121 | // MPedestalCam container. | 
|---|
| 122 |  | 
|---|
| 123 | MPointingPosCalc pointcalc; | 
|---|
| 124 | // Creates MPointingPos object and fills it with the telescope | 
|---|
| 125 | // orientation information taken from MMcEvt. | 
|---|
| 126 |  | 
|---|
| 127 | MCalibrateData calib; | 
|---|
| 128 | // | 
|---|
| 129 | // MCalibrateData transforms signals from ADC counts into photons or phe- | 
|---|
| 130 | // (this can be selected anove). In the first loop it applies a "dummy" | 
|---|
| 131 | // calibration supplied by MMcCalibrationUpdate, just to equalize inner | 
|---|
| 132 | // and outer pixels, and calculates SIZE in "equivalent number of inner | 
|---|
| 133 | // ADC counts". At the end of the first loop, in the PostProcess of | 
|---|
| 134 | // MMcCalibrationCalc (see below) the true calibration constants | 
|---|
| 135 | // are calculated. | 
|---|
| 136 | // | 
|---|
| 137 | calib.SetCalibrationMode(MCalibrateData::kFfactor); | 
|---|
| 138 | // Do not change the CalibrationMode above for MC...! | 
|---|
| 139 |  | 
|---|
| 140 | // Now set also whether to calibrate in photons or phe-: | 
|---|
| 141 | calib.SetSignalType(mccalibupdate.GetSignalType()); | 
|---|
| 142 |  | 
|---|
| 143 | MImgCleanStd clean; | 
|---|
| 144 | // | 
|---|
| 145 | // Applies tail cuts to image. Since the calibration is performed on | 
|---|
| 146 | // noiseless camera files, the precise values of the cleaning levels | 
|---|
| 147 | // are unimportant (in any case, only pixels without any C-photon will | 
|---|
| 148 | // be rejected). | 
|---|
| 149 | // | 
|---|
| 150 |  | 
|---|
| 151 | MHillasCalc hcalc; | 
|---|
| 152 | hcalc.Disable(MHillasCalc::kCalcHillasSrc); | 
|---|
| 153 | // Calculates Hillas parameters not dependent on source position. | 
|---|
| 154 |  | 
|---|
| 155 | MMcCalibrationCalc mccalibcalc; | 
|---|
| 156 | // Calculates calibration constants to convert from ADC counts to photons. | 
|---|
| 157 |  | 
|---|
| 158 | tlist.AddToList(&read); | 
|---|
| 159 | tlist.AddToList(&geom); | 
|---|
| 160 |  | 
|---|
| 161 | MF notrigger("MMcTrig.fNumFirstLevel<1"); | 
|---|
| 162 | MContinue skipnotrig(¬rigger); | 
|---|
| 163 | tlist.AddToList(&skipnotrig); | 
|---|
| 164 |  | 
|---|
| 165 | tlist.AddToList(&pcopy); | 
|---|
| 166 | tlist.AddToList(&pointcalc); | 
|---|
| 167 | tlist.AddToList(&sigextract); | 
|---|
| 168 | tlist.AddToList(&mccalibupdate); | 
|---|
| 169 | tlist.AddToList(&calib); | 
|---|
| 170 | tlist.AddToList(&clean); | 
|---|
| 171 | tlist.AddToList(&hcalc); | 
|---|
| 172 |  | 
|---|
| 173 | tlist.AddToList(&mccalibcalc); | 
|---|
| 174 |  | 
|---|
| 175 | // | 
|---|
| 176 | // Open output files: | 
|---|
| 177 | // | 
|---|
| 178 | MWriteRootFile write1(OutFilename1->Data()); // Writes output | 
|---|
| 179 |  | 
|---|
| 180 | MWriteRootFile write1_b(OutFilename1->Data()); | 
|---|
| 181 | write1_b.AddContainer("MMcEvtBasic", "OriginalMC", kFALSE); | 
|---|
| 182 | // Add the MMcEvtBasic container only in case it exists in | 
|---|
| 183 | // the input camera files | 
|---|
| 184 |  | 
|---|
| 185 | write1.AddContainer("MGeomCam",            "RunHeaders"); | 
|---|
| 186 | write1.AddContainer("MMcConfigRunHeader",  "RunHeaders"); | 
|---|
| 187 | write1.AddContainer("MMcCorsikaRunHeader", "RunHeaders"); | 
|---|
| 188 | write1.AddContainer("MMcFadcHeader",       "RunHeaders"); | 
|---|
| 189 | write1.AddContainer("MMcRunHeader",        "RunHeaders"); | 
|---|
| 190 | write1.AddContainer("MMcTrigHeader",       "RunHeaders"); | 
|---|
| 191 | write1.AddContainer("MRawRunHeader",       "RunHeaders"); | 
|---|
| 192 |  | 
|---|
| 193 | write1.AddContainer("MMcEvt",        "Events"); | 
|---|
| 194 | write1.AddContainer("MMcTrig",       "Events"); | 
|---|
| 195 | write1.AddContainer("MPointingPos",  "Events"); | 
|---|
| 196 | write1.AddContainer("MRawEvtHeader", "Events"); | 
|---|
| 197 | write1.AddContainer("MCerPhotEvt",   "Events"); | 
|---|
| 198 | write1.AddContainer("MPedPhotCam",   "Events"); | 
|---|
| 199 |  | 
|---|
| 200 | if (OutFilename2) | 
|---|
| 201 | { | 
|---|
| 202 | MWriteRootFile write2(OutFilename2->Data()); // Writes output | 
|---|
| 203 |  | 
|---|
| 204 | MWriteRootFile write2_b(OutFilename2->Data()); | 
|---|
| 205 | write2_b.AddContainer("MMcEvtBasic", "OriginalMC", kFALSE); | 
|---|
| 206 | // Add the MMcEvtBasic container only in case it exists in | 
|---|
| 207 | // the input camera files | 
|---|
| 208 |  | 
|---|
| 209 | write2.AddContainer("MGeomCam",            "RunHeaders"); | 
|---|
| 210 | write2.AddContainer("MMcConfigRunHeader",  "RunHeaders"); | 
|---|
| 211 | write2.AddContainer("MMcCorsikaRunHeader", "RunHeaders"); | 
|---|
| 212 | write2.AddContainer("MMcFadcHeader",       "RunHeaders"); | 
|---|
| 213 | write2.AddContainer("MMcRunHeader",        "RunHeaders"); | 
|---|
| 214 | write2.AddContainer("MMcTrigHeader",       "RunHeaders"); | 
|---|
| 215 | write2.AddContainer("MRawRunHeader",       "RunHeaders"); | 
|---|
| 216 |  | 
|---|
| 217 | write2.AddContainer("MMcEvt",        "Events"); | 
|---|
| 218 | write2.AddContainer("MMcTrig",       "Events"); | 
|---|
| 219 | write2.AddContainer("MPointingPos",  "Events"); | 
|---|
| 220 | write2.AddContainer("MRawEvtHeader", "Events"); | 
|---|
| 221 | write2.AddContainer("MCerPhotEvt",   "Events"); | 
|---|
| 222 | write2.AddContainer("MPedPhotCam",   "Events"); | 
|---|
| 223 |  | 
|---|
| 224 | // | 
|---|
| 225 | // Divide output in train and test samples, using the event number | 
|---|
| 226 | // (odd/even) to achieve otherwise unbiased event samples: | 
|---|
| 227 | // | 
|---|
| 228 | MF filter1("{MMcEvt.fEvtNumber%2}>0.5"); | 
|---|
| 229 | MF filter2("{MMcEvt.fEvtNumber%2}<0.5"); | 
|---|
| 230 |  | 
|---|
| 231 | write1.SetFilter(&filter1); | 
|---|
| 232 | write2.SetFilter(&filter2); | 
|---|
| 233 |  | 
|---|
| 234 | write1_b.SetFilter(&filter1); | 
|---|
| 235 | write2_b.SetFilter(&filter2); | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | // | 
|---|
| 239 | // First loop: Calibration loop | 
|---|
| 240 | // | 
|---|
| 241 |  | 
|---|
| 242 | MProgressBar bar; | 
|---|
| 243 | bar.SetWindowName("Calibrating..."); | 
|---|
| 244 |  | 
|---|
| 245 | MEvtLoop evtloop; | 
|---|
| 246 | evtloop.SetProgressBar(&bar); | 
|---|
| 247 | evtloop.SetParList(&plist); | 
|---|
| 248 |  | 
|---|
| 249 | if (CalibrationFilename) | 
|---|
| 250 | { | 
|---|
| 251 | if (!evtloop.Eventloop()) | 
|---|
| 252 | return; | 
|---|
| 253 | mccalibcalc->GetHistADC2PhotEl()->Write(); | 
|---|
| 254 | mccalibcalc->GetHistPhot2PhotEl()->Write(); | 
|---|
| 255 | // Writes out the histograms used for calibration. In case of | 
|---|
| 256 | // aslit output in train and test file, this is written only | 
|---|
| 257 | // in the test file for now. | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | // | 
|---|
| 261 | // Second loop: apply calibration factors to MC events in the | 
|---|
| 262 | // file to be anlyzed: | 
|---|
| 263 | // | 
|---|
| 264 |  | 
|---|
| 265 | // | 
|---|
| 266 | // Change the read task by another one which reads the file we want to analyze: | 
|---|
| 267 | // | 
|---|
| 268 |  | 
|---|
| 269 | MReadMarsFile read2("Events"); | 
|---|
| 270 | read2.AddFile(AnalysisFilename); | 
|---|
| 271 | read2.DisableAutoScheme(); | 
|---|
| 272 | tlist.AddToListBefore(&read2, &read, "All"); | 
|---|
| 273 | tlist.RemoveFromList(&read); | 
|---|
| 274 |  | 
|---|
| 275 | // Now add tasks to write MMcEvtBasic to the OriginalMC tree: | 
|---|
| 276 | tlist.AddToListBefore(&write1_b, &skipnotrig, "All"); | 
|---|
| 277 | if (OutFilename2) | 
|---|
| 278 | tlist.AddToListBefore(&write2_b, &skipnotrig, "All"); | 
|---|
| 279 |  | 
|---|
| 280 |  | 
|---|
| 281 | if (OutFilename2) // Add filters to split output in train and test | 
|---|
| 282 | { | 
|---|
| 283 | tlist.AddToListBefore(&filter1, &write1_b, "All"); | 
|---|
| 284 | tlist.AddToListBefore(&filter2, &write1_b, "All"); | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | bar.SetWindowName("Writing..."); | 
|---|
| 288 |  | 
|---|
| 289 | tlist.RemoveFromList(&clean); | 
|---|
| 290 | tlist.RemoveFromList(&hcalc); | 
|---|
| 291 | tlist.RemoveFromList(&mccalibcalc); | 
|---|
| 292 |  | 
|---|
| 293 | tlist.AddToList(&write1); | 
|---|
| 294 |  | 
|---|
| 295 | if (OutFilename2) | 
|---|
| 296 | tlist.AddToList(&write2); | 
|---|
| 297 | // Add tasks to write the Events and RunHeaders trees to output. | 
|---|
| 298 |  | 
|---|
| 299 | if (!evtloop.Eventloop()) | 
|---|
| 300 | return; | 
|---|
| 301 |  | 
|---|
| 302 |  | 
|---|
| 303 | tlist.PrintStatistics(); | 
|---|
| 304 | } | 
|---|