Crombie Tools
FileConfigReader.h
Go to the documentation of this file.
1 /**
2  @file FileConfigReader.h
3 
4  Defines the FileConfigReader class. Since the FileConfigReader class is never used alone,
5  the entire class it defined in this header.
6 
7  @author Daniel Abercrombie <dabercro@mit.edu>
8 */
9 
10 #ifndef CROMBIETOOLS_COMMONTOOLS_FILECONFIGREADER_H
11 #define CROMBIETOOLS_COMMONTOOLS_FILECONFIGREADER_H
12 
13 #include <fstream>
14 #include <iostream>
15 #include <map>
16 #include <set>
17 #include <vector>
18 #include "TColor.h"
19 #include "TString.h"
20 #include "TCut.h"
21 #include "TFile.h"
22 #include "TTree.h"
23 #include "TChain.h"
24 #include "TH1D.h"
25 
26 #include "InOutDirectoryHolder.h"
27 #include "FileInfo.h"
28 #include "PlotHists.h"
29 
30 /**
31  @ingroup commongroup
32  @class FileConfigReader
33  This class reads a formatted [MC Config file](@ref formatmc).
34 */
35 
37 {
38  public:
40  virtual ~FileConfigReader();
41 
42  /// Differentiates between background, signal MC and data
43  enum FileType {
44  kBackground = 0, ///< Specifies the standard model background files
45  kSignal, ///< The signals in the analysis
46  kData, ///< Real data from the detector
47  };
48 
49  /// Resets the information from the for one type
50  void ResetConfig (FileType type = kBackground);
51  /// Resets the information from the config files being held for all types
52  void ResetAllConfig ();
53 
54  /// Returns a vector of limit tree names that have been read from the configs
55  std::set<TString> ReturnTreeNames ( FileType type = kBackground);
56 
57  /// Determines how to match to return files
58  enum SearchBy {
59  kLimitName = 0, ///< Match the LimitTree entry
60  kLegendEntry ///< Match the legend entry
61  };
62  /// Returns a vector of file names that have been read from the configs
63  std::vector<TString> ReturnFileNames ( FileType type = kBackground, TString matchName = "",
64  SearchBy search = kLimitName, Bool_t match = true );
65 
66  /// Returns a TChain of files that match the FileType and name for the LimitTreeMaker
67  TChain* ReturnTChain ( TString treeName = "events", FileType type = kBackground,
68  TString matchName = "", SearchBy search = kLimitName, Bool_t match = true );
69 
70  /// Add a data file
71  void AddDataFile ( TString fileName );
72 
73  /// Reads an MC configuration file
74  void ReadMCConfig ( TString config, TString fileDir = "" );
75 
76  /// This is the default MC File adder
77  void AddFile ( TString treeName, TString fileName, Double_t XSec,
78  TString entry = "", Int_t colorstyle = 0 );
79 
80  /// The multipliers for Data can be set separately.
81  inline void SetDataWeight ( TCut weight ) { fDataWeight = weight; }
82  /// The multipliers for Data can be set separately.
83  inline void SetDataWeight ( const char* weight ) { SetDataWeight(TCut(weight)); }
84  /// The multipliers for MC can be set separately.
85  inline void SetMCWeight ( TCut weight ) { fMCWeight = weight; }
86  /// The multipliers for MC can be set separately.
87  inline void SetMCWeight ( const char* weight ) { SetMCWeight(TCut(weight)); }
88 
89  /// Default File adder with FileType changing
90  inline void AddFile ( TString treeName, TString fileName, Double_t XSec,
91  TString entry, Int_t colorstyle, FileType type )
92  { SetFileType(type); AddFile(treeName,fileName,XSec,entry,colorstyle); }
93  /// This is for when you don't care about limit trees and are adding by hand
94  inline void AddFile ( TString fileName, Double_t XSec, TString entry, Int_t colorstyle )
95  { AddFile("",fileName,XSec,entry,colorstyle); }
96  /// For when you don't care about limit trees and are adding by hand with type changing
97  inline void AddFile ( TString fileName, Double_t XSec, TString entry,
98  Int_t colorstyle, FileType type )
99  { SetFileType(type); AddFile(fileName,XSec,entry,colorstyle); }
100 
101  /// Set the tree name for data
102  inline void SetDataTreeName ( TString name ) { fDataTreeName = name; }
103  /// Set the legend entry for data
104  inline void SetDataEntry ( TString entry ) { fDataEntry = entry; }
105 
106  /// Set the all histogram and luminosity for normalization.
107  inline void SetAllHistName ( TString name ) { fAllHistName = name; }
108  /// Set the Luminosity in inverse pb.
109  inline void SetLuminosity ( Double_t lum ) { fLuminosity = lum; }
110 
111  /// Set the FileType of the next config file read.
112  inline void SetFileType ( FileType type ) { fFileType = type; }
113  /// Get the FileType of the current file.
114  inline FileType GetFileType () { return fFileType; }
115 
116  /// Reads an MC configuration while changing the FileType
117  inline void ReadMCConfig ( TString config, FileType type, TString fileDir = "" )
118  { SetFileType(type); ReadMCConfig(config,fileDir); }
119  /// Set the tree of the files you are trying to plot
120  inline void SetTreeName ( TString name ) { fTreeName = name; }
121 
122  /// Use this to set a different expression for data from MC
123  inline void SetDataExpression ( TString expr ) { fDataExpression = expr; }
124 
125  /**
126  Use this function to replace the Legend Entries of the FileInfo with the Limit Tree Names.
127  Note that this destroys the known Legend Entries.
128  You should also call FileConfigReader::SetLegendColor for each entry that you want to
129  ensure consistency between your plots.
130  */
131  void NameTreesAfterLimits ( FileType type = kBackground );
132 
133  /// Sets the FileInfo with the matching Legend Names to the new colors
134  void SetLegendColor ( TString entry, Int_t color, FileType type = kBackground );
135 
136  /// Scales the cross section of the matching MC samples by a factor of 1.0 + scale, destroying the old cross section in the process.
137  void ScaleBackgrounds ( TString entry, Double_t scale, SearchBy search = kLimitName, FileType type = kBackground );
138 
139  /// Loads the fit result into memory for this stacks to make
140  void LoadFitResult ( const char* fit_result );
141 
142  /// Allows reader to avoid skipping when reading in exception configs
143  void SetKeepAllFiles ( Bool_t keep ) { fKeepAllFiles = keep; }
144 
145  protected:
146  Double_t fLuminosity = 2000.0; ///< The Luminosity in inverse pb
147  TString fDataTreeName = "data"; ///< The base name of the data in a limit tree
148  TString fDataEntry = "Data"; ///< The legend entry for data
149  TString fAllHistName = "htotal"; ///< The all histogram name used ingenerating cross section weights
150  std::vector<FileInfo*> fDataFileInfo; ///< Vector of data FileInfo objects
151  std::vector<FileInfo*> fMCFileInfo; ///< Vector of background FileInfo objects
152  std::vector<FileInfo*> fSignalFileInfo; ///< Vector of signal FileInfo objects
153 
154  /// Draws histograms for using the default expression and a file list
155  std::vector<TH1D*> GetHistList ( Int_t NumXBins, Double_t *XBins, std::vector<TString> FileList, FileType type );
156 
157  /// Draws histograms for using the default expression and file configuration
158  std::vector<TH1D*> GetHistList ( Int_t NumXBins, Double_t *XBins, FileType type, TString matchName = "",
159  SearchBy search = kLimitName, Bool_t match = true);
160 
161  /// Draw a single histogram using the default expression and a file list
162  TH1D* GetHist ( std::vector<TH1D*> HistList );
163 
164  /// Draw a single histogram using the default expression and a file list
165  TH1D* GetHist ( Int_t NumXBins, Double_t *XBins, std::vector<TString> FileList, FileType type );
166 
167  /// Draw a single histogram using the default expression and file configuration
168  TH1D* GetHist ( Int_t NumXBins, Double_t *XBins, FileType type, TString matchName = "",
169  SearchBy search = kLimitName, Bool_t match = true);
170 
171  void OpenFiles ( std::vector<TString> fileNames ); ///< Opens the files in a vector
172  void CloseFiles (); ///< Closes the files that are open
173 
174  /// Return a pointer to a proper vector of FileInfo
175  std::vector<FileInfo*> *GetFileInfo ( FileType type );
176 
177  /// Allows reader to avoid skipping when reading in exception configs
178  void SetMultiplyLumi ( Bool_t doMultiply ) { fMultiplyLumi = doMultiply; }
179 
180  TCut fDataWeight = ""; ///< Separate Data weights if needed
181  TCut fMCWeight = ""; ///< Separate MC weights if needed
182 
183  TString fTreeName = "events"; ///< Stores name of tree from file
184 
185  private:
186  FileType fFileType = kBackground; ///< Type of files in the next config
187  Bool_t fKeepAllFiles = false; ///< Keeps FileInfo stored usually deleted by exception configs
188  Bool_t fMultiplyLumi = true; ///< Returns XSecWeight with luminosity multiplied
189  std::vector<TObject*> fDeleteThese; ///< Vector of object pointers to free memory at the end
190  std::vector<TFile*> fFiles; ///< Vector of active files
191  std::vector<TH1D*> fHists; ///< Vector of histograms to use
192  std::vector<std::vector<TFile*>> fAllFiles; ///< Vector of all open files
193 
194  TString fDataExpression = ""; ///< Holds an alternative expression to plot data in
195 
196  std::map<TString, double> fFitResult; ///< Holds the fit results
197 
198 };
199 
200 // Dump this into global namespace because fuck typing
202 const std::vector<FileType> gFileTypes {FileType::kBackground, FileType::kSignal, FileType::kData};
203 
204 //--------------------------------------------------------------------
206 { }
207 
208 //--------------------------------------------------------------------
210 {
211  CloseFiles();
212  ResetAllConfig();
213 }
214 
215 //--------------------------------------------------------------------
216 void
218 {
219  DisplayFunc(__func__);
220  std::vector<FileInfo*>* fileInfo = GetFileInfo(type);
221 
222  for (UInt_t iInfo = 0; iInfo != fileInfo->size(); ++iInfo)
223  delete (*fileInfo)[iInfo];
224  fileInfo->clear();
225 }
226 
227 //--------------------------------------------------------------------
228 void
230 {
231  DisplayFunc(__func__);
235 
236  for (UInt_t iDelete = 0; iDelete != fDeleteThese.size(); ++iDelete)
237  delete fDeleteThese[iDelete];
238  fDeleteThese.clear();
239 }
240 
241 //--------------------------------------------------------------------
242 std::vector<FileInfo*>*
244 {
245  std::vector<FileInfo*> *fileInfo;
246  switch (type)
247  {
248  case kBackground:
249  fileInfo = &fMCFileInfo;
250  break;
251  case kSignal:
252  fileInfo = &fSignalFileInfo;
253  break;
254  case kData:
255  fileInfo = &fDataFileInfo;
256  break;
257  default:
258  Message(eError, "What case is that?");
259  exit(1);
260  }
261  return fileInfo;
262 }
263 
264 //--------------------------------------------------------------------
265 std::set<TString>
267 {
268  std::set<TString> output;
269  std::vector<FileInfo*> *fileInfo = GetFileInfo(type);
270 
271  for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo)
272  output.insert((*iInfo)->fTreeName);
273 
274  return output;
275 }
276 
277 //--------------------------------------------------------------------
278 std::vector<TString>
279 FileConfigReader::ReturnFileNames(FileType type, TString matchName, SearchBy search, Bool_t match)
280 {
281  DisplayFunc(__func__);
282 
283  Message(eDebug, "Searching for files that match", matchName);
284  std::vector<TString> output;
285  std::vector<FileInfo*> *fileInfo = GetFileInfo(type);
286 
287  for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo) {
288 
289  if (search == kLimitName) {
290  Message(eDebug, "Comparing to Limit Tree", (*iInfo)->fTreeName);
291  if (matchName != "" && (((*iInfo)->fTreeName != matchName && match) || ((*iInfo)->fTreeName == matchName && !match)))
292  continue;
293 
294  } else if (search == kLegendEntry) {
295  Message(eDebug, "Comparing to Legend Entry", (*iInfo)->fEntry);
296  if (matchName != "" && (((*iInfo)->fEntry != matchName && match) || ((*iInfo)->fEntry == matchName && !match)))
297  continue;
298 
299  }
300 
301  Message(eDebug, "Found a match! Adding file", (*iInfo)->fFileName);
302 
303  output.push_back((*iInfo)->fFileName);
304 
305  }
306 
307  return output;
308 }
309 
310 //--------------------------------------------------------------------
311 TChain*
312 FileConfigReader::ReturnTChain(TString treeName, FileType type, TString matchName, SearchBy search, Bool_t match)
313 {
314  TChain *theChain = new TChain(treeName, treeName + "_chain");
315  std::vector<TString> fileList = ReturnFileNames(type, matchName, search, match);
316 
317  for (std::vector<TString>::iterator iFile = fileList.begin(); iFile != fileList.end(); ++iFile)
318  theChain->Add(iFile->Data());
319 
320  fDeleteThese.push_back(theChain);
321  return theChain;
322 }
323 
324 //--------------------------------------------------------------------
325 void FileConfigReader::AddDataFile(TString fileName)
326 {
327  FileType tempType = fFileType;
329  AddFile(fDataTreeName, fileName, -1, fDataEntry);
330  SetFileType(tempType);
331 }
332 
333 //--------------------------------------------------------------------
334 void FileConfigReader::AddFile(TString treeName, TString fileName, Double_t XSec,
335  TString entry, Int_t colorstyle)
336 {
337 
338  DisplayFunc(__func__);
339  FileInfo* tempInfo = new FileInfo(treeName,AddInDir(fileName),XSec,
340  entry,colorstyle,fAllHistName);
341  if (fMultiplyLumi)
342  tempInfo->fXSecWeight *= fLuminosity;
343 
344  if (fFileType == kBackground)
345  fMCFileInfo.push_back(tempInfo);
346 
347  else if (fFileType == kSignal)
348  fSignalFileInfo.push_back(tempInfo);
349 
350  else if (fFileType == kData)
351  fDataFileInfo.push_back(tempInfo);
352 
353  else {
354 
355  Message(eError, "Don't have a correct MC Type. Not saving fileInfo.");
356  delete tempInfo;
357 
358  }
359 
360 }
361 
362 //--------------------------------------------------------------------
363 
364 /**
365  Reads in a configuration file assuming it has the format descrbed
366  in [Formatting MC Configs](@ref formatmc). Contents of
367  this MC file is stored in one of two vectors FileInfo pointers.
368 */
369 
370 void FileConfigReader::ReadMCConfig(TString config, TString fileDir)
371 {
372  DisplayFunc(__func__);
373 
374  Message(eInfo, "Reading MC Config file", config);
375 
376  if (fileDir != "")
377  SetInDirectory(fileDir);
378 
379  Message(eInfo, "Input directory is", GetInDirectory());
380 
381  std::ifstream configFile {config.Data()};
382  TString LimitTreeName;
383  TString FileName;
384  TString XSec;
385  TString LegendEntry;
386  TString ColorStyleEntry;
387  TString currTreeName;
388  TString currLegend;
389  TString currColorStyle;
390  TString red;
391  TString green;
392  TString blue;
393  Int_t newColors = 0;
394 
395  std::vector<FileInfo*> *FileInfo = (fFileType == kSignal) ? &fSignalFileInfo : &fMCFileInfo;
396 
397  std::vector<UInt_t> SplitSamples;
398 
399  while (!configFile.eof()) {
400 
401  Message(eDebug, "About to read line.");
402  Message(eDebug, "currTreeName: ", currTreeName);
403  Message(eDebug, "currLegend: ", currLegend);
404  Message(eDebug, "currColorStyle: ", currColorStyle);
405  Message(eDebug, "Now reading...");
406 
407  configFile >> LimitTreeName;
408 
409  Message(eDebug, "LimitTreeName: ", LimitTreeName);
410 
411  if (LimitTreeName == ".")
412  LimitTreeName = currTreeName;
413 
414  else if (!(LimitTreeName == "#." || LimitTreeName == "INGROUP" || LimitTreeName == "ENDGROUP")) {
415 
416  if (LimitTreeName.BeginsWith('#'))
417  currTreeName = TString(LimitTreeName.Strip(TString::kLeading, '#'));
418 
419  else
420  currTreeName = LimitTreeName;
421 
422  }
423 
424  Message(eDebug, "Processed tree name, now:", LimitTreeName);
425 
426  // Do the checking here for merging groups
427  if (LimitTreeName == "INGROUP") {
428 
429  SplitSamples.push_back((*FileInfo).size());
430  Message(eDebug, "Starting merged group. Split at: %i, Size: %i",
431  (*FileInfo).size(), SplitSamples.size());
432 
433  } else if (LimitTreeName == "ENDGROUP") {
434 
435  Message(eDebug, "Group ended.");
436 
437  // Get the uncertainties for each sample
438  std::vector<Double_t> Uncerts;
439  SplitSamples.push_back((*FileInfo).size());
440  for (UInt_t iSample = 0; iSample != SplitSamples.size() - 1; ++iSample) {
441 
442  Double_t Uncert = 0.0;
443 
444  for (UInt_t iFile = SplitSamples[iSample]; iFile != SplitSamples[iSample + 1]; ++iFile)
445  Uncert += (*FileInfo)[iFile]->fXSec * (*FileInfo)[iFile]->fXSecWeight;
446 
447  Uncerts.push_back(Uncert);
448 
449  }
450 
451  // Get the weights from the uncertainties
452  std::vector<Double_t> Weights;
453  Double_t SumOfWeights = 0.0;
454  for (UInt_t iSample = 0; iSample != SplitSamples.size() - 1; ++iSample) {
455 
456  Double_t Weight = 1.0;
457 
458  for (UInt_t iUncert = 0; iUncert != Uncerts.size(); ++iUncert) {
459 
460  if (iSample != iUncert)
461  Weight *= Uncerts[iUncert];
462 
463  }
464 
465  Weights.push_back(Weight);
466  SumOfWeights += Weight;
467 
468  }
469 
470  // Apply the weights
471  for (UInt_t iSample = 0; iSample != SplitSamples.size() - 1; ++iSample) {
472 
473  for (UInt_t iFile = SplitSamples[iSample]; iFile != SplitSamples[iSample + 1]; ++iFile)
474  (*FileInfo)[iFile]->fXSecWeight *= Weights[iSample]/SumOfWeights;
475 
476  }
477 
478  SplitSamples.resize(0);
479 
480  } else {
481 
482  configFile >> FileName;
483  Message(eDebug, "FileName: ", FileName);
484 
485  if (LimitTreeName == "skip") {
486 
487  Message(eInfo, "Skipping files!");
488 
489  if (!fKeepAllFiles) {
490 
491  for (UInt_t iFile = 0; iFile != (*FileInfo).size(); ++iFile) {
492 
493  Message(eDebug, "File:", iFile, "/", (*FileInfo).size(), "comparing",
494  ((*FileInfo)[iFile]->fFileName), "to", (AddInDir(FileName)));
495 
496  if ((*FileInfo)[iFile]->fFileName == AddInDir(FileName)) {
497 
498  Message(eInfo, "Removing file", (*FileInfo)[iFile]->fFileName);
499  delete (*FileInfo)[iFile];
500  (*FileInfo).erase((*FileInfo).begin() + iFile);
501  break;
502 
503  }
504 
505  }
506 
507  }
508 
509  } else {
510 
511  configFile >> XSec >> LegendEntry >> ColorStyleEntry;
512 
513  if (LegendEntry == ".")
514  LegendEntry = currLegend;
515 
516  else
517  currLegend = LegendEntry;
518 
519  if (ColorStyleEntry == ".")
520  ColorStyleEntry = currColorStyle;
521 
522  else if (ColorStyleEntry == "rgb") {
523 
524  ++newColors;
525  ColorStyleEntry = TString::Format("%i", 5000 + newColors);
526  currColorStyle = ColorStyleEntry;
527  configFile >> red >> green >> blue;
528  TColor* setColor = new TColor(ColorStyleEntry.Atoi(),red.Atof()/255,green.Atof()/255,blue.Atof()/255);
529 
530  } else
531 
532  currColorStyle = ColorStyleEntry;
533 
534  Message(eDebug, "XSec: ", XSec);
535  Message(eDebug, "LegendEntry: ", LegendEntry);
536  Message(eDebug, "ColorStyleEntry:", ColorStyleEntry);
537 
538  if (ColorStyleEntry != "" && (fKeepAllFiles || !LimitTreeName.BeginsWith('#')))
539  // Replace _ with spaces, but put the escaped ones back
540  AddFile(LimitTreeName, FileName, XSec.Atof(),
541  LegendEntry.ReplaceAll("_", " ").ReplaceAll("\\ ", "_"),
542  ColorStyleEntry.Atoi());
543 
544  }
545 
546  }
547 
548  }
549 }
550 
551 //--------------------------------------------------------------------
552 std::vector<TH1D*>
553 FileConfigReader::GetHistList(Int_t NumXBins, Double_t *XBins, std::vector<TString> FileList, FileType type)
554 {
555 
556  DisplayFunc(__func__);
557 
558  Message(eDebug, "Number of Files to plot: %i", FileList.size());
559 
560  std::vector<TString> theFileNames = FileList;
561  OpenFiles(theFileNames);
562 
563  std::vector<FileInfo*> *theFileInfo = &fMCFileInfo;
564  TCut tempCutHolder;
565  TString tempExprHolder;
566 
567  if (type == kSignal)
568  theFileInfo = &fSignalFileInfo;
569 
570  if (type == kData && fDataWeight != "") {
571 
572  tempCutHolder = fDefaultCut;
573  SetDefaultWeight(tempCutHolder + fDataWeight);
574 
575  } else if (type != kData && fMCWeight != "") {
576 
577  tempCutHolder = fDefaultCut;
578  SetDefaultWeight(tempCutHolder * fMCWeight);
579 
580  }
581 
582  if (type == kData && fDataExpression != "") {
583 
584  tempExprHolder = fDefaultExpr;
586 
587  }
588 
590 
591  if (type == kBackground && fSystematicBranches.size() > 0) {
592 
593  // Create the systematics and apply them to each histogram
594  TString sys_expr = fSystematicBranches[0] + "*" + fSystematicBranches[0];
595  for (UInt_t iBranch = 1; iBranch != fSystematicBranches.size(); ++iBranch)
596  sys_expr += "+" + fSystematicBranches[iBranch] + "*" + fSystematicBranches[iBranch];
597 
598  SetUncertaintySquared(sys_expr);
599  }
600 
601  std::vector<TH1D*> theHists = MakeHists(NumXBins, XBins);
602 
603  if (type == kData && fDataExpression != "")
604  SetDefaultExpr(tempExprHolder);
605 
606  if (tempCutHolder != "")
607  SetDefaultWeight(tempCutHolder);
608 
609  if (type != kData) {
610 
611  UInt_t iFileName = 0;
612 
613  for (UInt_t iFile = 0; iFile < theFileInfo->size(); iFile++) {
614 
615  if ((*theFileInfo)[iFile]->fFileName != theFileNames[iFileName])
616  continue;
617 
618  theHists[iFileName]->Scale((*theFileInfo)[iFile]->fXSecWeight);
619  Message(eDebug, "Scaling hist %i by %f, now integral %f",
620  iFileName, (*theFileInfo)[iFile]->fXSecWeight, theHists[iFileName]->Integral("width"));
621 
622  auto scale = fFitResult.find((*theFileInfo)[iFile]->fTreeName);
623  if (scale != fFitResult.end()) {
624  Message(eDebug, "Scaling %s by %f", scale->first.Data(), scale->second);
625  theHists[iFileName]->Scale(scale->second);
626  }
627 
628  ++iFileName;
629 
630  }
631  }
632 
633  return theHists;
634 }
635 
636 //--------------------------------------------------------------------
637 std::vector<TH1D*>
638 FileConfigReader::GetHistList(Int_t NumXBins, Double_t *XBins, FileType type,
639  TString matchName, SearchBy search, Bool_t match)
640 {
641 
642  DisplayFunc(__func__);
643  std::vector<TString> theFileNames = ReturnFileNames(type, matchName, search, match);
644  return GetHistList(NumXBins, XBins, theFileNames, type);
645 
646 }
647 
648 //--------------------------------------------------------------------
649 TH1D*
650 FileConfigReader::GetHist(std::vector<TH1D*> HistList)
651 {
652 
653  DisplayFunc(__func__);
654  std::vector<TH1D*> theHists = HistList;
655 
656  Message(eDebug, "Number of histograms to merge: %i", theHists.size());
657 
658  if (theHists.size() == 0) {
659  Message(eError, "The list of histograms called is empty.");
660  exit(30);
661  }
662 
663  TH1D *output = (TH1D*) theHists[0]->Clone();
664  output->Reset("M");
665 
666  for (std::vector<TH1D*>::iterator iHist = theHists.begin(); iHist != theHists.end(); ++iHist)
667  output->Add(*iHist);
668 
669  return output;
670 
671 }
672 
673 //--------------------------------------------------------------------
674 TH1D*
675 FileConfigReader::GetHist(Int_t NumXBins, Double_t *XBins, std::vector<TString> FileList, FileType type)
676 {
677  DisplayFunc(__func__);
678  return GetHist(GetHistList(NumXBins, XBins, FileList, type));
679 }
680 
681 //--------------------------------------------------------------------
682 TH1D*
683 FileConfigReader::GetHist(Int_t NumXBins, Double_t *XBins, FileType type,
684  TString matchName, SearchBy search, Bool_t match)
685 {
686  DisplayFunc(__func__);
687  return GetHist(GetHistList(NumXBins, XBins, type, matchName, search, match));
688 }
689 
690 //--------------------------------------------------------------------
691 void
692 FileConfigReader::OpenFiles(std::vector<TString> fileNames)
693 {
694  DisplayFunc(__func__);
695  fFiles.resize(0);
696  fInTrees.resize(0);
697 
698  Message(eDebug, "Looking for tree named:", fTreeName);
699 
700  for (std::vector<TString>::iterator iFile = fileNames.begin(); iFile != fileNames.end(); ++iFile) {
701 
702  TFile *tempFile = TFile::Open(iFile->Data());
703  TTree *tempTree;
704  if (fTreeName.Contains("/"))
705  tempTree = (TTree*) tempFile->Get(fTreeName);
706  else
707  tempTree = (TTree*) tempFile->FindObjectAny(fTreeName);
708 
709  Message(eDebug, "File: %s, located at %p, has tree at %p", iFile->Data(), tempFile, tempTree);
710 
711  if (!tempTree) {
712  Message(eError, "Tree not found in file!");
713  Message(eError, "File: %s at %p", iFile->Data(), tempFile);
714  Message(eError, "Tree: %s at %p", fTreeName.Data(), tempTree);
715  exit(1010);
716  }
717 
718  fFiles.push_back(tempFile);
719  fInTrees.push_back(tempTree);
720 
721  Message(eDebug, "Number of files used: %i, Number of trees: %i", fFiles.size(), fInTrees.size());
722 
723  }
724 
725  fAllFiles.push_back(fFiles);
726  Message(eDebug, "Total number of times files opened so far: %i", fAllFiles.size());
727 }
728 
729 //--------------------------------------------------------------------
730 void
732 {
733  DisplayFunc(__func__);
734  for (UInt_t iFiles = 0; iFiles != fAllFiles.size(); ++iFiles) {
735  for (UInt_t iFile = 0; iFile != fAllFiles[iFiles].size(); ++iFile) {
736  Message(eDebug, "About to close:", fAllFiles[iFiles][iFile]->GetName());
737  fAllFiles[iFiles][iFile]->Close();
738  }
739  fAllFiles[iFiles].clear();
740  }
741 
742  fFiles.clear();
743  fInTrees.clear();
744  fAllFiles.clear();
745 
746 }
747 
748 //--------------------------------------------------------------------
749 void
751 {
752  std::vector<FileInfo*>* fileInfo = GetFileInfo(type);
753  for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo)
754  (*iInfo)->fEntry = (*iInfo)->fTreeName;
755 }
756 
757 //--------------------------------------------------------------------
758 void
759 FileConfigReader::SetLegendColor(TString entry, Int_t color, FileType type)
760 {
761  std::vector<FileInfo*>* fileInfo = GetFileInfo(type);
762  for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo) {
763  if (entry == (*iInfo)->fEntry)
764  (*iInfo)->fColorStyle = color;
765  }
766 }
767 
768 //--------------------------------------------------------------------
769 void
770 FileConfigReader::ScaleBackgrounds(TString entry, Double_t scale, SearchBy search, FileType type)
771 {
772 
773  DisplayFunc(__func__);
774 
775  Message(eDebug, "Scaling %s by %f", entry, scale);
776 
777  std::vector<FileInfo*>* fileInfo = GetFileInfo(type);
778  for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo) {
779 
780  TString match = (search == kLimitName) ? (*iInfo)->fTreeName : (*iInfo)->fEntry;
781  if (match == entry) {
782  Message(eDebug, "File %s XSec before %f", (*iInfo)->fFileName.Data(), (*iInfo)->fXSecWeight);
783  (*iInfo)->fXSecWeight *= (1.0 + scale);
784  Message(eDebug, "File %s XSec after %f", (*iInfo)->fFileName.Data(), (*iInfo)->fXSecWeight);
785  }
786 
787  }
788 
789 }
790 
791 //--------------------------------------------------------------------
792 void
793 FileConfigReader::LoadFitResult(const char* fit_result) {
794 
795  DisplayFunc(__func__);
796 
797  // Clear out previous loaded results
798  fFitResult.clear();
799 
800  // Open file
801  std::ifstream fitFile {fit_result};
802 
803  // Read it in
804  TString key;
805  double value;
806 
807  while (!fitFile.eof()) {
808  fitFile >> key >> value;
809 
810  if (key != "") {
811  Message(eDebug, "Key: %s, Value: %f", key.Data(), value);
812  fFitResult[key] = value;
813  }
814  }
815 }
816 
817 #endif