Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_test3.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file my_test3.cc
1 #include <iostream>
2 
3 #include <GTL/graph.h>
4 #include <GTL/bellman_ford.h>
5 #include <GTL/edge_map.h>
6 #include <GTL/node_map.h>
7 #include <GTL/dfs.h>
8 #include <GTL/bfs.h>
9 #include <memory>
10 #include <list>
11 #include <vector>
12 #include "helper.h"
13 
14 class vertex {
15 
16 public:
17  vertex() {};
18  vertex(int myId) {id=myId;}
19  virtual ~vertex() {cout<<" Vertex Detructor ..."<<endl;}
20  int id;
21 };
22 
23 class parton {
24 
25 public:
26 
27  parton() {};
28  parton (double mpt, double meta, double mphi, double me, bool mfinal) {pt=mpt;eta=meta;phi=mphi;e=me;final=mfinal;}
29  virtual ~parton() {cout<<" Parton Destructor ..."<<endl;}
30 
31  bool isFinal() {return final;}
32 
33  double pt, eta, phi, e;
34  bool final;
35 };
36 
37 class shower2 : public graph {
38 
39 public:
40 
41  shower2() : graph() {cout<<"Shower2 Constrcutor ..."<<endl;}
42  virtual ~shower2() {cout<<"Shower2 Detrcutor ..."<<endl;}
43 
44  node new_vertex(shared_ptr<vertex> v) {node n=graph::new_node();XX[n]=v; return n;}
45  //void new_parton(node s, node t, shared_ptr<parton> p) {edge e=graph::new_edge(s,t);PP[e]=p;}//std::move(p);} //; return e;}
46  void new_parton(node s, node t, unique_ptr<parton> p) {edge e=graph::new_edge(s,t);PP[e]=std::move(p);}
47 
48  //unique_ptr<parton> GetParton(edge e) {return unique_ptr<parton> (PP[e]);}
49 
50  //int GetNodeValue(node n) const {return XX[n];}
51  void save_node_info_handler (ostream *o, node n) const { *o<<"MyID "<<XX[n]->id<<endl;}
52  void save_edge_info_handler (ostream *o, edge n) const { *o<<"pT "<<PP[n]->pt<<endl;}
53 
55  cout<<"Load edge ... "<<e<<endl;
56  struct GML_pair* tmp = read;
57 
58  while (tmp) {
59 
60  //printf ("*KEY* : %s \n", tmp->key);
61  if (((string) (tmp->key)).find("pT")<1)
62  {
63 
64  PP[e]=make_shared<parton>(tmp->value.floating,0,0,tmp->value.floating,false);
65  }
66 
67  tmp = tmp->next;
68  }
69  }
70 
71  //maybe smarter to store node id and edge source target in vertex/parton vector and recreate graph from scratch ....
73  cout<<"Load node ... "<<n<<endl;
74  //cout<<read->key<<endl;//" "<<read->value.floating<<endl;
75  struct GML_pair* tmp = read;
76  int i;
77  int level=2;
78  int nC=0;
79 
80  while (tmp) {
81 
82  //for (i = 0; i < level; i++) {
83  // printf (" ");
84  //}
85 
86  //printf ("*KEY* : %s \n", tmp->key);
87 
88  //printf ("%s", tmp->key.id);
89 
90  //if (((string) (tmp->key)).find("text")) cout<<"text .."<<endl;
91 
92  /*
93  switch (tmp->kind) {
94  case GML_INT:
95  printf (" *VALUE* (long) : %ld \n", tmp->value.integer);
96  break;
97 
98  case GML_DOUBLE:
99  printf (" *VALUE* (double) : %f \n", tmp->value.floating);
100  break;
101 
102  case GML_STRING:
103  printf (" *VALUE* (string) : %s \n", tmp->value.str);
104  break;
105 
106  case GML_LIST:
107  printf (" *VALUE* (list) : \n");
108  GML_print_list (tmp->value.list, level+1);
109  break;
110 
111  default:
112  break;
113  }
114  */
115 
116  //cout<<((string) (tmp->key)).find("text")<<endl;
117  if (((string) (tmp->key)).find("MyID")<1)
118  {
119  //cout<<n<<" "<<nC<<" "<<i<<endl;
120  //cout<<n<<" "<<tmp->value.integer<<endl;
121  XX[n]=make_shared<vertex>(tmp->value.integer);
122  }
123 
124  //nC++;
125  //cout<<nC<<endl;
126  tmp = tmp->next;
127  }
128  }
129 
130  double GetEdgeValue(edge n) const {return PP[n]->pt;}
131  int GetNodeValue(node n) const {return XX[n]->id;}
132  bool GetFinal(edge e) const {return PP[e]->final;}
133  //void pre_new_edge_handler(node s,node t) {};
134  //void post_new_edge_handler(edge e) {};
136  edge_iterator git3, gend3;
137  for (git3 = edges_begin(), gend3 = edges_end(); git3 != gend3; ++git3)
138  {
139  //cout<<*git3<<" "<<mS->GetEdgeValue(*git3)<<endl;
140  PP[*git3]=nullptr;
141  }
142  node_iterator git4, gend4;
143  for (git4 = nodes_begin(), gend4 = nodes_end(); git4 != gend4; ++git4)
144  {
145  XX[*git4]=nullptr;
146  }
147  }
148 
149  // vector<shared_ptr<vertex>> GetVertices() {};
150  /*
151  vector<shared_ptr<parton>> GetPartons() {
152  vector<shared_ptr<parton>> myv; list<edge> le=all_edges();
153  //int n=le.size();
154  //for (int i=0;i<n;i++)
155  int n=0;
156 
157  for (list<edge>::iterator it=le.begin(); it!=le.end(); ++it)
158  {
159  if (PP[*it]->isFinal())
160  myv.push_back(PP[*it]);
161  //cout<<PP[*it]->pt<<endl;
162  }
163  return myv;
164  }
165  */
166 
167  vector<weak_ptr<parton>> GetPartons() {
168  vector<weak_ptr<parton>> myv; list<edge> le=all_edges();
169  //int n=le.size();
170  //for (int i=0;i<n;i++)
171  int n=0;
172 
173  for (list<edge>::iterator it=le.begin(); it!=le.end(); ++it)
174  {
175  if (PP[*it]->isFinal())
176  myv.push_back(PP[*it]);
177  //cout<<PP[*it]->pt<<endl;
178  }
179  return myv;
180  }
181 
182  private:
183 
185  edge_map<shared_ptr<parton>> PP; //unique_ptr not working !???
186 
187 };
188 
189 int main (int argc, char* argv[])
190 {
191  auto mS=make_shared<shower2>();
192  mS->make_directed();
193 
194  node nnn11=mS->new_vertex(make_shared<vertex>(0)); //if c++14 or c++11 and helper.h ...
195  //node nnn11=mS->new_vertex(unique_ptr<vertex>(new vertex(0)));
196  node nnn22=mS->new_vertex(make_shared<vertex>(1));
197  node nnn33=mS->new_vertex(make_shared<vertex>(1));
198  node nnn44=mS->new_vertex(make_shared<vertex>(2));
199  node nnn55=mS->new_vertex(make_shared<vertex>(2));
200  node nnn66=mS->new_vertex(make_shared<vertex>(3));
201  node nnn77=mS->new_vertex(make_shared<vertex>(3));
202  node nnn88=mS->new_vertex(make_shared<vertex>(4));
203 
204  /*
205  node nnn11=mS->new_vertex(make_unique<vertex>(1)); //if c++14 or c++11 and helper.h ...
206  //node nnn11=mS->new_vertex(unique_ptr<vertex>(new vertex(0)));
207  node nnn22=mS->new_vertex(unique_ptr<vertex>(new vertex(1)));
208  node nnn33=mS->new_vertex(unique_ptr<vertex>(new vertex(1)));
209  node nnn44=mS->new_vertex(unique_ptr<vertex>(new vertex(2)));
210  node nnn55=mS->new_vertex(unique_ptr<vertex>(new vertex(2)));
211  node nnn66=mS->new_vertex(unique_ptr<vertex>(new vertex(3)));
212  node nnn77=mS->new_vertex(unique_ptr<vertex>(new vertex(3)));
213  node nnn88=mS->new_vertex(unique_ptr<vertex>(new vertex(4)));
214  */
215 
216  mS->new_parton(nnn11,nnn22,unique_ptr<parton>(new parton(100,0,0,100,false)));
217  mS->new_parton(nnn11,nnn33,unique_ptr<parton>(new parton(200,0,0,200,false)));
218  mS->new_parton(nnn33,nnn44,unique_ptr<parton>(new parton(50,0,0,50,true)));
219  mS->new_parton(nnn33,nnn55,unique_ptr<parton>(new parton(150,0,0,150,false)));
220  mS->new_parton(nnn55,nnn66,unique_ptr<parton>(new parton(80,0,0,80,true)));
221  mS->new_parton(nnn55,nnn77,unique_ptr<parton>(new parton(70,0,0,70,false)));
222  mS->new_parton(nnn77,nnn88,unique_ptr<parton>(new parton(60,0,0,60,true)));
223 
224  //reverse changes all counts --> new inverse graph ... ? (shared pointers ...)
225  /*
226  mS->new_parton(nnn22,nnn11,unique_ptr<parton>(new parton(100,0,0,100,false)));
227  mS->new_parton(nnn33,nnn11,unique_ptr<parton>(new parton(200,0,0,200,false)));
228  mS->new_parton(nnn44,nnn33,unique_ptr<parton>(new parton(50,0,0,50,true)));
229  mS->new_parton(nnn55,nnn33,unique_ptr<parton>(new parton(150,0,0,150,false)));
230  mS->new_parton(nnn66,nnn55,unique_ptr<parton>(new parton(80,0,0,80,true)));
231  mS->new_parton(nnn77,nnn55,unique_ptr<parton>(new parton(70,0,0,70,true)));
232  */
233 
234  //mS->new_parton(nnn11,nnn22,make_shared<parton>(100,0,0,100,false));
235  //mS->save();
236 
237  //mS->insert_reverse_edges(); //problem with new parton creation .... so post/pre handler
238  // not really doing the trick
239 
240  shower2::node_iterator git0, gend0;
241 
242  for (git0 = mS->nodes_begin(), gend0 = mS->nodes_end(); git0 != gend0; ++git0)
243  {
244  cout<<*git0<<" "<<mS->GetNodeValue(*git0)<<" "<<git0->indeg()<<" "<<git0->outdeg()<<endl;
245  }
246 
247  shower2::edge_iterator git3, gend3;
248 
249  for (git3 = mS->edges_begin(), gend3 = mS->edges_end(); git3 != gend3; ++git3)
250  {
251  cout<<*git3<<" "<<mS->GetEdgeValue(*git3)<<" "<<mS->GetFinal(*git3)<<endl;
252  }
253 
254  list<node> ln=mS->all_nodes();
255  cout<<ln.size()<<endl;
256 
257  list<edge> le=mS->all_edges();
258  cout<<le.size()<<endl;
259  //le.remove_if( // hmmm, not with parton in graph (inheritence !??) Check ..
260 
261  dfs search;
262  search.start_node(nnn11);
263  search.run(*mS);
264  cout<< search.number_of_reached_nodes () <<endl;
265 
266  dfs::dfs_iterator itt2, endt2;
267  for (itt2 = search.begin(), endt2=search.end(); itt2 !=endt2; ++itt2)
268  {
269  cout<<*itt2<<endl;
270  }
271 
272  dfs::tree_edges_iterator itt, endt;
273  for (itt = search.tree_edges_begin(), endt=search.tree_edges_end(); itt !=endt; ++itt)
274  {
275  cout<<*itt<<endl;
276  }
277 
278  bfs search2;
279  search2.start_node(nnn11);
280  search2.run(*mS);
281  cout<< search2.number_of_reached_nodes () <<endl;
282 
283  bfs::bfs_iterator itt22, endt22;
284  for (itt22 = search2.begin(), endt22=search2.end(); itt22 !=endt22; ++itt22)
285  {
286  cout<<*itt22<<endl;
287  }
288 
289  // -----
290  // why only with shared vectors ? Not reall happy!?? explore with unique_ptr !???
291  // somehow issues with the GTL !???
292 
293  /*
294  vector<shared_ptr<parton>> myv=mS->GetPartons(); // this is dangerous with shared pointers ... !!!!
295  cout<<myv.size()<<endl;
296  myv.clear();
297  */
298 
299  vector<weak_ptr<parton>> myv=mS->GetPartons(); //better with weak ... !!!!
300  cout<<myv.size()<<endl;
301  //if (myv.size()>0)
302  for (const auto& v : myv)
303  cout<<v.lock()->pt<<endl;
304  //cout<<myv[0].lock()->pt<<endl;
305  //myv.clear();
306 
307  mS->save("test.gml");
308 
309  mS->clear(); //does not call parton destructor but with handler it does ...
310  cout<<" mS->clear()"<<endl;
311  //mS->save();
312 
313  mS=0;
314 
315  // --------------
316  // not really working with inhereting edge and node ...
317  /*
318  cout<<" -------------- "<<endl;
319 
320  auto mS3=make_shared<shower3>();
321  mS3->make_directed();
322 
323  mS3->new_vertex(0);
324 
325  mS3->save();
326 
327  cout<<"Done .."<<endl;
328  */
329 }
330 
331 /*
332 // not really working though ... !????
333 class vertex : public node
334 {
335 
336 public :
337 
338  vertex() : node() {};
339  vertex(int mylabel) : node() {label=mylabel;}
340  virtual ~vertex() {cout<<"Vertex destructor ..."<<endl;}
341 
342  int label;
343 };
344 
345 class parton3 : public edge
346 {
347 
348 public:
349 
350  parton3() : edge() {};
351  parton3 (double mpt, double meta, double mphi, double me, bool mfinal) :edge () {pt=mpt;eta=meta;phi=mphi;e=me;final=mfinal;}
352  virtual ~parton3() {cout<<" Parton3 Destructor ..."<<endl;}
353 
354  bool isFinal() {return final;}
355 
356  double pt, eta, phi, e;
357  bool final;
358 };
359 */
360 /*
361 class shower3 : public graph {
362 
363 public:
364 
365  shower3() : graph() {cout<<"Shower3 Constrcutor ..."<<endl;}
366  virtual ~shower3() {cout<<"Shower3 Detrcutor ..."<<endl;}
367 
368  //node new_vertex(int x) {node n=graph::new_node();XX[n]=x; return n;}
369  //void new_parton(node s, node t, unique_ptr<parton> p) {edge e=graph::new_edge(s,t);PP[e]=std::move(p);}
370  //void save_edge_info_handler (ostream *o, edge n) const { *o<<"Value = "<<PP[n]->pt<<endl;}
371 
372  //vertex new_vertex(int i) {unique_ptr<vertex> n = unique_ptr<vertex>(new vertex(i)); *(static_cast<edge>(n)) = graph::new_node();return n;} //XX[n]=x; return n;}
373 
374  void new_vertex(int i) {shared_ptr<vertex> n = make_shared<vertex>(i); (node) (*n) = graph::new_node();}//return n;} //XX[n]=x; return n;}
375  void new_parton(node s, node t, unique_ptr<parton> p) {edge e=graph::new_edge(s,t);} ;//PP[e]=std::move(p);}
376  /*
377  //void save_edge_info_handler (ostream *o, edge n) const { *o<<"Value = "<<PP[n]->pt<<endl;}
378 
379  //double GetEdgeValue(edge n) const {return PP[n]->pt;}
380  //int GetNodeValue(node n) const {return XX[n];}
381  //bool GetFinal(edge e) const {return PP[e]->final;}
382  //void pre_new_edge_handler(node s,node t) {};
383  //void post_new_edge_handler(edge e) {};
384 
385  /*
386  void pre_clear_handler() {
387  edge_iterator git3, gend3;
388  for (git3 = edges_begin(), gend3 = edges_end(); git3 != gend3; ++git3)
389  {
390  //cout<<*git3<<" "<<mS->GetEdgeValue(*git3)<<endl;
391  PP[*git3]=nullptr;
392  }
393  }
394  */
395 /*
396  private:
397 
398  //node_map<int> XX;
399  //edge_map<shared_ptr<parton>> PP; //unique_ptr not working !???
400 
401 };
402 */