StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StJetFilter.cxx
1 
2 //std
3 #include "Stiostream.h"
4 #include <cmath>
5 #include <vector>
6 using namespace std;
7 
8 //ROOT
9 #include "TFile.h"
10 #include "TTree.h"
11 
12 //UpsilonAna
13 #include "StJetMuEvent.h"
14 #include "StJetFilter.h"
15 
16 ClassImp(StJetFilter)
17 
18  StJetFilter::StJetFilter(ioType type, const char* file)
19  : mIoType(type), mEventCounter(0), mFile(0), mTree(0), mEvent(new StJetMuEvent)
20 {
21  cout <<"StJetFilter::StJetFilter()"<<endl;
22 
23  if (type==kWrite) {
24  cout <<"\tOpen file in write mode"<<endl;
25  mFile = new TFile(file,"RECREATE");
26  mFile->SetCompressionLevel(9);
27 
28  //Stole these lines from Jamie/Gans
29  mTree = new TTree("mTree","StUpsilon Tree");
30  mTree->SetBranchStyle(0);
31 
32  Int_t bufsize = 20000;
33  Int_t split = 1;
34 
35  mTree->Branch("StJetMuEvent","StJetMuEvent",&mEvent,bufsize,split);
36  }
37  else if (type==kRead) {
38  cout <<"\tOpen file in read mode"<<endl;
39  mFile = new TFile(file,"READ");
40 
41  //Retrieve the TTree from the file
42  TTree* temp = dynamic_cast<TTree*>(mFile->Get("mTree"));
43  if (!temp) {
44  cout <<"Could not recover TTree from file:\t"<<file<<" Prepare to crash"<<endl;
45  }
46  mTree=temp;
47  mTree->SetBranchAddress("StJetMuEvent",&mEvent);
48 
49  }
50  else {
51  cout <<"StJetFilter::StJetFilter(). ERROR:\t"
52  <<"Undefined ioType. Abort"<<endl;
53  }
54 }
55 
56 StJetFilter::~StJetFilter()
57 {
58  cout <<"StJetFilter::~StJetFilter()"<<endl;
59 
60  if (mIoType==kWrite) {
61  mFile->Write();
62  }
63  mFile->Close();
64 
65  /*
66  delete mFile;
67  mFile=0;
68  delete mTree;
69  mTree=0;
70  */
71  delete mEvent;
72  mEvent=0;
73 }
74 
75 int StJetFilter::nEvents() const
76 {
77  return (mTree) ? static_cast<int>(mTree->GetEntries()) : 0;
78 }
79 
80 void StJetFilter::setCuts(const AnaCuts& c)
81 {
82  mEvent->setCuts(c);
83 }
84 
85 void StJetFilter::fill(StMuDstMaker* maker)
86 {
87  //cout <<"StJetFilter::fill(StMuDstMaker*)"<<endl;
88  if (mIoType==kWrite) {
89  if (mEvent->fill(maker)) {
90  cout <<"Event Accepted. Fill TTree"<<endl;
91  mTree->Fill();
92  }
93  else {
94  cout <<"Event Rejected, write log"<<endl;
95  }
96  }
97  else if (mIoType==kRead) {
98  //cout <<"\tRead event:\t"<<mEventCounter<<endl;
99  if (mEventCounter<mTree->GetEntries()) {
100  mTree->GetEntry(mEventCounter++);
101  }
102  else {
103  cout <<"StJetFilter::fill(StMuDstMaker*). Error:\t"
104  <<"No more entries to get. No action taken"<<endl;
105  }
106  }
107  else {
108  cout <<"StJetFilter::fill(StMuDstMaker*). ERROR:\t"
109  <<"Undefined ioType. No action taken"<<endl;
110  }
111 }