example1.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * A short example demonstrating some of the basic functionality of the FilVal
  33. * Value system.
  34. */
  35. #include <iostream>
  36. #include <vector>
  37. #include <TSystem.h>
  38. #include <dataset.hpp>
  39. #include <TROOT.h>
  40. #include <TChain.h>
  41. #include <filval.hpp>
  42. #include <root_filval.hpp>
  43. using namespace std;
  44. class TrackingNtuple {
  45. public :
  46. TTree *fChain; //!pointer to the analyzed TTree or TChain
  47. Int_t fCurrent; //!current Tree number in a TChain
  48. vector<float> *trk_px;
  49. vector<float> *trk_py;
  50. vector<float> *trk_pz;
  51. // List of branches
  52. TBranch *b_trk_px; //!
  53. TBranch *b_trk_py; //!
  54. TBranch *b_trk_pz; //!
  55. TrackingNtuple(TTree *tree=0);
  56. virtual ~TrackingNtuple();
  57. virtual Int_t GetEntry(Long64_t entry);
  58. virtual Long64_t LoadTree(Long64_t entry);
  59. virtual void Init(TTree *tree);
  60. };
  61. TrackingNtuple::TrackingNtuple(TTree *tree) : fChain(0) {
  62. // if parameter tree is not specified (or zero), connect the file
  63. // used to generate this class and read the Tree.
  64. if (tree == 0) {
  65. TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("trackingNtuple_1.root");
  66. if (!f || !f->IsOpen()) {
  67. f = new TFile("trackingNtuple_1.root");
  68. }
  69. TDirectory * dir = (TDirectory*)f->Get("trackingNtuple_1.root:/trackingNtuple");
  70. dir->GetObject("tree",tree);
  71. }
  72. Init(tree);
  73. }
  74. TrackingNtuple::~TrackingNtuple() {
  75. if (!fChain) return;
  76. delete fChain->GetCurrentFile();
  77. }
  78. Int_t TrackingNtuple::GetEntry(Long64_t entry) {
  79. // Read contents of entry.
  80. if (!fChain) return 0;
  81. return fChain->GetEntry(entry);
  82. }
  83. Long64_t TrackingNtuple::LoadTree(Long64_t entry) {
  84. // Set the environment to read one entry
  85. if (!fChain) return -5;
  86. Long64_t centry = fChain->LoadTree(entry);
  87. if (centry < 0) return centry;
  88. if (fChain->GetTreeNumber() != fCurrent) {
  89. fCurrent = fChain->GetTreeNumber();
  90. }
  91. return centry;
  92. }
  93. void TrackingNtuple::Init(TTree *tree) {
  94. // Set object pointer
  95. trk_px = 0;
  96. trk_py = 0;
  97. trk_pz = 0;
  98. // Set branch addresses and branch pointers
  99. if (!tree) return;
  100. fChain = tree;
  101. fCurrent = -1;
  102. fChain->SetMakeClass(1);
  103. fChain->SetBranchAddress("trk_px", &trk_px, &b_trk_px);
  104. fChain->SetBranchAddress("trk_py", &trk_py, &b_trk_py);
  105. fChain->SetBranchAddress("trk_pz", &trk_pz, &b_trk_pz);
  106. }
  107. void run(bool silent) {
  108. using namespace fv;
  109. using namespace fv_root;
  110. auto file_list = the_config->get_source_files();
  111. string output_filename = the_config->get_output_filename();
  112. TreeDataSet<TrackingNtuple> tds(output_filename, file_list, "trackingNtuple/tree");
  113. auto& px = *tds.track_branch_obj<vector<float>>("trk_py");
  114. auto& cont = *tds.register_container<ContainerTH1<int>>("trk_py", "Track Px", TH1Params::lookup("track_px"));
  115. while(tds.next(silent)) {
  116. for (const float& p : px()) {
  117. cont.fill(p);
  118. }
  119. }
  120. tds.save_all();
  121. }
  122. int main(int argc, char* argv[]){
  123. using namespace fv_util;
  124. ArgParser args(argc, argv);
  125. bool silent = args.cmdOptionExists("-s");
  126. if(args.cmdOptionExists("-c")) {
  127. init_config(args.getCmdOption("-c"));
  128. init_log(fv_util::LogPriority::kLogInfo);
  129. // gSystem->Load("libfilval.so");
  130. run(silent);
  131. } else {
  132. cout << "Usage: ./" << argv[0] << " (-s) -c config_file.yaml" << endl;
  133. }
  134. return 0;
  135. }