| 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 | //  MSimBundlePhotons | 
|---|
| 28 | // | 
|---|
| 29 | // This task sets a new tag (index) for all photons in a list MPhotonEvent | 
|---|
| 30 | // based on a look-up table. | 
|---|
| 31 | // | 
|---|
| 32 | // Input: | 
|---|
| 33 | //  MPhotonEvent | 
|---|
| 34 | //  MPhotonStatistics | 
|---|
| 35 | // | 
|---|
| 36 | // Output | 
|---|
| 37 | //  MPhotonEvent | 
|---|
| 38 | // | 
|---|
| 39 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 40 | #include "MSimBundlePhotons.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "MLog.h" | 
|---|
| 43 | #include "MLogManip.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include "MArrayI.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include "MParList.h" | 
|---|
| 48 |  | 
|---|
| 49 | #include "MPhotonEvent.h" | 
|---|
| 50 | #include "MPhotonData.h" | 
|---|
| 51 |  | 
|---|
| 52 | ClassImp(MSimBundlePhotons); | 
|---|
| 53 |  | 
|---|
| 54 | using namespace std; | 
|---|
| 55 |  | 
|---|
| 56 | // -------------------------------------------------------------------------- | 
|---|
| 57 | // | 
|---|
| 58 | //  Default Constructor. | 
|---|
| 59 | // | 
|---|
| 60 | MSimBundlePhotons::MSimBundlePhotons(const char* name, const char *title) | 
|---|
| 61 | : fEvt(0), fStat(0)//, fFileName("mreflector/dwarf-apdmap.txt") | 
|---|
| 62 | { | 
|---|
| 63 | fName  = name  ? name  : "MSimBundlePhotons"; | 
|---|
| 64 | fTitle = title ? title : "Task to bundle (re-index) photons according to a look-up table"; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | // -------------------------------------------------------------------------- | 
|---|
| 68 | // | 
|---|
| 69 | // Check for the needed containers and read the oruting table. | 
|---|
| 70 | // | 
|---|
| 71 | Int_t MSimBundlePhotons::PreProcess(MParList *pList) | 
|---|
| 72 | { | 
|---|
| 73 | fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent"); | 
|---|
| 74 | if (!fEvt) | 
|---|
| 75 | { | 
|---|
| 76 | *fLog << err << "MPhotonEvent not found... aborting." << endl; | 
|---|
| 77 | return kFALSE; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | fStat = (MPhotonStatistics*)pList->FindObject("MPhotonStatistics"); | 
|---|
| 81 | if (!fStat) | 
|---|
| 82 | { | 
|---|
| 83 | *fLog << err << "MPhotonStatistics not found... aborting." << endl; | 
|---|
| 84 | return kFALSE; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | if (fFileName.IsNull()) | 
|---|
| 88 | return kSKIP; | 
|---|
| 89 |  | 
|---|
| 90 | // Read the look-up table | 
|---|
| 91 | fLut.Delete(); | 
|---|
| 92 | if (!fFileName.IsNull() && fLut.ReadFile(fFileName)<0) | 
|---|
| 93 | return kFALSE; | 
|---|
| 94 |  | 
|---|
| 95 | // If the table is empty remove this task from the tasklist | 
|---|
| 96 | if (fLut.IsEmpty()) | 
|---|
| 97 | { | 
|---|
| 98 | *fLog << inf << "Look-up table to bundle photons empty... skipping." << endl; | 
|---|
| 99 | return kSKIP; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | // Now invert the tablee. Otherwise we have to do a lot of | 
|---|
| 103 | // searching for every index. | 
|---|
| 104 | fLut.Invert(); | 
|---|
| 105 |  | 
|---|
| 106 | // Make sure that each line has exactly one row | 
|---|
| 107 | if (!fLut.HasConstantLength() && fLut.GetMaxEntries()!=1) | 
|---|
| 108 | { | 
|---|
| 109 | *fLog << err << fFileName << " wrongly formatted." << endl; | 
|---|
| 110 | return kFALSE; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | *fLog << inf << "Using look-up table from " << fFileName << endl; | 
|---|
| 114 |  | 
|---|
| 115 | return kTRUE; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | // -------------------------------------------------------------------------- | 
|---|
| 119 | // | 
|---|
| 120 | //  Re-index all photons according to the look-up table. | 
|---|
| 121 | // | 
|---|
| 122 | Int_t MSimBundlePhotons::Process() | 
|---|
| 123 | { | 
|---|
| 124 | // Make sure that we don't get a seg-fault | 
|---|
| 125 | /* | 
|---|
| 126 | if (fStat->GetMaxIndex()>=fLut.GetEntriesFast()) | 
|---|
| 127 | { | 
|---|
| 128 | *fLog << err; | 
|---|
| 129 | *fLog << "ERROR - MSimBundlePhotons::Process: Maximum pixel index stored" << endl; | 
|---|
| 130 | *fLog << "        in tag of MPhotonData exceeds the look-up table length." << endl; | 
|---|
| 131 | return kERROR; | 
|---|
| 132 | }*/ | 
|---|
| 133 |  | 
|---|
| 134 | // FIXME: Add a range check comparing the min and max tag with | 
|---|
| 135 | // the number of entries in the routing table | 
|---|
| 136 |  | 
|---|
| 137 | // Get total number of photons | 
|---|
| 138 | const Int_t num = fEvt->GetNumPhotons(); | 
|---|
| 139 |  | 
|---|
| 140 | // If there are no photons we can do nothing | 
|---|
| 141 | if (num==0) | 
|---|
| 142 | return kTRUE; | 
|---|
| 143 |  | 
|---|
| 144 | // Get maximum index allowed | 
|---|
| 145 | const Int_t max = fLut.GetEntriesFast(); | 
|---|
| 146 |  | 
|---|
| 147 | // Initialize a counter for the final number of photons. | 
|---|
| 148 | Int_t cnt=0; | 
|---|
| 149 |  | 
|---|
| 150 | // Loop over all photons | 
|---|
| 151 | for (Int_t i=0; i<num; i++) | 
|---|
| 152 | { | 
|---|
| 153 | // Get i-th photon from array | 
|---|
| 154 | MPhotonData &ph = (*fEvt)[i]; | 
|---|
| 155 |  | 
|---|
| 156 | // Get pixel index (tag) from photon | 
|---|
| 157 | const Int_t tag = ph.GetTag(); | 
|---|
| 158 |  | 
|---|
| 159 | // Check if the photon was tagged at all and | 
|---|
| 160 | // whether the corresponding lut entry exists | 
|---|
| 161 | if (tag<0 || tag>=max) | 
|---|
| 162 | continue; | 
|---|
| 163 |  | 
|---|
| 164 | // Get the routing assigned to this index | 
|---|
| 165 | const MArrayI &row = fLut.GetRow(tag); | 
|---|
| 166 |  | 
|---|
| 167 | // Sanity check: Check if we were routed to a | 
|---|
| 168 | // valid entry if not throw away this photon. | 
|---|
| 169 | if (row.GetSize()==0) | 
|---|
| 170 | continue; | 
|---|
| 171 |  | 
|---|
| 172 | // Get corresponding entry from routing table | 
|---|
| 173 | const Int_t &idx = row[0]; | 
|---|
| 174 |  | 
|---|
| 175 | // Check if we were routed to a valid entry ("not connected") | 
|---|
| 176 | // if not throw away this photon. | 
|---|
| 177 | if (idx<0) | 
|---|
| 178 | continue; | 
|---|
| 179 |  | 
|---|
| 180 | // Set Tag to new index | 
|---|
| 181 | ph.SetTag(idx); | 
|---|
| 182 |  | 
|---|
| 183 | // Copy photon to its now position in array and increade counter | 
|---|
| 184 | (*fEvt)[cnt++] = ph; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | // Shrink the list of photons to its new size | 
|---|
| 188 | fEvt->Shrink(cnt); | 
|---|
| 189 |  | 
|---|
| 190 | // Set new maximum index (Note, that this is the maximum index | 
|---|
| 191 | // available in the LUT, which does not necessarily correspond | 
|---|
| 192 | // to, e.g., the number of pixels although it should) | 
|---|
| 193 | fStat->SetMaxIndex(fLut.GetMaxIndex()); | 
|---|
| 194 |  | 
|---|
| 195 | return kTRUE; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | // -------------------------------------------------------------------------- | 
|---|
| 199 | // | 
|---|
| 200 | // FileName: lut.txt | 
|---|
| 201 | // | 
|---|
| 202 | Int_t MSimBundlePhotons::ReadEnv(const TEnv &env, TString prefix, Bool_t print) | 
|---|
| 203 | { | 
|---|
| 204 | Bool_t rc = kFALSE; | 
|---|
| 205 | if (IsEnvDefined(env, prefix, "FileName", print)) | 
|---|
| 206 | { | 
|---|
| 207 | rc = kTRUE; | 
|---|
| 208 | fFileName = GetEnvValue(env, prefix, "FileName", fFileName); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | return rc; | 
|---|
| 212 | } | 
|---|