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 
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 
93  return m_Type == kInvisibleJigsaw;
94  }
95 
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";
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 
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 
202  ConstRestFrameList frames;
203  for(int i = 0; i < m_Nchild; i++)
204  frames += GetChildFrames(i);
205  return frames;
206  }
207 
209  if(i < 0 || i >= m_Nchild)
210  return RestFrame::EmptyList();
211  return m_ChildFrames[i];
212  }
213 
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  FillJigsawDependancies(jigsaws);
338  jigsaws -= *this;
339  m_DependancyJigsaws.Add(jigsaws);
340 
341  return true;
342  }
343 
344  void Jigsaw::AddChildFrame(const RestFrame& frame, int i){
345  if(!frame) return;
346  if(!frame.IsRecoFrame()) return;
347  if(i < 0 || i >= m_Nchild) return;
348 
349  SetBody(false);
350 
351  m_ChildFrames[i] += frame;
352  }
353 
354  void Jigsaw::AddDependancyFrame(const RestFrame& frame, int i){
355  if(!frame) return;
356  if(!frame.IsRecoFrame()) return;
357  if(i < 0 || i >= m_Ndeps) return;
358 
359  SetBody(false);
360 
361  m_DependancyFrames[i] += frame;
362  }
363 
364  void Jigsaw::RemoveFrame(const RestFrame& frame){
365  if(!frame) return;
366 
367  SetBody(false);
368 
369  for(int i = 0; i < m_Nchild; i++)
370  m_ChildFrames[i].Remove(frame);
371 
372  for(int i = 0; i < m_Ndeps; i++)
373  m_DependancyFrames[i].Remove(frame);
374  }
375 
377  int N = frames.GetN();
378  for(int i = 0; i < N; i++)
379  RemoveFrame(frames[i]);
380  }
381 
382  bool Jigsaw::IsSoundBody() const {
383  if(RFBase::IsSoundBody())
384  return true;
385 
386  for(int i = 0; i < m_Nchild-1; i++){
387  for(int j = i+1; j < m_Nchild; j++){
388  if(m_ChildFrames[i].Intersection(m_ChildFrames[j]).GetN() > 0){
389  m_Log << LogWarning;
390  m_Log << "Child frames are repeated between ";
391  m_Log << "more than one output index: ";
392  m_Log << Log(m_ChildFrames[i].Intersection(m_ChildFrames[j]));
393  m_Log << LogEnd;
394  return SetBody(false);
395  }
396  }
397  }
398  for(int i = 0; i < m_Nchild; i++){
399  if(m_ChildFrames[i].GetN() == 0){
400  m_Log << LogWarning;
401  m_Log << "No child frames at index ";
402  m_Log << i << LogEnd;
403  return SetBody(false);
404  }
405  }
406 
407  return SetBody(true);
408  }
409 
410  bool Jigsaw::DependsOnJigsaw(const Jigsaw& jigsaw) const {
411  return m_DependancyJigsaws.Contains(jigsaw);
412  }
413 
414  void Jigsaw::FillJigsawDependancies(JigsawList& jigsaws) const {
415  if(jigsaws.Contains(*this)) return;
416  jigsaws.Add((Jigsaw&)(*m_This));
417 
418  if(m_ParentStatePtr)
419  m_ParentStatePtr->GetParentJigsaw().FillJigsawDependancies(jigsaws);
420 
421  for(int i = 0; i < m_Ndeps; i++){
422  int M = m_DependancyStates[i].GetN();
423  for(int j = 0; j < M; j++){
424  Jigsaw& jigsaw = m_DependancyStates[i][j].GetParentJigsaw();
425  jigsaw.FillJigsawDependancies(jigsaws);
426  }
427  }
428  }
429 
430  void Jigsaw::FillGroupJigsawDependancies(JigsawList& jigsaws) const {
431  if(jigsaws.Contains(*this)) return;
432  jigsaws.Add((Jigsaw&)(*m_This));
433 
434  if(m_ParentStatePtr)
435  m_ParentStatePtr->GetParentJigsaw().FillGroupJigsawDependancies(jigsaws);
436  }
437 
438  void Jigsaw::FillGroupJigsawDependants(JigsawList& jigsaws) const {
439  if(jigsaws.Contains(*this)) return;
440  jigsaws.Add((Jigsaw&)(*m_This));
441 
442  for(int i = 0; i < m_Nchild; i++)
443  GetChildState(i).GetChildJigsaw().FillGroupJigsawDependants(jigsaws);
444  }
445 
446 }
RestFrames::Jigsaw::PrintString
virtual std::string PrintString(LogType type) const
Prints string of information associated with Jigsaw.
Definition: Jigsaw.cc:100
InvisibleJigsaw.hh
RestFrames::InvisibleJigsaw::Empty
static InvisibleJigsaw & Empty()
Returns empty InvisibleJigsaw.
Definition: InvisibleJigsaw.cc:62
RestFrames::State::EmptyList
static StateList const & EmptyList()
Returns empty StateList.
Definition: State.cc:72
RestFrames::Jigsaw::SetGroup
virtual void SetGroup(Group &group=Group::Empty())
Sets group (Group) to current jigsaw.
Definition: Jigsaw.cc:109
RestFrames::Jigsaw::GetGroup
virtual Group & GetGroup() const
Returns group (Group) associated with this jigsaw.
Definition: Jigsaw.cc:130
RestFrames::Jigsaw::Jigsaw
Jigsaw()
Empty constructor.
Definition: Jigsaw.cc:61
RestFrames::Group::Empty
static Group & Empty()
Returns empty Group.
Definition: Group.cc:58
RestFrames::Jigsaw::GetDependancyFrames
virtual ConstRestFrameList const & GetDependancyFrames(int i) const
Returns list of frames in which this jigsaw depends on.
Definition: Jigsaw.cc:214
RestFrames::RestFrame
abstract base class for all Frame objects
Definition: RestFrame.hh:45
RestFrames::Group
abstract base class for all Group objects
Definition: Group.hh:46
RestFrames::RFBase::IsEmpty
bool IsEmpty() const
Checks whether this is default (empty) instance of class.
Definition: RFBase.cc:84
RestFrames::RFList< const RestFrame >
RestFrames::RFBase
Base class for all RestFrame package objects.
Definition: RFBase.hh:53
RestFrames::Jigsaw::GetChildFrames
virtual ConstRestFrameList const & GetChildFrames(int i) const
Returns list of child frames associated with this jigsaw.
Definition: Jigsaw.cc:208
RestFrames::Jigsaw::Clear
virtual void Clear()
Clears Jigsaw of all connections to other objects.
Definition: Jigsaw.cc:74
Jigsaw.hh
RestFrames::Group::RemoveJigsaw
void RemoveJigsaw(Jigsaw &jigsaw)
Removes a jigsaw (Jigsaw) from this group.
Definition: Group.cc:134
RestFrames::Jigsaw::IsInvisibleJigsaw
bool IsInvisibleJigsaw() const
Is invisible jigsaw? (true/false)
Definition: Jigsaw.cc:92
RestFrames::LogType
LogType
Type of Log Message.
Definition: RFLog.hh:45
RestFrames::RFBase::GetName
std::string GetName() const
Returns object name.
Definition: RFBase.cc:104
RestFrames::RFLog::SetSource
void SetSource(const std::string &source)
Sets name of instance that is associated with.
Definition: RFLog.cc:182
RestFrames::State::SetChildJigsaw
virtual void SetChildJigsaw(Jigsaw &jigsaw=Jigsaw::Empty())
Sets the child jigsaw (Jigsaw) to jigsaw
Definition: State.cc:129
State.hh
RestFrames::State::Empty
static State & Empty()
Returns empty state.
Definition: State.cc:68
RestFrames::Jigsaw::Empty
static Jigsaw & Empty()
Returns empty Jigsaw.
Definition: Jigsaw.cc:70
RestFrames::Jigsaw::GetParentFrames
virtual ConstRestFrameList GetParentFrames() const
Returns list of parent frames associated with this jigsaw.
Definition: Jigsaw.cc:201
RestFrames::Jigsaw::RemoveFrame
void RemoveFrame(const RestFrame &frame)
Removes a frame from this jigsaw.
Definition: Jigsaw.cc:364
RestFrames::RestFrame::EmptyList
static ConstRestFrameList const & EmptyList()
Returns empty RestFrameList.
Definition: RestFrame.cc:66
RestFrames::Jigsaw
abstract base class for all Jigsaw objects
Definition: Jigsaw.hh:44
RestFrames::Jigsaw::RemoveFrames
void RemoveFrames(const ConstRestFrameList &frames)
Removes a list of frames from this jigsaw.
Definition: Jigsaw.cc:376
RestFrames::Jigsaw::IsCombinatoricJigsaw
bool IsCombinatoricJigsaw() const
Is combinatoric jigsaw? (true/false)
Definition: Jigsaw.cc:96
RestFrames::State::GetChildJigsaw
virtual Jigsaw & GetChildJigsaw() const
Returns the child of this jigsaw (Jigsaw)
Definition: State.cc:145
RestFrames::RFBase::PrintString
virtual std::string PrintString(LogType type) const
String of information associated with object.
Definition: RFBase.cc:146
LabRecoFrame.hh
RestFrames::State::GetParentJigsaw
virtual Jigsaw & GetParentJigsaw() const
Returns the parent of this jigsaw (Jigsaw)
Definition: State.cc:138