combineFiles.C
Go to the documentation of this file.
1 // author: Adam Lister
2 // email : adam.lister@wisc.edu
3 // date : 2019-12-20
4 //
5 // this is a simple replacement for hadd which saves only histograms we
6 // care about when adding the output of the CMF CovarianceMatrixMaker.
7 //
8 // it only saves histograms we actually care about, and has 2 additional features:
9 // - the covariance matrix is correctly scaled by the number of universes
10 // - the shiftedBinByUniverse histogram is correctly handled
11 // (concatenated rather than directly added)
12 //
13 // this should be run with hadd_files.sh <intput files>
14 
15 void combineFiles(std::string fileString){
16 
17  // make file list
18  std::cout << "reading file list from " << fileString << std::endl;
19 
20  std::ifstream inputFile(fileString);
21  std::vector<std::string> fileList;
23 
24  if(!inputFile) {
25  std::cerr << "file list could not be opened\n";
26  return 1;
27  }
28 
29  while(std::getline(inputFile, line)) {
30  fileList.push_back(line);
31  }
32 
33  // from first file in list, get output file name
35  int initstr;
36  int endstr;
37 
38  // first truncate to filename and remove file number
39  initstr = fileList.at(0).find_last_of("/")+1;
40  endstr = fileList.at(0).find_last_of("_")-1;
41  fileName= fileList.at(0).substr(initstr,
42  endstr-initstr);
43 
44  // and now truncate the number of iterations in each file
45  initstr = 0;
46  endstr = fileName.find_last_of("_");
47  fileName = fileName.substr(initstr,
48  endstr);
49 
50  fileName = fileName+std::string(".root");
51 
52  std::cout << "outputting to file: " << fileName << std::endl;
53 
54  // from first file in list, get binning information
55 
56  TFile* firstFile = new TFile(fileList.at(0).c_str(), "read");
57  TH2D* firstCovarianceMatrix = (TH2D*)firstFile->Get("covar/CovarianceMatrix");
58  TH2D* firstShiftedByUni = (TH2D*)firstFile->Get("covar/ShiftedBinByUniverse");
59  TH1D* firstNominalBin = (TH1D*)firstFile->Get("covar/NominalBin");
60  TH1D* firstPlus1SigmaSpectrum = (TH1D*)firstFile->Get("covar/Plus1SigmaSpectrum");
61  TH1D* firstMinus1SigmaSpectrum = (TH1D*)firstFile->Get("covar/Minus1SigmaSpectrum");
62 
63  TFile* outFile = new TFile(fileName.c_str(), "recreate");
64  outFile->cd();
65  TDirectoryFile* covar = new TDirectoryFile("covar", "covar");
66  covar->cd();
67 
68  int totUniverses = firstShiftedByUni->GetNbinsY() * fileList.size();
69  int totEnergyBins = firstCovarianceMatrix->GetNbinsX();
70 
71  TH2D* covarianceMatrix = new TH2D("covarianceMatrix",
72  ";bin i;bin j",
73  totEnergyBins,
74  1,
75  totEnergyBins+1,
76  totEnergyBins,
77  1,
78  totEnergyBins+1);
79 
80 
81  TH2D* shiftedBinByUniverse = new TH2D("shiftedBinByUniverse",
82  ";bin i;universe number",
83  totEnergyBins,
84  1,
85  totEnergyBins+1,
86  totUniverses,
87  0,
88  totUniverses);
89 
90 
91  TH1D* nominalBin = (TH1D*)firstNominalBin->Clone("nominalBin");
92  TH1D* plus1Sigma = (TH1D*)firstPlus1SigmaSpectrum->Clone("Plus1SigmaSpectrum");
93  TH1D* minu1Sigma = (TH1D*)firstMinus1SigmaSpectrum->Clone("Minus1SigmaSpectrum");
94 
95  for (int i = 0; i < fileList.size(); i++){
96  std::cout << "file " << i+1 << ": " << fileList.at(i) << std::endl;
97 
98  TFile* infile = new TFile(fileList.at(i).c_str(), "open");
99 
100  TH2D* thisCovarianceMatrix = (TH2D*)infile->Get("covar/CovarianceMatrix");
101  TH2D* thisShiftedByUni = (TH2D*)infile->Get("covar/ShiftedBinByUniverse");
102 
103  covarianceMatrix->Add(thisCovarianceMatrix);
104 
105  for (int ix = 1; ix < thisShiftedByUni->GetNbinsX()+1; ix++){
106  for (int iy = 1; iy < thisShiftedByUni->GetNbinsY()+1; iy++){
107  shiftedBinByUniverse->SetBinContent(ix,
108  (i*firstShiftedByUni->GetNbinsY())+iy,
109  thisShiftedByUni->GetBinContent(ix, iy));
110  }
111  }
112 
113  infile->Close();
114  }
115 
116  covarianceMatrix->Scale(1./totUniverses);
117 
118  outFile->Write();
119 
120 }
TFile * inputFile
Definition: PlotXSec.C:134
fileName
Definition: plotROC.py:78
OStream cerr
Definition: OStream.cxx:7
list fileList
Definition: cafExposure.py:30
TFile * outFile
Definition: PlotXSec.C:135
string infile
void combineFiles(std::string fileString)
Definition: combineFiles.C:15
OStream cout
Definition: OStream.cxx:6
enum BeamMode string