Fork me on GitHub

source: git/external/ExRootAnalysis/ExRootTreeWriter.cc@ 7b518f0

ImprovedOutputFile
Last change on this file since 7b518f0 was 341014c, checked in by Pavel Demin <pavel-demin@…>, 6 years ago

apply .clang-format to all .h, .cc and .cpp files

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[d7d2da3]1
2/** \class ExRootTreeWriter
3 *
4 * Class handling output ROOT tree
5 *
6 * \author P. Demin - UCL, Louvain-la-Neuve
7 *
8 */
9
10#include "ExRootAnalysis/ExRootTreeWriter.h"
11#include "ExRootAnalysis/ExRootTreeBranch.h"
12
[341014c]13#include "TClonesArray.h"
[d7d2da3]14#include "TFile.h"
[341014c]15#include "TROOT.h"
[d7d2da3]16#include "TTree.h"
17
18#include <iostream>
19#include <sstream>
[341014c]20#include <stdexcept>
[d7d2da3]21
22using namespace std;
23
24ExRootTreeWriter::ExRootTreeWriter(TFile *file, const char *treeName) :
25 fFile(file), fTree(0), fTreeName(treeName)
26{
27}
28
29//------------------------------------------------------------------------------
30
31ExRootTreeWriter::~ExRootTreeWriter()
32{
[341014c]33 set<ExRootTreeBranch *>::iterator itBranches;
[d7d2da3]34 for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
35 {
[341014c]36 delete(*itBranches);
[d7d2da3]37 }
38
39 if(fTree) delete fTree;
40}
41
42//------------------------------------------------------------------------------
43
44ExRootTreeBranch *ExRootTreeWriter::NewBranch(const char *name, TClass *cl)
45{
46 if(!fTree) fTree = NewTree();
47 ExRootTreeBranch *branch = new ExRootTreeBranch(name, cl, fTree);
48 fBranches.insert(branch);
49 return branch;
50}
51
52//------------------------------------------------------------------------------
53
54void ExRootTreeWriter::Fill()
55{
56 if(fTree) fTree->Fill();
57}
58
59//------------------------------------------------------------------------------
60
61void ExRootTreeWriter::Write()
62{
63 fFile = fTree ? fTree->GetCurrentFile() : 0;
64 if(fFile) fFile->Write();
65}
66
67//------------------------------------------------------------------------------
68
69void ExRootTreeWriter::Clear()
70{
[341014c]71 set<ExRootTreeBranch *>::iterator itBranches;
[d7d2da3]72 for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
73 {
74 (*itBranches)->Clear();
75 }
76}
77
78//------------------------------------------------------------------------------
79
80TTree *ExRootTreeWriter::NewTree()
81{
82 if(!fFile) return 0;
83
84 TTree *tree = 0;
85 TDirectory *dir = gDirectory;
86
87 fFile->cd();
88 tree = new TTree(fTreeName, "Analysis tree");
89 dir->cd();
90
91 if(!tree)
92 {
93 throw runtime_error("can't create output ROOT tree");
94 }
95
96 tree->SetDirectory(fFile);
[341014c]97 tree->SetAutoSave(10000000); // autosave when 10 MB written
[d7d2da3]98
99 return tree;
100}
Note: See TracBrowser for help on using the repository browser.