Guest User

Untitled

a guest
Dec 9th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include "TFile.h"
  2. #include "TRandom.h"
  3. #include "TFile.h"
  4. #include "TTree.h"
  5. #include "TObjString.h"
  6. #include "TObject.h"
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <functional>
  11. #include <cmath>
  12. #include <thread>
  13. #include <mutex>
  14.  
  15. class TreeStructure {
  16.  
  17. public:
  18.  
  19. void clear() {
  20. etaVec.clear();
  21. phiVec.clear();
  22. ptVec.clear();
  23. }
  24.  
  25. std::vector<float> etaVec;
  26. std::vector<float> phiVec;
  27. std::vector<float> ptVec;
  28. };
  29.  
  30. using StringAttributes = std::vector<std::pair<const char *, std::string>>;
  31.  
  32. class Cache {
  33.  
  34. public:
  35.  
  36. Cache(const std::string filename, const StringAttributes& attributes)
  37. : file_(TFile::Open((filename + ".root").c_str(), "RECREATE"))
  38. , tree_(new TTree("tree", "tree"))
  39. {
  40. tree_->Branch("eta", &ts_.etaVec);
  41. tree_->Branch("phi", &ts_.phiVec);
  42. tree_->Branch("pt" , &ts_.ptVec);
  43.  
  44. for(auto const& attr : attributes) {
  45. TObjString* s = new TObjString(attr.second.c_str());
  46. s->Write(attr.first);
  47. }
  48.  
  49. // Try to write an integer to the Root file
  50. TObjString* n = new TObjString(std::to_string(7).c_str());
  51. n->Write("N_EVENTS");
  52. }
  53.  
  54. ~Cache() {
  55.  
  56. tree_->Print();
  57. tree_->Write();
  58.  
  59. delete file_;
  60. }
  61.  
  62. void fill(TreeStructure && ts, std::vector<TreeStructure>& buffer) {
  63. buffer.push_back(std::move(ts));
  64. if (buffer.size() > maxBufferSize_) write(buffer);
  65. }
  66.  
  67. void write(std::vector<TreeStructure>& buffer) {
  68.  
  69. std::lock_guard<std::mutex> lock(mutex_);
  70.  
  71. size_t n = buffer.size();
  72. for(auto& entry : buffer) {
  73. ts_ = std::move(entry);
  74. tree_->Fill();
  75. }
  76. if(n > 0) buffer.clear();
  77. }
  78.  
  79. private:
  80.  
  81. TFile * const file_;
  82. TTree * const tree_;
  83.  
  84. mutable std::mutex mutex_;
  85.  
  86. TreeStructure ts_;
  87.  
  88. const size_t maxBufferSize_ = 512;
  89. };
  90.  
  91.  
  92. void fillEvents(int nEvents, Cache* cache)
  93. {
  94. TRandom *eventGenerator = new TRandom();
  95. std::vector<TreeStructure> buffer;
  96.  
  97. for(int iEvent = 0; iEvent < nEvents; ++iEvent)
  98. {
  99. TreeStructure ts;
  100.  
  101. int nLeptons = 4 + eventGenerator->Integer(4);
  102. for(int iLepton = 0; iLepton < nLeptons; ++iLepton)
  103. {
  104. ts.phiVec.push_back(eventGenerator->Uniform() * 2*M_PI - M_PI);
  105. ts.etaVec.push_back(eventGenerator->Uniform() * 5.0 - 2.5 );
  106. ts.ptVec .push_back(eventGenerator->Exp(10) + 20. );
  107. }
  108. cache->fill(std::move(ts), buffer);
  109. }
  110.  
  111. cache->write(buffer);
  112. }
  113.  
  114. int main()
  115. {
  116. int nEvents = 100000;
  117. int nThreads = 8;
  118.  
  119. auto cache = std::make_shared<Cache>("data", StringAttributes{
  120. {"jonas", "idiot"}
  121. });
  122.  
  123. auto fillEventsThreaded = std::bind(fillEvents, nEvents/nThreads, cache.get());
  124.  
  125. std::vector<std::thread> threads;
  126.  
  127. for(int i = 0; i < nThreads; ++i) threads.emplace_back(fillEventsThreaded);
  128. for(auto& thread : threads) thread.join();
  129. }
Add Comment
Please, Sign In to add comment