source: trunk/MagicSoft/Mars/msimcamera/MSimBundlePhotons.cc@ 9366

Last change on this file since 9366 was 9318, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 5.9 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 return kFALSE;
109
110 *fLog << inf << "Using look-up table from " << fFileName << endl;
111
112 return kTRUE;
113}
114
115// --------------------------------------------------------------------------
116//
117// Re-index all photons according to the look-up table.
118//
119Int_t MSimBundlePhotons::Process()
120{
121 // Make sure that we don't get a seg-fault
122 /*
123 if (fStat->GetMaxIndex()>=fLut.GetEntriesFast())
124 {
125 *fLog << err;
126 *fLog << "ERROR - MSimBundlePhotons::Process: Maximum pixel index stored" << endl;
127 *fLog << " in tag of MPhotonData exceeds the look-up table length." << endl;
128 return kERROR;
129 }*/
130
131 // FIXME: Add a range check comparing the min and max tag with
132 // the number of entries in the routing table
133
134 // Get total number of photons
135 const Int_t num = fEvt->GetNumPhotons();
136
137 // If there are no photons we can do nothing
138 if (num==0)
139 return kTRUE;
140
141 // Get maximum index allowed
142 const Int_t max = fLut.GetEntriesFast();
143
144 // Initialize a counter for the final number of photons.
145 Int_t cnt=0;
146
147 // Loop over all photons
148 for (Int_t i=0; i<num; i++)
149 {
150 // Get i-th photon from array
151 MPhotonData &ph = (*fEvt)[i];
152
153 // Get pixel index (tag) from photon
154 const Int_t tag = ph.GetTag();
155
156 // Check if the photon was tagged at all and
157 // whether the corresponding lut entry exists
158 if (tag<0 || tag>=max)
159 continue;
160
161 // Get the routing assigned to this index
162 const MArrayI &row = fLut.GetRow(tag);
163
164 // Sanity check: Check if we were routed to a
165 // valid entry if not throw away this photon.
166 if (row.GetSize()==0)
167 continue;
168
169 // Get corresponding entry from routing table
170 const Int_t &idx = row[0];
171
172 // Check if we were routed to a valid entry
173 // if not throw away this photon.
174 if (idx<0)
175 continue;
176
177 // Set Tag to new index
178 ph.SetTag(idx);
179
180 // Copy photon to its now position in array and increade counter
181 (*fEvt)[cnt++] = ph;
182 }
183
184 // Shrink the list of photons to its new size
185 fEvt->Shrink(cnt);
186
187 // Set new maximum index (Note, that this is the maximum index
188 // available in the LUT, which does not necessarily correspond
189 // to, e.g., the number of pixels although it should)
190 fStat->SetMaxIndex(fLut.GetMaxIndex());
191
192 return kTRUE;
193}
194
195// --------------------------------------------------------------------------
196//
197// FileName: lut.txt
198//
199Int_t MSimBundlePhotons::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
200{
201 Bool_t rc = kFALSE;
202 if (IsEnvDefined(env, prefix, "FileName", print))
203 {
204 rc = kTRUE;
205 fFileName = GetEnvValue(env, prefix, "FileName", fFileName);
206 }
207
208 return rc;
209}
Note: See TracBrowser for help on using the repository browser.