LOGO

RestFrames  v1.0.1
RestFrames HEP Event Analysis Software Library
Jigsaw.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/Jigsaw.hh"
33 #include "RestFrames/State.hh"
34 
35 namespace RestFrames {
36 
38  // Jigsaw class methods
40  int Jigsaw::m_class_key = 0;
41 
42  Jigsaw::Jigsaw(const std::string& sname,
43  const std::string& stitle,
44  int Nchild, int Ndependancy)
45  : RFBase(sname, stitle, Jigsaw::m_class_key++),
46  m_Nchild(Nchild), m_Ndeps(Ndependancy)
47  {
48  m_Log.SetSource("Jigsaw "+GetName());
49  m_GroupPtr = nullptr;
50  m_ParentStatePtr = nullptr;
51  for(int i = 0; i < m_Nchild; i++){
52  m_ChildFrames.push_back(ConstRestFrameList());
53  }
54 
55  for(int i = 0; i < m_Ndeps; i++){
56  m_DependancyFrames.push_back(ConstRestFrameList());
57  m_DependancyStates.push_back(StateList());
58  }
59  }
60 
61  Jigsaw::Jigsaw() : RFBase(), m_Nchild(0), m_Ndeps(0){
62  m_Type = kVanillaJigsaw;
63  m_Log.SetSource("Jigsaw "+GetName());
64  }
65 
66  Jigsaw::~Jigsaw(){
67  Clear();
68  }
69 
70  Jigsaw& Jigsaw::Empty(){
71  return InvisibleJigsaw::Empty();
72  }
73 
74  void Jigsaw::Clear(){
75  SetBody(false);
76  SetGroup();
77  SetParentState();
78 
79  for(int i = 0; i < m_Nchild; i++){
80  m_ChildFrames[i].Clear();
81  m_ChildStates[i].Clear();
82  }
83 
84  for(int i = 0; i < m_Ndeps; i++){
85  m_DependancyStates[i].Clear();
86  m_DependancyFrames[i].Clear();
87  }
88 
89  m_DependancyJigsaws.Clear();
90  }
91 
92  bool Jigsaw::IsInvisibleJigsaw() const {
93  return m_Type == kInvisibleJigsaw;
94  }
95 
96  bool Jigsaw::IsCombinatoricJigsaw() const {
97  return m_Type == kCombinatoricJigsaw;
98  }
99 
100  std::string Jigsaw::PrintString(LogType type) const {
101  std::string output = RFBase::PrintString(type);
102  if(IsInvisibleJigsaw())
103  output += " Type: Invisible \n";
104  if(IsCombinatoricJigsaw())
105  output += " Type: Combinatoric \n";
106  return output;
107  }
108 
109  void Jigsaw::SetGroup(Group& group){
110  if(IsEmpty()) return;
111 
112  SetBody(false);
113  SetParentState();
114 
115  if(m_GroupPtr){
116  if(*m_GroupPtr == group){
117  return;
118  } else {
119  Group* groupPtr = m_GroupPtr;
120  m_GroupPtr = nullptr;
121  groupPtr->RemoveJigsaw(*this);
122  }
123  }
124  if(!group)
125  m_GroupPtr = nullptr;
126  else
127  m_GroupPtr = &group;
128  }
129 
130  Group& Jigsaw::GetGroup() const {
131  if(m_GroupPtr)
132  return *m_GroupPtr;
133  else
134  return Group::Empty();
135  }
136 
137  bool Jigsaw::CanResolve(const ConstRestFrameList& frames) const {
138  if(!IsSoundBody())
139  return SetBody(false);
140 
141  return GetParentFrames() == frames;
142  }
143 
144  bool Jigsaw::CanResolve(const State& state) const {
145  if(!state) return false;
146  return CanResolve(state.GetListFrames());
147  }
148 
149  void Jigsaw::SetParentState(){
150  SetParentState(State::Empty());
151  }
152 
153  void Jigsaw::SetParentState(State& state){
154  if(IsEmpty()) return;
155 
156  SetBody(false);
157  if(m_ParentStatePtr){
158  if(*m_ParentStatePtr == state)
159  return;
160  else {
161  m_ParentStatePtr = nullptr;
162  }
163  }
164  if(!state)
165  m_ParentStatePtr = nullptr;
166  else {
167  m_ParentStatePtr = &state;
168  state.SetChildJigsaw(*this);
169  }
170  }
171 
172  State const& Jigsaw::GetParentState() const {
173  if(m_ParentStatePtr)
174  return *m_ParentStatePtr;
175  else
176  return State::Empty();
177  }
178 
179  int Jigsaw::GetNChildren() const {
180  return m_Nchild;
181  }
182 
183  int Jigsaw::GetNDependancyStates() const {
184  return m_Ndeps;
185  }
186 
187  State& Jigsaw::GetChildState(int i) const {
188  return m_ChildStates[i];
189  }
190 
191  StateList const& Jigsaw::GetChildStates() const {
192  return m_ChildStates;
193  }
194 
195  StateList const& Jigsaw::GetDependancyStates(int i) const {
196  if(i < 0 || i >= m_Ndeps)
197  return State::EmptyList();
198  return m_DependancyStates[i];
199  }
200 
201  ConstRestFrameList Jigsaw::GetParentFrames() const {
202  ConstRestFrameList frames;
203  for(int i = 0; i < m_Nchild; i++)
204  frames += GetChildFrames(i);
205  return frames;
206  }
207 
208  ConstRestFrameList const& Jigsaw::GetChildFrames(int i) const {
209  if(i < 0 || i >= m_Nchild)
210  return RestFrame::EmptyList();
211  return m_ChildFrames[i];
212  }
213 
214  ConstRestFrameList const& Jigsaw::GetDependancyFrames(int i) const {
215  if(i < 0 || i >= m_Ndeps)
216  return RestFrame::EmptyList();
217  return m_DependancyFrames[i];
218  }
219 
220  bool Jigsaw::InitializeTree(){
221  if(!IsSoundBody())
222  return SetMind(false);
223 
224  if(!GetParentState()){
225  m_Log << LogWarning;
226  m_Log << "Unable to initialize Jigsaw. ";
227  m_Log << "No parent State set." << LogEnd;
228  return SetMind(false);
229  }
230 
231  if(!CanResolve(GetParentState())){
232  m_Log << LogWarning;
233  m_Log << "Unable to resolve input parent State. ";
234  m_Log << " Frames (capable): " << Log(GetParentFrames()) << std::endl;
235  m_Log << " Frames (requested): " << Log(GetParentState().GetListFrames());
236  m_Log << LogEnd;
237  return SetMind(false);
238  }
239 
240  for(int i = 0; i < m_Nchild; i++){
241  m_ChildStates[i].Clear();
242  m_ChildStates[i].SetParentJigsaw(*this);
243  m_ChildStates[i].AddFrames(GetChildFrames(i));
244  }
245 
246  return SetMind(true);
247  }
248 
249  bool Jigsaw::InitializeAnalysis(){
250  if(!IsSoundMind())
251  return SetMind(false);
252 
253  if(!GetGroup())
254  return SetMind(false);
255 
256  // get list of states and groups from lab frame
257  const LabRecoFrame& lab_frame =
258  static_cast<const LabRecoFrame&>(GetGroup().GetLabFrame());
259  StateList states = lab_frame.GetTreeStates();
260  GroupList groups = lab_frame.GetListGroups();
261 
262  int Ngroup = groups.GetN();
263  std::vector<ConstRestFrameList> group_frames;
264  for(int i = 0; i < Ngroup; i++)
265  group_frames.push_back(ConstRestFrameList());
266 
267  for(int d = 0; d < m_Ndeps; d++){
268  m_DependancyStates[d].Clear();
269 
270  for(int i = 0; i < Ngroup; i++)
271  group_frames[i].Clear();
272 
273  int Nf = m_DependancyFrames[d].GetN();
274  for(int f = 0; f < Nf; f++){
275  const RestFrame& frame = m_DependancyFrames[d][f];
276 
277  bool no_group = true;
278  for(int g = 0; g < Ngroup; g++){
279  if(groups[g].ContainsFrame(frame)){
280  group_frames[g].Add(frame);
281  no_group = false;
282  break;
283  }
284  }
285 
286  if(no_group){
287  State& state = states.GetFrame(frame);
288  if(!state){
289  m_Log << LogWarning;
290  m_Log << "Cannot find State corresponding to frame: " << std::endl;
291  m_Log << Log(frame) << " " << Log(states) << LogEnd;
292  return SetMind(false);
293  }
294  m_Log << LogVerbose;
295  m_Log << "Adding dependancy State for index " << d;
296  m_Log << " corresponding to frame:";
297  m_Log << Log(frame) << LogEnd;
298  m_DependancyStates[d] += state;
299  }
300  }
301  for(int g = 0; g < Ngroup; g++){
302  if(group_frames[g].GetN() == 0) continue;
303  StateList group_states = groups[g].GetChildStates(group_frames[g]);
304  if(group_states.GetN() == 0){
305  m_Log << "Cannot find States in Group:" << std::endl;
306  m_Log << " Frames:" << std::endl << " ";
307  m_Log << Log(group_frames[g]) << std::endl;
308  m_Log << " Group:" << std::endl;
309  m_Log << Log(groups.Get(g)) << LogEnd;
310  return SetMind(false);
311  }
312  m_Log << LogVerbose;
313  m_Log << "Sucessfully found dependancy States for index " << d << std::endl;
314  int Ns = group_states.GetN();
315  m_Log << " Frames:" << std::endl << " ";
316  m_Log << Log(group_frames[g]) << std::endl;
317  m_Log << " States:" << std::endl;
318  for(int s = 0; s < Ns; s++){
319  m_Log << " state " << s << ": ";
320  m_Log << Log(group_states[s].GetListFrames()) << std::endl;
321  }
322  m_Log << LogEnd;
323  m_DependancyStates[d] += group_states;
324  }
325  }
326 
327  return SetMind(true);
328  }
329 
330  bool Jigsaw::InitializeDependancyJigsaws(){
331  if(!IsSoundMind())
332  return false;
333 
334  m_DependancyJigsaws.Clear();
335 
336  JigsawList jigsaws;
337  FillStateJigsawDependancies(jigsaws);
338  jigsaws -= *this;
339  m_DependancyJigsaws.Add(jigsaws);
340 
341  jigsaws.Clear();
342  FillGroupJigsawDependancies(jigsaws);
343  jigsaws -= *this;
344  m_DependancyJigsaws += jigsaws;
345 
346  return true;
347  }
348 
349  void Jigsaw::AddChildFrame(const RestFrame& frame, int i){
350  if(!frame) return;
351  if(!frame.IsRecoFrame()) return;
352  if(i < 0 || i >= m_Nchild) return;
353 
354  SetBody(false);
355 
356  m_ChildFrames[i] += frame;
357  }
358 
359  void Jigsaw::AddDependancyFrame(const RestFrame& frame, int i){
360  if(!frame) return;
361  if(!frame.IsRecoFrame()) return;
362  if(i < 0 || i >= m_Ndeps) return;
363 
364  SetBody(false);
365 
366  m_DependancyFrames[i] += frame;
367  }
368 
369  void Jigsaw::RemoveFrame(const RestFrame& frame){
370  if(!frame) return;
371 
372  SetBody(false);
373 
374  for(int i = 0; i < m_Nchild; i++)
375  m_ChildFrames[i].Remove(frame);
376 
377  for(int i = 0; i < m_Ndeps; i++)
378  m_DependancyFrames[i].Remove(frame);
379  }
380 
381  void Jigsaw::RemoveFrames(const ConstRestFrameList& frames){
382  int N = frames.GetN();
383  for(int i = 0; i < N; i++)
384  RemoveFrame(frames[i]);
385  }
386 
387  bool Jigsaw::IsSoundBody() const {
388  if(RFBase::IsSoundBody())
389  return true;
390 
391  for(int i = 0; i < m_Nchild-1; i++){
392  for(int j = i+1; j < m_Nchild; j++){
393  if(m_ChildFrames[i].Intersection(m_ChildFrames[j]).GetN() > 0){
394  m_Log << LogWarning;
395  m_Log << "Child frames are repeated between ";
396  m_Log << "more than one output index: ";
397  m_Log << Log(m_ChildFrames[i].Intersection(m_ChildFrames[j]));
398  m_Log << LogEnd;
399  return SetBody(false);
400  }
401  }
402  }
403  for(int i = 0; i < m_Nchild; i++){
404  if(m_ChildFrames[i].GetN() == 0){
405  m_Log << LogWarning;
406  m_Log << "No child frames at index ";
407  m_Log << i << LogEnd;
408  return SetBody(false);
409  }
410  }
411 
412  return SetBody(true);
413  }
414 
415  bool Jigsaw::DependsOnJigsaw(const Jigsaw& jigsaw) const {
416  return m_DependancyJigsaws.Contains(jigsaw);
417  }
418 
419  void Jigsaw::FillGroupJigsawDependancies(JigsawList& jigsaws) const {
420  if(jigsaws.Contains(*this)) return;
421  jigsaws.Add((Jigsaw&)(*m_This));
422  if(m_ParentStatePtr)
423  m_ParentStatePtr->GetParentJigsaw().FillGroupJigsawDependancies(jigsaws);
424  }
425 
426  void Jigsaw::FillStateJigsawDependancies(JigsawList& jigsaws) const {
427  if(jigsaws.Contains(*this)) return;
428  jigsaws.Add((Jigsaw&)(*m_This));
429 
430  for(int i = 0; i < m_Ndeps; i++){
431  int M = m_DependancyStates[i].GetN();
432  for(int j = 0; j < M; j++){
433  m_DependancyStates[i][j].GetParentJigsaw().
434  FillStateJigsawDependancies(jigsaws);
435  }
436  }
437  }
438 
439 }
virtual std::string PrintString(LogType type) const
String of information associated with object.
Definition: RFBase.cc:146
std::string GetName() const
Returns object name.
Definition: RFBase.cc:104
bool IsEmpty() const
Checks whether this is default (empty) instance of class.
Definition: RFBase.cc:84
abstract base class for all Group objects
Definition: Group.hh:46
virtual std::string PrintString(LogType type) const
String of information associated with Jigsaw.
Definition: Jigsaw.cc:100
virtual void Clear()
Clears Jigsaw of all connections to other objects.
Definition: Jigsaw.cc:74