ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RootMacros/addBranchToTreesInFiles.C
Revision: 1.1
Committed: Fri Feb 20 20:58:54 2009 UTC (16 years, 2 months ago) by anderson
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Adds a branch to existing TTree in given files

File Contents

# User Rev Content
1 anderson 1.1 /*************************************************
2     This will add a branch to the specified TTree
3     in the specified files. Useful for adding
4     event weights, cross-sections, etc...
5    
6     Michael B. Anderson
7     Feb 20, 2009
8     *************************************************/
9    
10     #include <vector>
11     #include <cmath>
12    
13     void addBranchToTreesInFiles() {
14     //************************************************************
15     // Variables //
16     vector<TString> fileName;
17     fileName.push_back( "PhotonJetPt15_realPhotons.root" );
18     fileName.push_back( "PhotonJetPt30_realPhotons.root" );
19    
20     // The following 4 variables set the scale
21     float invLuminosityToScaleTo = 200; // in pb-1
22    
23     vector<float> crossSection;
24     crossSection.push_back( 2.9E5 );
25     crossSection.push_back( 3.2E4 );
26    
27     vector<float> filterEffeciency;
28     filterEffeciency.push_back( 1.0 );
29     filterEffeciency.push_back( 1.0 );
30    
31     vector<float> eventsAnalyzied;
32     eventsAnalyzied.push_back( 950E3 );
33     eventsAnalyzied.push_back( 670E3 );
34     // END of setting scale
35    
36     TString treeName = "TreePhotonMatched";
37    
38     // END of Variables //
39     //************************************************************
40    
41    
42     // Loop over all the Files
43     for (int i=0; i < fileName.size(); i++) {
44     TFile* currentFile = new TFile(fileName[i],"update");
45    
46     // this is the variable to be added
47     Float_t scale=crossSection[i]*invLuminosityToScaleTo*filterEffeciency[i]/eventsAnalyzied[i];
48    
49     cout << "Opened " << fileName[i] << ", adding new branch with value=" << scale;
50    
51     TTree *tree = (TTree*)currentFile->Get(treeName);
52     TBranch *newBranch = tree->Branch("weight", &scale,"weight/F");
53    
54     // Loop over all the entries, and add the new branch
55     Int_t numEntries = (Int_t)tree->GetEntries();
56     for (Int_t j=0; j<numEntries; j++) {
57     newBranch->Fill();
58     }
59     tree->Write("",TObject::kOverwrite); // save new version only
60     currentFile->Close();
61     cout << "...closed file." << endl;
62     }
63     }