Crombie Tools
FileInfo.h
Go to the documentation of this file.
1 /**
2  @file FileInfo.h
3 
4  Defines the FileInfo class and a function to get cross section weight.
5 
6  @author Daniel Abercrombie <dabercro@mit.edu>
7 */
8 
9 #ifndef CROMBIETOOLS_COMMONTOOLS_FILEINFO_H
10 #define CROMBIETOOLS_COMMONTOOLS_FILEINFO_H
11 
12 #include <iostream>
13 #include <sys/stat.h>
14 
15 #include "TFile.h"
16 #include "TH1D.h"
17 #include "TH1.h"
18 #include "TString.h"
19 
20 #include "UncertaintyInfo.h"
21 
22 /**
23  @ingroup commongroup
24  Returns the XSection weight of each event.
25 
26  @param fileName is the name of the file containing all
27  events in the sample.
28  @param XSec is the cross section in pb in the sample.
29  @param allHistName is the name of the histogram counting
30  all events that were generated for the sample.
31 
32  @returns output that should be multiplied by the
33  luminosity (in pb) to get the overall scaling of the histogram.
34  This multiplication is done already in FileReader.
35 */
36 
37 Double_t GetXSecWeight(TString fileName, Double_t XSec, TString allHistName)
38 {
39  TFile *theFile = TFile::Open(fileName);
40  if (!theFile) {
41  std::cout << "Cannot open file " << fileName << std::endl;
42  exit(1);
43  }
44  TH1D* allHist = (TH1D*) theFile->Get(allHistName);
45  Double_t weight = -1;
46  if (allHist)
47  weight = XSec/allHist->GetBinContent(1);
48  else
49  std::cout << "Just so you know, I can't find " << allHistName << " in " << fileName << std::endl;
50 
51  theFile->Close();
52  return weight;
53 }
54 
55 /**
56  @ingroup commongroup
57  @struct FileInfo
58  Structure holding all the information desired from each file.
59 */
60 
61 struct FileInfo : public Debug
62 {
63  FileInfo () {}
64  /// The constructor fills all of the entries
65  FileInfo ( TString treeName, TString fileName, Double_t XSec,
66  TString entry, Int_t colorstyle, TString allHist,
67  UncertaintyInfo *uncInfo = nullptr )
68  : fTreeName{treeName},
69  fFileName{fileName},
70  fXSec{XSec},
71  fEntry{entry},
72  fColorStyle{colorstyle},
73  fXSecWeight{(allHist == "" || fXSec < 0) ? 1.0 : GetXSecWeight(fFileName, fXSec, allHist)},
74  fUncertaintyInfo{uncInfo}
75  {
76  struct stat file_stat;
77  stat(fFileName.Data(), &file_stat);
78  fSize = file_stat.st_size;
79 
80  TFile inFile {fileName};
81  auto* _allHist = (TH1D*) inFile.Get(allHist);
82  Message(eDebug, "AllHist at", _allHist);
83  if (_allHist)
84  fRenormHistogram = *_allHist;
85  }
86 
87  ~FileInfo () {
88  if (fUncertaintyInfo)
89  delete fUncertaintyInfo;
90  }
91 
92  TString fTreeName; ///< Base name for the Limit Tree made by LimitTreeMaker
93  TString fFileName; ///< Name of the file
94  Double_t fXSec; ///< Cross section of the sample contained in the file
95  TString fEntry; ///< Legend entry for that file
96  Int_t fColorStyle; ///< Fill color or line style (if signal) for that file
97  Double_t fXSecWeight; ///< Weight determined by GetXSecWeight()
98  UncertaintyInfo *fUncertaintyInfo; ///< Structure to supply uncertainty weight to a file
99  unsigned long fSize; ///< Size of the file
100  TH1D fRenormHistogram; ///< Histogram to hold the sum of weights for this file
101 };
102 
103 inline Bool_t operator<(const FileInfo& a, const FileInfo& b) { return a.fSize < b.fSize; }
104 
105 #endif