LOGO

RestFrames  v1.0.1
RestFrames HEP Event Analysis Software Library
ReconstructionFrame.cc
Go to the documentation of this file.
1 // RestFrames: particle physics event analysis library
3 // --------------------------------------------------------------------
4 // Copyright (c) 2014-2016, Christopher Rogan
14 // This file is part of RestFrames.
15 //
16 // RestFrames is free software; you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation; either version 2 of the License, or
19 // (at your option) any later version.
20 //
21 // RestFrames is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with RestFrames. If not, see <http://www.gnu.org/licenses/>.
29 
33 #include "RestFrames/Group.hh"
35 
36 namespace RestFrames {
37 
39  // ReconstructionFrame class methods
41  ReconstructionFrame::ReconstructionFrame(const std::string& sname,
42  const std::string& stitle)
43  : RestFrame(sname, stitle)
44  {
45  m_Ana = kRecoFrame;
46  m_GroupPtr = nullptr;
47  }
48 
49  ReconstructionFrame::ReconstructionFrame()
50  : RestFrame() {}
51 
52  ReconstructionFrame::~ReconstructionFrame(){
53  Clear();
54  }
55 
57  SetGroup();
58  m_ChildStates.clear();
59  RestFrame::Clear();
60  }
61 
62  ReconstructionFrame& ReconstructionFrame::Empty(){
63  return VisibleRecoFrame::Empty();
64  }
65 
67  if(!frame) return;
68  if(!frame.IsRecoFrame()) return;
69  RestFrame::AddChildFrame(frame);
70  }
71 
73  SetBody(false);
74  bool contains = m_ChildFrames.Contains(frame);
75  m_ChildBoosts.erase(&frame);
76  m_ChildStates.erase(&frame);
77  m_ChildFrames.Remove(frame);
78  if(contains)
79  frame.SetParentFrame();
80  }
81 
83  SetBody(false);
84  while(GetNChildren() > 0)
85  RemoveChildFrame(m_ChildFrames[0]);
86  m_ChildFrames.Clear();
87  m_ChildBoosts.clear();
88  m_ChildStates.clear();
89  }
90 
92  if(!frame) return;
93  if(!frame.IsRecoFrame()) return;
94  RestFrame::SetParentFrame(frame);
95  }
96 
98  const RestFrame& frame = RestFrame::GetParentFrame();
99  if(!frame.IsEmpty())
100  return static_cast<const ReconstructionFrame&>(frame);
101  else
102  return ReconstructionFrame::Empty();
103  }
104 
106  RestFrame& frame = RestFrame::GetChildFrame(i);
107  if(!frame.IsEmpty())
108  return static_cast<ReconstructionFrame&>(frame);
109  else
110  return ReconstructionFrame::Empty();
111  }
112 
113  StateList const& ReconstructionFrame::GetChildStates(int i) const {
114  return GetChildStates(GetChildFrame(i));
115  }
116 
117  StateList const& ReconstructionFrame::GetChildStates(const RestFrame& child) const {
118  if(m_ChildStates.count(&child) <= 0)
119  return State::EmptyList();
120 
121  return m_ChildStates[&child];
122  }
123 
124  void ReconstructionFrame::SetGroup(Group& group){
125  if(IsEmpty()) return;
126 
127  SetMind(false);
128 
129  if(m_GroupPtr){
130  if(*m_GroupPtr != group){
131  Group* groupPtr = m_GroupPtr;
132  m_GroupPtr = nullptr;
133  groupPtr->RemoveFrame(*this);
134  }
135  }
136  if(!group)
137  m_GroupPtr = nullptr;
138  else
139  m_GroupPtr = &group;
140  }
141 
142  Group& ReconstructionFrame::GetGroup() const {
143  if(m_GroupPtr)
144  return *m_GroupPtr;
145  else
146  return Group::Empty();
147  }
148 
149  GroupList ReconstructionFrame::GetListGroups() const {
150  GroupList groups;
151  FillListGroupsRecursive(groups);
152  return groups;
153  }
154 
155  void ReconstructionFrame::FillListGroupsRecursive(GroupList& groups) const {
156  if(m_GroupPtr) groups.Add(*m_GroupPtr);
157  int Nchild = GetNChildren();
158  for(int i = 0; i < Nchild; i++)
159  GetChildFrame(i).FillListGroupsRecursive(groups);
160  }
161 
162  bool ReconstructionFrame::InitializeVisibleStates(){
163  m_ChildStates.clear();
164  if(!GetLabFrame())
165  return false;
166 
167  const VisibleStateList& states =
168  static_cast<const LabRecoFrame&>(GetLabFrame()).GetTreeStates();
169 
170  int Nchild = GetNChildren();
171  for(int i = 0; i < Nchild; i++){
172  RestFrame& child = GetChildFrame(i);
173  m_ChildStates[&child] = StateList();
174  RFList<ReconstructionFrame> frames = child.GetListVisibleFrames();
175  int Nf = frames.GetN();
176  for(int f = 0; f < Nf; f++)
177  if(!frames[f].GetGroup())
178  if(!m_ChildStates[&child].Add(StateList(states).GetFrame(frames[f]))){
179  m_Log << LogWarning;
180  m_Log << "Unable to associate State with Group-less Frame:";
181  m_Log << Log(frames[f]) << LogEnd;
182  return false;
183  }
184  }
185  return true;
186  }
187 
188  bool ReconstructionFrame::InitializeGroupStates(){
189  GroupList groups = GetListGroups();
190  int Ngroup = groups.GetN();
191  int Nchild = GetNChildren();
192 
193  for(int c = 0; c < Nchild; c++){
194  RestFrameList frames =
195  GetChildFrame(c).GetListVisibleFrames() +
196  GetChildFrame(c).GetListInvisibleFrames();
197  int Nframe = frames.GetN();
198  for(int f = 0; f < Nframe; f++){
199  for(int g = 0; g < Ngroup; g++){
200  if(groups[g].ContainsFrame(frames[f])){
201  State& state = groups[g].GetChildState(frames[f]);
202  if(!state){
203  m_Log << LogWarning;
204  m_Log << "Unable to get State associated with Group Frame: " << std::endl;
205  m_Log << " Frame:" << Log(frames[f]);
206  m_Log << " Group:" << Log(groups[g]) << LogEnd;
207  return false;
208  }
209  m_ChildStates[&GetChildFrame(c)].Add(state);
210  break;
211  }
212  }
213  }
214  }
215  return true;
216  }
217 
218  bool ReconstructionFrame::InitializeAnalysisRecursive(){
219  if(!IsSoundBody()){
220  UnSoundBody(RF_FUNCTION);
221  return SetMind(false);
222  }
223 
224  if(!InitializeVisibleStates())
225  return SetMind(false);
226  if(!InitializeGroupStates())
227  return SetMind(false);
228 
229  int Nchild = GetNChildren();
230  for(int i = 0; i < Nchild; i++)
231  if(!GetChildFrame(i).InitializeAnalysisRecursive()){
232  m_Log << LogWarning;
233  m_Log << "Unable to recursively initialize analysis for frame:";
234  m_Log << Log(GetChildFrame(i)) << LogEnd;
235  return SetMind(false);
236  }
237 
238  return SetMind(true);
239  }
240 
241  bool ReconstructionFrame::ResetRecoFrame(){
242  return true;
243  }
244 
245  bool ReconstructionFrame::ClearEventRecursive(){
246  if(!IsSoundMind()){
247  UnSoundMind(RF_FUNCTION);
248  return false;
249  }
250 
251  if(!ResetRecoFrame())
252  return false;
253 
254  int Nf = GetNChildren();
255  for(int i = 0; i < Nf; i++)
256  if(!GetChildFrame(i).ClearEventRecursive())
257  return false;
258 
259  return true;
260  }
261 
262  bool ReconstructionFrame::ReconstructFrame(){
263  if(!IsSoundMind()){
264  UnSoundMind(RF_FUNCTION);
265  return SetSpirit(false);
266  }
267 
268  TLorentzVector Ptot(0,0,0,0);
269 
270  int Nchild = GetNChildren();
271  for(int i = 0; i < Nchild; i++){
272  ReconstructionFrame& child = GetChildFrame(i);
273 
274  TLorentzVector P = m_ChildStates[&child].GetFourVector();
275  if(P.M() > 0.)
276  SetChildBoostVector(child, P.BoostVector());
277  else
278  SetChildBoostVector(child, m_Empty3Vector);
279  Ptot += P;
280 
281  child.SetFourVector(P,*this);
282 
283  if(child.IsVisibleFrame())
284  static_cast<VisibleRecoFrame&>(child).
285  SetCharge(m_ChildStates[&child].GetCharge());
286  }
287 
288  if(IsLabFrame()) SetFourVector(Ptot,*this);
289 
290  return SetSpirit(true);
291  }
292 
293  bool ReconstructionFrame::AnalyzeEventRecursive(){
294  if(!IsSoundMind()){
295  UnSoundMind(RF_FUNCTION);
296  return SetSpirit(false);
297  }
298  if(!ReconstructFrame()){
299  m_Log << LogWarning;
300  m_Log << "Unable to reconstruct event for this frame.";
301  m_Log << LogEnd;
302  return SetSpirit(false);
303  }
304 
305  int Nchild = GetNChildren();
306  for(int i = 0; i < Nchild; i++){
307  ReconstructionFrame& child = GetChildFrame(i);
308  TVector3 boost = GetChildBoostVector(child);
309 
310  bool terminal = child.IsVisibleFrame() || child.IsInvisibleFrame();
311  if(!terminal){
312  boost *= -1.;
313  m_ChildStates[&child].Boost(boost);
314  }
315 
316  if(!child.AnalyzeEventRecursive())
317  return SetSpirit(false);
318 
319  if(!terminal){
320  boost *= -1.;
321  m_ChildStates[&child].Boost(boost);
322  }
323  }
324 
325  return SetSpirit(true);
326  }
327 
328 }
virtual void Clear()
Clears ReconstructionFrame of all connections to other objects.
virtual void RemoveChildFrame(RestFrame &frame)
Remove a child of this frame.
virtual void AddChildFrame(RestFrame &frame)
Add a child RestFrame to this frame.
abstract base class for all Frame objects
virtual ReconstructionFrame & GetChildFrame(int i=0) const
Get the frame of the i th child.
void RemoveChildFrames()
Remove all the children of this frame.
virtual ReconstructionFrame const & GetParentFrame() const
Returns the parent of this frame.
virtual void SetParentFrame(RestFrame &frame=RestFrame::Empty())
Set the parent frame for this frame.