[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 |
|
---|
[dc883b4] | 13 | #include "TParameter.h"
|
---|
[341014c] | 14 | #include "TClonesArray.h"
|
---|
[d7d2da3] | 15 | #include "TFile.h"
|
---|
[341014c] | 16 | #include "TROOT.h"
|
---|
[d7d2da3] | 17 | #include "TTree.h"
|
---|
| 18 |
|
---|
| 19 | #include <iostream>
|
---|
| 20 | #include <sstream>
|
---|
[341014c] | 21 | #include <stdexcept>
|
---|
[d7d2da3] | 22 |
|
---|
| 23 | using namespace std;
|
---|
| 24 |
|
---|
| 25 | ExRootTreeWriter::ExRootTreeWriter(TFile *file, const char *treeName) :
|
---|
| 26 | fFile(file), fTree(0), fTreeName(treeName)
|
---|
| 27 | {
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | //------------------------------------------------------------------------------
|
---|
| 31 |
|
---|
| 32 | ExRootTreeWriter::~ExRootTreeWriter()
|
---|
| 33 | {
|
---|
[341014c] | 34 | set<ExRootTreeBranch *>::iterator itBranches;
|
---|
[d7d2da3] | 35 | for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
|
---|
| 36 | {
|
---|
[341014c] | 37 | delete(*itBranches);
|
---|
[d7d2da3] | 38 | }
|
---|
| 39 |
|
---|
| 40 | if(fTree) delete fTree;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | //------------------------------------------------------------------------------
|
---|
| 44 |
|
---|
| 45 | ExRootTreeBranch *ExRootTreeWriter::NewBranch(const char *name, TClass *cl)
|
---|
| 46 | {
|
---|
| 47 | if(!fTree) fTree = NewTree();
|
---|
| 48 | ExRootTreeBranch *branch = new ExRootTreeBranch(name, cl, fTree);
|
---|
| 49 | fBranches.insert(branch);
|
---|
| 50 | return branch;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | //------------------------------------------------------------------------------
|
---|
[dc883b4] | 54 |
|
---|
| 55 | void ExRootTreeWriter::AddInfo(const char *name, Double_t value)
|
---|
| 56 | {
|
---|
| 57 | if(!fTree) fTree = NewTree();
|
---|
| 58 | fTree->GetUserInfo()->Add(new TParameter<Double_t>(name, value));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | //------------------------------------------------------------------------------
|
---|
[d7d2da3] | 62 |
|
---|
| 63 | void ExRootTreeWriter::Fill()
|
---|
| 64 | {
|
---|
| 65 | if(fTree) fTree->Fill();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | //------------------------------------------------------------------------------
|
---|
| 69 |
|
---|
| 70 | void ExRootTreeWriter::Write()
|
---|
| 71 | {
|
---|
| 72 | fFile = fTree ? fTree->GetCurrentFile() : 0;
|
---|
| 73 | if(fFile) fFile->Write();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | //------------------------------------------------------------------------------
|
---|
| 77 |
|
---|
| 78 | void ExRootTreeWriter::Clear()
|
---|
| 79 | {
|
---|
[341014c] | 80 | set<ExRootTreeBranch *>::iterator itBranches;
|
---|
[d7d2da3] | 81 | for(itBranches = fBranches.begin(); itBranches != fBranches.end(); ++itBranches)
|
---|
| 82 | {
|
---|
| 83 | (*itBranches)->Clear();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | //------------------------------------------------------------------------------
|
---|
| 88 |
|
---|
| 89 | TTree *ExRootTreeWriter::NewTree()
|
---|
| 90 | {
|
---|
| 91 | if(!fFile) return 0;
|
---|
| 92 |
|
---|
| 93 | TTree *tree = 0;
|
---|
| 94 | TDirectory *dir = gDirectory;
|
---|
| 95 |
|
---|
| 96 | fFile->cd();
|
---|
| 97 | tree = new TTree(fTreeName, "Analysis tree");
|
---|
| 98 | dir->cd();
|
---|
| 99 |
|
---|
| 100 | if(!tree)
|
---|
| 101 | {
|
---|
| 102 | throw runtime_error("can't create output ROOT tree");
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | tree->SetDirectory(fFile);
|
---|
[341014c] | 106 | tree->SetAutoSave(10000000); // autosave when 10 MB written
|
---|
[d7d2da3] | 107 |
|
---|
| 108 | return tree;
|
---|
| 109 | }
|
---|