source: branches/Corsika7405Compatibility/msimcamera/MSimBundlePhotons.cc

Last change on this file was 18180, checked in by dbaack, 9 years ago
Fixed bug with non cleared "PhotonEvent"
File size: 6.0 KB
Line 
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
52ClassImp(MSimBundlePhotons);
53
54using namespace std;
55
56// --------------------------------------------------------------------------
57//
58// Default Constructor.
59//
60MSimBundlePhotons::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//
71Int_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//
122Int_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
151 // Loop over all photons
152 for (Int_t i=0; i<num; i++)
153 {
154 // Get i-th photon from array
155 MPhotonData &ph = (*fEvt)[i];
156
157 // Get pixel index (tag) from photon
158 const Int_t tag = ph.GetTag();
159
160 // Check if the photon was tagged at all and
161 // whether the corresponding lut entry exists
162 if (tag<0 || tag>=max)
163 continue;
164
165 // Get the routing assigned to this index
166 const MArrayI &row = fLut.GetRow(tag);
167
168 // Sanity check: Check if we were routed to a
169 // valid entry if not throw away this photon.
170 if (row.GetSize()==0)
171 continue;
172
173 // Get corresponding entry from routing table
174 const Int_t &idx = row[0];
175
176 // Check if we were routed to a valid entry ("not connected")
177 // if not throw away this photon.
178 if (idx<0)
179 continue;
180
181 // Set Tag to new index
182 ph.SetTag(idx);
183
184 // Copy photon to its now position in array and increade counter
185 (*fEvt)[cnt++] = ph;
186 }
187
188 // Shrink the list of photons to its new size
189 fEvt->Shrink(cnt);
190
191 // Set new maximum index (Note, that this is the maximum index
192 // available in the LUT, which does not necessarily correspond
193 // to, e.g., the number of pixels although it should)
194 fStat->SetMaxIndex(fLut.GetMaxIndex());
195
196 return kTRUE;
197}
198
199// --------------------------------------------------------------------------
200//
201// FileName: lut.txt
202//
203Int_t MSimBundlePhotons::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
204{
205 Bool_t rc = kFALSE;
206 if (IsEnvDefined(env, prefix, "FileName", print))
207 {
208 rc = kTRUE;
209 fFileName = GetEnvValue(env, prefix, "FileName", fFileName);
210 }
211
212 return rc;
213}
Note: See TracBrowser for help on using the repository browser.