10 #ifndef CROMBIETOOLS_COMMONTOOLS_FILECONFIGREADER_H 11 #define CROMBIETOOLS_COMMONTOOLS_FILECONFIGREADER_H 74 void ReadMCConfig ( TString config, TString fileDir =
"" );
77 void AddFile ( TString treeName, TString fileName, Double_t XSec,
78 TString entry =
"", Int_t colorstyle = 0 );
90 inline void AddFile ( TString treeName, TString fileName, Double_t XSec,
91 TString entry, Int_t colorstyle,
FileType type )
94 inline void AddFile ( TString fileName, Double_t XSec, TString entry, Int_t colorstyle )
95 {
AddFile(
"",fileName,XSec,entry,colorstyle); }
97 inline void AddFile ( TString fileName, Double_t XSec, TString entry,
155 std::vector<TH1D*>
GetHistList ( Int_t NumXBins, Double_t *XBins, std::vector<TString> FileList,
FileType type );
158 std::vector<TH1D*>
GetHistList ( Int_t NumXBins, Double_t *XBins,
FileType type, TString matchName =
"",
162 TH1D*
GetHist ( std::vector<TH1D*> HistList );
165 TH1D*
GetHist ( Int_t NumXBins, Double_t *XBins, std::vector<TString> FileList,
FileType type );
168 TH1D*
GetHist ( Int_t NumXBins, Double_t *XBins,
FileType type, TString matchName =
"",
171 void OpenFiles ( std::vector<TString> fileNames );
202 const std::vector<FileType>
gFileTypes {FileType::kBackground, FileType::kSignal, FileType::kData};
220 std::vector<FileInfo*>* fileInfo =
GetFileInfo(type);
222 for (UInt_t iInfo = 0; iInfo != fileInfo->size(); ++iInfo)
223 delete (*fileInfo)[iInfo];
236 for (UInt_t iDelete = 0; iDelete !=
fDeleteThese.size(); ++iDelete)
242 std::vector<FileInfo*>*
245 std::vector<FileInfo*> *fileInfo;
268 std::set<TString> output;
269 std::vector<FileInfo*> *fileInfo =
GetFileInfo(type);
271 for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo)
272 output.insert((*iInfo)->fTreeName);
283 Message(
eDebug,
"Searching for files that match", matchName);
284 std::vector<TString> output;
285 std::vector<FileInfo*> *fileInfo =
GetFileInfo(type);
287 for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo) {
290 Message(
eDebug,
"Comparing to Limit Tree", (*iInfo)->fTreeName);
291 if (matchName !=
"" && (((*iInfo)->fTreeName != matchName && match) || ((*iInfo)->fTreeName == matchName && !match)))
295 Message(
eDebug,
"Comparing to Legend Entry", (*iInfo)->fEntry);
296 if (matchName !=
"" && (((*iInfo)->fEntry != matchName && match) || ((*iInfo)->fEntry == matchName && !match)))
301 Message(
eDebug,
"Found a match! Adding file", (*iInfo)->fFileName);
303 output.push_back((*iInfo)->fFileName);
314 TChain *theChain =
new TChain(treeName, treeName +
"_chain");
315 std::vector<TString> fileList =
ReturnFileNames(type, matchName, search, match);
317 for (std::vector<TString>::iterator iFile = fileList.begin(); iFile != fileList.end(); ++iFile)
318 theChain->Add(iFile->Data());
335 TString entry, Int_t colorstyle)
355 Message(
eError,
"Don't have a correct MC Type. Not saving fileInfo.");
381 std::ifstream configFile {config.Data()};
382 TString LimitTreeName;
386 TString ColorStyleEntry;
387 TString currTreeName;
389 TString currColorStyle;
397 std::vector<UInt_t> SplitSamples;
399 while (!configFile.eof()) {
407 configFile >> LimitTreeName;
411 if (LimitTreeName ==
".")
412 LimitTreeName = currTreeName;
414 else if (!(LimitTreeName ==
"#." || LimitTreeName ==
"INGROUP" || LimitTreeName ==
"ENDGROUP")) {
416 if (LimitTreeName.BeginsWith(
'#'))
417 currTreeName = TString(LimitTreeName.Strip(TString::kLeading,
'#'));
420 currTreeName = LimitTreeName;
427 if (LimitTreeName ==
"INGROUP") {
429 SplitSamples.push_back((*FileInfo).size());
430 Message(
eDebug,
"Starting merged group. Split at: %i, Size: %i",
431 (*FileInfo).size(), SplitSamples.size());
433 }
else if (LimitTreeName ==
"ENDGROUP") {
438 std::vector<Double_t> Uncerts;
439 SplitSamples.push_back((*FileInfo).size());
440 for (UInt_t iSample = 0; iSample != SplitSamples.size() - 1; ++iSample) {
442 Double_t Uncert = 0.0;
444 for (UInt_t iFile = SplitSamples[iSample]; iFile != SplitSamples[iSample + 1]; ++iFile)
445 Uncert += (*FileInfo)[iFile]->fXSec * (*FileInfo)[iFile]->fXSecWeight;
447 Uncerts.push_back(Uncert);
452 std::vector<Double_t> Weights;
453 Double_t SumOfWeights = 0.0;
454 for (UInt_t iSample = 0; iSample != SplitSamples.size() - 1; ++iSample) {
456 Double_t Weight = 1.0;
458 for (UInt_t iUncert = 0; iUncert != Uncerts.size(); ++iUncert) {
460 if (iSample != iUncert)
461 Weight *= Uncerts[iUncert];
465 Weights.push_back(Weight);
466 SumOfWeights += Weight;
471 for (UInt_t iSample = 0; iSample != SplitSamples.size() - 1; ++iSample) {
473 for (UInt_t iFile = SplitSamples[iSample]; iFile != SplitSamples[iSample + 1]; ++iFile)
474 (*FileInfo)[iFile]->fXSecWeight *= Weights[iSample]/SumOfWeights;
478 SplitSamples.resize(0);
482 configFile >> FileName;
485 if (LimitTreeName ==
"skip") {
491 for (UInt_t iFile = 0; iFile != (*FileInfo).size(); ++iFile) {
493 Message(
eDebug,
"File:", iFile,
"/", (*FileInfo).size(),
"comparing",
494 ((*FileInfo)[iFile]->fFileName),
"to", (
AddInDir(FileName)));
496 if ((*FileInfo)[iFile]->fFileName ==
AddInDir(FileName)) {
498 Message(
eInfo,
"Removing file", (*FileInfo)[iFile]->fFileName);
499 delete (*FileInfo)[iFile];
500 (*FileInfo).erase((*FileInfo).begin() + iFile);
511 configFile >> XSec >> LegendEntry >> ColorStyleEntry;
513 if (LegendEntry ==
".")
514 LegendEntry = currLegend;
517 currLegend = LegendEntry;
519 if (ColorStyleEntry ==
".")
520 ColorStyleEntry = currColorStyle;
522 else if (ColorStyleEntry ==
"rgb") {
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);
532 currColorStyle = ColorStyleEntry;
538 if (ColorStyleEntry !=
"" && (
fKeepAllFiles || !LimitTreeName.BeginsWith(
'#')))
540 AddFile(LimitTreeName, FileName, XSec.Atof(),
541 LegendEntry.ReplaceAll(
"_",
" ").ReplaceAll(
"\\ ",
"_"),
542 ColorStyleEntry.Atoi());
558 Message(
eDebug,
"Number of Files to plot: %i", FileList.size());
560 std::vector<TString> theFileNames = FileList;
563 std::vector<FileInfo*> *theFileInfo = &
fMCFileInfo;
565 TString tempExprHolder;
595 for (UInt_t iBranch = 1; iBranch != fSystematicBranches.size(); ++iBranch)
596 sys_expr +=
"+" + fSystematicBranches[iBranch] +
"*" + fSystematicBranches[iBranch];
601 std::vector<TH1D*> theHists =
MakeHists(NumXBins, XBins);
606 if (tempCutHolder !=
"")
611 UInt_t iFileName = 0;
613 for (UInt_t iFile = 0; iFile < theFileInfo->size(); iFile++) {
615 if ((*theFileInfo)[iFile]->fFileName != theFileNames[iFileName])
618 theHists[iFileName]->Scale((*theFileInfo)[iFile]->fXSecWeight);
620 iFileName, (*theFileInfo)[iFile]->fXSecWeight, theHists[iFileName]->Integral(
"width"));
624 Message(
eDebug,
"Scaling %s by %f", scale->first.Data(), scale->second);
625 theHists[iFileName]->Scale(scale->second);
639 TString matchName,
SearchBy search, Bool_t match)
643 std::vector<TString> theFileNames =
ReturnFileNames(type, matchName, search, match);
644 return GetHistList(NumXBins, XBins, theFileNames, type);
654 std::vector<TH1D*> theHists = HistList;
656 Message(
eDebug,
"Number of histograms to merge: %i", theHists.size());
658 if (theHists.size() == 0) {
663 TH1D *output = (TH1D*) theHists[0]->Clone();
666 for (std::vector<TH1D*>::iterator iHist = theHists.begin(); iHist != theHists.end(); ++iHist)
684 TString matchName,
SearchBy search, Bool_t match)
700 for (std::vector<TString>::iterator iFile = fileNames.begin(); iFile != fileNames.end(); ++iFile) {
702 TFile *tempFile = TFile::Open(iFile->Data());
705 tempTree = (TTree*) tempFile->Get(
fTreeName);
707 tempTree = (TTree*) tempFile->FindObjectAny(
fTreeName);
709 Message(
eDebug,
"File: %s, located at %p, has tree at %p", iFile->Data(), tempFile, tempTree);
718 fFiles.push_back(tempFile);
734 for (UInt_t iFiles = 0; iFiles !=
fAllFiles.size(); ++iFiles) {
735 for (UInt_t iFile = 0; iFile !=
fAllFiles[iFiles].size(); ++iFile) {
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;
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;
777 std::vector<FileInfo*>* fileInfo =
GetFileInfo(type);
778 for (std::vector<FileInfo*>::iterator iInfo = fileInfo->begin(); iInfo != fileInfo->end(); ++iInfo) {
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);
801 std::ifstream fitFile {fit_result};
807 while (!fitFile.eof()) {
808 fitFile >> key >> value;