LOGO

RestFrames  v1.0.1
RestFrames HEP Event Analysis Software Library
Group.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 
30 #include "RestFrames/Group.hh"
33 #include "RestFrames/Jigsaw.hh"
34 #include "RestFrames/State.hh"
35 
36 namespace RestFrames {
37 
38  int Group::m_class_key = 0;
39 
40  Group::Group(const std::string& sname,
41  const std::string& stitle)
42  : RFBase(sname, stitle, Group::m_class_key++)
43  {
44  m_Type = kVanillaGroup;
45  m_GroupStatePtr = nullptr;
46  m_Log.SetSource("Group "+GetName());
47  }
48 
49  Group::Group() : RFBase() {
50  m_Type = kVanillaGroup;
51  m_Log.SetSource("Group "+GetName());
52  }
53 
54  Group::~Group(){
55  Clear();
56  }
57 
58  Group& Group::Empty(){
59  return InvisibleGroup::Empty();
60  }
61 
62  void Group::Clear(){
63  SetBody(false);
64 
65  m_GroupStatePtr = nullptr;
66 
67  RemoveFrames();
68  RemoveJigsaws();
69 
70  m_States.Clear();
71  m_StatesToResolve.Clear();
72  m_JigsawsToUse.Clear();
73  }
74 
75  bool Group::IsInvisibleGroup() const{
76  return m_Type == kInvisibleGroup;
77  }
78 
79  bool Group::IsCombinatoricGroup() const{
80  return m_Type == kCombinatoricGroup;
81  }
82 
83  void Group::AddFrame(RestFrame& frame){
84  if(IsEmpty()) return;
85  if(!frame) return;
86  if(!frame.IsRecoFrame()) return;
87  SetBody(false);
88 
89  static_cast<ReconstructionFrame&>(frame).SetGroup(*this);
90  m_Frames.Add(frame);
91  }
92 
93  void Group::AddFrames(const RestFrameList& frames){
94  int N = frames.GetN();
95  for(int i = 0; i < N; i++)
96  AddFrame(frames[i]);
97  }
98 
99  void Group::AddJigsaw(Jigsaw& jigsaw){
100  if(IsEmpty()) return;
101  if(!jigsaw) return;
102 
103  if(!jigsaw.GetGroup().IsEmpty()){
104  if(jigsaw.GetGroup() == *this)
105  return;
106  Group& group = jigsaw.GetGroup();
107  jigsaw.SetGroup();
108  group.RemoveJigsaw(jigsaw);
109  }
110 
111  SetBody(false);
112 
113  if(m_JigsawsToUse.Add(jigsaw))
114  jigsaw.SetGroup(*this);
115  }
116 
117  void Group::RemoveFrame(RestFrame& frame){
118  if(!m_Frames.Contains(frame))
119  return;
120 
121  SetBody(false);
122 
123  static_cast<ReconstructionFrame&>(frame).SetGroup();
124  m_Frames.Remove(frame);
125  }
126 
127  void Group::RemoveFrames(){
128  int N = m_Frames.GetN();
129  for(int i = N-1; i >= 0; i--){
130  RemoveFrame(m_Frames[i]);
131  }
132  }
133 
134  void Group::RemoveJigsaw(Jigsaw& jigsaw){
135  if(!m_Jigsaws.Contains(jigsaw) &&
136  !m_JigsawsToUse.Contains(jigsaw))
137  return;
138 
139  SetBody(false);
140 
141  jigsaw.SetGroup();
142  m_Jigsaws.Remove(jigsaw);
143  m_JigsawsToUse.Remove(jigsaw);
144  }
145 
146  void Group::RemoveJigsaws(){
147  int N = m_Jigsaws.GetN();
148  for(int i = N-1; i >= 0; i--){
149  RemoveJigsaw(m_Jigsaws[i]);
150  }
151  N = m_JigsawsToUse.GetN();
152  for(int i = N-1; i >= 0; i--){
153  RemoveJigsaw(m_JigsawsToUse[i]);
154  }
155  }
156 
157  bool Group::ContainsFrame(const RestFrame& frame) const {
158  return m_Frames.Contains(frame);
159  }
160 
161  int Group::GetNFrames() const {
162  return m_Frames.GetN();
163  }
164 
165  const RestFrameList& Group::GetListFrames() const {
166  return m_Frames;
167  }
168 
169  const JigsawList& Group::GetListJigsaws() const {
170  return m_Jigsaws;
171  }
172 
173  State& Group::GetParentState() const {
174  if(m_GroupStatePtr)
175  return *m_GroupStatePtr;
176  else
177  return State::Empty();
178  }
179 
180  bool Group::InitializeAnalysis(){
181  m_Log << LogVerbose;
182  m_Log << "Initializing Group for analysis...";
183  m_Log << LogEnd;
184 
185  m_GroupStatePtr = &InitializeParentState();
186  m_GroupStatePtr->AddFrames(m_Frames);
187 
188  if(!ResolveUnknowns()){
189  m_Log << LogWarning;
190  m_Log << "Unable to resolve unknowns associated with ";
191  m_Log << "Frames in this Group with available Jigsaws";
192  m_Log << LogEnd;
193  return SetBody(false);
194  }
195 
196  m_Log << LogVerbose;
197  m_Log << "...Done initializing group for analysis" << LogEnd;
198  SetBody(true);
199  return SetMind(true);
200  }
201 
202  bool Group::ResolveUnknowns(){
203  m_JigsawsToUse += m_Jigsaws;
204  m_Jigsaws.Clear();
205 
206  int Njigsaw = m_JigsawsToUse.GetN();
207  for(int i = 0; i < Njigsaw; i++){
208  if(m_JigsawsToUse[i].GetNChildren() == 1){
209  m_JigsawsToUse[i].RemoveFrames(m_Frames);
210  int N = GetNFrames();
211  for(int j = 0; j < N; j++)
212  m_JigsawsToUse[i].AddChildFrame(m_Frames[j]);
213  }
214  }
215 
216  m_States.Clear();
217  m_StatesToResolve.Clear();
218  m_States.Add(*m_GroupStatePtr);
219  m_StatesToResolve.Add(*m_GroupStatePtr);
220 
221  while(m_StatesToResolve.GetN() > 0){
222  State& state = m_StatesToResolve.Get(0);
223  if(!ResolveState(state)){
224  if(state.GetNFrames() != 1){
225  m_Log << LogWarning;
226  m_Log << "Cannot find Jigsaw to Resolve State for frames:";
227  m_Log << std::endl << " " << Log(state.GetListFrames());
228  m_Log << LogEnd;
229  return false;
230  }
231  m_StatesToResolve.Remove(state);
232  }
233  }
234  return true;
235  }
236 
237  bool Group::ResolveState(const State& state){
238  Jigsaw* jigsawSolutionPtr = nullptr;
239 
240  int N = m_JigsawsToUse.GetN();
241  for(int i = 0; i < N; i++){
242  Jigsaw* jigsawPtr = &m_JigsawsToUse[i];
243  if(jigsawPtr->CanResolve(state)){
244  if(!jigsawSolutionPtr){
245  jigsawSolutionPtr = jigsawPtr;
246  continue;
247  }
248  if(jigsawPtr->GetNChildren() <= jigsawSolutionPtr->GetNChildren())
249  if((jigsawPtr->GetNChildren() < jigsawSolutionPtr->GetNChildren()) ||
250  (jigsawPtr->GetNDependancyStates() < jigsawSolutionPtr->GetNDependancyStates()))
251  jigsawSolutionPtr = jigsawPtr;
252  }
253  }
254  if(!jigsawSolutionPtr)
255  return false;
256 
257  m_Log << LogVerbose;
258  m_Log << "Found Jigsaw to resolve State:" << std::endl;
259  m_Log << " Frames:" << std::endl << " ";
260  m_Log << Log(state.GetListFrames()) << std::endl;
261  m_Log << " Jigsaw:" << Log(jigsawSolutionPtr);
262  m_Log << LogEnd;
263 
264  if(!InitializeJigsaw(*jigsawSolutionPtr))
265  return false;
266 
267  m_JigsawsToUse.Remove(*jigsawSolutionPtr);
268  return true;
269  }
270 
271  bool Group::InitializeJigsaw(Jigsaw& jigsaw){
272  State& state = m_StatesToResolve[0];
273  jigsaw.SetParentState(state);
274 
275  if(!jigsaw.InitializeTree()){
276  m_Log << LogWarning;
277  m_Log << "Unable to initialize Jigsaw:";
278  m_Log << Log(jigsaw) << LogEnd;
279  return false;
280  }
281 
282  m_States += jigsaw.GetChildStates();
283  m_StatesToResolve -= state;
284  m_StatesToResolve += jigsaw.GetChildStates();
285  m_Jigsaws += jigsaw;
286 
287  return true;
288  }
289 
290  int Group::GetNChildStates() const {
291  return m_States.GetN();
292  }
293 
294  State& Group::GetChildState(int i) const {
295  if(i < 0 || i > GetNChildStates()-1)
296  return State::Empty();
297  else
298  return m_States[i];
299  }
300 
301  State& Group::GetChildState(const RestFrame& frame) const {
302  if(!frame) return State::Empty();
303  int Ns = GetNChildStates();
304  for(int i = Ns-1; i >= 0; i--)
305  if(m_States[i].IsFrame(frame))
306  return m_States[i];
307  SetMind(false);
308  return State::Empty();
309  }
310 
311  StateList Group::GetChildStates(const RestFrameList& frames) const {
312  // Find States that correspond to these frames, giving
313  // preference to States that include more frames (less dependancies)
314  StateList states;
315  int Ns = m_States.GetN();
316  for(int i = 0; i < Ns; i++){
317  RestFrameList iframes = m_States[i].GetListFrames();
318  if(frames.Contains(iframes)){
319  int Nsol = states.GetN();
320  bool isnew = true;
321  for(int j = 0; j < Nsol; j++){
322  // if new copy of existing frame list appears, discard old
323  RestFrameList jframes = states[j].GetListFrames();
324  if(iframes.Contains(jframes)){
325  states.Remove(states[j]);
326  break;
327  }
328  // if superset of existing frame list appears, discard new
329  if(jframes.Contains(iframes)){
330  isnew = false;
331  break;
332  }
333  }
334  if(isnew) states.Add(m_States[i]);
335  }
336  }
337 
338  RestFrameList match_frames;
339  Ns = states.GetN();
340  for(int i = 0; i < Ns; i++)
341  match_frames += states[i].GetListFrames();
342 
343  if(!(frames == match_frames)){
344  m_Log << LogWarning;
345  m_Log << "Unable to find States corresponding to Frames: " << std::endl;
346  m_Log << Log(frames) << LogEnd;
347  SetMind(false);
348  return StateList();
349  }
350  return states;
351  }
352 
353  RestFrame const& Group::GetLabFrame() const {
354  if(m_Frames.GetN() < 1)
355  return RestFrame::Empty();
356  else
357  return m_Frames[0].GetLabFrame();
358  }
359 
360 }
bool IsEmpty() const
Checks whether this is default (empty) instance of class.
Definition: RFBase.cc:84
virtual void Clear()
Clears Group of all connections to other objects.
Definition: Group.cc:62