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