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 |
|
---|
24 | using namespace std;
|
---|
25 |
|
---|
26 | ExRootTreeWriter::ExRootTreeWriter(TFile *file, const char *treeName) :
|
---|
27 | fFile(file), fTree(0), fTreeName(treeName)
|
---|
28 | {
|
---|
29 | }
|
---|
30 |
|
---|
31 | //------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | ExRootTreeWriter::~ExRootTreeWriter()
|
---|
34 | {
|
---|
35 | set<ExRootTreeBranch*>::iterator it_set;
|
---|
36 | for(it_set = fBranches.begin(); it_set != fBranches.end(); ++it_set)
|
---|
37 | {
|
---|
38 | delete (*it_set);
|
---|
39 | }
|
---|
40 |
|
---|
41 | if(fTree) delete fTree;
|
---|
42 | }
|
---|
43 |
|
---|
44 | //------------------------------------------------------------------------------
|
---|
45 |
|
---|
46 | ExRootTreeBranch *ExRootTreeWriter::NewBranch(const char *name, TClass *cl)
|
---|
47 | {
|
---|
48 | if(!fTree) fTree = NewTree();
|
---|
49 | ExRootTreeBranch *branch = new ExRootTreeBranch(name, cl, fTree);
|
---|
50 | fBranches.insert(branch);
|
---|
51 | return branch;
|
---|
52 | }
|
---|
53 |
|
---|
54 | //------------------------------------------------------------------------------
|
---|
55 |
|
---|
56 | ExRootTreeBranch *ExRootTreeWriter::NewFactory(const char *name, TClass *cl)
|
---|
57 | {
|
---|
58 | ExRootTreeBranch *branch = new ExRootTreeBranch(name, cl, 0);
|
---|
59 | fBranches.insert(branch);
|
---|
60 | return branch;
|
---|
61 | }
|
---|
62 |
|
---|
63 | //------------------------------------------------------------------------------
|
---|
64 |
|
---|
65 | void ExRootTreeWriter::Fill()
|
---|
66 | {
|
---|
67 | if(fTree) fTree->Fill();
|
---|
68 | }
|
---|
69 |
|
---|
70 | //------------------------------------------------------------------------------
|
---|
71 |
|
---|
72 | void ExRootTreeWriter::Write()
|
---|
73 | {
|
---|
74 | fFile = fTree ? fTree->GetCurrentFile() : 0;
|
---|
75 | if(fFile) fFile->Write();
|
---|
76 | }
|
---|
77 |
|
---|
78 | //------------------------------------------------------------------------------
|
---|
79 |
|
---|
80 | void ExRootTreeWriter::Clear()
|
---|
81 | {
|
---|
82 | set<ExRootTreeBranch*>::iterator it_set;
|
---|
83 | for(it_set = fBranches.begin(); it_set != fBranches.end(); ++it_set)
|
---|
84 | {
|
---|
85 | (*it_set)->Clear();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | //------------------------------------------------------------------------------
|
---|
90 |
|
---|
91 | TTree *ExRootTreeWriter::NewTree()
|
---|
92 | {
|
---|
93 | if(!fFile) return 0;
|
---|
94 |
|
---|
95 | TTree *tree = 0;
|
---|
96 | TDirectory *dir = gDirectory;
|
---|
97 |
|
---|
98 | fFile->cd();
|
---|
99 | tree = new TTree(fTreeName, "Analysis tree");
|
---|
100 | dir->cd();
|
---|
101 |
|
---|
102 | if(!tree)
|
---|
103 | {
|
---|
104 | cout << "** ERROR: cannot create tree" << endl;
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | tree->SetDirectory(fFile);
|
---|
109 | tree->SetAutoSave(10000000); // autosave when 10 MB written
|
---|
110 |
|
---|
111 | return tree;
|
---|
112 | }
|
---|