ATLAS Offline Software
JetCalibTools_PlotJESFactors.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include "xAODJet/Jet.h"
8 #include "xAODJet/JetContainer.h"
10 
11 #include "xAODRootAccess/TEvent.h"
12 #include "xAODRootAccess/TStore.h"
13 
15 
16 #include <vector>
17 #include "TString.h"
18 #include "TH2D.h"
19 #include "TCanvas.h"
20 
21 
22 namespace jet
23 {
33  public:
35  xAOD::JetFourMom_t operator()(const xAOD::Jet & jet) const {return const_cast<JetFourMomAccessor*>(this)->getAttribute(jet);}
36  };
37 }
38 
39 
40 
41 int main (int argc, char* argv[])
42 {
43  // Check argument usage
44  if (argc < 4 || argc > 6)
45  {
46  std::cout << "USAGE: " << argv[0] << " <JetCollection> <ConfigFile> <OutputFile> (dev mode switch) (JES_vs_E switch)" << std::endl;
47  return 1;
48  }
49 
50  // Store the arguments
51  const TString jetAlgo = argv[1];
52  const TString config = argv[2];
53  const TString outFile = argv[3];
54  const bool isDevMode = argc > 4 && (TString(argv[4]) == "true" || TString(argv[4]) == "dev");
55  const bool vsE = argc > 5 && (TString(argv[5]) == "true");
56 
57  // Derived information
58  const bool outFileIsExtensible = outFile.EndsWith(".pdf") || outFile.EndsWith(".ps") || outFile.EndsWith(".root");
59 
60  // Assumed constants
61  const TString calibSeq = "EtaJES"; // only want to apply the JES here
62  const bool isData = false; // doesn't actually matter for JES, which is always active
63  float massForScan = 80.385e3; // W-boson
64  if ( !jetAlgo.Contains("AntiKt10") ) massForScan = 0;
65 
66  // Accessor strings
67  TString startingScaleString = "JetConstitScaleMomentum";
68  if ( !jetAlgo.Contains("AntiKt10") ) startingScaleString = "JetPileupScaleMomentum";
69  const TString endingScaleString = "JetEtaJESScaleMomentum";
70  const TString detectorEtaString = "DetectorEta";
71 
72  // Accessors
73  jet::JetFourMomAccessor startingScale(startingScaleString.Data());
74  jet::JetFourMomAccessor endingScale(endingScaleString.Data());
75  SG::AuxElement::Accessor<float> detectorEta(detectorEtaString.Data());
76 
77 
78  // Create the calib tool
80  calibTool.setTypeAndName("JetCalibrationTool/MyJetCalibTool");
81  {
82  if (calibTool.initialize().isFailure())
83  {
84  std::cout << "Failed to make ana tool" << std::endl;
85  return 2;
86  }
87  if (calibTool.setProperty("JetCollection",jetAlgo.Data()).isFailure())
88  {
89  std::cout << "Failed to set JetCollection: " << jetAlgo.Data() << std::endl;
90  return 3;
91  }
92  if (calibTool.setProperty("ConfigFile",config.Data()).isFailure())
93  {
94  std::cout << "Failed to set ConfigFile: " << config.Data() << std::endl;
95  return 3;
96  }
97  if (calibTool.setProperty("CalibSequence",calibSeq.Data()).isFailure())
98  {
99  std::cout << "Failed to set CalibSequence: " << calibSeq.Data() << std::endl;
100  return 3;
101  }
102  if (calibTool.setProperty("IsData",isData).isFailure())
103  {
104  std::cout << "Failed to set IsData: " << (isData ? std::string("true") : std::string("false")) << std::endl;
105  return 3;
106  }
107  if (isDevMode && calibTool.setProperty("DEVmode",isDevMode).isFailure())
108  {
109  std::cout << "Failed to set DEVmode" << std::endl;
110  return 4;
111  }
112  if (calibTool.retrieve().isFailure())
113  {
114  std::cout << "Failed to initialize the JetCalibTool" << std::endl;
115  return 5;
116  }
117  }
118 
119 
120  // Build a jet container and a jet for us to manipulate later
124  jets->setStore(new xAOD::JetAuxContainer());
125  jets->push_back(new xAOD::Jet());
126  xAOD::Jet* jet = jets->at(0);
127 
128 
129  // Make the histogram to fill
130  TH2D* hist_pt_eta;
131  if ( jetAlgo.Contains("AntiKt10") ) hist_pt_eta = new TH2D("JES_pt_eta",Form("JES for jets with mass=%.1f GeV",massForScan/1.e3),1200,100,2500,60,-3,3);
132  else { hist_pt_eta = new TH2D("JES_pt_eta",Form("JES for jets with mass=%.1f GeV",massForScan/1.e3),2500,20,5000,90,-4.5,4.5); }
133 
134  // Fill the histogram
135  for (int xBin = 1; xBin <= hist_pt_eta->GetNbinsX(); ++xBin)
136  {
137  const double pt = hist_pt_eta->GetXaxis()->GetBinCenter(xBin)*1.e3; // E if vsE
138  for (int yBin = 1; yBin <= hist_pt_eta->GetNbinsY(); ++yBin)
139  {
140  const double eta = hist_pt_eta->GetYaxis()->GetBinCenter(yBin);
141 
142  // Set the main 4-vector and scale 4-vector
143  if ( !vsE ){
144  jet->setJetP4(xAOD::JetFourMom_t(pt,eta,0,massForScan));
145  detectorEta(*jet) = eta;
146  startingScale.setAttribute(*jet,xAOD::JetFourMom_t(pt,eta,0,massForScan));
147  } else {
148  const double E = pt; // pt is actually E if vsE
149  const double pT = sqrt((E*E)-(massForScan*massForScan))/cosh(eta);
150  jet->setJetP4(xAOD::JetFourMom_t(pT,eta,0,massForScan));
151  detectorEta(*jet) = eta;
152  startingScale.setAttribute(*jet,xAOD::JetFourMom_t(pT,eta,0,massForScan));
153  }
154  const double startingE = startingScale(*jet).e();
155 
156  // Jet kinematics set, now apply calibration
157  if(calibTool->modify(*jets).isFailure()){
158  std::cout << "Failed to apply jet calibration" << std::endl;
159  return 6;
160  }
161  // Calculate the scale factors
162  const double JES = jet->e()/startingE;
163 
164  // JMS retrieved, fill the plot(s)
165  hist_pt_eta->SetBinContent(xBin,yBin,JES);
166  }
167  }
168 
169 
170 
171  // Make the plots
172 
173  // First the canvas
174  TCanvas* canvas = new TCanvas("canvas");
175  canvas->SetMargin(0.07,0.13,0.1,0.10);
176  canvas->SetFillStyle(4000);
177  canvas->SetFillColor(0);
178  canvas->SetFrameBorderMode(0);
179  canvas->cd();
180  canvas->SetLogx(true);
181 
182  // Now labels/etc
183  hist_pt_eta->SetStats(false);
184  if ( !vsE ) hist_pt_eta->GetXaxis()->SetTitle("Jet #it{p}_{T} [GeV]");
185  else { hist_pt_eta->GetXaxis()->SetTitle("Jet E [GeV]"); }
186  hist_pt_eta->GetXaxis()->SetTitleOffset(1.35);
187  hist_pt_eta->GetXaxis()->SetMoreLogLabels();
188  hist_pt_eta->GetYaxis()->SetTitle("#eta");
189  hist_pt_eta->GetYaxis()->SetTitleOffset(0.9);
190  hist_pt_eta->GetZaxis()->SetTitle("JES_{Factor}");
191 
192  // Now write them out
193  if (outFileIsExtensible)
194  {
195 
196  if ( outFile.EndsWith(".pdf") || outFile.EndsWith(".ps") ){
197  canvas->Print(outFile+"[");
198  hist_pt_eta->Draw("colz");
199  canvas->Print(outFile);
200  canvas->Print(outFile+"]");
201  } else if ( outFile.EndsWith(".root") ){
202  TFile *fout = new TFile(outFile.Data(),"RECREATE");
203  hist_pt_eta->Write();
204  fout->Close();
205  delete fout;
206  }
207 
208  }
209  else
210  {
211  unsigned int counter = 1;
212  hist_pt_eta->Draw("colz");
213  canvas->Print(Form("%u-fixMass-%s",counter++,outFile.Data()));
214  }
215 
216  delete hist_pt_eta;
217 
218  return 0;
219 }
store
StoreGateSvc * store
Definition: fbtTestBasics.cxx:71
CalculateHighPtTerm.pT
pT
Definition: ICHEP2016/CalculateHighPtTerm.py:57
Jet.h
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
xAOD::JetAttributeAccessor::AccessorWrapper::setAttribute
void setAttribute(SG::AuxElement &p, const TYPE &v) const
Definition: JetAccessors.h:54
test_pyathena.pt
pt
Definition: test_pyathena.py:11
xAOD::JetAttributeAccessor::AccessorWrapper< xAOD::JetFourMom_t >::getAttribute
void getAttribute(const SG::AuxElement &p, xAOD::JetFourMom_t &v) const
Definition: JetAccessors.h:58
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
xAOD::JetAuxContainer_v1
Temporary container used until we have I/O for AuxStoreInternal.
Definition: JetAuxContainer_v1.h:37
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
JetCalibrationTool.h
asg::StandaloneToolHandle::initialize
StatusCode initialize()
initialize the tool, will fail if the tool was already initialized
Definition: StandaloneToolHandle.h:158
CheckAppliedSFs.e3
e3
Definition: CheckAppliedSFs.py:264
jet::JetFourMomAccessor::operator()
xAOD::JetFourMom_t operator()(const xAOD::Jet &jet) const
Definition: JetCalibTools_PlotJESFactors.cxx:35
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
asg::StandaloneToolHandle::setProperty
StatusCode setProperty(const std::string &name, T2 &&value)
Definition: StandaloneToolHandle.h:105
asg::StandaloneToolHandle
an "initializing" ToolHandle for stand-alone applications
Definition: StandaloneToolHandle.h:44
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
DeMoAtlasDataLoss.canvas
dictionary canvas
Definition: DeMoAtlasDataLoss.py:187
dqt_zlumi_alleff_HIST.fout
fout
Definition: dqt_zlumi_alleff_HIST.py:59
TEvent.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
jet::JetFourMomAccessor
JetFourMomAccessor is an extension of JetAttributeAccessor::AccessorWrapper<xAOD::JetFourMom_t> Acces...
Definition: JetCalibTools_PlotJESFactors.cxx:32
IJetCalibrationTool::modify
virtual StatusCode modify(xAOD::JetContainer &jets) const override final
Apply calibration to a jet container (for IJetModifier interface).
Definition: IJetCalibrationTool.h:33
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
asg::StandaloneToolHandle::retrieve
StatusCode retrieve()
initialize the tool, will succeed if the tool was already initialized
Definition: StandaloneToolHandle.h:147
DQPostProcessTest.outFile
outFile
Comment Out Those You do not wish to run.
Definition: DQPostProcessTest.py:37
xAOD::JetFourMom_t
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition: JetTypes.h:17
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21
asg::StandaloneToolHandle::setTypeAndName
void setTypeAndName(const std::string &typeAndName)
Definition: StandaloneToolHandle.h:101
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
main
int main(int argc, char *argv[])
Definition: JetCalibTools_PlotJESFactors.cxx:41
xAOD::TStore
A relatively simple transient store for objects created in analysis.
Definition: TStore.h:44
JetContainer.h
xAOD::JetAttributeAccessor::AccessorWrapper
Definition: JetAccessors.h:49
JetAuxContainer.h
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
StandaloneToolHandle.h
test_pyathena.counter
counter
Definition: test_pyathena.py:15
TStore.h
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84