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 | void ExRootTreeWriter::Write()
|
---|
62 | {
|
---|
63 | fFile = fTree ? fTree->GetCurrentFile() : 0;
|
---|
64 | if(fFile) fFile->Write();
|
---|
65 | }
|
---|
66 |
|
---|
67 | //------------------------------------------------------------------------------
|
---|
68 |
|
---|
69 | void ExRootTreeWriter::Clear()
|
---|
70 | {
|
---|
71 | set<ExRootTreeBranch*>::iterator itBranches;
|
---|
72 | for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
|
---|
73 | {
|
---|
74 | (*itBranches)->Clear();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | //------------------------------------------------------------------------------
|
---|
79 |
|
---|
80 | TTree *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);
|
---|
97 | tree->SetAutoSave(10000000); // autosave when 10 MB written
|
---|
98 |
|
---|
99 | return tree;
|
---|
100 | }
|
---|