Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:19:41

0001 #include <iostream>
0002 
0003 #include <GTL/graph.h>
0004 #include <GTL/bellman_ford.h>
0005 #include <GTL/edge_map.h>
0006 #include <GTL/node_map.h>
0007 #include <GTL/dfs.h>
0008 #include <GTL/bfs.h>
0009 #include <memory>
0010 #include <list>
0011 #include <vector>
0012 #include "helper.h"
0013 
0014 class vertex {
0015 
0016 public:
0017   vertex() {};
0018   vertex(int myId) {id=myId;}
0019   virtual ~vertex() {cout<<" Vertex Detructor ..."<<endl;}
0020   int id;
0021 };
0022 
0023 class parton {
0024 
0025 public:
0026   
0027   parton() {};
0028   parton (double mpt, double meta, double mphi, double me, bool mfinal) {pt=mpt;eta=meta;phi=mphi;e=me;final=mfinal;}
0029   virtual ~parton() {cout<<" Parton Destructor ..."<<endl;}
0030 
0031   bool isFinal() {return final;}
0032        
0033   double pt, eta, phi, e;
0034   bool final;
0035 };
0036 
0037 class shower2 : public graph {
0038 
0039 public:
0040   
0041   shower2() : graph() {cout<<"Shower2 Constrcutor ..."<<endl;}
0042   virtual ~shower2() {cout<<"Shower2 Detrcutor ..."<<endl;}
0043   
0044   node new_vertex(shared_ptr<vertex> v) {node n=graph::new_node();XX[n]=v; return n;}
0045   //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;}
0046   void new_parton(node s, node t, unique_ptr<parton> p) {edge e=graph::new_edge(s,t);PP[e]=std::move(p);}
0047 
0048   //unique_ptr<parton> GetParton(edge e) {return unique_ptr<parton> (PP[e]);}
0049   
0050   //int GetNodeValue(node n) const {return XX[n];}
0051   void save_node_info_handler (ostream *o, node n) const { *o<<"MyID "<<XX[n]->id<<endl;}
0052   void save_edge_info_handler (ostream *o, edge n) const { *o<<"pT "<<PP[n]->pt<<endl;}
0053 
0054    void load_edge_info_handler (edge e, GML_pair *read) {
0055     cout<<"Load edge ... "<<e<<endl;
0056     struct GML_pair* tmp = read;
0057     
0058     while (tmp) {
0059 
0060       //printf ("*KEY* : %s \n", tmp->key);
0061       if (((string) (tmp->key)).find("pT")<1)
0062       {
0063       
0064         PP[e]=make_shared<parton>(tmp->value.floating,0,0,tmp->value.floating,false);
0065       }
0066     
0067     tmp = tmp->next;
0068     }
0069   }
0070   
0071   //maybe smarter to store node id and edge source target in vertex/parton vector and recreate graph from scratch ....
0072   void load_node_info_handler (node n, GML_pair *read) {
0073     cout<<"Load node ... "<<n<<endl;
0074     //cout<<read->key<<endl;//" "<<read->value.floating<<endl;
0075     struct GML_pair* tmp = read;
0076     int i;
0077     int level=2;
0078     int nC=0;
0079     
0080     while (tmp) {
0081      
0082       //for (i = 0; i < level; i++) {
0083       //    printf ("    ");
0084       //}
0085 
0086     //printf ("*KEY* : %s \n", tmp->key);
0087 
0088     //printf ("%s", tmp->key.id);
0089     
0090     //if (((string) (tmp->key)).find("text")) cout<<"text .."<<endl;
0091 
0092     /*
0093     switch (tmp->kind) {
0094     case GML_INT:
0095         printf ("  *VALUE* (long) : %ld \n", tmp->value.integer);
0096         break;
0097 
0098     case GML_DOUBLE:
0099         printf ("  *VALUE* (double) : %f \n", tmp->value.floating);
0100         break;
0101 
0102     case GML_STRING:
0103         printf ("  *VALUE* (string) : %s \n", tmp->value.str);
0104         break;
0105         
0106     case GML_LIST:
0107         printf ("  *VALUE* (list) : \n");
0108         GML_print_list (tmp->value.list, level+1);
0109         break;
0110 
0111     default:
0112         break;
0113     }
0114     */
0115 
0116     //cout<<((string) (tmp->key)).find("text")<<endl;
0117     if (((string) (tmp->key)).find("MyID")<1)
0118       {
0119         //cout<<n<<" "<<nC<<" "<<i<<endl;
0120         //cout<<n<<" "<<tmp->value.integer<<endl;
0121         XX[n]=make_shared<vertex>(tmp->value.integer);
0122       }
0123     
0124     //nC++;
0125     //cout<<nC<<endl;
0126     tmp = tmp->next;
0127     }
0128   }
0129   
0130   double GetEdgeValue(edge n) const {return PP[n]->pt;}
0131   int GetNodeValue(node n) const {return XX[n]->id;}
0132   bool GetFinal(edge e) const {return PP[e]->final;}
0133   //void pre_new_edge_handler(node s,node t) {};
0134   //void post_new_edge_handler(edge e) {};
0135   void pre_clear_handler() {
0136     edge_iterator git3, gend3;    
0137     for (git3 = edges_begin(), gend3 = edges_end(); git3 != gend3; ++git3)      
0138       {
0139     //cout<<*git3<<" "<<mS->GetEdgeValue(*git3)<<endl;
0140     PP[*git3]=nullptr;  
0141       }
0142     node_iterator git4, gend4; 
0143     for (git4 = nodes_begin(), gend4 = nodes_end(); git4 != gend4; ++git4)      
0144       {
0145     XX[*git4]=nullptr;
0146       }
0147   }
0148 
0149   // vector<shared_ptr<vertex>> GetVertices() {};
0150   /*
0151   vector<shared_ptr<parton>> GetPartons() {
0152     vector<shared_ptr<parton>> myv; list<edge> le=all_edges();
0153     //int n=le.size();
0154     //for (int i=0;i<n;i++)
0155     int n=0;
0156 
0157     for (list<edge>::iterator it=le.begin(); it!=le.end(); ++it)
0158       {
0159     if (PP[*it]->isFinal())
0160       myv.push_back(PP[*it]);
0161     //cout<<PP[*it]->pt<<endl;
0162       }
0163     return myv;
0164   }
0165   */
0166   
0167   vector<weak_ptr<parton>> GetPartons() {
0168     vector<weak_ptr<parton>> myv; list<edge> le=all_edges();
0169     //int n=le.size();
0170     //for (int i=0;i<n;i++)
0171     int n=0;
0172 
0173     for (list<edge>::iterator it=le.begin(); it!=le.end(); ++it)
0174       {
0175     if (PP[*it]->isFinal())
0176       myv.push_back(PP[*it]);
0177     //cout<<PP[*it]->pt<<endl;
0178       }
0179     return myv;
0180   }
0181   
0182  private:
0183  
0184   node_map<shared_ptr<vertex>> XX;
0185   edge_map<shared_ptr<parton>> PP; //unique_ptr not working !???
0186   
0187 };
0188 
0189 int main (int argc, char* argv[])
0190 {
0191     auto mS=make_shared<shower2>();
0192     mS->make_directed();
0193 
0194     node nnn11=mS->new_vertex(make_shared<vertex>(0)); //if c++14 or c++11 and helper.h ...
0195     //node nnn11=mS->new_vertex(unique_ptr<vertex>(new vertex(0)));
0196     node nnn22=mS->new_vertex(make_shared<vertex>(1));
0197     node nnn33=mS->new_vertex(make_shared<vertex>(1));
0198     node nnn44=mS->new_vertex(make_shared<vertex>(2));
0199     node nnn55=mS->new_vertex(make_shared<vertex>(2));
0200     node nnn66=mS->new_vertex(make_shared<vertex>(3));
0201     node nnn77=mS->new_vertex(make_shared<vertex>(3));
0202     node nnn88=mS->new_vertex(make_shared<vertex>(4));
0203     
0204     /*
0205     node nnn11=mS->new_vertex(make_unique<vertex>(1)); //if c++14 or c++11 and helper.h ...
0206     //node nnn11=mS->new_vertex(unique_ptr<vertex>(new vertex(0)));
0207     node nnn22=mS->new_vertex(unique_ptr<vertex>(new vertex(1)));
0208     node nnn33=mS->new_vertex(unique_ptr<vertex>(new vertex(1)));
0209     node nnn44=mS->new_vertex(unique_ptr<vertex>(new vertex(2)));
0210     node nnn55=mS->new_vertex(unique_ptr<vertex>(new vertex(2)));
0211     node nnn66=mS->new_vertex(unique_ptr<vertex>(new vertex(3)));
0212     node nnn77=mS->new_vertex(unique_ptr<vertex>(new vertex(3)));
0213     node nnn88=mS->new_vertex(unique_ptr<vertex>(new vertex(4)));
0214     */
0215     
0216     mS->new_parton(nnn11,nnn22,unique_ptr<parton>(new parton(100,0,0,100,false)));
0217     mS->new_parton(nnn11,nnn33,unique_ptr<parton>(new parton(200,0,0,200,false)));
0218     mS->new_parton(nnn33,nnn44,unique_ptr<parton>(new parton(50,0,0,50,true)));
0219     mS->new_parton(nnn33,nnn55,unique_ptr<parton>(new parton(150,0,0,150,false)));
0220     mS->new_parton(nnn55,nnn66,unique_ptr<parton>(new parton(80,0,0,80,true)));
0221     mS->new_parton(nnn55,nnn77,unique_ptr<parton>(new parton(70,0,0,70,false)));
0222     mS->new_parton(nnn77,nnn88,unique_ptr<parton>(new parton(60,0,0,60,true)));
0223 
0224     //reverse changes all counts --> new inverse graph ... ? (shared pointers ...)
0225     /*
0226     mS->new_parton(nnn22,nnn11,unique_ptr<parton>(new parton(100,0,0,100,false)));
0227     mS->new_parton(nnn33,nnn11,unique_ptr<parton>(new parton(200,0,0,200,false)));
0228     mS->new_parton(nnn44,nnn33,unique_ptr<parton>(new parton(50,0,0,50,true)));
0229     mS->new_parton(nnn55,nnn33,unique_ptr<parton>(new parton(150,0,0,150,false)));
0230     mS->new_parton(nnn66,nnn55,unique_ptr<parton>(new parton(80,0,0,80,true)));
0231     mS->new_parton(nnn77,nnn55,unique_ptr<parton>(new parton(70,0,0,70,true)));
0232     */
0233     
0234     //mS->new_parton(nnn11,nnn22,make_shared<parton>(100,0,0,100,false));
0235     //mS->save();
0236 
0237     //mS->insert_reverse_edges(); //problem with new parton creation .... so post/pre handler
0238     // not really doing the trick
0239     
0240     shower2::node_iterator git0, gend0;
0241     
0242     for (git0 = mS->nodes_begin(), gend0 = mS->nodes_end(); git0 != gend0; ++git0)      
0243       {
0244     cout<<*git0<<" "<<mS->GetNodeValue(*git0)<<" "<<git0->indeg()<<" "<<git0->outdeg()<<endl;
0245       }
0246     
0247     shower2::edge_iterator git3, gend3;
0248     
0249     for (git3 = mS->edges_begin(), gend3 = mS->edges_end(); git3 != gend3; ++git3)      
0250       {
0251     cout<<*git3<<" "<<mS->GetEdgeValue(*git3)<<" "<<mS->GetFinal(*git3)<<endl;
0252       }
0253 
0254     list<node> ln=mS->all_nodes();
0255     cout<<ln.size()<<endl;
0256 
0257     list<edge> le=mS->all_edges(); 
0258     cout<<le.size()<<endl;
0259     //le.remove_if( // hmmm, not with parton in graph (inheritence !??) Check ..
0260 
0261     dfs search;
0262     search.start_node(nnn11);
0263     search.run(*mS);
0264     cout<< search.number_of_reached_nodes () <<endl;
0265 
0266     dfs::dfs_iterator itt2, endt2;
0267     for (itt2 = search.begin(), endt2=search.end(); itt2 !=endt2; ++itt2)
0268       {
0269     cout<<*itt2<<endl;
0270       }
0271     
0272     dfs::tree_edges_iterator itt, endt;
0273     for (itt = search.tree_edges_begin(), endt=search.tree_edges_end(); itt !=endt; ++itt)
0274       {
0275     cout<<*itt<<endl;
0276       }
0277 
0278     bfs search2;
0279     search2.start_node(nnn11);
0280     search2.run(*mS);
0281     cout<< search2.number_of_reached_nodes () <<endl;
0282 
0283     bfs::bfs_iterator itt22, endt22;
0284     for (itt22 = search2.begin(), endt22=search2.end(); itt22 !=endt22; ++itt22)
0285       {
0286     cout<<*itt22<<endl;
0287       }
0288     
0289     // -----
0290     // why only with shared vectors ? Not reall happy!?? explore with unique_ptr !???
0291     // somehow issues with the GTL !???
0292 
0293     /*
0294     vector<shared_ptr<parton>> myv=mS->GetPartons(); // this is dangerous with shared pointers ... !!!!
0295     cout<<myv.size()<<endl; 
0296     myv.clear();
0297     */
0298     
0299     vector<weak_ptr<parton>> myv=mS->GetPartons(); //better with weak ... !!!!
0300     cout<<myv.size()<<endl;
0301     //if (myv.size()>0)
0302     for (const auto& v : myv)
0303       cout<<v.lock()->pt<<endl;
0304       //cout<<myv[0].lock()->pt<<endl;
0305     //myv.clear();
0306 
0307     mS->save("test.gml");
0308     
0309     mS->clear(); //does not call parton destructor but with handler it does  ...
0310     cout<<" mS->clear()"<<endl; 
0311     //mS->save();
0312     
0313     mS=0;
0314 
0315     // --------------
0316     // not really working with inhereting edge and node ...
0317     /*
0318     cout<<"  -------------- "<<endl;
0319     
0320     auto mS3=make_shared<shower3>();
0321     mS3->make_directed();
0322 
0323     mS3->new_vertex(0);
0324     
0325     mS3->save();
0326     
0327     cout<<"Done .."<<endl;
0328     */
0329 }
0330 
0331 /*
0332 // not really working though ... !????
0333 class vertex : public node
0334 {
0335 
0336 public :
0337   
0338   vertex() : node() {};
0339   vertex(int mylabel) : node() {label=mylabel;}
0340   virtual ~vertex() {cout<<"Vertex destructor ..."<<endl;}
0341   
0342   int label;
0343 };
0344 
0345 class parton3 : public edge
0346 {
0347 
0348 public:
0349   
0350   parton3() : edge() {};
0351   parton3 (double mpt, double meta, double mphi, double me, bool mfinal) :edge () {pt=mpt;eta=meta;phi=mphi;e=me;final=mfinal;}
0352   virtual ~parton3() {cout<<" Parton3 Destructor ..."<<endl;}
0353 
0354   bool isFinal() {return final;}
0355        
0356   double pt, eta, phi, e;
0357   bool final;
0358 };
0359 */
0360 /*
0361 class shower3 : public graph {
0362 
0363 public:
0364   
0365   shower3() : graph() {cout<<"Shower3 Constrcutor ..."<<endl;}
0366   virtual ~shower3() {cout<<"Shower3 Detrcutor ..."<<endl;}
0367   
0368   //node new_vertex(int x) {node n=graph::new_node();XX[n]=x; return n;}
0369   //void new_parton(node s, node t, unique_ptr<parton> p) {edge e=graph::new_edge(s,t);PP[e]=std::move(p);}
0370   //void save_edge_info_handler (ostream *o, edge n) const { *o<<"Value = "<<PP[n]->pt<<endl;}
0371 
0372   //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;}
0373 
0374   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;}
0375   void new_parton(node s, node t, unique_ptr<parton> p) {edge e=graph::new_edge(s,t);} ;//PP[e]=std::move(p);}
0376   /*
0377   //void save_edge_info_handler (ostream *o, edge n) const { *o<<"Value = "<<PP[n]->pt<<endl;}
0378   
0379   //double GetEdgeValue(edge n) const {return PP[n]->pt;}
0380   //int GetNodeValue(node n) const {return XX[n];}
0381   //bool GetFinal(edge e) const {return PP[e]->final;}
0382   //void pre_new_edge_handler(node s,node t) {};
0383   //void post_new_edge_handler(edge e) {};
0384 
0385   /*
0386   void pre_clear_handler() {
0387     edge_iterator git3, gend3;    
0388     for (git3 = edges_begin(), gend3 = edges_end(); git3 != gend3; ++git3)      
0389       {
0390     //cout<<*git3<<" "<<mS->GetEdgeValue(*git3)<<endl;
0391     PP[*git3]=nullptr;
0392       }
0393   }
0394   */
0395 /*  
0396  private:
0397  
0398   //node_map<int> XX;
0399   //edge_map<shared_ptr<parton>> PP; //unique_ptr not working !???
0400   
0401 };
0402 */