Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-18 02:29:47

0001 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 #include "FWCore/ServiceRegistry/interface/Service.h"
0004 #include "TH1.h"
0005 #include "TTree.h"
0006 class TestTFileServiceAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0007 public:
0008   /// constructor
0009   TestTFileServiceAnalyzer(const edm::ParameterSet&);
0010 
0011 private:
0012   /// process one event
0013   void analyze(const edm::Event&, const edm::EventSetup&) override;
0014 
0015   /// histograms
0016   TH1F *h_test1, *h_test2;
0017   /// TTree
0018   TTree* tree_test;
0019   /// entry for TTree test
0020   int testInt;
0021   /// sub-directory name
0022   std::string dir1_, dir2_;
0023 };
0024 
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 using namespace edm;
0027 using namespace std;
0028 
0029 TestTFileServiceAnalyzer::TestTFileServiceAnalyzer(const ParameterSet& cfg)
0030     : dir1_(cfg.getParameter<string>("dir1")), dir2_(cfg.getParameter<string>("dir2")) {
0031   usesResource(TFileService::kSharedResource);
0032   edm::Service<TFileService> fs;
0033   if (dir1_.empty()) {
0034     h_test1 = fs->make<TH1F>("test1", "test histogram #1", 100, 0., 100.);
0035   } else {
0036     TFileDirectory dir1 = fs->mkdir(dir1_);
0037     h_test1 = dir1.make<TH1F>("test1", "test histogram #1", 100, 0., 100.);
0038   }
0039   if (dir2_.empty()) {
0040     h_test2 = fs->make<TH1F>("test2", "test histogram #2", 100, 0., 100.);
0041   } else {
0042     TFileDirectory dir2 = fs->mkdir(dir2_);
0043     h_test2 = dir2.make<TH1F>("test2", "test histogram #2", 100, 0., 100.);
0044   }
0045   tree_test = fs->make<TTree>("Test", "Test Tree", 1);
0046   tree_test->Branch("TestBranch", &testInt, "testInt/I");
0047 }
0048 
0049 void TestTFileServiceAnalyzer::analyze(const Event& evt, const EventSetup&) {
0050   h_test1->Fill(50.);
0051   h_test2->Fill(60.);
0052   // fill test TTree
0053   testInt = 70;
0054   tree_test->Fill();
0055 }
0056 
0057 #include "FWCore/Framework/interface/MakerMacros.h"
0058 
0059 DEFINE_FWK_MODULE(TestTFileServiceAnalyzer);