1 |
anderson |
1.1 |
/************************************************************
|
2 |
|
|
|
3 |
|
|
This will SetWeight to TTree's in given files. This
|
4 |
|
|
Allows for them to be TChain'ed together with correct
|
5 |
|
|
cross sections.
|
6 |
|
|
|
7 |
|
|
Example use of this command:
|
8 |
|
|
root [0] .x setTreeWeights.C("filelist.txt","TreePhotonAndJet")
|
9 |
|
|
|
10 |
|
|
Format the list of file names with weights like so:
|
11 |
|
|
|
12 |
|
|
QCDEnrichedpt20-30.root 175.0
|
13 |
|
|
QCDEnrichedpt30-50.root 105.2
|
14 |
|
|
QCDEnrichedpt50-80.root 80.1
|
15 |
|
|
QCDEnrichedpt80-120.root 53.7
|
16 |
|
|
QCDEnrichedpt120-170.root 25.4
|
17 |
|
|
QCDEnrichedpt170-300.root 3.2
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
After running the command you can TChain things together:
|
21 |
|
|
TChain *whatever = new TChain("TreePhotonAndJet");
|
22 |
|
|
whatever->Add("*.root")
|
23 |
|
|
whatever->Draw("genJet1.et","genJet1.et>0&&genJet1.et<200")
|
24 |
|
|
|
25 |
|
|
Michael B. Anderson
|
26 |
|
|
June 10, 2008
|
27 |
|
|
************************************************************/
|
28 |
|
|
|
29 |
|
|
#include "vector.h"
|
30 |
|
|
#include "Riostream.h"
|
31 |
|
|
|
32 |
|
|
void setTreeWeights(TString filelistName, TString treeName) {
|
33 |
|
|
|
34 |
|
|
ifstream fileToReadFrom;
|
35 |
|
|
|
36 |
|
|
cout << "Reading filenames and weights from " << filelistName << endl;
|
37 |
|
|
fileToReadFrom.open(filelistName);
|
38 |
|
|
|
39 |
|
|
// Make sure the file is ok to read from
|
40 |
|
|
if ( !fileToReadFrom.good() ) {
|
41 |
|
|
cout << "ERROR reading from file " << filelistName << endl;
|
42 |
|
|
return;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
vector<TString> filename;
|
46 |
|
|
vector<Float_t> weight;
|
47 |
|
|
|
48 |
|
|
//************************************************************
|
49 |
|
|
// Read from the text file to
|
50 |
|
|
// figure out file names and weights
|
51 |
|
|
TString currentFilename;
|
52 |
|
|
Float_t currentWeight;
|
53 |
|
|
Int_t numberOfFiles = 0;
|
54 |
|
|
|
55 |
|
|
while (1) {
|
56 |
|
|
fileToReadFrom >> currentFilename >> currentWeight;
|
57 |
|
|
if ( !fileToReadFrom.good() ) break;
|
58 |
|
|
//cout << currentFilename << currentWeight << endl;
|
59 |
|
|
|
60 |
|
|
filename.push_back(currentFilename);
|
61 |
|
|
weight.push_back(currentWeight);
|
62 |
|
|
|
63 |
|
|
numberOfFiles++;
|
64 |
|
|
}
|
65 |
|
|
cout << " found " << numberOfFiles << " in " << filelistName << endl;
|
66 |
|
|
//************************************************************
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
// If no files were found, exit
|
70 |
|
|
if ( numberOfFiles == 0 ) {
|
71 |
|
|
cout << "ERROR: " << filelistName << " may be formatted incorrectly." << endl;
|
72 |
|
|
cout << "Each line must be in format: <filename> <weight>"<< endl;
|
73 |
|
|
cout << "Such as" << endl;
|
74 |
|
|
cout << "QCDEnrichedpt20-30.root 175.0" << endl;
|
75 |
|
|
return;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
//************************************************************
|
80 |
|
|
// Set the weight to the given TTree
|
81 |
|
|
cout << "Copying TTree " << treeName << " to new files..." << endl;
|
82 |
|
|
for (int i = 0; i < numberOfFiles ; i ++) {
|
83 |
|
|
|
84 |
|
|
// Open the file
|
85 |
|
|
TFile* file1 = new TFile( filename[i], "update");
|
86 |
|
|
|
87 |
|
|
// Make sure file was found
|
88 |
|
|
if ( file1->IsZombie() ) {
|
89 |
|
|
cout << "ERROR: " << filename[i] << " was not found" << endl;
|
90 |
|
|
continue;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
// Create the output filename and open that file
|
94 |
|
|
TString newFilename = filename[i];
|
95 |
|
|
newFilename = newFilename.ReplaceAll(".root","");
|
96 |
|
|
newFilename = newFilename+"_setWeight.root";
|
97 |
|
|
TFile* outputFile = new TFile(newFilename, "RECREATE");
|
98 |
|
|
|
99 |
|
|
// Grab the original TTree
|
100 |
|
|
TTree* treeAny = file1->Get(treeName);
|
101 |
|
|
|
102 |
|
|
// Make sure the TTree was found
|
103 |
|
|
if ( !treeAny ) {
|
104 |
|
|
cout << "ERROR: " << filename[i] << " does not contain TTree named " << treeName << endl;
|
105 |
|
|
continue;
|
106 |
|
|
} else {
|
107 |
|
|
cout << " " << filename[i] << " -> " << newFilename << ", doing SetWeight( " << weight[i] << " )" << endl;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
// Create a new TTree and copy information into it
|
111 |
|
|
TTree *newTree = treeAny->CloneTree(0);
|
112 |
|
|
newTree->CopyEntries(treeAny);
|
113 |
|
|
// Set the weight, write file, and close the files
|
114 |
|
|
newTree->SetWeight( weight[i] );
|
115 |
|
|
outputFile->Write();
|
116 |
|
|
outputFile->Close();
|
117 |
|
|
file1->Close();
|
118 |
|
|
}
|
119 |
|
|
//************************************************************
|
120 |
|
|
|
121 |
|
|
cout << endl << "To chain up the trees: " << endl;
|
122 |
|
|
cout << " TChain *theChain = new TChain(\"TreePhotonAndJet\"); " << endl;
|
123 |
|
|
cout << " theChain->Add(\"*_setWeight.root\") " << endl;
|
124 |
|
|
|
125 |
|
|
}
|