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

     1  // Copyright ©2020 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-map.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  	dict, err := rtests.GenROOTDictCode(event, link)
    25  	if err != nil {
    26  		log.Fatalf("could not run ROOT dict: %+v", err)
    27  	}
    28  
    29  	out, err := rtests.RunCxxROOT("gentree", []byte(event+string(dict)+script), *root, *split)
    30  	if err != nil {
    31  		log.Fatalf("could not run ROOT macro:\noutput:\n%v\nerror: %+v", string(out), err)
    32  	}
    33  }
    34  
    35  const event = `
    36  #ifndef EVENT_H
    37  #define EVENT_H 1
    38  
    39  #include <map>
    40  #include <vector>
    41  #include <string>
    42  
    43  struct P3 {
    44  	int32_t Px;
    45  	double  Py;
    46  	int32_t Pz;
    47  };
    48  
    49  struct Event {
    50  	std::map<int32_t, int32_t> mi32;
    51  
    52  	std::map<std::string, int32_t>     msi32;
    53  	std::map<std::string, std::string> mss;
    54  //	std::map<std::string, P3>          msp3;
    55  
    56  	std::map<std::string, std::vector<std::string> > msvs;
    57  	std::map<std::string, std::vector<int32_t> >     msvi32;
    58  //	std::map<std::string, std::vector<P3> >          msvp3;
    59  
    60  	void clear() {
    61  		this->mi32.clear();
    62  
    63  		this->msi32.clear();
    64  		this->mss.clear();
    65  //		this->msp3.clear();
    66  
    67  		this->msvs.clear();
    68  		this->msvi32.clear();
    69  //		this->msvp3.clear();
    70  	}
    71  };
    72  
    73  #endif // EVENT_H
    74  `
    75  
    76  const link = `
    77  #ifdef __CINT__
    78  
    79  #pragma link off all globals;
    80  #pragma link off all classes;
    81  #pragma link off all functions;
    82  
    83  //#pragma link C++ class P3+;
    84  #pragma link C++ class Event+;
    85  
    86  #endif
    87  `
    88  
    89  const script = `
    90  void gentree(const char* fname, int splitlvl = 99) {
    91  	int bufsize = 32000;
    92  	int evtmax = 10;
    93  
    94  	auto f = TFile::Open(fname, "RECREATE");
    95  	auto t = new TTree("tree", "my tree title");
    96  
    97  	Event e;
    98  
    99  	t->Branch("evt", &e, bufsize, splitlvl);
   100  
   101  	for (int i = 0; i != evtmax; i++) {
   102  		e.clear();
   103  		for (int ii = 0; ii < i; ii++) {
   104  			e.mi32[int32_t(ii)] = int32_t(ii);
   105  
   106  			std::string key = std::string(TString::Format("key-%03d", ii).Data());
   107  
   108  			e.msi32[key] = int32_t(ii);
   109  			e.mss[key] = std::string(TString::Format("val-%03d", ii).Data());
   110  //			e.msp3[key] = P3{ii, double(ii+1), ii+2};
   111  
   112  			e.msvs[key] = std::vector<std::string>({
   113  					{TString::Format("val-%03d", ii).Data()}
   114  					,{TString::Format("val-%03d", ii+1).Data()}
   115  					,{TString::Format("val-%03d", ii+2).Data()}
   116  			});
   117  			e.msvi32[key] = std::vector<int32_t>({1, ii, 3, ii});
   118  //			e.msvp3[key] = std::vector<P3>({{ii, double(ii+1), ii+2}, {ii+1, double(ii+2), ii+3}});
   119  		}
   120  
   121  		t->Fill();
   122  	}
   123  
   124  	f->Write();
   125  	f->Close();
   126  
   127  	exit(0);
   128  }
   129  `