7#ifndef __TPILEUPREWEIGHTING__
8#define __TPILEUPREWEIGHTING__
61 Int_t
SetBinning(Int_t nbinsx, Double_t* xbins, Int_t nbinsy=0, Double_t* ybins=0);
62 Int_t
SetUniformBinning(Int_t nbinsx, Double_t xlow, Double_t xup, Int_t nbinsy=0, Double_t ylow=0, Double_t yup=0);
76 if(global->numberOfEntries.find(channel)==global->numberOfEntries.end()) {
77 Error(
"GetNumberOfEvents",
"Unknown channel: %d",channel);
80 return global->numberOfEntries[channel];
85 if(global->sumOfWeights.find(channel)==global->sumOfWeights.end()) {
86 Error(
"GetSumOfEventWeights",
"Unknown channel: %d",channel);
89 return global->sumOfWeights[channel];
93 void RemapPeriod(Int_t periodNumber1, Int_t periodNumber2);
125 if(action<0 || action>3) {
126 Fatal(
"SetUnrepresentedDataAction",
"Set action=%d - I'm afraid I can't let you do that, Dave",action);
127 throw std::runtime_error(
"Throwing 2001");
136 Warning(
"GetUnrepresentedDataWeight",
"You should not be applying this weight unless the UnrepresentedDataAction=2");
149 Int_t
AddPeriod(Int_t periodNumber, UInt_t start, UInt_t end);
165 Int_t
AddLumiCalcFile(
const TString& fileName,
const TString& trigger=
"None");
166 Int_t
AddMetaDataFile(
const TString& fileName,
const TString& channelBranchName=
"mc_channel_number");
188 Double_t
GetMetaData(
const TString& metadataName,Int_t channelNumber) {
190 Error(
"GetMetaData",
"Metadata %s not known",metadataName.Data());
194 Error(
"GetMetaData",
"Metadata %s not known in channel %d",metadataName.Data(), channelNumber);
197 return m_metadata[metadataName][channelNumber];
201 Int_t
GenerateMetaDataFile(
const TString& fileName,
const TString& channelBranchName=
"mc_channel_number");
207 Int_t
Fill(Int_t runNumber,Int_t channelNumber,Float_t w,Float_t
x, Float_t
y=0.);
215 ULong64_t
GetPRWHash(Int_t periodNumber, Int_t channelNumber, Float_t
x, Float_t
y=0.);
216 Bool_t
MakeWeightTree(TString channelNumbers, TString outFile, TString hashBranch=
"PRWHash", TString weightBranch=
"PileupWeight");
238 Int_t
Merge(TCollection *coll);
243 std::vector<int> out;
244 for(
auto& p :
m_periods) { out.push_back(p.first); }
253 m_periods[periodNumber]->inputHists.find(channelNumber)==
m_periods[periodNumber]->inputHists.end()) {
256 return m_periods[periodNumber]->inputHists[channelNumber].get();
261 m_periods[periodNumber]->primaryHists.find(channelNumber)==
m_periods[periodNumber]->primaryHists.end()) {
264 return m_periods[periodNumber]->primaryHists[channelNumber].get();
269 m_triggerObjs[trigger]->triggerHists[periodNumber].find(triggerBits) ==
m_triggerObjs[trigger]->triggerHists[periodNumber].end())
return 0;
275 return m_triggerObjs[trigger]->triggerHists[periodNumber][triggerBits].get();
279 Double_t
GetDataWeight(Int_t runNumber,
const TString& trigger, Double_t
x,
bool run_dependent=
false);
280 Double_t
GetDataWeight(Int_t runNumber,
const TString& trigger);
283 Double_t
GetPrescaleWeight(Int_t runNumber,
const TString& trigger, Double_t
x,
bool run_dependent=
false);
290 Int_t
AddDistribution(TH1* hist, Int_t runNumber, Int_t channelNumber);
308 virtual bool runLbnOK(Int_t , Int_t ) {
return true; }
313 }
catch(...) {
return true; }
368 std::unique_ptr< CompositeTrigger >
trig1;
369 std::unique_ptr< CompositeTrigger >
trig2;
375 case 0:
if(m[
val][
run].
find(lbn)==m[
val][
run].end() || !m[
val][
run][lbn] || !tool->passTriggerBeforePrescale(
val))
return 0;
376 return 1./m[
val][
run][lbn];
377 case 1:
return 1. - (1. -
trig1->eval(m,
run,lbn,tool))*(1.-
trig2->eval(m,
run,lbn,tool));
384 else {
trig1->getTriggers(s);
trig2->getTriggers(s); }
403 std::unique_ptr<CompositeTrigger>
makeTrigger(
const TString& s);
425 if(runNumber >=
start && runNumber <=
end)
return true;
426 for(
auto p :
subPeriods)
if(p->contains(runNumber))
return true;
431 for(
auto p :
subPeriods) p->SetDefaultChannel(channel);
434 std::cout << prefix <<
id <<
"[" <<
start <<
"," <<
end <<
"] : ";
435 for(
auto& hist :
inputHists) std::cout << hist.first <<
" , ";
436 std::cout << std::endl;
437 for(
auto p :
subPeriods) p->print(TString::Format(
" %s",prefix).
Data()); };
443 std::map<UInt_t, std::pair<Double_t,Double_t> >
lumiByLbn;
void normalizeHistogram(TH1 *histo)
Normalize histograms.
Double_t GetLumiBlockIntegratedLumi(Int_t runNumber, UInt_t lb)
get integrated lumi for specific run and lumiblock number .
Int_t Fill(Int_t runNumber, Int_t channelNumber, Float_t w, Float_t x, Float_t y=0.)
std::map< UInt_t, Run > & GetRunMap()
void RemapPeriod(Int_t periodNumber1, Int_t periodNumber2)
Combine two period numbers.
void ResetCountingMode()
This method is DEFINITELY EXPERT USE ONLY.
Int_t WriteToFile(const TString &filename="")
std::map< Int_t, Double_t > unrepDataByChannel
Double_t GetIntegratedLumiFraction(Int_t periodNumber, UInt_t start, UInt_t end)
return fraction of lumi assigned to periodNumber (or mcRunNumber) that is between start and end data ...
virtual bool passTriggerBeforePrescale(const TString &trigger) const
double GetRunAverageMu(int run)
Int_t Merge(TCollection *coll)
std::map< TString, std::map< Int_t, Double_t > > m_metadata
channel metadata map
TH1 * GetInputHistogram(Int_t channelNumber, Int_t periodNumber)
Float_t GetSecondaryWeight(Int_t periodNumber, Int_t channelNumber, Float_t x, Float_t y)
std::map< UInt_t, Run > m_runs
Double_t GetNumberOfEvents(Int_t channel)
totalMC maps should also hold the total number of entries for each channel
Float_t GetDataScaleFactor() const
Int_t Initialize()
Initialize this class once before the event loop starts If distribution information is provided,...
void SetMCScaleFactors(Float_t x, Float_t y=1.)
Bool_t m_doPrescaleWeight
Int_t GetRandomPeriodNumber(Int_t mcRunNumber)
Get random period number from the sub-periods assigned to this run number.
Int_t AddPeriod(Int_t periodNumber, UInt_t start, UInt_t end)
Assign an mc RunNumber to a data period.
std::unique_ptr< TRandom3 > m_random3
Double_t GetIntegratedLumi(UInt_t start, UInt_t end)
get the amount of integrated lumi between the two given runs
void EnableDebugging(Bool_t in)
Indicate if additional debugging information should be output.
Int_t GetNearestGoodBin(Int_t thisMCRunNumber, Int_t bin)
TH1 * GetPrimaryDistribution(Int_t channelNumber, Int_t periodNumber)
Bool_t m_lumiVectorIsLoaded
std::list< Period > m_periodList
List physically holding (owning) period objects.
Float_t GetCombinedWeight(Int_t periodNumber, Int_t channelNumber, Float_t x, Float_t y=0.)
Int_t UsePeriodConfig(const TString &configName)
use a hardcoded period configuration
void SetDefaultChannel(Int_t channel, Int_t mcRunNumber=-1)
Set which channel should be used as a default when specific mc channel distributions cannot be found.
std::vector< int > GetPeriodNumbers() const
Bool_t m_ignoreFilePeriods
std::unique_ptr< CompositeTrigger > makeTrigger(const TString &s)
Bool_t IsUnrepresentedData(Int_t runNumber, Float_t x, Float_t y=0.)
void CalculatePrescaledLuminosityHistograms(const TString &trigger, int run_dependent=0)
Double_t GetIntegratedLumi(const TString &trigger="")
total luminosity loaded and accepted by the tool (in inverse pb)
Int_t SetUniformBinning(Int_t nbinsx, Double_t xlow, Double_t xup, Int_t nbinsy=0, Double_t ylow=0, Double_t yup=0)
Double_t GetDataWeight(Int_t runNumber, const TString &trigger, Double_t x, bool run_dependent=false)
Method for weighting data to account for prescales and mu bias.
std::map< Int_t, Period * > m_periods
Int_t AddMetaDataFile(const TString &fileName, const TString &channelBranchName="mc_channel_number")
std::unique_ptr< TH1 > m_emptyHistogram
the empty histogram used for this weight... effectively holds the configuration of the binning
Bool_t RemoveChannel(int chanNum)
Removes a channel from the inputs ... this is for experts only.
UInt_t GetRandomRunNumber(Int_t mcRunNumber)
Gets a random data run number according to the integrated lumi distribution associated to this mcRunN...
Float_t GetPrimaryWeight(Int_t periodNumber, Int_t channelNumber, Float_t x)
Int_t GenerateMetaDataFile(const TString &fileName, const TString &channelBranchName="mc_channel_number")
void SetTriggerBit(const TString &trigger, bool in=true)
TH1 * GetPrimaryTriggerDistribution(const TString &trigger, Int_t periodNumber, long triggerBits)
Double_t GetPrescaleWeight(Int_t runNumber, const TString &trigger, Double_t x, bool run_dependent=false)
Method for prescaling MC to account for prescales in data.
void PrintInfo(Bool_t in)
disable info
Int_t AddLumiCalcFile(const TString &fileName, const TString &trigger="None")
Int_t GetFirstFoundPeriodNumber(UInt_t runNumber)
Get the first period number with the data run number contained - assume all periods are disconnected ...
void calculateHistograms(CompositeTrigger *trigger, int run_dependent)
Float_t m_dataScaleFactorX
Int_t AddConfigFile(const TString &fileName)
Float_t m_dataScaleFactorY
UInt_t GetRandomLumiBlockNumber(UInt_t runNumber)
Get a random lumi block from the run number given.
void DisableWarnings(Bool_t in)
Indicate if warnings should be suppressed.
std::map< TString, bool > m_triggerPassBits
Float_t GetLumiBlockMu(Int_t runNumber, UInt_t lb)
get the lumiblock mu, useful for 'updating' the mu coming from data to account for new lumitags
Int_t GetDefaultChannel(Int_t mcRunNumber=-1)
void AddDistributionTree(TTree *tree, TFile *file)
Int_t SetBinning(Int_t nbinsx, Double_t *xbins, Int_t nbinsy=0, Double_t *ybins=0)
Add a histogram binning config.
std::map< TString, std::unique_ptr< CompositeTrigger > > m_triggerObjs
Bool_t m_doGlobalDataWeight
void CopyProperties(const TPileupReweighting *in)
Double_t GetUnrepresentedDataFraction(Int_t periodNumber, Int_t channel)
return the unrepresented data fraction in a given channel .
Float_t GetUnrepresentedDataWeight(Int_t periodNumber, Int_t channel)
TTree * GetMetaDataTree()
combines loaded metadata with channel sumsofweights and entry counts
virtual bool runLbnOK(Int_t, Int_t)
TPileupReweighting(const char *name="TPileupReweighting")
Standard constructor.
Bool_t m_ignoreBadChannels
ULong64_t GetPRWHash(Int_t periodNumber, Int_t channelNumber, Float_t x, Float_t y=0.)
std::string m_prwFilesPathPrefix
void IgnoreConfigFilePeriods(Bool_t in)
Should the tool ignore period assignments encoded in config file.
Int_t IsBadBin(Int_t thisMCRunNumber, Int_t bin)
TPileupReweighting * m_parentTool
Float_t GetPeriodWeight(Int_t periodNumber, Int_t channelNumber)
void SetUnrepresentedDataAction(Int_t action, Double_t tolerance=0.05)
Set how to handle configurations where some of your data has no corresponding mc to describe it 0=Def...
std::unique_ptr< TH1 > CloneEmptyHistogram(Int_t runNumber, Int_t channelNumber)
std::map< TString, std::vector< TString > > m_lumicalcFiles
map storing the lumicalc file locations - used when building DataPileupWeights
void SetRandomSeed(int seed)
Int_t AddDistribution(TH1 *hist, Int_t runNumber, Int_t channelNumber)
Double_t m_unrepDataTolerance
Int_t m_lumicalcRunNumberOffset
Double_t GetSumOfEventWeights(Int_t channel)
void SetParentTool(TPileupReweighting *tool)
Bool_t MakeWeightTree(TString channelNumbers, TString outFile, TString hashBranch="PRWHash", TString weightBranch="PileupWeight")
void SetDataScaleFactors(Float_t x, Float_t y=1.)
Scale the LumiMetaData mu values by this factor.
Int_t m_unrepresentedDataAction
Double_t GetMetaData(const TString &metadataName, Int_t channelNumber)
std::string find(const std::string &s)
return a remapped string
Select isolated Photons, Electrons and Muons.
std::unique_ptr< CompositeTrigger > trig2
long getBits(const TPileupReweighting *tool)
void getTriggers(std::vector< TString > &s)
double eval(std::map< TString, std::map< Int_t, std::map< Int_t, Float_t > > > &m, int run, int lbn, const TPileupReweighting *tool)
std::map< int, std::map< long, std::unique_ptr< TH1 > > > triggerHists
std::vector< TString > subTriggers
std::unique_ptr< CompositeTrigger > trig1
std::map< Int_t, std::unique_ptr< TH1 > > inputHists
std::map< Int_t, Double_t > sumOfWeights
std::map< Int_t, std::unique_ptr< TH1 > > primaryHists
std::map< Int_t, Int_t > inputBinRedirect
bool contains(unsigned int runNumber)
std::map< Int_t, Int_t > numberOfEntries
std::vector< UInt_t > runNumbers
std::map< Int_t, std::unique_ptr< TH2 > > secondaryHists
std::map< Int_t, Double_t > unrepData
std::vector< Period * > subPeriods
Period(Int_t _id, UInt_t _start, UInt_t _end, Int_t _defaultChannel)
void SetDefaultChannel(Int_t channel)
void print(const char *prefix)
std::map< Int_t, Double_t > badBins
std::map< TString, std::unique_ptr< TH1 > > inputHists
std::map< UInt_t, std::pair< Double_t, Double_t > > lumiByLbn
std::unique_ptr< TH1 > muDist