AnalyzeWeighters_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: AnalyzeWeighters
3 // Module Type: analyzer
4 // File: AnalyzeWeighters_module.cc
5 //
6 //Purpose: The purpose of this module is to make histograms of Systematic weights
7 // as a function of neutrino energy for different values of sigmas
8 //
9 // Generated at Mon Mar 13 12:34:32 2017 by Prabhjot Singh using artmod
10 // from cetpkgsupport v1_10_02.
11 ////////////////////////////////////////////////////////////////////////
12 
17 #include "fhiclcpp/ParameterSet.h"
21 
22 //FNEX includes
23 #include "FNEX/core/Event.h"
25 #include "FNEX/core/VarVals.h"
29 
30 //ROOT includes
31 #include <TGraph.h>
32 #include <TCanvas.h>
33 #include <TLegend.h>
34 #include <TStyle.h>
35 
36 namespace fnex {
37  class AnalyzeWeighters;
38 }
39 
41 public:
42  explicit AnalyzeWeighters(fhicl::ParameterSet const & p);
43  // The destructor generated by the compiler is fine for classes
44  // without bare pointers or other resource use.
45 
46  // Plugins should not be copied or assigned.
47  AnalyzeWeighters(AnalyzeWeighters const &) = delete;
49  AnalyzeWeighters & operator = (AnalyzeWeighters const &) = delete;
51 
52  // Required functions.
53  void analyze(art::Event const & e) override;
54  void reconfigure(fhicl::ParameterSet const & p);
55 
56 private:
57  // Declare member data here.
58  std::string fWeighter; //Name of the weighter that we want to test.
59  std::unique_ptr<fnex::InputPoint> fInitialGuessInputPoint; //Initial guess used to get Oscillation parameters, Systematic pulls etc
61 
62  TH1* hSigmahistograms[30]; //Array of histograms
63 
64  void Weights(); //Function to get weights
65 };
66 //--------------------------------------------------------------------------------------------//
68  :
69  EDAnalyzer(p) // ,
70  // More initializers here.
71 {
72  this->reconfigure(p);
73 
74  return;
75 }
76 //--------------------------------------------------------------------------------------------//
78 {
79  int nevents = 12; //Number of events. 12 events for 12 bin. 1 event per bin
80  int n_sigmas = 3; //Set number of sigmas that you want to make histograms to.
81  float pull = 0.0; //Initial Shift
82  float pull_increment = 0.5; //Increment in shift
83  double energy = 0.; //Energy initialization
84  double eventweight = 0.; //weight initialization
85 
86  //Canvas for storing all histograms
87  gStyle->SetOptStat("");
88  TCanvas* canvas = tfs->make<TCanvas>("canvas", "Weights for different Sigma shifts", 800, 800);
89  canvas->cd();
90  canvas->SetLeftMargin(0.20);
91 
92  //Legends for different histograms
93  TLegend* legend = new TLegend(0.65, 0.63, 0.88, 0.88);
94  legend ->SetBorderSize(0);
95  legend ->SetFillStyle(0);
96  legend ->SetTextSize(0.04);
97 
98  //Metadata: used by EventWeight function
99  fnex::MetaData md(true,
101  fnex::kSwap,
103  fnex::kNuECC,
104  "3b");
105 
107 
108  int Nsigmas = (int)n_sigmas/pull_increment;
109 
110  for(int i_sigma = 0; i_sigma < Nsigmas; ++i_sigma){
111 
112  //Make histograms
113  hSigmahistograms[i_sigma] = tfs->make<TH1F>(TString::Format("sigma_%2.2f", pull), ";E_{#nu} (GeV); Systematic. Weight Values", 12, 0, 3);
114  hSigmahistograms[i_sigma] ->SetTitle(TString::Format("Sigma_%2.2f", pull) );
115  hSigmahistograms[i_sigma] ->SetLineColor(kBlack+i_sigma);
116  hSigmahistograms[i_sigma] ->SetLineWidth(2);
117 
118  //Systematic pull used from the initial guess input point
119  ParameterDetMap pSystematicPulls = fInitialGuessInputPoint->SystematicPulls();
120 
121  //Print keys
122  for(auto pd: pSystematicPulls)
123  std::cout << "Keys are: " << pd.first.first << std::endl;
124 
125  //Set Systematic Sigma values here
127  if(pSystematicPulls.count(tempPairFar) != 0){
128  std::cout << fWeighter << " is the correct key." << std::endl;
129  pSystematicPulls[tempPairFar] = pull;
130  }
131  else std::cout << fWeighter << " is not the correct key" << std::endl;
132 
133  //New input point for every sigma shift value
134  //Initial Guess Input Point is used to make a new input point for each Sigma value
135  InputPoint newPoint(fInitialGuessInputPoint->ParameterSpaceLocation(),
136  pSystematicPulls,
137  fInitialGuessInputPoint->FixedParameters(),
138  fInitialGuessInputPoint->FixedSystematics());
139 
141 
142  //double parVal = newPoint.ParameterValue(std::make_pair("RadCorrNueWgt", md.detector));
143 
144  //loop over events
145  for(int e = 0; e < nevents; ++e){
146 
147  //Make a fake event here
148  fnex::EventPtr event(new fnex::Event);
149 
150  //Set energies for a new fake event
151  energy = (1. + double(e))*0.25 - 0.125;
152 
153  //Set some variable for fake event here
154  event->dataVals = std::make_unique<fnex::DataVarVals>();
155  event->mcVals = std::make_unique<fnex::MCVarVals >();
156 
157 // event->dataVals->set_val_at(fnex::kLep_RecoE, energy);
158 // event->dataVals->set_val_at(fnex::kHad_RecoE, 0. );
159  event->dataVals->set_val_at(fnex::kNu_RecoE, energy);
160  event->mcVals ->set_val_at(fnex::kTruePDGOrig, 14 );
161  event->mcVals ->set_val_at(fnex::kTruePDG, 14 );
162  event->mcVals ->set_val_at(fnex::kTrueCCNC, 0. );
163  event->mcVals ->set_val_at(fnex::kTrueIntType, 1000 );
164  event->dataVals->set_val_at(fnex::kNuE_CVN, 0.8 );
165 
166  //Get the value of the weight for the weighter
167  // if(event->mcVals->val_at(fnex::kTrueCCNC) == 1 &&
168  // event->mcVals->val_at(fnex::kTruePDG ) == 12 )
169  // eventweight = 1.0 + 0.02 * parVal;
170  // else eventweight = 1.0;
171  eventweight = fnex::ShifterAndWeighter::Instance()->Weight(event, md);
172  //eventweight = fnex::ShifterAndWeighter::Instance()->TotalWeightFromWeighters();
173 
174  //Fill and Draw histograms
175  hSigmahistograms[i_sigma]->Fill(energy, eventweight);
176  hSigmahistograms[i_sigma]->Draw("HIST same");
177  LOG_VERBATIM("AnalyzeWeighters")
178  << "Event energy set: "
179  << energy
180  << " Sigma: "
181  << pull
182  << " Weight: "
183  << eventweight;
184 
185  }//end of loop over different energies
186 
187  //Add legend entry for each histogram here
188  legend ->AddEntry(hSigmahistograms[i_sigma], TString::Format("%2.2f #sigma", pull), "l");
189 
190  //Increment systematic pull term here
191  pull += pull_increment;
192 
193  }//end of loop over different sigma values
194 
195  //Set user defined axis range and draw legend here.
196  //hSigmahistograms[0]->GetYaxis()->SetRangeUser(0, 1.5);
197  hSigmahistograms[0]->SetTitle(fWeighter.c_str());
198  hSigmahistograms[0]->GetXaxis()->CenterTitle();
199  hSigmahistograms[0]->GetXaxis()->SetDecimals();
200  hSigmahistograms[0]->GetYaxis()->CenterTitle();
201  hSigmahistograms[0]->GetYaxis()->SetDecimals();
202  hSigmahistograms[0]->GetYaxis()->SetTitleOffset(1.5);
203 
204 
205  legend ->Draw();
206 
207  // Save the canvas to file
208  canvas ->Update();
209  canvas ->Write();
210 
211  return;
212 }
213 
214 //--------------------------------------------------------------------------------------------//
216 {
217  // Implementation of optional member function here.
218  fWeighter = p.get< std::string >("Weighter" );
219  fInitialGuessInputPoint = std::make_unique<InputPoint>(p.get< fhicl::ParameterSet>("InitialGuessInputPoint") );
220 }
221 //--------------------------------------------------------------------------------------------//
void SetCurrentVals(fnex::InputPoint const &curPoint)
static const unsigned char kNu_RecoE
Definition: VarVals.h:36
static const unsigned char kNuE_CVN
Definition: VarVals.h:32
void analyze(art::Event const &e) override
art::ServiceHandle< art::TFileService > tfs
void InitShiftsAndWeightsToUse(fnex::InputPoint const &curPoint, std::string CalibFilename="FNEX/data/calibration/CalibrationSystematics2018.root")
const char * p
Definition: xmltok.h:285
void reconfigure(fhicl::ParameterSet const &p)
std::pair< Spectrum *, CheatDecomp * > make_pair(SpectrumLoaderBase &loader_data, SpectrumLoaderBase &loader_mc, HistAxis *axis, Cut *cut, const SystShifts &shift, const Var &wei)
Definition: DataMCLoad.C:336
Create a list of fnex::Events to be used in fits.
static ShifterAndWeighter * Instance()
Int_t nevents
Definition: SimpleIterate.C:25
DEFINE_ART_MODULE(SpectrumTest)
static const unsigned char kTruePDGOrig
Definition: VarVals.h:46
static const unsigned char kTrueCCNC
Definition: VarVals.h:44
Far Detector at Ash River, MN.
AnalyzeWeighters & operator=(AnalyzeWeighters const &)=delete
AnalyzeWeighters(fhicl::ParameterSet const &p)
double Weight(fnex::EventPtr const &curEvent, fnex::MetaData const &md, fnex::WeightType_t const wgtType=kAllWgt)
T get(std::string const &key) const
Definition: ParameterSet.h:231
double energy
Definition: plottest35.C:25
static const unsigned char kTrueIntType
Definition: VarVals.h:45
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
std::unordered_map< ParameterDet, double, ParameterDetHasher > ParameterDetMap
Definition: Constants.h:63
OStream cout
Definition: OStream.cxx:6
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T * make(ARGS...args) const
std::shared_ptr< Event > EventPtr
Definition: Event.h:50
std::pair< std::string, novadaq::cnv::DetId > ParameterDet
Definition: Constants.h:38
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
#define LOG_VERBATIM(category)
Float_t e
Definition: plot.C:35
std::unique_ptr< fnex::InputPoint > fInitialGuessInputPoint
static const unsigned char kTruePDG
Definition: VarVals.h:43