LOGO

RestFrames  v1.0.0
RestFrames HEP Event Analysis Software Library
InvisibleJigsaw.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 
32 #include "RestFrames/RestFrame.hh"
33 
34 namespace RestFrames {
35 
37  // InvisibleJigsaw class
39 
40  InvisibleJigsaw::InvisibleJigsaw(const std::string& sname,
41  const std::string& stitle,
42  int Ninvisible, int Nvisible) :
43  Jigsaw(sname, stitle, Ninvisible, 2*Nvisible),
44  m_Ninv(Ninvisible), m_Nvis(Nvisible)
45  {
46  m_Type = kInvisibleJigsaw;
47  m_InvMassDependancy = false;
48  for(int i = 0; i < m_Ninv; i++){
49  m_ChildStates += GetNewChildState();
50  }
51  }
52 
53  InvisibleJigsaw::InvisibleJigsaw()
54  : Jigsaw(), m_Ninv(0), m_Nvis(0) {}
55 
56  InvisibleJigsaw::~InvisibleJigsaw() {}
57 
59  Jigsaw::Clear();
60  }
61 
62  InvisibleJigsaw& InvisibleJigsaw::Empty(){
63  return SetMassInvJigsaw::Empty();
64  }
65 
66  void InvisibleJigsaw::SetGroup(Group& group){
67  if(!group) return;
68  if(!group.IsInvisibleGroup()) return;
69  Jigsaw::SetGroup(group);
70  }
71 
72  InvisibleGroup& InvisibleJigsaw::GetGroup() const {
73  if(!Jigsaw::GetGroup())
74  return InvisibleGroup::Empty();
75  else
76  return static_cast<InvisibleGroup&>(Jigsaw::GetGroup());
77  }
78 
79  void InvisibleJigsaw::SetParentState(State& state){
80  if(!state) return;
81  if(!state.IsInvisibleState()) return;
82  Jigsaw::SetParentState(state);
83  }
84 
85  InvisibleState const& InvisibleJigsaw::GetParentState() const {
86  if(!Jigsaw::GetParentState())
87  return InvisibleState::Empty();
88  else
89  return static_cast<const InvisibleState&>(Jigsaw::GetParentState());
90  }
91 
92  InvisibleState& InvisibleJigsaw::GetChildState(int i) const{
93  if(!Jigsaw::GetChildState(i))
94  return InvisibleState::Empty();
95  else
96  return static_cast<InvisibleState&>(Jigsaw::GetChildState(i));
97  }
98 
99  void InvisibleJigsaw::AddVisibleFrame(const RestFrame& frame, int i){
100  if(!frame) return;
101  if(i < 0 || i >= m_Nvis) return;
102 
103  ConstRestFrameList frames = frame.GetListVisibleFrames();
104  int N = frames.GetN();
105  for(int f = 0; f < N; f++)
106  AddDependancyFrame(frames[f], i);
107  }
108 
109  void InvisibleJigsaw::AddVisibleFrames(const ConstRestFrameList& frames, int i){
110  int N = frames.GetN();
111  for(int f = 0; f < N; f++)
112  AddVisibleFrame(frames[f], i);
113  }
114 
115  void InvisibleJigsaw::AddMassFrame(const RestFrame& frame, int i){
116  if(!frame) return;
117  if(i < 0 || i >= m_Nvis) return;
118 
119  ConstRestFrameList frames = frame.GetListVisibleFrames();
120  int N = frames.GetN();
121  for(int f = 0; f < N; f++)
122  AddDependancyFrame(frames[f], m_Nvis + i);
123  }
124 
125  void InvisibleJigsaw::AddMassFrames(const ConstRestFrameList& frames, int i){
126  int N = frames.GetN();
127  for(int f = 0; f < N; f++)
128  AddMassFrame(frames[f], i);
129  }
130 
131  void InvisibleJigsaw::AddInvisibleFrame(const RestFrame& frame, int i){
132  if(!frame) return;
133  if(i < 0 || i >= m_Ninv) return;
134 
135  ConstRestFrameList frames = frame.GetListInvisibleFrames();
136  int N = frames.GetN();
137  for(int f = 0; f < N; f++)
138  AddChildFrame(frames[f], i);
139  }
140 
141  void InvisibleJigsaw::AddInvisibleFrames(const ConstRestFrameList& frames, int i){
142  int N = frames.GetN();
143  for(int f = 0; f < N; f++)
144  AddInvisibleFrame(frames[f], i);
145  }
146 
147  InvisibleState& InvisibleJigsaw::GetNewChildState(){
148  char strn[10];
149  sprintf(strn,"%d",GetNChildren());
150  std::string name = GetName()+"_"+std::string(strn);
151  InvisibleState* statePtr = new InvisibleState(name, name);
152  AddDependent(statePtr);
153  return *statePtr;
154  }
155 
156  double InvisibleJigsaw::GetMinimumMass() const {
157  if(!IsSoundMind()) return 0.;
158 
159  int N = GetNChildren();
160  double M = 0.;
161  for(int i = 0; i < N; i++)
162  M += std::max(0., GetChildState(i).GetMinimumMass());
163  return M;
164  }
165 
166  bool InvisibleJigsaw::InitializeAnalysis(){
167  if(!IsSoundMind())
168  return SetMind(false);
169 
170  for(int v = 0; v < m_Nvis; v++)
171  if(GetDependancyFrames(m_Nvis + v).GetN() == 0)
172  AddMassFrames(GetDependancyFrames(v), v);
173 
174  SetMind(true);
175  return Jigsaw::InitializeAnalysis();
176  }
177 
178  void InvisibleJigsaw::FillInvisibleMassJigsawDependancies(JigsawList& jigsaws) const {
179  if(m_InvMassDependancy){
180  for(int i = m_Nvis; i < 2*m_Nvis; i++){
181  int M = GetDependancyStates(i).GetN();
182  for(int j = 0; j < M; j++){
183  GetDependancyStates(i)[j].GetParentJigsaw().
184  FillGroupJigsawDependancies(jigsaws);
185  }
186  }
187  }
188  for(int i = 0; i < m_Ninv; i++)
189  static_cast<const InvisibleJigsaw&>(GetChildState(i).GetChildJigsaw()).
190  FillInvisibleMassJigsawDependancies(jigsaws);
191  }
192 
193  bool InvisibleJigsaw::InitializeDependancyJigsaws(){
194  if(!IsSoundMind()) return false;
195 
196  m_DependancyJigsaws.Clear();
197 
198  JigsawList jigsaws;
199  FillStateJigsawDependancies(jigsaws);
200  jigsaws -= *this;
201  m_DependancyJigsaws += jigsaws;
202 
203  jigsaws.Clear();
204  FillInvisibleMassJigsawDependancies(jigsaws);
205  jigsaws -= *this;
206  m_DependancyJigsaws += jigsaws;
207 
208  jigsaws.Clear();
209  FillGroupJigsawDependancies(jigsaws);
210  jigsaws -= *this;
211  m_DependancyJigsaws += jigsaws;
212 
213  return true;
214  }
215 
216  bool InvisibleJigsaw::InitializeJigsawExecutionList(JigsawList& exec_jigsaws){
217  if(!IsSoundMind()) return false;
218  if(exec_jigsaws.Contains(*this)) return true;
219 
220  // Add group dependancy jigsaws first
221  JigsawList group_jigsaws;
222  FillGroupJigsawDependancies(group_jigsaws);
223  group_jigsaws -= *this;
224 
225  int Ngroup = group_jigsaws.GetN();
226  for(int i = Ngroup-1; i >= 0; i--){
227  Jigsaw& jigsaw = group_jigsaws[i];
228  if(!exec_jigsaws.Contains(jigsaw))
229  if(!jigsaw.InitializeJigsawExecutionList(exec_jigsaws))
230  return SetMind(false);
231  m_DependancyJigsaws -= jigsaw;
232  }
233  // Satisfy dependancy jigsaws
234  while(m_DependancyJigsaws.GetN() > 0){
235  Jigsaw& jigsaw = m_DependancyJigsaws[m_DependancyJigsaws.GetN()-1];
236  if(exec_jigsaws.Contains(jigsaw)){
237  m_DependancyJigsaws -= jigsaw;
238  continue;
239  }
240  if(jigsaw.DependsOnJigsaw(*this))
241  m_DependancyJigsaws -= jigsaw;
242  if(!jigsaw.InitializeJigsawExecutionList(exec_jigsaws))
243  return SetMind(false);
244  m_DependancyJigsaws -= jigsaw;
245  }
246 
247  exec_jigsaws += *this;
248  return true;
249  }
250 
251  bool InvisibleJigsaw::IsSoundBody() const {
252  if(RFBase::IsSoundBody())
253  return true;
254 
255  if(!Jigsaw::IsSoundBody())
256  return SetBody(false);
257 
258  for(int i = 0; i < m_Nvis; i++){
259  if(GetDependancyFrames(i).GetN() <= 0){
260  m_Log << LogWarning;
261  m_Log << "Empty collection of visible frames: " << i;
262  m_Log << LogEnd;
263  return SetBody(false);
264  }
265  }
266 
267  return SetBody(true);
268  }
269 
270 }
std::string GetName() const
Returns object name.
Definition: RFBase.cc:104
virtual void Clear()
Clears Jigsaw of all connections to other objects.
void AddDependent(RFBase *dep)
pointer to RFBase object owned by this one
Definition: RFBase.cc:88
virtual void Clear()
Clears Jigsaw of all connections to other objects.
Definition: Jigsaw.cc:74