Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:47:50

0001 #include "TProfile.h"
0002 #include "TThread.h"
0003 #include "TObject.h"
0004 #include "TVirtualStreamerInfo.h"
0005 #include <thread>
0006 #include <memory>
0007 #include <vector>
0008 #include <atomic>
0009 #include <sstream>
0010 #include <iostream>
0011 
0012 void printHelp(const char* iName, int iDefaultNThreads)
0013 {
0014   std::cout << iName <<" [number of threads] \n\n"
0015             <<"If no arguments are given "<<iDefaultNThreads<<" threads will be used"<<std::endl;
0016 }
0017 
0018 int parseOptionsForNumberOfThreads(int argc, char** argv)
0019 {
0020   constexpr int kDefaultNThreads = 4;
0021   int returnValue = kDefaultNThreads;
0022   if( argc == 2 ) {
0023     if(strcmp("-h",argv[1]) ==0) {
0024       printHelp(argv[0],kDefaultNThreads);
0025       exit( 0 );
0026     }
0027     
0028     returnValue = atoi(argv[1]);
0029  }
0030   
0031   if( argc > 2) {
0032     printHelp(argv[0],kDefaultNThreads);
0033     exit(1);
0034   }
0035   return returnValue ;
0036 }
0037 
0038 int main(int argc, char** argv)
0039 {
0040   const int kNThreads = parseOptionsForNumberOfThreads(argc, argv);
0041 
0042   std::atomic<bool> canStart{false};
0043   std::vector<std::unique_ptr<TProfile>> profiles;
0044   std::vector<std::thread> threads;
0045   
0046   TH1::AddDirectory(kFALSE);
0047   
0048   TThread::Initialize();
0049   //When threading, also have to keep ROOT from logging all TObjects into a list
0050   TObject::SetObjectStat(false);
0051   
0052   //Have to avoid having Streamers modify themselves after they have been used
0053   TVirtualStreamerInfo::Optimize(false);
0054 
0055   
0056   for(int i=0; i<kNThreads; ++i) {
0057     std::ostringstream s;
0058     profiles.push_back(std::make_unique<TProfile>(s.str().c_str(),s.str().c_str(), 100,10,11,0,10));
0059     profiles.back()->SetCanExtend(TH1::kAllAxes);
0060     auto profile = profiles.back().get();
0061     threads.emplace_back([profile,&canStart]() {
0062         static thread_local TThread guard;
0063         while(not canStart) {}
0064         for(int x=10; x>0; --x) {
0065           for(int y=0; y<20; ++y) {
0066             profile->Fill(double(x), double(y),1.);
0067           }
0068         }
0069       });
0070   }
0071   canStart = true;
0072 
0073   for(auto& thread: threads) {
0074     thread.join();
0075   }
0076   
0077   return 0;
0078 }