TestTFileServiceAnalyzer

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "TH1.h"
#include "TTree.h"
class TestTFileServiceAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
public:
  /// constructor
  TestTFileServiceAnalyzer(const edm::ParameterSet&);

private:
  /// process one event
  void analyze(const edm::Event&, const edm::EventSetup&) override;

  /// histograms
  TH1F *h_test1, *h_test2;
  /// TTree
  TTree* tree_test;
  /// entry for TTree test
  int testInt;
  /// sub-directory name
  std::string dir1_, dir2_;
};

#include "FWCore/ParameterSet/interface/ParameterSet.h"
using namespace edm;
using namespace std;

TestTFileServiceAnalyzer::TestTFileServiceAnalyzer(const ParameterSet& cfg)
    : dir1_(cfg.getParameter<string>("dir1")), dir2_(cfg.getParameter<string>("dir2")) {
  usesResource(TFileService::kSharedResource);
  edm::Service<TFileService> fs;
  if (dir1_.empty()) {
    h_test1 = fs->make<TH1F>("test1", "test histogram #1", 100, 0., 100.);
  } else {
    TFileDirectory dir1 = fs->mkdir(dir1_);
    h_test1 = dir1.make<TH1F>("test1", "test histogram #1", 100, 0., 100.);
  }
  if (dir2_.empty()) {
    h_test2 = fs->make<TH1F>("test2", "test histogram #2", 100, 0., 100.);
  } else {
    TFileDirectory dir2 = fs->mkdir(dir2_);
    h_test2 = dir2.make<TH1F>("test2", "test histogram #2", 100, 0., 100.);
  }
  tree_test = fs->make<TTree>("Test", "Test Tree", 1);
  tree_test->Branch("TestBranch", &testInt, "testInt/I");
}

void TestTFileServiceAnalyzer::analyze(const Event& evt, const EventSetup&) {
  h_test1->Fill(50.);
  h_test2->Fill(60.);
  // fill test TTree
  testInt = 70;
  tree_test->Fill();
}

#include "FWCore/Framework/interface/MakerMacros.h"

DEFINE_FWK_MODULE(TestTFileServiceAnalyzer);