go-hep.org/x/hep@v0.38.1/groot/riofs/gendata/gen-std-containers.go (about)

     1  // Copyright ©2022 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"log"
    12  
    13  	"go-hep.org/x/hep/groot/internal/rtests"
    14  )
    15  
    16  var (
    17  	root  = flag.String("f", "std-containers.root", "output ROOT file")
    18  	split = flag.Int("split", 0, "default split-level for TTree")
    19  )
    20  
    21  func main() {
    22  	flag.Parse()
    23  
    24  	out, err := rtests.RunCxxROOT("gentree", []byte(script), *root, *split)
    25  	if err != nil {
    26  		log.Fatalf("could not run ROOT macro:\noutput:\n%v\nerror: %+v", string(out), err)
    27  	}
    28  }
    29  
    30  const script = `
    31  #include "TInterpreter.h"
    32  #include "TFile.h"
    33  #include "TTree.h"
    34  #include "TBranch.h"
    35  #include "TString.h"
    36  
    37  #include <iostream>
    38  #include <vector>
    39  #include <set>
    40  #include <map>
    41  #include <unordered_set>
    42  #include <unordered_map>
    43  #include <list>
    44  #include <deque>
    45  
    46  void gentree(const char* fname, int splitlvl = 99) {
    47  	int bufsize = 32000;
    48  
    49  	auto f = TFile::Open(fname, "RECREATE");
    50  	auto t = new TTree("tree", "my tree title");
    51  
    52  	gInterpreter->GenerateDictionary("list<int>", "list");
    53  	gInterpreter->GenerateDictionary("deque<int>", "deque");
    54  	gInterpreter->GenerateDictionary("vector<vector<int> >", "vector");
    55  	gInterpreter->GenerateDictionary("vector<vector<unsigned int> >", "vector");
    56  	gInterpreter->GenerateDictionary("vector<vector<string> >", "vector;string");
    57  	gInterpreter->GenerateDictionary("vector<vector<TString> >", "vector");
    58  	gInterpreter->GenerateDictionary("vector<set<int> >", "vector;set");
    59  	gInterpreter->GenerateDictionary("vector<set<unsigned int> >", "vector;set");
    60  	gInterpreter->GenerateDictionary("vector<set<string> >", "vector;set;string");
    61  	gInterpreter->GenerateDictionary("vector<set<TString> >", "vector;set");
    62  	gInterpreter->GenerateDictionary("set<int>", "set");
    63  	gInterpreter->GenerateDictionary("set<unsigned int>", "set");
    64  	gInterpreter->GenerateDictionary("set<short>", "set");
    65  	gInterpreter->GenerateDictionary("set<string>", "set;string");
    66  	gInterpreter->GenerateDictionary("set<TString>", "set");
    67  	gInterpreter->GenerateDictionary("unordered_set<string>", "unordered_set;string");
    68  	gInterpreter->GenerateDictionary("pair<int,short>", "pair");
    69  	gInterpreter->GenerateDictionary("map<int,short>", "map");
    70  	gInterpreter->GenerateDictionary("map<unsigned int,unsigned short>", "map");
    71  	gInterpreter->GenerateDictionary("map<int,vector<short> >", "map;vector");
    72  	gInterpreter->GenerateDictionary("map<unsigned int,vector<unsigned short> >", "map;vector");
    73  	gInterpreter->GenerateDictionary("map<int,vector<string> >", "map;vector;string");
    74  	gInterpreter->GenerateDictionary("pair<int,set<short> >", "pair;set");
    75  	gInterpreter->GenerateDictionary("pair<int,set<string> >", "pair;set;string");
    76  	gInterpreter->GenerateDictionary("map<int,set<short> >", "map;set");
    77  	gInterpreter->GenerateDictionary("map<int,set<string> >", "map;set;string");
    78  	gInterpreter->GenerateDictionary("map<string,short>", "map;string");
    79  	gInterpreter->GenerateDictionary("map<string,vector<short> >", "map;vector;string");
    80  	gInterpreter->GenerateDictionary("map<string,vector<string> >", "map;vector;string");
    81  	gInterpreter->GenerateDictionary("map<string,set<short> >", "map;set;string");
    82  	gInterpreter->GenerateDictionary("map<string,set<string> >", "map;set;string");
    83  	gInterpreter->GenerateDictionary("map<int,vector<vector<short> > >", "map;vector");
    84  	gInterpreter->GenerateDictionary("map<int,vector<set<short> > >", "map;vector;set");
    85  	gInterpreter->GenerateDictionary("map<string,string>", "map;string");
    86  	gInterpreter->GenerateDictionary("map<string,TString>", "map;string");
    87  	gInterpreter->GenerateDictionary("map<TString,TString>", "map");
    88  	gInterpreter->GenerateDictionary("map<TString,string>", "map;string");
    89  	gInterpreter->GenerateDictionary("unordered_map<string,string>", "unordered_map;string");
    90  	gInterpreter->GenerateDictionary("unordered_map<string,TString>", "unordered_map;string");
    91  
    92  
    93  	std::string str;
    94  	TString tstr;
    95  	std::list<int32_t> lst_i32;
    96  	std::deque<int32_t> deq_i32;
    97  	std::vector<int32_t> vec_i32;
    98  	std::vector<uint32_t> vec_u32;
    99  	std::vector<std::string> vec_str;
   100  	std::vector<TString> vec_tstr;
   101  	std::vector<std::vector<int32_t>> vec_vec_i32;
   102  	std::vector<std::vector<uint32_t>> vec_vec_u32;
   103  	std::vector<std::vector<std::string>> vec_vec_str;
   104  	std::vector<std::vector<TString>> vec_vec_tstr;
   105  	std::vector<std::set<int32_t>> vec_set_i32;
   106  	std::vector<std::set<uint32_t>> vec_set_u32;
   107  	std::vector<std::set<std::string>> vec_set_str;
   108  	std::vector<std::set<TString>> vec_set_tstr;
   109  	std::set<int32_t> set_i32;
   110  	std::set<uint32_t> set_u32;
   111  	std::set<std::string> set_str;
   112  	std::set<TString> set_tstr;
   113  	std::unordered_set<std::string> uset_str;
   114  	std::map<int32_t, int16_t> map_i32_i16;
   115  	std::map<uint32_t, uint16_t> map_u32_u16;
   116  	std::map<int32_t, std::vector<int16_t> > map_i32_vec_i16;
   117  	std::map<uint32_t, std::vector<uint16_t> > map_u32_vec_u16;
   118  	std::map<int32_t, std::vector<std::string> > map_i32_vec_str;
   119  	std::map<int32_t, std::set<int16_t> > map_i32_set_i16;
   120  	std::map<int32_t, std::set<std::string> > map_i32_set_str;
   121  	std::map<std::string, int16_t> map_str_i16;
   122  	std::map<std::string, std::vector<int16_t> > map_str_vec_i16;
   123  	std::map<std::string, std::vector<std::string> > map_str_vec_str;
   124  	std::map<std::string, std::set<int16_t> > map_str_set_i16;
   125  	std::map<std::string, std::set<std::string> > map_str_set_str;
   126  	std::map<int32_t, std::vector<std::vector<int16_t> > > map_i32_vec_vec_i16;
   127  	std::map<int32_t, std::vector<std::set<int16_t> > > map_i32_vec_set_i16;
   128  	std::map<std::string, std::string> map_str_str;
   129  	std::map<std::string, TString> map_str_tstr;
   130  	std::map<TString, TString> map_tstr_tstr;
   131  	std::map<TString, std::string> map_tstr_str;
   132  	std::unordered_map<std::string, std::string> umap_str_str;
   133  	std::unordered_map<std::string, TString> umap_str_tstr;
   134  
   135  	t->Branch("str", &str);
   136  	t->Branch("tstr", &tstr);
   137  	t->Branch("lst_i32", &lst_i32);
   138  	t->Branch("deq_i32", &deq_i32);
   139  	t->Branch("vec_i32", &vec_i32);
   140  	t->Branch("vec_u32", &vec_u32);
   141  	t->Branch("vec_str", &vec_str);
   142  	t->Branch("vec_tstr", &vec_tstr);
   143  	t->Branch("vec_vec_i32", &vec_vec_i32);
   144  	t->Branch("vec_vec_u32", &vec_vec_u32);
   145  	t->Branch("vec_vec_str", &vec_vec_str);
   146  	t->Branch("vec_vec_tstr", &vec_vec_tstr);
   147  	t->Branch("vec_set_i32", &vec_set_i32);
   148  	t->Branch("vec_set_u32", &vec_set_u32);
   149  	t->Branch("vec_set_str", &vec_set_str);
   150  	t->Branch("vec_set_tstr", &vec_set_tstr);
   151  	t->Branch("set_i32", &set_i32);
   152  	t->Branch("set_u32", &set_u32);
   153  	t->Branch("set_str", &set_str);
   154  	t->Branch("set_tstr", &set_tstr);
   155  	t->Branch("uset_str", &uset_str);
   156  	t->Branch("map_i32_i16", &map_i32_i16, bufsize, splitlvl);
   157  	t->Branch("map_u32_u16", &map_u32_u16, bufsize, splitlvl);
   158  	t->Branch("map_i32_vec_i16", &map_i32_vec_i16, bufsize, splitlvl);
   159  	t->Branch("map_u32_vec_u16", &map_u32_vec_u16, bufsize, splitlvl);
   160  	t->Branch("map_i32_vec_str", &map_i32_vec_str, bufsize, splitlvl);
   161  	t->Branch("map_i32_set_i16", &map_i32_set_i16, bufsize, splitlvl);
   162  	t->Branch("map_i32_set_str", &map_i32_set_str, bufsize, splitlvl);
   163  	t->Branch("map_str_i16", &map_str_i16, bufsize, splitlvl);
   164  	t->Branch("map_str_vec_i16", &map_str_vec_i16, bufsize, splitlvl);
   165  	t->Branch("map_str_vec_str", &map_str_vec_str, bufsize, splitlvl);
   166  	t->Branch("map_str_set_i16", &map_str_set_i16, bufsize, splitlvl);
   167  	t->Branch("map_str_set_str", &map_str_set_str, bufsize, splitlvl);
   168  	t->Branch("map_i32_vec_vec_i16", &map_i32_vec_vec_i16, bufsize, splitlvl);
   169  	t->Branch("map_i32_vec_set_i16", &map_i32_vec_set_i16, bufsize, splitlvl);
   170  	t->Branch("map_str_str", &map_str_str, bufsize, splitlvl);
   171  	t->Branch("map_str_tstr", &map_str_tstr, bufsize, splitlvl);
   172  	t->Branch("map_tstr_tstr", &map_tstr_tstr, bufsize, splitlvl);
   173  	t->Branch("map_tstr_str", &map_tstr_str, bufsize, splitlvl);
   174  	t->Branch("umap_str_str", &umap_str_str, bufsize, splitlvl);
   175  	
   176  	str.clear();
   177  	str.assign("one");
   178  	tstr.Clear();
   179  	tstr.Append("one");
   180  	lst_i32.clear();
   181  	lst_i32.push_back(-1);
   182  	deq_i32.clear();
   183  	deq_i32.push_back(-1);
   184  	vec_i32.clear();
   185  	vec_i32.push_back(-1);
   186  	vec_u32.clear();
   187  	vec_u32.push_back(1);
   188  	vec_str.clear();
   189  	vec_str.push_back("one");
   190  	vec_tstr.clear();
   191  	vec_tstr.push_back("one");
   192  	vec_vec_i32.clear();
   193  	vec_vec_i32.push_back(std::vector<int32_t>{ -1 });
   194  	vec_vec_u32.clear();
   195  	vec_vec_u32.push_back(std::vector<uint32_t>{ 1 });
   196  	vec_vec_str.clear();
   197  	vec_vec_str.push_back(std::vector<std::string>{ "one" });
   198  	vec_vec_tstr.clear();
   199  	vec_vec_tstr.push_back(std::vector<TString>{ "one" });
   200  	vec_set_i32.clear();
   201  	vec_set_i32.push_back(std::set<int32_t>{ -1 });
   202  	vec_set_u32.clear();
   203  	vec_set_u32.push_back(std::set<uint32_t>{ 1 });
   204  	vec_set_str.clear();
   205  	vec_set_str.push_back(std::set<std::string>{ "one" });
   206  	vec_set_tstr.clear();
   207  	vec_set_tstr.push_back(std::set<TString>{ "one" });
   208  	set_i32.clear();
   209  	set_i32.insert(-1);
   210  	set_u32.clear();
   211  	set_u32.insert(1);
   212  	set_str.clear();
   213  	set_str.insert("one");
   214  	set_tstr.clear();
   215  	set_tstr.insert("one");
   216  	uset_str.clear();
   217  	uset_str.insert("one");
   218  	map_i32_i16.clear();
   219  	map_i32_i16[-1] = -1;
   220  	map_u32_u16.clear();
   221  	map_u32_u16[1] = 1;
   222  	map_i32_vec_i16.clear();
   223  	map_i32_vec_i16[-1] = std::vector<int16_t>({ -1 });
   224  	map_u32_vec_u16.clear();
   225  	map_u32_vec_u16[1] = std::vector<uint16_t>({ 1 });
   226  	map_i32_vec_str.clear();
   227  	map_i32_vec_str[-1] = std::vector<std::string>({ "one" });
   228  	map_i32_set_i16.clear();
   229  	map_i32_set_i16[-1] = std::set<int16_t>({ -1 });
   230  	map_i32_set_str.clear();
   231  	map_i32_set_str[-1] = std::set<std::string>({ "one" });
   232  	map_str_i16.clear();
   233  	map_str_i16["one"] = -1;
   234  	map_str_vec_i16.clear();
   235  	map_str_vec_i16["one"] = std::vector<int16_t>({ -1 });
   236  	map_str_vec_str.clear();
   237  	map_str_vec_str["one"] = std::vector<std::string>({ "one" });
   238  	map_str_set_i16.clear();
   239  	map_str_set_i16["one"] = std::set<int16_t>({ -1 });
   240  	map_str_set_str.clear();
   241  	map_str_set_str["one"] = std::set<std::string>({ "one" });
   242  	map_i32_vec_vec_i16.clear();
   243  	map_i32_vec_vec_i16[-1] = std::vector<std::vector<int16_t>>{ std::vector<int16_t>{ -1 } };
   244  	map_i32_vec_set_i16.clear();
   245  	map_i32_vec_set_i16[-1] = std::vector<std::set<int16_t>>{ std::set<int16_t>{ -1 } };
   246  	map_str_str.clear();
   247  	map_str_str["one"] = "ONE";
   248  	map_str_tstr.clear();
   249  	map_str_tstr["one"] = "ONE";
   250  	map_tstr_tstr.clear();
   251  	map_tstr_tstr["one"] = "ONE";
   252  	map_tstr_str.clear();
   253  	map_tstr_str["one"] = "ONE";
   254  	umap_str_str.clear();
   255  	umap_str_str["one"] = "ONE";
   256  	
   257  	t->Fill();
   258  	
   259  	str.clear();
   260  	str.assign("two");
   261  	tstr.Clear();
   262  	tstr.Append("two");
   263  	lst_i32.clear();
   264  	lst_i32.push_back(-1);
   265  	lst_i32.push_back(-2);
   266  	deq_i32.clear();
   267  	deq_i32.push_back(-1);
   268  	deq_i32.push_back(-2);
   269  	vec_i32.clear();
   270  	vec_i32.push_back(-1);
   271  	vec_i32.push_back(-2);
   272  	vec_u32.clear();
   273  	vec_u32.push_back(1);
   274  	vec_u32.push_back(2);
   275  	vec_str.clear();
   276  	vec_str.push_back("one");
   277  	vec_str.push_back("two");
   278  	vec_tstr.clear();
   279  	vec_tstr.push_back("one");
   280  	vec_tstr.push_back("two");
   281  	vec_vec_i32.clear();
   282  	vec_vec_i32.push_back(std::vector<int32_t>{ -1 });
   283  	vec_vec_i32.push_back(std::vector<int32_t>{ -1, -2 });
   284  	vec_vec_u32.clear();
   285  	vec_vec_u32.push_back(std::vector<uint32_t>{ 1 });
   286  	vec_vec_u32.push_back(std::vector<uint32_t>{ 1, 2 });
   287  	vec_vec_str.clear();
   288  	vec_vec_str.push_back(std::vector<std::string>{ "one" });
   289  	vec_vec_str.push_back(std::vector<std::string>{ "one", "two" });
   290  	vec_vec_tstr.clear();
   291  	vec_vec_tstr.push_back(std::vector<TString>{ "one" });
   292  	vec_vec_tstr.push_back(std::vector<TString>{ "one", "two" });
   293  	vec_set_i32.clear();
   294  	vec_set_i32.push_back(std::set<int32_t>{ -1 });
   295  	vec_set_i32.push_back(std::set<int32_t>{ -1, -2 });
   296  	vec_set_u32.clear();
   297  	vec_set_u32.push_back(std::set<uint32_t>{ 1 });
   298  	vec_set_u32.push_back(std::set<uint32_t>{ 1, 2 });
   299  	vec_set_str.clear();
   300  	vec_set_str.push_back(std::set<std::string>{ "one" });
   301  	vec_set_str.push_back(std::set<std::string>{ "one", "two" });
   302  	vec_set_tstr.clear();
   303  	vec_set_tstr.push_back(std::set<TString>{ "one" });
   304  	vec_set_tstr.push_back(std::set<TString>{ "one", "two" });
   305  	set_i32.clear();
   306  	set_i32.insert(-1);
   307  	set_i32.insert(-2);
   308  	set_u32.clear();
   309  	set_u32.insert(1);
   310  	set_u32.insert(2);
   311  	set_str.clear();
   312  	set_str.insert("one");
   313  	set_str.insert("two");
   314  	set_tstr.clear();
   315  	set_tstr.insert("one");
   316  	set_tstr.insert("two");
   317  	uset_str.clear();
   318  	uset_str.insert("one");
   319  	uset_str.insert("two");
   320  	map_i32_i16.clear();
   321  	map_i32_i16[-1] = -1;
   322  	map_i32_i16[-2] = -2;
   323  	map_u32_u16.clear();
   324  	map_u32_u16[1] = 1;
   325  	map_u32_u16[2] = 2;
   326  	map_i32_vec_i16.clear();
   327  	map_i32_vec_i16[-1] = std::vector<int16_t>({ -1 });
   328  	map_i32_vec_i16[-2] = std::vector<int16_t>({ -1, -2 });
   329  	map_u32_vec_u16.clear();
   330  	map_u32_vec_u16[1] = std::vector<uint16_t>({ 1 });
   331  	map_u32_vec_u16[2] = std::vector<uint16_t>({ 1, 2 });
   332  	map_i32_vec_str.clear();
   333  	map_i32_vec_str[-1] = std::vector<std::string>({ "one" });
   334  	map_i32_vec_str[-2] = std::vector<std::string>({ "one", "two" });
   335  	map_i32_set_i16.clear();
   336  	map_i32_set_i16[-1] = std::set<int16_t>({ -1 });
   337  	map_i32_set_i16[-2] = std::set<int16_t>({ -1, -2 });
   338  	map_i32_set_str.clear();
   339  	map_i32_set_str[-1] = std::set<std::string>({ "one" });
   340  	map_i32_set_str[-2] = std::set<std::string>({ "one", "two" });
   341  	map_str_i16.clear();
   342  	map_str_i16["one"] = -1;
   343  	map_str_i16["two"] = -2;
   344  	map_str_vec_i16.clear();
   345  	map_str_vec_i16["one"] = std::vector<int16_t>({ -1 });
   346  	map_str_vec_i16["two"] = std::vector<int16_t>({ -1, -2 });
   347  	map_str_vec_str.clear();
   348  	map_str_vec_str["one"] = std::vector<std::string>({ "one" });
   349  	map_str_vec_str["two"] = std::vector<std::string>({ "one", "two" });
   350  	map_str_set_i16.clear();
   351  	map_str_set_i16["one"] = std::set<int16_t>({ -1 });
   352  	map_str_set_i16["two"] = std::set<int16_t>({ -1, -2 });
   353  	map_str_set_str.clear();
   354  	map_str_set_str["one"] = std::set<std::string>({ "one" });
   355  	map_str_set_str["two"] = std::set<std::string>({ "one", "two" });
   356  	map_i32_vec_vec_i16.clear();
   357  	map_i32_vec_vec_i16[-1] = std::vector<std::vector<int16_t>>{ std::vector<int16_t>{ -1 } };
   358  	map_i32_vec_vec_i16[-2] = std::vector<std::vector<int16_t>>{ std::vector<int16_t>{ -1 }, std::vector<int16_t>{-1, -2} };
   359  	map_i32_vec_set_i16.clear();
   360  	map_i32_vec_set_i16[-1] = std::vector<std::set<int16_t>>{ std::set<int16_t>{ -1 } };
   361  	map_i32_vec_set_i16[-2] = std::vector<std::set<int16_t>>{ std::set<int16_t>{ -1 }, std::set<int16_t>{ -1, -2 } };
   362  	map_str_str.clear();
   363  	map_str_str["one"] = "ONE";
   364  	map_str_str["two"] = "TWO";
   365  	map_str_tstr.clear();
   366  	map_str_tstr["one"] = "ONE";
   367  	map_str_tstr["two"] = "TWO";
   368  	map_tstr_tstr.clear();
   369  	map_tstr_tstr["one"] = "ONE";
   370  	map_tstr_tstr["two"] = "TWO";
   371  	map_tstr_str.clear();
   372  	map_tstr_str["one"] = "ONE";
   373  	map_tstr_str["two"] = "TWO";
   374  	umap_str_str.clear();
   375  	umap_str_str["one"] = "ONE";
   376  	umap_str_str["two"] = "TWO";
   377  	
   378  	t->Fill();
   379  
   380  	f->Write();
   381  	f->Close();
   382  
   383  	exit(0);
   384  }
   385  `