All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UniverseOracle.cxx
Go to the documentation of this file.
2 
4 
5 #include "TFile.h"
6 #include "TTree.h"
7 #include "TROOT.h"
8 #include "TSeqCollection.h"
9 
10 #include <cassert>
11 #include <cmath>
12 #include <fstream>
13 #include <iostream>
14 
15 namespace ana
16 {
17  // --------------------------------------------------------------------------
18  // Go grubbing through all the open files looking for a globalTree to pull an
19  // SRGlobal object from. This is a hack, probably SpectrumLoader should have
20  // some hooks to send SRGlobal to interested parties each time it opens a new
21  // file(?)
23  {
24  caf::SRGlobal global;
25  bool got = false;
26 
27  TSeqCollection* seq = gROOT->GetListOfFiles();
28  for(int i = 0; i < seq->GetEntries(); ++i){
29  TFile* f = (TFile*)seq->At(i);
30  if(f->GetOption() != std::string("READ")) continue;
31  TTree* tr = (TTree*)f->Get("globalTree");
32  if(!tr) continue;
33  if(tr->GetEntries() < 1) continue;
34 
35  if(got){
36  std::cout << "\nUniverseOracle: Found globalTree in multiple places. Will use first one, but this is unexpected" << std::endl;
37  }
38 
39  caf::SRGlobal* pglobal = &global;
40  tr->SetBranchAddress("global", &pglobal);
41  tr->GetEntry(0);
42  got = true;
43  }
44 
45  if(!got){
46  std::cout << "\nUniverseOracle: Failed to find globalTree in any open input file" << std::endl;
47  abort();
48  }
49 
50  return global;
51  }
52 
53  // --------------------------------------------------------------------------
54  void PrintSRGlobal(const caf::SRGlobal& global)
55  {
56  std::cout << global.wgts.size() << " parameter sets:" << std::endl;
57  for(unsigned int i = 0; i < global.wgts.size(); ++i){
58  const caf::SRWeightPSet& pset = global.wgts[i];
59  std::cout << " " << i << ": " << pset.name << ", type " << pset.type << ", " << pset.nuniv << " universes, adjusted parameters:" << std::endl;
60 
61  for(const caf::SRWeightMapEntry& entry: pset.map){
62  std::cout << " " << entry.param.name << std::endl;
63  }
64  }
65  }
66 
67  // --------------------------------------------------------------------------
69  {
70  static UniverseOracle uo;
71  return uo;
72  }
73 
74  // --------------------------------------------------------------------------
76  {
77  const caf::SRGlobal global = GetSRGlobal();
78  std::cout << "\nSystematic weights in file:" << std::endl;
79  PrintSRGlobal(global);
80 
81  for(unsigned int i = 0; i < global.wgts.size(); ++i){
82  const caf::SRWeightPSet& pset = global.wgts[i];
83 
84  // Save the pset index in all cases
85  fPSetIdxs[pset.name] = i;
86 
87  // Only save the remaining fields in parameter sets where only a single
88  // knob is shifted
89  if(pset.map.size() != 1) continue;
90 
91  // Save which position in the vector this was
92  fSystIdxs[pset.map[0].param.name] = i;
93  // Save all the knob values
94  fShiftVals[pset.map[0].param.name] = pset.map[0].vals;
95  }
96  }
97 
98  // --------------------------------------------------------------------------
99  bool UniverseOracle::SystExists(const std::string& name) const
100  {
101  return fShiftVals.find(name) != fShiftVals.end();
102  }
103 
104  // --------------------------------------------------------------------------
105  std::vector<std::string> UniverseOracle::Systs() const
106  {
107  std::vector<std::string> ret;
108  ret.reserve(fShiftVals.size());
109  for(auto it: fShiftVals) ret.push_back(it.first);
110  return ret;
111  }
112 
113  // --------------------------------------------------------------------------
114  const std::vector<float>& UniverseOracle::ShiftsForSyst(const std::string& name) const
115  {
116  assert(SystExists(name));
117  return fShiftVals.find(name)->second;
118  }
119 
120  // --------------------------------------------------------------------------
121  unsigned int UniverseOracle::ParameterSetIndex(const std::string& name) const
122  {
123  auto it = fPSetIdxs.find(name);
124  if(it == fPSetIdxs.end()){
125  std::cout << "UniverseOracle: pset '" << name << "' not known" << std::endl;
126  abort();
127  }
128  return it->second;
129  }
130 
131  // --------------------------------------------------------------------------
132  unsigned int UniverseOracle::SystIndex(const std::string& name) const
133  {
134  auto it = fSystIdxs.find(name);
135  if(it == fSystIdxs.end()){
136  std::cout << "UniverseOracle: syst '" << name << "' not known" << std::endl;
137  abort();
138  }
139  return it->second;
140  }
141 
142  // --------------------------------------------------------------------------
143  unsigned int UniverseOracle::ClosestShiftIndex(const std::string& name,
144  double shift,
145  ESide side,
146  double* trueShift) const
147  {
148  const std::vector<float>& v = ShiftsForSyst(name);
149  int bestIdx = -1;
150  double bestDist;
151  for(unsigned int i = 0; i < v.size(); ++i){
152  if(side == ESide::kBelow && v[i] > shift) continue;
153  if(side == ESide::kAbove && v[i] < shift) continue;
154  const double dv = fabs(v[i]-shift);
155  if(bestIdx == -1 || dv < bestDist){
156  bestIdx = i;
157  bestDist = dv;
158  if(trueShift) *trueShift = v[i];
159  }
160  }
161  return unsigned(bestIdx);
162  }
163 }
unsigned int SystIndex(const std::string &name) const
Which index in the weights array corresponds to the shifting of just this syst (in any parameter set)...
caf::SRGlobal GetSRGlobal()
std::map< std::string, unsigned int > fSystIdxs
ReweightType_t type
Definition: SRWeightPSet.h:17
const std::vector< float > & ShiftsForSyst(const std::string &name) const
List of shifts for this syst in each universe.
std::string name
Definition: SRWeightParam.h:11
ESide
Desired match type in UniverseOracle::ClosestShiftIndex.
process_name opflashCryoW ana
std::vector< SRWeightPSet > wgts
Definition: SRGlobal.h:13
shift
Definition: fcl_checks.sh:26
static UniverseOracle & Instance()
void PrintSRGlobal(const caf::SRGlobal &global)
std::vector< SRWeightMapEntry > map
Definition: SRWeightPSet.h:21
std::map< std::string, std::vector< float > > fShiftVals
std::string name
Definition: SRWeightPSet.h:16
std::vector< std::string > Systs() const
List of all known syst names.
std::map< std::string, unsigned int > fPSetIdxs
unsigned int ParameterSetIndex(const std::string &name) const
Which index in the weights array corresponds to this parameter set?
unsigned int ClosestShiftIndex(const std::string &name, double shift, ESide side=ESide::kEither, double *trueShift=0) const
Within that entry, which index corresponds most closely to &#39;shift&#39;?
bool SystExists(const std::string &name) const
then echo fcl name
BEGIN_PROLOG could also be cout