[d7d2da3] | 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 |
|
---|
[341014c] | 13 | #include "TClonesArray.h"
|
---|
[d7d2da3] | 14 | #include "TFile.h"
|
---|
[341014c] | 15 | #include "TROOT.h"
|
---|
[d7d2da3] | 16 | #include "TTree.h"
|
---|
| 17 |
|
---|
| 18 | #include <iostream>
|
---|
| 19 | #include <sstream>
|
---|
[341014c] | 20 | #include <stdexcept>
|
---|
[d7d2da3] | 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 | {
|
---|
[341014c] | 33 | set<ExRootTreeBranch *>::iterator itBranches;
|
---|
[d7d2da3] | 34 | for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
|
---|
| 35 | {
|
---|
[341014c] | 36 | delete(*itBranches);
|
---|
[d7d2da3] | 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 | {
|
---|
[341014c] | 71 | set<ExRootTreeBranch *>::iterator itBranches;
|
---|
[d7d2da3] | 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);
|
---|
[341014c] | 97 | tree->SetAutoSave(10000000); // autosave when 10 MB written
|
---|
[d7d2da3] | 98 |
|
---|
| 99 | return tree;
|
---|
| 100 | }
|
---|