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 |
|
---|
13 | #include "TROOT.h"
|
---|
14 | #include "TFile.h"
|
---|
15 | #include "TTree.h"
|
---|
16 | #include "TClonesArray.h"
|
---|
17 |
|
---|
18 | #include <iostream>
|
---|
19 | #include <stdexcept>
|
---|
20 | #include <sstream>
|
---|
21 |
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 | ExRootTreeWriter::ExRootTreeWriter(TFile *file, const char *treeName) :
|
---|
25 | fFile(file), fTree(0), fTreeName(treeName)
|
---|
26 | {
|
---|
27 | }
|
---|
28 |
|
---|
29 | //------------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | ExRootTreeWriter::~ExRootTreeWriter()
|
---|
32 | {
|
---|
33 | set<ExRootTreeBranch*>::iterator itBranches;
|
---|
34 | for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
|
---|
35 | {
|
---|
36 | delete (*itBranches);
|
---|
37 | }
|
---|
38 |
|
---|
39 | if(fTree) delete fTree;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //------------------------------------------------------------------------------
|
---|
43 |
|
---|
44 | ExRootTreeBranch *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 |
|
---|
54 | void ExRootTreeWriter::Fill()
|
---|
55 | {
|
---|
56 | if(fTree) fTree->Fill();
|
---|
57 | }
|
---|
58 |
|
---|
59 | //------------------------------------------------------------------------------
|
---|
60 |
|
---|
61 | Int_t ExRootTreeWriter::Write(const char *name, Int_t option, Int_t bufsize) const
|
---|
62 | {
|
---|
63 | if (fTree != 0) {
|
---|
64 | return fTree->GetCurrentFile()->Write(name, option, bufsize);
|
---|
65 | }
|
---|
66 | return 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | //------------------------------------------------------------------------------
|
---|
70 |
|
---|
71 | Int_t ExRootTreeWriter::Write(const char *name, Int_t option, Int_t bufsize)
|
---|
72 | {
|
---|
73 | // forward call to const version of this function
|
---|
74 | return static_cast<const ExRootTreeWriter*>(this)->Write(name, option, bufsize);
|
---|
75 | }
|
---|
76 |
|
---|
77 | //------------------------------------------------------------------------------
|
---|
78 |
|
---|
79 | void ExRootTreeWriter::Clear(Option_t *option)
|
---|
80 | {
|
---|
81 | set<ExRootTreeBranch*>::iterator itBranches;
|
---|
82 | for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
|
---|
83 | {
|
---|
84 | (*itBranches)->Clear(option);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | //------------------------------------------------------------------------------
|
---|
89 |
|
---|
90 | TTree *ExRootTreeWriter::NewTree()
|
---|
91 | {
|
---|
92 | if(!fFile) return 0;
|
---|
93 |
|
---|
94 | TTree *tree = 0;
|
---|
95 | TDirectory *dir = gDirectory;
|
---|
96 |
|
---|
97 | fFile->cd();
|
---|
98 | tree = new TTree(fTreeName, "Analysis tree");
|
---|
99 | dir->cd();
|
---|
100 |
|
---|
101 | if(!tree)
|
---|
102 | {
|
---|
103 | throw runtime_error("can't create output ROOT tree");
|
---|
104 | }
|
---|
105 |
|
---|
106 | tree->SetDirectory(fFile);
|
---|
107 | tree->SetAutoSave(10000000); // autosave when 10 MB written
|
---|
108 |
|
---|
109 | return tree;
|
---|
110 | }
|
---|