ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Util/src/filestuff.cc
Revision: 1.2
Committed: Mon Dec 17 17:23:07 2012 UTC (12 years, 5 months ago) by dkralph
Content type: text/plain
Branch: MAIN
CVS Tags: compiled
Changes since 1.1: +7 -15 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 dkralph 1.1 #include "filestuff.h"
2     //----------------------------------------------------------------------------------------
3     filestuff::filestuff(TString name, TString fname, TString dataset, bool isdata, int era, bool openFile)
4     {
5     name_ = name;
6     fname_ = fname;
7     dataset_ = dataset;
8     isdata_ = isdata;
9     era_ = era;
10    
11     TString txtname(fname_);
12     ifstream ifs(txtname.ReplaceAll(".root",".nevents"));
13     if(ifs.is_open())
14     ifs >> total_entries_;
15     else {
16     total_entries_ = 0;
17     cout << dataset_ << "\t" << total_entries_ << endl;
18     if(!isdata_) cerr << "WARNING: total entries not found for MC file: " << txtname << endl;
19     }
20     ifs.close();
21    
22     if(openFile) {
23     hasfo_=false;
24     haszz_=false;
25     TFile tmpFile(fname_);
26     if(tmpFile.Get("FOtree")) hasfo_ = true;
27     if(tmpFile.Get("zznt")) haszz_ = true;
28     tmpFile.Close();
29    
30 dkralph 1.2 hasJets_ = false;
31 dkralph 1.1 if(hasfo_) {
32     fo_chain_ = new TChain("fo_"+name_,"fo_"+name_);
33     fo_chain_->AddFile(fname_,-1,"FOtree");
34     failingL = new vector<SimpleLepton> ();
35     passingL = new vector<SimpleLepton> ();
36     fo_chain_->SetBranchAddress("FOs", &failingL);
37     fo_chain_->SetBranchAddress("Zleptons", &passingL);
38     if(fo_chain_->GetBranch("jets")) {
39 dkralph 1.2 jets = new vector<SimpleLepton> ();
40 dkralph 1.1 hasJets_ = true;
41     fo_chain_->SetBranchAddress("jets", &jets);
42     }
43     }
44    
45     if(haszz_) {
46     zz_chain_ = new TChain("zz_"+name_,"zz_"+name_);
47     zz_chain_->AddFile(fname_,-1,"zznt");
48     angles = new Angles;
49     kine = new KinematicsStruct;
50     info = new InfoStruct;
51     gen = new GenInfoStruct;
52     weights = new WeightStruct;
53     ji = new JetInfoStruct;
54     zz_chain_->SetBranchAddress("angles",angles);
55     zz_chain_->SetBranchAddress("kinematics",kine);
56     zz_chain_->SetBranchAddress("info",info);
57     if(zz_chain_->GetBranch("geninfo"))
58     zz_chain_->SetBranchAddress("geninfo",gen);
59     if(zz_chain_->GetBranch("weights"))
60     zz_chain_->SetBranchAddress("weights",weights);
61     if(zz_chain_->GetBranch("jetinfo")) {
62     hasJi_ = true;
63     zz_chain_->SetBranchAddress("jetinfo",ji);
64     } else hasJi_ = false;
65 dkralph 1.2
66     vk = new varkeepter("DataStruct/data/jetAngleVars.txt","r","",0,"zznt",zz_chain_);
67 dkralph 1.1 }
68     }
69     }
70     //----------------------------------------------------------------------------------------
71     void filestuff::del()
72     {
73     if(fo_chain_) {
74     delete fo_chain_;
75     delete zz_chain_;
76     delete angles;
77     delete kine;
78     delete info;
79     delete gen;
80     delete weights;
81     delete failingL;
82     delete passingL;
83     delete jets;
84     }
85     }
86     //----------------------------------------------------------------------------------------
87     void filestuff::initRlrm(TString jsonFile)
88     {
89     ifstream ifs(jsonFile);
90     assert(ifs.is_open());
91     ifs.close();
92     rlrm_.AddJSONFile(jsonFile.Data());
93     }
94     //----------------------------------------------------------------------------------------
95     void filestuff::getentry(unsigned ientry, TString brname, TString treename) {
96     if(brname=="") {
97 dkralph 1.2 if(treename=="FOtree" || treename=="") fo_chain_->GetEntry(ientry); // WARNING: does not get all the fo branches, oddly...
98 dkralph 1.1 if(treename=="zznt" || treename=="") zz_chain_->GetEntry(ientry);
99     } else {
100     if(treename=="FOtree" || treename=="") fo_chain_->GetBranch(brname)->GetEntry(ientry);
101     if(treename=="zznt" || treename=="") zz_chain_->GetBranch(brname)->GetEntry(ientry);
102     }
103     }
104     //----------------------------------------------------------------------------------------
105     unsigned filestuff::getentries(TString treename)
106     {
107     if(treename=="FOtree") return fo_chain_->GetEntries();
108     else if(treename=="zznt") return zz_chain_->GetEntries();
109     else assert(0);
110     return -1;
111     }
112     //----------------------------------------------------------------------------------------
113     void filestuff::makeOutputFile(TString fname, bool makeFoTree)
114     {
115     outtuple = new AngleTuple( (const char*)fname.Data(), (const char*)"zznt");
116     outtuple->makeAngleBranch(*angles);
117     outtuple->makeKinematicsBranch(*kine);
118     outtuple->makeInfoBranch(*info);
119     outtuple->makeGenInfoBranch(*gen);
120     outtuple->makeWeightBranch(*weights);
121     if(hasJi_) outtuple->makeJetInfoBranch(*ji);
122 dkralph 1.2 outtuple->makeJetAngleBranches("DataStruct/data/jetAngleVars.txt", &vk->int_map_, &vk->float_map_);
123 dkralph 1.1
124     if(makeFoTree) {
125     outFotuple = new FOTuple( outtuple->getFile(), (const char*)"FOtree");
126     outFotuple->makeInfoBranch(*info);
127     outFotuple->makeFailedLeptonBranch(*failingL);
128     outFotuple->makePassedLeptonBranch(*passingL);
129 dkralph 1.2 // if(hasJets_) outFotuple->makeJetBranch(*jets);
130 dkralph 1.1 }
131     }
132     //----------------------------------------------------------------------------------------
133     void filestuff::initCounters(TString fname)
134     {
135     if(fname=="") { // called if ctrl.writessofratio
136     counters_["OS"] = double(0);
137     counters_["SS"] = double(0);
138     counters_["OS_4e"] = double(0);
139     counters_["OS_4m"] = double(0);
140     counters_["OS_2e2m"] = double(0);
141     counters_["SS_4e"] = double(0);
142     counters_["SS_4m"] = double(0);
143     counters_["SS_2e2m"] = double(0);
144     } else {
145     ifstream ifs(fname);
146     assert(ifs.is_open());
147     string line;
148     cout << "Reading in ssof counters: " << fname << endl;
149     while(getline(ifs,line)) {
150     stringstream ss(line);
151     TString name;
152     double count;
153     ss >> name >> count;
154     cout << " " << setw(9) << name << " " << fixed << setprecision(2) << count << endl;
155     assert(count==count);
156     assert(count!=0);
157     counters_[name] = double(count);
158     }
159     counters_["ratio"] = counters_["OS"] / counters_["SS"]; assert(counters_["ratio"] > 0.2 && counters_["ratio"] < 3);
160     counters_["ratio_4e"] = counters_["OS_4e"] / counters_["SS_4e"]; assert(counters_["ratio_4e"] > 0.2 && counters_["ratio_4e"] < 3);
161     counters_["ratio_4m"] = counters_["OS_4m"] / counters_["SS_4m"]; assert(counters_["ratio_4m"] > 0.2 && counters_["ratio_4m"] < 3);
162     counters_["ratio_2e2m"] = counters_["OS_2e2m"] / counters_["SS_2e2m"]; assert(counters_["ratio_2e2m"] > 0.2 && counters_["ratio_2e2m"] < 3);
163     }
164     }
165     void filestuff::writeSsofRatios()
166     {
167     TString ssofname(fname_);
168     ssofname.ReplaceAll(".root","-ssof.txt");
169     ofstream ofs_ssof(ssofname);
170     cout << "\nWriting ssof counters: " << endl;
171     for(map<TString,double>::iterator cit=counters_.begin(); cit!=counters_.end(); cit++) {
172     ofs_ssof << left << setw(9) << (*cit).first << (*cit).second << endl;
173     cout << "\t\t\t" << left << setw(8) << (*cit).first << fixed << setprecision(2) << (*cit).second << endl;
174     }
175     ofs_ssof.close();
176     }