1 |
|
---|
2 | /** \class ExRootTreeWriter
|
---|
3 | *
|
---|
4 | * Class handling output ROOT tree
|
---|
5 | *
|
---|
6 | * $Date: 2008-11-04 10:32:27 $
|
---|
7 | * $Revision: 1.1 $
|
---|
8 | *
|
---|
9 | *
|
---|
10 | * \author S. Ovyn - UCL, Louvain-la-Neuve
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 | #include "Utilities/ExRootAnalysis/interface/ExRootTreeWriter.h"
|
---|
15 | #include "Utilities/ExRootAnalysis/interface/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(const TString &fileName, const TString &treeName)
|
---|
27 | {
|
---|
28 | TDirectory *dir = gDirectory;
|
---|
29 | fNewFile = TFile::Open(fileName,"UPDATE");
|
---|
30 | fNewFile->cd();
|
---|
31 | fNewTree = new TTree(treeName, "Analysis tree");
|
---|
32 | dir->cd();
|
---|
33 |
|
---|
34 | if(!fNewTree)
|
---|
35 | {
|
---|
36 | cout << "** ERROR: cannot create tree" << endl;
|
---|
37 | return;
|
---|
38 | }
|
---|
39 |
|
---|
40 | fNewTree->SetAutoSave(10000000); // autosave when 10 MB written
|
---|
41 | }
|
---|
42 |
|
---|
43 | //------------------------------------------------------------------------------
|
---|
44 |
|
---|
45 | ExRootTreeWriter::~ExRootTreeWriter()
|
---|
46 | {
|
---|
47 | set<ExRootTreeBranch*>::iterator it_set;
|
---|
48 | for(it_set = fBranches.begin(); it_set != fBranches.end(); ++it_set)
|
---|
49 | {
|
---|
50 | delete (*it_set);
|
---|
51 | }
|
---|
52 |
|
---|
53 | if(fNewTree) delete fNewTree;
|
---|
54 | if(fNewFile) delete fNewFile;
|
---|
55 | }
|
---|
56 |
|
---|
57 | //------------------------------------------------------------------------------
|
---|
58 |
|
---|
59 | ExRootTreeBranch *ExRootTreeWriter::NewBranch(const TString &name, TClass *cl)
|
---|
60 | {
|
---|
61 | ExRootTreeBranch *branch = new ExRootTreeBranch(name, cl, fNewTree);
|
---|
62 | fBranches.insert(branch);
|
---|
63 | return branch;
|
---|
64 | }
|
---|
65 |
|
---|
66 | //------------------------------------------------------------------------------
|
---|
67 |
|
---|
68 | void ExRootTreeWriter::Fill()
|
---|
69 | {
|
---|
70 | if(fNewTree) fNewTree->Fill();
|
---|
71 | }
|
---|
72 |
|
---|
73 | //------------------------------------------------------------------------------
|
---|
74 |
|
---|
75 | void ExRootTreeWriter::Write()
|
---|
76 | {
|
---|
77 | fNewFile = fNewTree ? fNewTree->GetCurrentFile() : 0;
|
---|
78 | if(fNewFile) fNewFile->Write();
|
---|
79 | }
|
---|
80 |
|
---|
81 | //------------------------------------------------------------------------------
|
---|
82 |
|
---|
83 | void ExRootTreeWriter::Clear()
|
---|
84 | {
|
---|
85 | set<ExRootTreeBranch*>::iterator it_set;
|
---|
86 | for(it_set = fBranches.begin(); it_set != fBranches.end(); ++it_set)
|
---|
87 | {
|
---|
88 | (*it_set)->Clear();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | //------------------------------------------------------------------------------
|
---|
93 |
|
---|