JHUGen MELA  v2.4.1
Matrix element calculations as used in JHUGen. MELA is an important tool that was used for the Higgs boson discovery and for precise measurements of its structure and interactions. Please see the website https://spin.pha.jhu.edu/ and papers cited there for more details, and kindly cite those papers when using this code.
calcC_lintolog.c
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <iomanip>
4 #include <utility>
5 #include <algorithm>
6 #include <cmath>
7 #include <cassert>
8 #include <string>
9 #include <vector>
10 #include <fstream>
11 #include <cstdlib>
12 #include <unordered_map>
13 #include "TROOT.h"
14 #include "TMath.h"
15 #include "TLorentzVector.h"
16 #include "TLorentzRotation.h"
17 #include "TFile.h"
18 #include "TTree.h"
19 #include "TChain.h"
20 #include "TString.h"
21 #include "TF1.h"
22 #include "TSpline.h"
23 #include "TCanvas.h"
24 #include "TH1F.h"
25 #include "TH2F.h"
26 #include "TH3F.h"
27 #include "TProfile.h"
28 #include "TGraphErrors.h"
29 #include "TRandom.h"
30 #include "RooNumIntConfig.h"
31 #include "RooRealIntegral.h"
32 #include "Mela.h"
33 #include "ScalarPdfFactory_VH.h"
34 
35 
36 using namespace std;
37 using namespace RooFit;
38 
39 
40 TString inputdir_7TeV = "/work-zfs/lhc/ianderso/hep/CJLST/140519/PRODFSR";
41 TString inputdir_8TeV = "/work-zfs/lhc/ianderso/hep/CJLST/140519b/PRODFSR_8TeV";
42 TString inputdir_13TeV = "/work-zfs/lhc/CJLSTtrees/180214_2016MC";
43 
44 
45 template<typename T> void addByLowest(std::vector<T>& valArray, T val, bool unique){
46  bool inserted = false;
47  if (unique){
48  for (typename std::vector<T>::iterator it = valArray.begin(); it<valArray.end(); it++){
49  if (*it==val){
50  inserted=true;
51  break;
52  }
53  }
54  }
55  if (!inserted){
56  for (typename std::vector<T>::iterator it = valArray.begin(); it<valArray.end(); it++){
57  if (*it>=val){
58  inserted=true;
59  valArray.insert(it, val);
60  break;
61  }
62  }
63  }
64  if (!inserted) valArray.push_back(val);
65 }
66 
67 template<typename T, typename U> void addByLowest(std::vector<std::pair<T, U>>& valArray, T val, U index){
68  bool inserted = false;
69  for (typename std::vector<std::pair<T, U>>::iterator it = valArray.begin(); it<valArray.end(); it++){
70  if ((*it).first>=val){
71  inserted=true;
72  if ((*it).second!=index) valArray.insert(it, std::pair<T, U>(val, index));
73  break;
74  }
75  }
76  if (!inserted) valArray.push_back(std::pair<T, U>(val, index));
77 }
78 
79 template<typename T> void appendVector(std::vector<T>& a, std::vector<T>& b){ a.insert(a.end(), b.begin(), b.end()); }
80 
81 template<typename T> bool checkListVariable(const vector<T>& list, const T& var){
82  for (unsigned int v=0; v<list.size(); v++){
83  if (list.at(v)==var) return true; // Look for exact match
84  }
85  return false;
86 }
87 
88 void splitOption(const string rawoption, string& wish, string& value, char delimiter){
89  size_t posEq = rawoption.find(delimiter);
90  if (posEq!=string::npos){
91  wish=rawoption;
92  value=rawoption.substr(posEq+1);
93  wish.erase(wish.begin()+posEq, wish.end());
94  }
95  else{
96  wish="";
97  value=rawoption;
98  }
99 }
100 void splitOptionRecursive(const string rawoption, vector<string>& splitoptions, char delimiter){
101  string suboption=rawoption, result=rawoption;
102  string remnant;
103  while (result!=""){
104  splitOption(suboption, result, remnant, delimiter);
105  if (result!="" && !checkListVariable(splitoptions, result)) splitoptions.push_back(result);
106  suboption = remnant;
107  }
108  if (remnant!="") splitoptions.push_back(remnant);
109 }
110 
111 struct ExtBin{
112  double binlow;
113  double binhigh;
114  vector<int> events;
115  vector<float> masses;
116  vector<float> mevals;
117  vector<float> me2vals;
118  vector<float> weights;
119 
120  void addEvent(float mass, float me, float me2, float weight=1){
121  masses.push_back(mass);
122  mevals.push_back(me);
123  me2vals.push_back(me2);
124  weights.push_back(weight);
125  }
126  void sift(){
127  if (masses.size()==0) return;
128 
129  vector<int> take_out;
130  vector<pair<float, int>> me_entry[2];
131  for (unsigned int ev=0; ev<masses.size(); ev++){
132  addByLowest<float, int>(me_entry[0], mevals.at(ev), ev);
133  addByLowest<float, int>(me_entry[1], me2vals.at(ev), ev);
134  }
135  for (unsigned int im=0; im<2; im++){
136  int at99p8ev = (float(me_entry[im].size()))*0.998;
137  if (at99p8ev==(int)me_entry[im].size()) at99p8ev--;
138  int bin=me_entry[im].size()-1;
139  while (bin>at99p8ev){
140  if (
141  me_entry[im].at(at99p8ev).first*2.<me_entry[im].at(bin).first
142  ) addByLowest<int>(take_out, me_entry[im].at(bin).second, true);
143  bin--;
144  }
145  }
146  for (int bin=take_out.size()-1; bin>=0; bin--){
147  int t_ev = take_out.at(bin);
148  cout << "Discarding event " << events.at(t_ev) << endl;
149  vector<int>::iterator itev;
150  vector<float>::iterator it;
151  itev=events.begin()+t_ev; events.erase(itev);
152  it=masses.begin()+t_ev; masses.erase(it);
153  it=mevals.begin()+t_ev; mevals.erase(it);
154  it=me2vals.begin()+t_ev; me2vals.erase(it);
155  it=weights.begin()+t_ev; weights.erase(it);
156  }
157  }
158  void adjustWeights(float refth=3.){
159  if (masses.size()==0) return;
160 
161  vector<pair<float, int>> weight_entry;
162  for (unsigned int ev=0; ev<masses.size(); ev++) addByLowest<float, int>(weight_entry, weights.at(ev), ev);
163  int bin=weight_entry.size()-1;
164 
165  int at99p0ev = (float(weight_entry.size()))*0.99;
166  if (at99p0ev==(int)weight_entry.size()) at99p0ev--;
167  float threshold = weight_entry.at(at99p0ev).first;
168  while (bin>at99p0ev){
169  if (
170  threshold*2.<weight_entry.at(bin).first
171  ){
172  weights.at(weight_entry.at(bin).second) = pow(threshold, 2)/weight_entry.at(bin).first;
173  cout << "Adjusting weight for event " << bin << endl;
174  }
175  bin--;
176  }
177  float sum[2]={ 0 };
178  for (unsigned int ev=0; ev<weights.size(); ev++){
179  sum[0] += weights.at(ev);
180  sum[1] += 1;
181  }
182  for (unsigned int ev=0; ev<weights.size(); ev++) weights.at(ev) *= sum[1]/sum[0];
183  if (refth>0.){
184  for (unsigned int ev=0; ev<weights.size(); ev++){
185  if (weights.at(ev)<=refth) continue;
186  weights.at(ev) = pow(refth, 2)/weights.at(ev);
187  }
188  }
189  }
190  void mergeBin(const ExtBin& other){
191  if (
192  other.events.size()!=other.masses.size()
193  ||
194  other.events.size()!=other.mevals.size()
195  ||
196  other.events.size()!=other.me2vals.size()
197  ||
198  other.events.size()!=other.weights.size()
199  ){
200  cerr << "Size of the following elements are not the same:" << endl;
201  cerr << "\t- Events: " << other.events.size() << endl;
202  cerr << "\t- Masses: " << other.masses.size() << endl;
203  cerr << "\t- MEs: " << other.mevals.size() << endl;
204  cerr << "\t- MEs (2): " << other.me2vals.size() << endl;
205  cerr << "\t- Weights: " << other.weights.size() << endl;
206  }
207  for (unsigned int ev=0; ev<other.events.size(); ev++){
208  events.push_back(other.events.at(ev));
209  addEvent(other.masses.at(ev), other.mevals.at(ev), other.me2vals.at(ev), other.weights.at(ev));
210  }
211  }
212 };
213 
214 float findPoleMass(TString samplename){
215  float mass = -1;
216  string strtmp = samplename.Data();
217  std::size_t extpos = strtmp.find(".root");
218  if (extpos!=string::npos) strtmp.erase(extpos, 5);
219  vector<string> strsplit;
220  splitOptionRecursive(strtmp, strsplit, 'H');
221  if (strsplit.size()>1){
222  string strmass = strsplit.at(1);
223  strsplit.clear();
224  splitOptionRecursive(strmass, strsplit, '_');
225  strmass = strsplit.at(0);
226  mass = std::stod(strmass);
227  }
228  return mass;
229 }
230 TTree* findTree(vector<TTree*> treeList, int evid){
231  int ev = evid;
232  for (unsigned int t=0; t<treeList.size(); t++){
233  TTree* tree = treeList.at(t);
234  int nevts = tree->GetEntries();
235  if (ev<nevts) return tree;
236  else ev -= nevts;
237  if (ev<0) cerr << "findTree::ERROR: Could not find the event " << evid << endl;
238  }
239  return 0;
240 }
241 void getEntry(vector<TTree*> treeList, int evid){
242  int ev = evid;
243  for (unsigned int t=0; t<treeList.size(); t++){
244  TTree* tree = treeList.at(t);
245  if (tree==0) cerr << "Something went wrong in getEntry. Tree = " << tree << endl;
246  int nevts = tree->GetEntries();
247  if (ev<nevts){ tree->GetEntry(ev); break; }
248  else ev -= nevts;
249  if (ev<0) cerr << "findTree::ERROR: Could not find the event " << evid << endl;
250  }
251 }
252 
253 vector<TString> constructSamplesList(TString strsample, float sqrts){
254  vector<TString> samples;
255  if (strsample=="JJVBF"){
256  if (sqrts<10.){
257  samples.push_back("HZZ4lTree_VBFH116.root");
258  samples.push_back("HZZ4lTree_VBFH117.root");
259  samples.push_back("HZZ4lTree_VBFH118.root");
260  samples.push_back("HZZ4lTree_VBFH119.root");
261  samples.push_back("HZZ4lTree_VBFH120.root");
262  samples.push_back("HZZ4lTree_VBFH121.root");
263  samples.push_back("HZZ4lTree_VBFH122.root");
264  samples.push_back("HZZ4lTree_VBFH123.root");
265  samples.push_back("HZZ4lTree_VBFH124.root");
266  samples.push_back("HZZ4lTree_VBFH125.root");
267  samples.push_back("HZZ4lTree_VBFH126.root");
268  samples.push_back("HZZ4lTree_VBFH127.root");
269  samples.push_back("HZZ4lTree_VBFH128.root");
270  samples.push_back("HZZ4lTree_VBFH129.root");
271  samples.push_back("HZZ4lTree_VBFH130.root");
272  samples.push_back("HZZ4lTree_VBFH135.root");
273  samples.push_back("HZZ4lTree_VBFH140.root");
274  samples.push_back("HZZ4lTree_VBFH145.root");
275  samples.push_back("HZZ4lTree_VBFH150.root");
276  samples.push_back("HZZ4lTree_VBFH160.root");
277  samples.push_back("HZZ4lTree_VBFH170.root");
278  samples.push_back("HZZ4lTree_VBFH180.root");
279  samples.push_back("HZZ4lTree_VBFH190.root");
280  samples.push_back("HZZ4lTree_powheg15VBFH200.root");
281  samples.push_back("HZZ4lTree_powheg15VBFH225.root");
282  samples.push_back("HZZ4lTree_powheg15VBFH250.root");
283  samples.push_back("HZZ4lTree_powheg15VBFH275.root");
284  samples.push_back("HZZ4lTree_powheg15VBFH300.root");
285  samples.push_back("HZZ4lTree_powheg15VBFH350.root");
286  samples.push_back("HZZ4lTree_powheg15VBFH400.root");
287  samples.push_back("HZZ4lTree_powheg15VBFH450.root");
288  samples.push_back("HZZ4lTree_powheg15VBFH500.root");
289  samples.push_back("HZZ4lTree_powheg15VBFH550.root");
290  samples.push_back("HZZ4lTree_powheg15VBFH600.root");
291  samples.push_back("HZZ4lTree_powheg15VBFH650.root");
292  samples.push_back("HZZ4lTree_powheg15VBFH700.root");
293  samples.push_back("HZZ4lTree_powheg15VBFH750.root");
294  samples.push_back("HZZ4lTree_powheg15VBFH800.root");
295  samples.push_back("HZZ4lTree_powheg15VBFH850.root");
296  samples.push_back("HZZ4lTree_powheg15VBFH900.root");
297  samples.push_back("HZZ4lTree_powheg15VBFH950.root");
298  samples.push_back("HZZ4lTree_powheg15VBFH1000.root");
299  }
300  else{
301  samples.push_back("VBFH115");
302  samples.push_back("VBFH120");
303  samples.push_back("VBFH124");
304  samples.push_back("VBFH125");
305  samples.push_back("VBFH126");
306  samples.push_back("VBFH130");
307  samples.push_back("VBFH135");
308  samples.push_back("VBFH140");
309  samples.push_back("VBFH145");
310  samples.push_back("VBFH150");
311  samples.push_back("VBFH155");
312  samples.push_back("VBFH160");
313  samples.push_back("VBFH165");
314  samples.push_back("VBFH170");
315  samples.push_back("VBFH175");
316  samples.push_back("VBFH180");
317  samples.push_back("VBFH190");
318  samples.push_back("VBFH200");
319  samples.push_back("VBFH210");
320  samples.push_back("VBFH230");
321  samples.push_back("VBFH250");
322  samples.push_back("VBFH270");
323  samples.push_back("VBFH300");
324  samples.push_back("VBFH350");
325  samples.push_back("VBFH400");
326  samples.push_back("VBFH450");
327  samples.push_back("VBFH500");
328  samples.push_back("VBFH550");
329  samples.push_back("VBFH600");
330  samples.push_back("VBFH700");
331  samples.push_back("VBFH750");
332  samples.push_back("VBFH800");
333  samples.push_back("VBFH900");
334  samples.push_back("VBFH1000");
335  samples.push_back("VBFH1500");
336  samples.push_back("VBFH2000");
337  samples.push_back("VBFH2500");
338  samples.push_back("VBFH3000");
339  }
340  }
341  else if (strsample=="WH"){
342  if (sqrts<10.){
343  samples.push_back("HZZ4lTree_WH110.root");
344  samples.push_back("HZZ4lTree_WH115.root");
345  samples.push_back("HZZ4lTree_WH120.root");
346  samples.push_back("HZZ4lTree_WH125.root");
347  samples.push_back("HZZ4lTree_WH126.root");
348  samples.push_back("HZZ4lTree_WH130.root");
349  samples.push_back("HZZ4lTree_WH140.root");
350  samples.push_back("HZZ4lTree_WH150.root");
351  samples.push_back("HZZ4lTree_WH160.root");
352  samples.push_back("HZZ4lTree_WH180.root");
353  samples.push_back("HZZ4lTree_WH200.root");
354  }
355  else{
356  samples.push_back("WminusH115");
357  samples.push_back("WminusH120");
358  samples.push_back("WminusH124");
359  samples.push_back("WminusH125");
360  samples.push_back("WminusH126");
361  samples.push_back("WminusH130");
362  samples.push_back("WminusH135");
363  samples.push_back("WminusH140");
364  samples.push_back("WminusH145");
365  samples.push_back("WminusH150");
366  samples.push_back("WminusH155");
367  samples.push_back("WminusH160");
368  samples.push_back("WminusH165");
369  samples.push_back("WminusH170");
370  samples.push_back("WminusH175");
371  samples.push_back("WminusH180");
372  samples.push_back("WminusH190");
373  samples.push_back("WminusH200");
374  samples.push_back("WminusH210");
375  samples.push_back("WminusH230");
376  samples.push_back("WminusH250");
377  samples.push_back("WminusH270");
378  samples.push_back("WminusH300");
379  samples.push_back("WminusH350");
380  samples.push_back("WminusH400");
381  samples.push_back("WminusH450");
382  samples.push_back("WminusH500");
383  samples.push_back("WminusH550");
384  samples.push_back("WminusH600");
385  samples.push_back("WminusH700");
386  samples.push_back("WminusH750");
387  samples.push_back("WminusH800");
388  samples.push_back("WminusH900");
389  samples.push_back("WminusH1000");
390  samples.push_back("WminusH1500");
391  samples.push_back("WminusH2000");
392  samples.push_back("WminusH2500");
393  samples.push_back("WminusH3000");
394  samples.push_back("WplusH115");
395  samples.push_back("WplusH120");
396  samples.push_back("WplusH124");
397  samples.push_back("WplusH125");
398  samples.push_back("WplusH126");
399  samples.push_back("WplusH130");
400  samples.push_back("WplusH135");
401  samples.push_back("WplusH140");
402  samples.push_back("WplusH145");
403  samples.push_back("WplusH150");
404  samples.push_back("WplusH155");
405  samples.push_back("WplusH160");
406  samples.push_back("WplusH165");
407  samples.push_back("WplusH170");
408  samples.push_back("WplusH175");
409  samples.push_back("WplusH180");
410  samples.push_back("WplusH190");
411  samples.push_back("WplusH200");
412  samples.push_back("WplusH210");
413  samples.push_back("WplusH230");
414  samples.push_back("WplusH250");
415  samples.push_back("WplusH270");
416  samples.push_back("WplusH300");
417  samples.push_back("WplusH350");
418  samples.push_back("WplusH400");
419  samples.push_back("WplusH450");
420  samples.push_back("WplusH500");
421  samples.push_back("WplusH550");
422  samples.push_back("WplusH600");
423  samples.push_back("WplusH700");
424  samples.push_back("WplusH750");
425  samples.push_back("WplusH800");
426  samples.push_back("WplusH900");
427  samples.push_back("WplusH1000");
428  samples.push_back("WplusH1500");
429  samples.push_back("WplusH2000");
430  samples.push_back("WplusH2500");
431  samples.push_back("WplusH3000");
432  }
433  }
434  else if (strsample=="ZH"){
435  if (sqrts<10.){
436  samples.push_back("HZZ4lTree_ZH110.root");
437  samples.push_back("HZZ4lTree_ZH115.root");
438  samples.push_back("HZZ4lTree_ZH120.root");
439  samples.push_back("HZZ4lTree_ZH125.root");
440  samples.push_back("HZZ4lTree_ZH126.root");
441  samples.push_back("HZZ4lTree_ZH130.root");
442  samples.push_back("HZZ4lTree_ZH140.root");
443  samples.push_back("HZZ4lTree_ZH150.root");
444  samples.push_back("HZZ4lTree_ZH160.root");
445  samples.push_back("HZZ4lTree_ZH180.root");
446  samples.push_back("HZZ4lTree_ZH200.root");
447  }
448  else{
449  samples.push_back("ZH115");
450  samples.push_back("ZH120");
451  samples.push_back("ZH124");
452  samples.push_back("ZH125");
453  samples.push_back("ZH126");
454  samples.push_back("ZH130");
455  samples.push_back("ZH135");
456  samples.push_back("ZH140");
457  samples.push_back("ZH145");
458  samples.push_back("ZH150");
459  samples.push_back("ZH155");
460  samples.push_back("ZH160");
461  samples.push_back("ZH165");
462  samples.push_back("ZH170");
463  samples.push_back("ZH175");
464  samples.push_back("ZH180");
465  samples.push_back("ZH190");
466  samples.push_back("ZH200");
467  samples.push_back("ZH210");
468  samples.push_back("ZH230");
469  samples.push_back("ZH250");
470  samples.push_back("ZH270");
471  samples.push_back("ZH300");
472  samples.push_back("ZH350");
473  samples.push_back("ZH400");
474  samples.push_back("ZH450");
475  samples.push_back("ZH500");
476  samples.push_back("ZH550");
477  samples.push_back("ZH600");
478  samples.push_back("ZH700");
479  samples.push_back("ZH750");
480  samples.push_back("ZH800");
481  samples.push_back("ZH900");
482  samples.push_back("ZH1000");
483  samples.push_back("ZH1500");
484  samples.push_back("ZH2000");
485  samples.push_back("ZH2500");
486  samples.push_back("ZH3000");
487  }
488  }
489  else if (strsample=="JJQCD"){
490  if (sqrts<10.){
491  samples.push_back("HZZ4lTree_minloH90.root");
492  samples.push_back("HZZ4lTree_minloH95.root");
493  samples.push_back("HZZ4lTree_minloH100.root");
494  samples.push_back("HZZ4lTree_minloH105.root");
495  samples.push_back("HZZ4lTree_minloH110.root");
496  samples.push_back("HZZ4lTree_minloH115.root");
497  samples.push_back("HZZ4lTree_minloH120.root");
498  samples.push_back("HZZ4lTree_minloH124.root");
499  samples.push_back("HZZ4lTree_minloH125.root");
500  samples.push_back("HZZ4lTree_minloH126.root");
501  samples.push_back("HZZ4lTree_minloH130.root");
502  samples.push_back("HZZ4lTree_minloH135.root");
503  samples.push_back("HZZ4lTree_minloH140.root");
504  samples.push_back("HZZ4lTree_minloH145.root");
505  samples.push_back("HZZ4lTree_minloH150.root");
506  samples.push_back("HZZ4lTree_minloH155.root");
507  samples.push_back("HZZ4lTree_minloH160.root");
508  samples.push_back("HZZ4lTree_minloH170.root");
509  samples.push_back("HZZ4lTree_minloH180.root");
510  samples.push_back("HZZ4lTree_minloH190.root");
511  samples.push_back("HZZ4lTree_minloH200.root");
512  samples.push_back("HZZ4lTree_minloH250.root");
513  samples.push_back("HZZ4lTree_minloH300.root");
514  samples.push_back("HZZ4lTree_minloH350.root");
515  samples.push_back("HZZ4lTree_minloH400.root");
516  samples.push_back("HZZ4lTree_minloH450.root");
517  samples.push_back("HZZ4lTree_minloH500.root");
518  samples.push_back("HZZ4lTree_minloH550.root");
519  samples.push_back("HZZ4lTree_minloH600.root");
520  samples.push_back("HZZ4lTree_minloH650.root");
521  samples.push_back("HZZ4lTree_minloH700.root");
522  samples.push_back("HZZ4lTree_minloH750.root");
523  samples.push_back("HZZ4lTree_minloH800.root");
524  samples.push_back("HZZ4lTree_minloH850.root");
525  samples.push_back("HZZ4lTree_minloH900.root");
526  samples.push_back("HZZ4lTree_minloH950.root");
527  samples.push_back("HZZ4lTree_minloH1000.root");
528  }
529  else{
530  samples.push_back("ggH115");
531  samples.push_back("ggH120");
532  samples.push_back("ggH124");
533  samples.push_back("ggH125");
534  samples.push_back("ggH126");
535  samples.push_back("ggH130");
536  samples.push_back("ggH135");
537  samples.push_back("ggH140");
538  samples.push_back("ggH145");
539  samples.push_back("ggH150");
540  samples.push_back("ggH155");
541  samples.push_back("ggH160");
542  samples.push_back("ggH165");
543  samples.push_back("ggH170");
544  samples.push_back("ggH175");
545  samples.push_back("ggH180");
546  samples.push_back("ggH190");
547  samples.push_back("ggH200");
548  samples.push_back("ggH210");
549  samples.push_back("ggH230");
550  samples.push_back("ggH250");
551  samples.push_back("ggH270");
552  samples.push_back("ggH300");
553  samples.push_back("ggH350");
554  samples.push_back("ggH400");
555  samples.push_back("ggH450");
556  samples.push_back("ggH500");
557  samples.push_back("ggH550");
558  samples.push_back("ggH600");
559  samples.push_back("ggH700");
560  samples.push_back("ggH750");
561  samples.push_back("ggH800");
562  samples.push_back("ggH900");
563  samples.push_back("ggH1000");
564  samples.push_back("ggH1500");
565  samples.push_back("ggH2000");
566  samples.push_back("ggH2500");
567  samples.push_back("ggH3000");
568  }
569  }
570  else if (strsample=="gg_Sig_JHUGen"){
571  if (sqrts<10.){
572  samples.push_back("HZZ4lTree_jhuGenV4-H91.2.root");
573  samples.push_back("HZZ4lTree_powheg15jhuGenV3-0PMH125.6.root");
574  }
575  else{
576  }
577  }
578  else if (strsample=="gg_Sig_MCFM"){
579  if (sqrts<10.){
580  samples.push_back("HZZ4lTree_ggTo4mu_SMH-MCFM67_H125.6.root");
581  samples.push_back("HZZ4lTree_ggTo4e_SMH-MCFM67_H125.6.root");
582  samples.push_back("HZZ4lTree_ggTo2e2mu_SMH-MCFM67_H125.6.root");
583  }
584  else{
585  samples.push_back("ggTo4mu_0PMH125_MCFM701");
586  samples.push_back("ggTo4e_0PMH125_MCFM701");
587  samples.push_back("ggTo2e2mu_0PMH125_MCFM701");
588  samples.push_back("ggTo2e2tau_0PMH125_MCFM701");
589  samples.push_back("ggTo2mu2tau_0PMH125_MCFM701");
590  samples.push_back("ggTo4tau_0PMH125_MCFM701");
591  }
592  }
593  else if (strsample=="gg_Bkg_MCFM"){
594  if (sqrts<10.){
595  samples.push_back("HZZ4lTree_ggTo2e2mu_Contin-MCFM67.root");
596  samples.push_back("HZZ4lTree_ggTo4mu_Contin-MCFM67.root");
597  samples.push_back("HZZ4lTree_ggTo4e_Contin-MCFM67.root");
598  }
599  else{
600  samples.push_back("ggTo4mu_Contin_MCFM701");
601  samples.push_back("ggTo4e_Contin_MCFM701");
602  samples.push_back("ggTo2e2mu_Contin_MCFM701");
603  samples.push_back("ggTo2e2tau_Contin_MCFM701");
604  samples.push_back("ggTo2mu2tau_Contin_MCFM701");
605  samples.push_back("ggTo4tau_Contin_MCFM701");
606  }
607  }
608  else if (strsample=="gg_Sig_ggVV"){
609  if (sqrts<10.){
610  samples.push_back("HZZ4lTree_ggTo2l2l_H125.6.root");
611  samples.push_back("HZZ4lTree_ggTo4l_H125.6.root");
612  }
613  else{
614  }
615  }
616  else if (strsample=="gg_Bkg_ggVV"){
617  if (sqrts<10.){
618  samples.push_back("HZZ4lTree_ggTo4l_Continuum.root");
619  samples.push_back("HZZ4lTree_ggZZ4l.root");
620  samples.push_back("HZZ4lTree_ggTo2l2l_Continuum.root");
621  samples.push_back("HZZ4lTree_ggZZ2l2l.root");
622  }
623  else{
624  }
625  }
626  else if (strsample=="VV_Sig_MCFM"){
627  if (sqrts<10.){
628  }
629  else{
630  samples.push_back("VBFTo2e2muJJ_0PMH125_phantom128");
631  samples.push_back("VBFTo4muJJ_0PMH125_phantom128");
632  samples.push_back("VBFTo4eJJ_0PMH125_phantom128");
633  }
634  }
635  else if (strsample=="VV_Bkg_MCFM"){
636  if (sqrts<10.){
637  }
638  else{
639  samples.push_back("VBFTo2e2muJJ_Contin_phantom128");
640  samples.push_back("VBFTo4muJJ_Contin_phantom128");
641  samples.push_back("VBFTo4eJJ_Contin_phantom128");
642  }
643  }
644  else if (strsample=="qq_Bkg"){
645  if (sqrts<10.){
646  samples.push_back("HZZ4lTree_ZZTo2e2mu.root");
647  samples.push_back("HZZ4lTree_ZZTo2e2tau.root");
648  samples.push_back("HZZ4lTree_ZZTo2mu2tau.root");
649  samples.push_back("HZZ4lTree_ZZTo4mu.root");
650  samples.push_back("HZZ4lTree_ZZTo4e.root");
651  samples.push_back("HZZ4lTree_ZZTo4tau.root");
652  samples.push_back("HZZ4lTree_ZZ95-160To2e2mu.root");
653  samples.push_back("HZZ4lTree_ZZ95-160To2e2tau.root");
654  samples.push_back("HZZ4lTree_ZZ95-160To2mu2tau.root");
655  samples.push_back("HZZ4lTree_ZZ95-160To4mu.root");
656  samples.push_back("HZZ4lTree_ZZ95-160To4e.root");
657  samples.push_back("HZZ4lTree_ZZ95-160To4tau.root");
658  }
659  else{
660  samples.push_back("ZZTo4l");
661  samples.push_back("ZZTo4l_ext");
662  }
663  }
664  return samples;
665 }
666 
667 /*
668 GENERAL COMMENTS
669 - ALL MES ARE TRANSFORMED TO LOG10(ME).
670 */
671 
672 /* SPECIFIC COMMENT: NONE */
674  int erg_tev=sqrts;
675  float mPOLE=125.;
676  TString TREE_NAME = "SelectedTree";
677  bool writeFinalTree=true;
678 
679  TVar::VerbosityLevel verbosity = TVar::ERROR;
680  Mela mela(erg_tev, mPOLE, verbosity);
681 
685 
686  TString strproc = ProcessName(proc);
687  TString strme = MatrixElementName(me);
688  TString strprod = ProductionName(prod);
689 
690  short NJets30;
691  std::vector<double>* JetPt=0;
692  std::vector<double>* JetEta=0;
693  std::vector<double>* JetPhi=0;
694  std::vector<double>* JetMass=0;
695  std::vector<double> myJetPt;
696  std::vector<double> myJetEta;
697  std::vector<double> myJetPhi;
698  std::vector<double> myJetMass;
699  TBranch* bJetPt=0;
700  TBranch* bJetEta=0;
701  TBranch* bJetPhi=0;
702  TBranch* bJetMass=0;
703  float jetptetaphimass[2][4];
704 
705  float mesq_conserveDifermMass=0;
706  float mesq_jetPtoEScale=0;
707  float mzz = 126.;
708  float m1 = 91.471450;
709  float m2 = 12.139782;
710  float h1 = 0.2682896;
711  float h2 = 0.1679779;
712  float phi = 1.5969792;
713  float hs = -0.727181;
714  float phi1 = 1.8828257;
715  float ZZPt, ZZPhi, ZZEta;
716  int LepID[4]={ 13, -13, 11, -11 };
717  short Z1Flav, Z2Flav;
718 
719  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
720  TString cinput_main;
721  if (sqrts==8) cinput_main = inputdir_8TeV;
722  else if (sqrts==7) cinput_main = inputdir_7TeV;
723  else return;
724 
725  vector<TString> strSamples = constructSamplesList("JJVBF", sqrts);
726 
727  vector<TFile*> finputList;
728  vector<TTree*> treeList;
729  int nEntries=0;
730 
731  for (int is=0; is<(int)strSamples.size(); is++){
732  for (int ic=0; ic<3; ic++){
733  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples[is]).Data());
734  TFile* finput = TFile::Open(cinput, "read");
735  cout << "Opening file " << cinput << "..." << endl;
736  TTree* tree=0;
737  if (finput!=0){
738  if (finput->IsOpen() && !finput->IsZombie()){
739  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
740  tree = (TTree*)finput->Get(TREE_NAME);
741  if (tree!=0){
742  cout << TREE_NAME << " is found." << endl;
743  tree->SetBranchStatus("*", 0);
744  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
745  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
746  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
747  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
748  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
749  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
750  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
751  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
752  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
753  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
754  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
755  tree->SetBranchStatus("NJets30", 1); tree->SetBranchAddress("NJets30", &NJets30);
756  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
757  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
758  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
759  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
760 
761  nEntries += tree->GetEntries();
762  treeList.push_back(tree);
763  finputList.push_back(finput);
764  }
765  else finput->Close();
766  }
767  else if (finput->IsOpen()) finput->Close();
768  }
769  }
770  }
771 
772  const int nSamples = treeList.size();
773 
774  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
775 
776  vector<pair<float, int>> index;
777  unsigned int ev_acc=0;
778  for (int ev=0; ev<nEntries; ev++){
779  getEntry(treeList, ev);
780  if (NJets30<2) continue;
781  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
782  addByLowest(index, mzz, ev);
783  ev_acc++;
784  }
785 
786  float firstVal=index.at(0).first;
787  float lastVal=index.at(index.size()-1).first;
788  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
789  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
790  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
791 
792  float divisor=85000;
793  int nbins = index.size()/divisor;
794  const int nbins_th=10/*50*/;
795  while (nbins<nbins_th){
796  if (divisor>1000) divisor -= 1000;
797  else if (divisor>100) divisor -= 100;
798  else break;
799  nbins=index.size()/divisor;
800  }
801  cout << "nbins=" << nbins << endl;
802  if (nbins<3) cerr << "Not enough bins!" << endl;
803  vector<ExtBin> binList;
804  float* binning = new float[nbins+1];
805  binning[0]=infimum;
806  binning[nbins]=supremum;
807  int ev_stepsize = index.size()/nbins;
808  cout << "Event step size: " << ev_stepsize << endl;
809  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
810  for (int ix=1; ix<nbins; ix++){
811  binning[ix]=(index[ix*ev_stepsize-1].first+index[ix*ev_stepsize].first)*0.5;
812  ExtBin tmpbin;
813  tmpbin.binlow = binning[ix-1];
814  tmpbin.binhigh = binning[ix];
815  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[(ix-1)*ev_stepsize+bin].second);
816  binList.push_back(tmpbin);
817  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
818  }
819  ExtBin tmpbin;
820  tmpbin.binlow = binning[nbins-1];
821  tmpbin.binhigh = binning[nbins];
822  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index.size(); bin++) tmpbin.events.push_back(index[bin].second);
823  binList.push_back(tmpbin);
824  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
825  cout << "Bin list has the following bins:" << endl;
826  for (unsigned int ib=0; ib<binList.size(); ib++){
827  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
828  }
829 
830  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
831 
832  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
833  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
834  TProfile* hmesq_jetPtoEScale = new TProfile("P_MomentumToEnergy", "", nbins, binning); hmesq_jetPtoEScale->Sumw2();
835 
836  TTree* newtree=0;
837  if (writeFinalTree){
838  newtree = new TTree("FinalTree", "");
839  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
840  newtree->Branch("mesq_jetPtoEScale", &mesq_jetPtoEScale);
841  newtree->Branch("ZZMass", &mzz);
842  }
843 
844  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
845 
846  for (unsigned int bin=0; bin<binList.size(); bin++){
847  cout << "Bin " << bin << " is now being scrutinized..." << endl;
848  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
849  int getEv = binList.at(bin).events.at(ev);
850  getEntry(treeList, getEv);
851  if (ev%1000==0) cout << "Doing event " << getEv << endl;
852  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
853  cerr << "Jet array sizes are less than Njets!" << endl;
854  continue;
855  }
856 
857  TLorentzVector jet[2], higgs;
858  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
859  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
860  TVector3 boostH = higgs.BoostVector();
861 
863  associated.push_back(SimpleParticle_t(0, jet[0]));
864  associated.push_back(SimpleParticle_t(0, jet[1]));
865 
866  TLorentzVector pDaughters[4];
867  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
868  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
870  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
871  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
872 
873  mela.setProcess(proc, me, prod);
874 
876  mela.computeProdP(mesq_conserveDifermMass, false);
878  mela.computeProdP(mesq_jetPtoEScale, false);
879 
880  bool doFill = !(
881  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
882  ||
883  isnan(mesq_jetPtoEScale) || isinf(mesq_jetPtoEScale)
884  );
885 
886  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, mesq_jetPtoEScale);
887 
888  mela.resetInputEvent();
889  }
890 
891  binList.at(bin).sift(); binList.at(bin).adjustWeights();
892 
893  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
894  mzz = binList.at(bin).masses.at(ev);
895  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
896  mesq_jetPtoEScale = binList.at(bin).me2vals.at(ev);
897  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
898  hmesq_jetPtoEScale->Fill(mzz, mesq_jetPtoEScale);
899  hvar->Fill(mzz, mzz);
900  if (writeFinalTree) newtree->Fill();
901  }
902  }
903 
904  double* xexyey[2][4];
905  for (int inorm=0; inorm<2; inorm++){
906  for (int ix=0; ix<4; ix++) xexyey[inorm][ix] = new double[nbins];
907  for (int bin=0; bin<nbins; bin++){
908  xexyey[inorm][0][bin] = hvar->GetBinContent(bin+1);
909  xexyey[inorm][1][bin] = hvar->GetBinError(bin+1);
910 
911  if (inorm==0) cout << "Bin " << bin << " x-center: " << xexyey[inorm][0][bin] << " +- " << xexyey[inorm][1][bin] << endl;
912 
913  if (inorm==0){
914  xexyey[inorm][2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
915  xexyey[inorm][3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
916  }
917  else{
918  xexyey[inorm][2][bin] = hmesq_jetPtoEScale->GetBinContent(bin+1);
919  xexyey[inorm][3][bin] = hmesq_jetPtoEScale->GetBinError(bin+1);
920  }
921  xexyey[inorm][3][bin] = log10(xexyey[inorm][3][bin])/xexyey[inorm][2][bin];
922  xexyey[inorm][2][bin] = log10(xexyey[inorm][2][bin]);
923  }
924  }
925 
926  for (int inorm=0; inorm<2; inorm++){
927  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[inorm][0], xexyey[inorm][2], xexyey[inorm][1], xexyey[inorm][3]);
928  if (inorm==0) tg->SetName("tg_P_ConserveDifermionMass");
929  else tg->SetName("tg_P_MomentumToEnergy");
930  foutput->WriteTObject(tg);
931  delete tg;
932  }
933 
934  for (int inorm=0; inorm<2; inorm++){
935  for (int ix=0; ix<4; ix++) delete[] xexyey[inorm][ix];
936  }
937  foutput->WriteTObject(hmesq_jetPtoEScale);
938  foutput->WriteTObject(hmesq_conserveDifermMass);
939  foutput->WriteTObject(hvar);
940  if (writeFinalTree) foutput->WriteTObject(newtree);
941  if (writeFinalTree) delete newtree;
942  delete hmesq_conserveDifermMass;
943  delete hmesq_jetPtoEScale;
944  delete hvar;
945  foutput->Close();
946  delete[] binning;
947  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
948 }
949 /*
950 Function
951 [0]*exp(-x/[1])*(1+[2]*exp(-pow(x/[3],2)))
952 with parameters
953 0.0187935
954 489.335
955 0.0870576
956 256.215
957 fits well.
958 */
959 
960 
961 /*
962 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
963 - ALPHAS(MZ)**4 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION
964 */
966  int erg_tev=sqrts;
967  float mPOLE=125.;
968  TString TREE_NAME = "SelectedTree";
969  bool writeFinalTree=true;
970 
971  TVar::VerbosityLevel verbosity = TVar::ERROR;
972  Mela mela(erg_tev, mPOLE, verbosity);
973 
977 
978  TString strproc = ProcessName(proc);
979  TString strme = MatrixElementName(me);
980  TString strprod = ProductionName(prod);
981 
982  short NJets30;
983  std::vector<double>* JetPt=0;
984  std::vector<double>* JetEta=0;
985  std::vector<double>* JetPhi=0;
986  std::vector<double>* JetMass=0;
987  std::vector<double> myJetPt;
988  std::vector<double> myJetEta;
989  std::vector<double> myJetPhi;
990  std::vector<double> myJetMass;
991  TBranch* bJetPt=0;
992  TBranch* bJetEta=0;
993  TBranch* bJetPhi=0;
994  TBranch* bJetMass=0;
995  float jetptetaphimass[2][4];
996 
997  float mesq_conserveDifermMass=0;
998  float mesq_jetPtoEScale=0;
999  float mzz = 126.;
1000  float m1 = 91.471450;
1001  float m2 = 12.139782;
1002  float h1 = 0.2682896;
1003  float h2 = 0.1679779;
1004  float phi = 1.5969792;
1005  float hs = -0.727181;
1006  float phi1 = 1.8828257;
1007  float ZZPt, ZZPhi, ZZEta;
1008  int LepID[4]={ 13, -13, 11, -11 };
1009  short Z1Flav, Z2Flav;
1010 
1011  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
1012  TString cinput_main;
1013  if (sqrts==8) cinput_main = inputdir_8TeV;
1014  else if (sqrts==7) cinput_main = inputdir_7TeV;
1015  else return;
1016 
1017  vector<TString> strSamples = constructSamplesList("JJQCD", sqrts);
1018 
1019  vector<TFile*> finputList;
1020  vector<TTree*> treeList;
1021  int nEntries=0;
1022 
1023  for (int is=0; is<(int)strSamples.size(); is++){
1024  for (int ic=0; ic<3; ic++){
1025  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples[is]).Data());
1026  TFile* finput = TFile::Open(cinput, "read");
1027  cout << "Opening file " << cinput << "..." << endl;
1028  TTree* tree=0;
1029  if (finput!=0){
1030  if (finput->IsOpen() && !finput->IsZombie()){
1031  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
1032  tree = (TTree*)finput->Get(TREE_NAME);
1033  if (tree!=0){
1034  cout << TREE_NAME << " is found." << endl;
1035  tree->SetBranchStatus("*", 0);
1036  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
1037  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
1038  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
1039  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
1040  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
1041  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
1042  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
1043  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
1044  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
1045  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
1046  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
1047  tree->SetBranchStatus("NJets30", 1); tree->SetBranchAddress("NJets30", &NJets30);
1048  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
1049  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
1050  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
1051  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
1052 
1053  nEntries += tree->GetEntries();
1054  treeList.push_back(tree);
1055  finputList.push_back(finput);
1056  }
1057  else finput->Close();
1058  }
1059  else if (finput->IsOpen()) finput->Close();
1060  }
1061  }
1062  }
1063 
1064  const int nSamples = treeList.size();
1065 
1066  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
1067 
1068  vector<pair<float, int>> index;
1069  unsigned int ev_acc=0;
1070  for (int ev=0; ev<nEntries; ev++){
1071  getEntry(treeList, ev);
1072  if (NJets30<2) continue;
1073  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
1074  addByLowest(index, mzz, ev);
1075  ev_acc++;
1076  }
1077 
1078  float firstVal=index.at(0).first;
1079  float lastVal=index.at(index.size()-1).first;
1080  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
1081  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
1082  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
1083 
1084  float divisor=85000;
1085  int nbins = index.size()/divisor;
1086  const int nbins_th=10/*50*/;
1087  while (nbins<nbins_th){
1088  if (divisor>1000) divisor -= 1000;
1089  else if (divisor>100) divisor -= 100;
1090  else break;
1091  nbins=index.size()/divisor;
1092  }
1093  cout << "nbins=" << nbins << endl;
1094  if (nbins<3) cerr << "Not enough bins!" << endl;
1095  vector<ExtBin> binList;
1096  float* binning = new float[nbins+1];
1097  binning[0]=infimum;
1098  binning[nbins]=supremum;
1099  int ev_stepsize = index.size()/nbins;
1100  cout << "Event step size: " << ev_stepsize << endl;
1101  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
1102  for (int ix=1; ix<nbins; ix++){
1103  binning[ix]=(index[ix*ev_stepsize-1].first+index[ix*ev_stepsize].first)*0.5;
1104  ExtBin tmpbin;
1105  tmpbin.binlow = binning[ix-1];
1106  tmpbin.binhigh = binning[ix];
1107  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[(ix-1)*ev_stepsize+bin].second);
1108  binList.push_back(tmpbin);
1109  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
1110  }
1111  ExtBin tmpbin;
1112  tmpbin.binlow = binning[nbins-1];
1113  tmpbin.binhigh = binning[nbins];
1114  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index.size(); bin++) tmpbin.events.push_back(index[bin].second);
1115  binList.push_back(tmpbin);
1116  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
1117  cout << "Bin list has the following bins:" << endl;
1118  for (unsigned int ib=0; ib<binList.size(); ib++){
1119  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
1120  }
1121 
1122  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
1123 
1124  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
1125  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
1126  TProfile* hmesq_jetPtoEScale = new TProfile("P_MomentumToEnergy", "", nbins, binning); hmesq_jetPtoEScale->Sumw2();
1127 
1128  TTree* newtree=0;
1129  if (writeFinalTree){
1130  newtree = new TTree("FinalTree", "");
1131  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
1132  newtree->Branch("mesq_jetPtoEScale", &mesq_jetPtoEScale);
1133  newtree->Branch("ZZMass", &mzz);
1134  }
1135 
1136  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
1137 
1138  for (unsigned int bin=0; bin<binList.size(); bin++){
1139  cout << "Bin " << bin << " is now being scrutinized..." << endl;
1140  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
1141  int getEv = binList.at(bin).events.at(ev);
1142  getEntry(treeList, getEv);
1143  if (ev%1000==0) cout << "Doing event " << getEv << endl;
1144  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
1145  cerr << "Jet array sizes are less than Njets!" << endl;
1146  continue;
1147  }
1148 
1149  TLorentzVector jet[2], higgs;
1150  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
1151  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
1152  TVector3 boostH = higgs.BoostVector();
1153 
1155  associated.push_back(SimpleParticle_t(0, jet[0]));
1156  associated.push_back(SimpleParticle_t(0, jet[1]));
1157 
1158  TLorentzVector pDaughters[4];
1159  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
1160  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
1162  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
1163  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
1164 
1165  mela.setProcess(proc, me, prod);
1166 
1167  double alphasVal;
1168 
1170  mela.computeProdP(mesq_conserveDifermMass, false);
1171  alphasVal = mela.getIORecord()->getAlphaSatMZ();
1172  mesq_conserveDifermMass /= pow(alphasVal, 4);
1173 
1175  mela.computeProdP(mesq_jetPtoEScale, false);
1176  alphasVal = mela.getIORecord()->getAlphaSatMZ();
1177  mesq_jetPtoEScale /= pow(alphasVal, 4);
1178 
1179  bool doFill = !(
1180  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
1181  ||
1182  isnan(mesq_jetPtoEScale) || isinf(mesq_jetPtoEScale)
1183  );
1184 
1185  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, mesq_jetPtoEScale);
1186 
1187  mela.resetInputEvent();
1188  }
1189 
1190  binList.at(bin).sift(); binList.at(bin).adjustWeights();
1191 
1192  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
1193  mzz = binList.at(bin).masses.at(ev);
1194  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
1195  mesq_jetPtoEScale = binList.at(bin).me2vals.at(ev);
1196  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
1197  hmesq_jetPtoEScale->Fill(mzz, mesq_jetPtoEScale);
1198  hvar->Fill(mzz, mzz);
1199  if (writeFinalTree) newtree->Fill();
1200  }
1201  }
1202 
1203  double* xexyey[2][4];
1204  for (int inorm=0; inorm<2; inorm++){
1205  for (int ix=0; ix<4; ix++) xexyey[inorm][ix] = new double[nbins];
1206  for (int bin=0; bin<nbins; bin++){
1207  xexyey[inorm][0][bin] = hvar->GetBinContent(bin+1);
1208  xexyey[inorm][1][bin] = hvar->GetBinError(bin+1);
1209 
1210  if (inorm==0) cout << "Bin " << bin << " x-center: " << xexyey[inorm][0][bin] << " +- " << xexyey[inorm][1][bin] << endl;
1211 
1212  if (inorm==0){
1213  xexyey[inorm][2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
1214  xexyey[inorm][3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
1215  }
1216  else{
1217  xexyey[inorm][2][bin] = hmesq_jetPtoEScale->GetBinContent(bin+1);
1218  xexyey[inorm][3][bin] = hmesq_jetPtoEScale->GetBinError(bin+1);
1219  }
1220  xexyey[inorm][3][bin] = log10(xexyey[inorm][3][bin])/xexyey[inorm][2][bin];
1221  xexyey[inorm][2][bin] = log10(xexyey[inorm][2][bin]);
1222  }
1223  }
1224 
1225  for (int inorm=0; inorm<2; inorm++){
1226  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[inorm][0], xexyey[inorm][2], xexyey[inorm][1], xexyey[inorm][3]);
1227  if (inorm==0) tg->SetName("tg_P_ConserveDifermionMass");
1228  else tg->SetName("tg_P_MomentumToEnergy");
1229  foutput->WriteTObject(tg);
1230  delete tg;
1231  }
1232 
1233  for (int inorm=0; inorm<2; inorm++){
1234  for (int ix=0; ix<4; ix++) delete[] xexyey[inorm][ix];
1235  }
1236  foutput->WriteTObject(hmesq_jetPtoEScale);
1237  foutput->WriteTObject(hmesq_conserveDifermMass);
1238  foutput->WriteTObject(hvar);
1239  if (writeFinalTree) foutput->WriteTObject(newtree);
1240  if (writeFinalTree) delete newtree;
1241  delete hmesq_conserveDifermMass;
1242  delete hmesq_jetPtoEScale;
1243  delete hvar;
1244  foutput->Close();
1245  delete[] binning;
1246  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
1247 }
1248 /*
1249 Function
1250 [0]*exp(-x/[1])*(1+[2]*exp(-pow(x/[3],2)) + [4]*exp(-x/[5]))
1251 with parameters
1252 152.197
1253 496.507
1254 0.812036
1255 349.521
1256 9.00697
1257 55.4923
1258 fits well.
1259 */
1260 
1261 /* SPECIFIC COMMENT: OUTPUT ME DIVIDED BY ALPHAS(MZ)**3 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION */
1263  int erg_tev=sqrts;
1264  float mPOLE=125.;
1265  TString TREE_NAME = "SelectedTree";
1266  bool writeFinalTree=true;
1267 
1268  TVar::VerbosityLevel verbosity = TVar::ERROR;
1269  Mela mela(erg_tev, mPOLE, verbosity);
1270 
1274 
1275  TString strproc = ProcessName(proc);
1276  TString strme = MatrixElementName(me);
1277  TString strprod = ProductionName(prod);
1278 
1279  short NJets30;
1280  std::vector<double>* JetPt=0;
1281  std::vector<double>* JetEta=0;
1282  std::vector<double>* JetPhi=0;
1283  std::vector<double>* JetMass=0;
1284  std::vector<double> myJetPt;
1285  std::vector<double> myJetEta;
1286  std::vector<double> myJetPhi;
1287  std::vector<double> myJetMass;
1288  TBranch* bJetPt=0;
1289  TBranch* bJetEta=0;
1290  TBranch* bJetPhi=0;
1291  TBranch* bJetMass=0;
1292  float jetptetaphimass[2][4];
1293 
1294  float mesq_conserveDifermMass=0;
1295  float mesq_jetPtoEScale=0;
1296  float mzz = 126.;
1297  float m1 = 91.471450;
1298  float m2 = 12.139782;
1299  float h1 = 0.2682896;
1300  float h2 = 0.1679779;
1301  float phi = 1.5969792;
1302  float hs = -0.727181;
1303  float phi1 = 1.8828257;
1304  float ZZPt, ZZPhi, ZZEta;
1305  int LepID[4]={ 13, -13, 11, -11 };
1306  short Z1Flav, Z2Flav;
1307 
1308  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
1309  TString cinput_main;
1310  if (sqrts==8) cinput_main = inputdir_8TeV;
1311  else if (sqrts==7) cinput_main = inputdir_7TeV;
1312  else return;
1313 
1314  vector<TString> strSamples = constructSamplesList("JJQCD", sqrts);
1315 
1316  vector<TFile*> finputList;
1317  vector<TTree*> treeList;
1318  int nEntries=0;
1319 
1320  for (int is=0; is<(int)strSamples.size(); is++){
1321  for (int ic=0; ic<3; ic++){
1322  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples[is]).Data());
1323  TFile* finput = TFile::Open(cinput, "read");
1324  cout << "Opening file " << cinput << "..." << endl;
1325  TTree* tree=0;
1326  if (finput!=0){
1327  if (finput->IsOpen() && !finput->IsZombie()){
1328  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
1329  tree = (TTree*)finput->Get(TREE_NAME);
1330  if (tree!=0){
1331  cout << TREE_NAME << " is found." << endl;
1332  tree->SetBranchStatus("*", 0);
1333  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
1334  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
1335  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
1336  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
1337  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
1338  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
1339  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
1340  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
1341  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
1342  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
1343  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
1344  tree->SetBranchStatus("NJets30", 1); tree->SetBranchAddress("NJets30", &NJets30);
1345  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
1346  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
1347  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
1348  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
1349 
1350  nEntries += tree->GetEntries();
1351  treeList.push_back(tree);
1352  finputList.push_back(finput);
1353  }
1354  else finput->Close();
1355  }
1356  else if (finput->IsOpen()) finput->Close();
1357  }
1358  }
1359  }
1360 
1361  const int nSamples = treeList.size();
1362 
1363  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
1364 
1365  vector<pair<float, int>> index;
1366  unsigned int ev_acc=0;
1367  for (int ev=0; ev<nEntries; ev++){
1368  getEntry(treeList, ev);
1369  if (NJets30!=1) continue;
1370  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
1371  addByLowest(index, mzz, ev);
1372  ev_acc++;
1373  }
1374 
1375  float firstVal=index.at(0).first;
1376  float lastVal=index.at(index.size()-1).first;
1377  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
1378  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
1379  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
1380 
1381  float divisor=85000;
1382  int nbins = index.size()/divisor;
1383  const int nbins_th=10/*50*/;
1384  while (nbins<nbins_th){
1385  if (divisor>1000) divisor -= 1000;
1386  else if (divisor>100) divisor -= 100;
1387  else break;
1388  nbins=index.size()/divisor;
1389  }
1390  cout << "nbins=" << nbins << endl;
1391  if (nbins<3) cerr << "Not enough bins!" << endl;
1392  vector<ExtBin> binList;
1393  float* binning = new float[nbins+1];
1394  binning[0]=infimum;
1395  binning[nbins]=supremum;
1396  int ev_stepsize = index.size()/nbins;
1397  cout << "Event step size: " << ev_stepsize << endl;
1398  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
1399  for (int ix=1; ix<nbins; ix++){
1400  binning[ix]=(index[ix*ev_stepsize-1].first+index[ix*ev_stepsize].first)*0.5;
1401  ExtBin tmpbin;
1402  tmpbin.binlow = binning[ix-1];
1403  tmpbin.binhigh = binning[ix];
1404  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[(ix-1)*ev_stepsize+bin].second);
1405  binList.push_back(tmpbin);
1406  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
1407  }
1408  ExtBin tmpbin;
1409  tmpbin.binlow = binning[nbins-1];
1410  tmpbin.binhigh = binning[nbins];
1411  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index.size(); bin++) tmpbin.events.push_back(index[bin].second);
1412  binList.push_back(tmpbin);
1413  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
1414  cout << "Bin list has the following bins:" << endl;
1415  for (unsigned int ib=0; ib<binList.size(); ib++){
1416  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
1417  }
1418 
1419  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
1420 
1421  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
1422  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
1423  TProfile* hmesq_jetPtoEScale = new TProfile("P_MomentumToEnergy", "", nbins, binning); hmesq_jetPtoEScale->Sumw2();
1424 
1425  TTree* newtree=0;
1426  if (writeFinalTree){
1427  newtree = new TTree("FinalTree", "");
1428  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
1429  newtree->Branch("mesq_jetPtoEScale", &mesq_jetPtoEScale);
1430  newtree->Branch("ZZMass", &mzz);
1431  }
1432 
1433  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
1434 
1435  for (unsigned int bin=0; bin<binList.size(); bin++){
1436  cout << "Bin " << bin << " is now being scrutinized..." << endl;
1437  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
1438  int getEv = binList.at(bin).events.at(ev);
1439  getEntry(treeList, getEv);
1440  if (ev%1000==0) cout << "Doing event " << getEv << endl;
1441  if (JetPt->size()<1 || JetEta->size()<1 || JetPhi->size()<1 || JetMass->size()<1){
1442  cerr << "Jet array sizes are less than Njets!" << endl;
1443  continue;
1444  }
1445 
1446  TLorentzVector jet, higgs;
1447  jet.SetPtEtaPhiM(JetPt->at(0), JetEta->at(0), JetPhi->at(0), JetMass->at(0));
1448  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
1449  TVector3 boostH = higgs.BoostVector();
1450 
1452  associated.push_back(SimpleParticle_t(0, jet));
1453 
1454  TLorentzVector pDaughters[4];
1455  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
1456  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
1458  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
1459  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
1460 
1461  mela.setProcess(proc, me, prod);
1462 
1463  double alphasVal;
1464 
1466  mela.computeProdP(mesq_conserveDifermMass, false);
1467  alphasVal = mela.getIORecord()->getAlphaSatMZ();
1468  mesq_conserveDifermMass /= pow(alphasVal, 3);
1469 
1471  mela.computeProdP(mesq_jetPtoEScale, false);
1472  alphasVal = mela.getIORecord()->getAlphaSatMZ();
1473  mesq_jetPtoEScale /= pow(alphasVal, 3);
1474 
1475  bool doFill = !(
1476  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
1477  ||
1478  isnan(mesq_jetPtoEScale) || isinf(mesq_jetPtoEScale)
1479  );
1480 
1481  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, mesq_jetPtoEScale);
1482 
1483  mela.resetInputEvent();
1484  }
1485 
1486  binList.at(bin).sift(); binList.at(bin).adjustWeights();
1487 
1488  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
1489  mzz = binList.at(bin).masses.at(ev);
1490  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
1491  mesq_jetPtoEScale = binList.at(bin).me2vals.at(ev);
1492  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
1493  hmesq_jetPtoEScale->Fill(mzz, mesq_jetPtoEScale);
1494  hvar->Fill(mzz, mzz);
1495  if (writeFinalTree) newtree->Fill();
1496  }
1497  }
1498 
1499  double* xexyey[2][4];
1500  for (int inorm=0; inorm<2; inorm++){
1501  for (int ix=0; ix<4; ix++) xexyey[inorm][ix] = new double[nbins];
1502  for (int bin=0; bin<nbins; bin++){
1503  xexyey[inorm][0][bin] = hvar->GetBinContent(bin+1);
1504  xexyey[inorm][1][bin] = hvar->GetBinError(bin+1);
1505 
1506  if (inorm==0) cout << "Bin " << bin << " x-center: " << xexyey[inorm][0][bin] << " +- " << xexyey[inorm][1][bin] << endl;
1507 
1508  if (inorm==0){
1509  xexyey[inorm][2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
1510  xexyey[inorm][3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
1511  }
1512  else{
1513  xexyey[inorm][2][bin] = hmesq_jetPtoEScale->GetBinContent(bin+1);
1514  xexyey[inorm][3][bin] = hmesq_jetPtoEScale->GetBinError(bin+1);
1515  }
1516  xexyey[inorm][3][bin] = log10(xexyey[inorm][3][bin])/xexyey[inorm][2][bin];
1517  xexyey[inorm][2][bin] = log10(xexyey[inorm][2][bin]);
1518  }
1519  }
1520 
1521  for (int inorm=0; inorm<2; inorm++){
1522  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[inorm][0], xexyey[inorm][2], xexyey[inorm][1], xexyey[inorm][3]);
1523  if (inorm==0) tg->SetName("tg_P_ConserveDifermionMass");
1524  else tg->SetName("tg_P_MomentumToEnergy");
1525  foutput->WriteTObject(tg);
1526  delete tg;
1527  }
1528 
1529  for (int inorm=0; inorm<2; inorm++){
1530  for (int ix=0; ix<4; ix++) delete[] xexyey[inorm][ix];
1531  }
1532  foutput->WriteTObject(hmesq_jetPtoEScale);
1533  foutput->WriteTObject(hmesq_conserveDifermMass);
1534  foutput->WriteTObject(hvar);
1535  if (writeFinalTree) foutput->WriteTObject(newtree);
1536  if (writeFinalTree) delete newtree;
1537  delete hmesq_conserveDifermMass;
1538  delete hmesq_jetPtoEScale;
1539  delete hvar;
1540  foutput->Close();
1541  delete[] binning;
1542  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
1543 }
1544 /*
1545 Function
1546 [0]*exp(-x/[1])*(1+[2]*exp(-pow(x/[3],2)) + [4]*exp(-x/[5]))
1547 with parameters
1548 196.358
1549 291.176
1550 14.6094
1551 92.3443
1552 13.2622
1553 133.669
1554 fits well.
1555 */
1556 
1557 
1558 /***** 13 TeV Samples *****/
1559 /*
1560 SPECIFIC COMMENT:
1561 - NJets30 -> nCleanedJetsPt30
1562 - vector<double> -> vector<float>
1563 */
1564 
1565 /* SPECIFIC COMMENT: NONE */
1566 void get_PAvgProfile_JHUGen_JJVBF_HSMHiggs_13TeV(int sqrts=13, bool recalculate = true){
1567  int erg_tev=sqrts;
1568  float mPOLE=125.;
1569  TString TREE_NAME = "ZZTree/candTree";
1570  TString COUNTERS_NAME = "ZZTree/Counters";
1571  bool writeFinalTree=true;
1572 
1576 
1577  TString strproc = ProcessName(proc);
1578  TString strme = MatrixElementName(me);
1579  TString strprod = ProductionName(prod);
1580 
1581  TVar::VerbosityLevel verbosity = TVar::ERROR;
1582  Mela mela(erg_tev, mPOLE, verbosity);
1583 
1584  std::vector<short>* LepLepId=0;
1585  std::vector<float>* LepPt=0;
1586  std::vector<float>* LepEta=0;
1587  std::vector<float>* LepPhi=0;
1588 
1589  short NJets30;
1590  std::vector<float>* JetPt=0;
1591  std::vector<float>* JetEta=0;
1592  std::vector<float>* JetPhi=0;
1593  std::vector<float>* JetMass=0;
1594  std::vector<float> myJetPt;
1595  std::vector<float> myJetEta;
1596  std::vector<float> myJetPhi;
1597  std::vector<float> myJetMass;
1598 
1599  float mesq_calc=0., cconst_calc=1.;
1600  float mesq_conserveDifermMass=0;
1601  float mesq_jetPtoEScale=0;
1602  float mzz = 126.;
1603  float m1 = 91.471450;
1604  float m2 = 12.139782;
1605  float h1 = 0.2682896;
1606  float h2 = 0.1679779;
1607  float phi = 1.5969792;
1608  float hs = -0.727181;
1609  float phi1 = 1.8828257;
1610  float ZZPt, ZZPhi, ZZEta;
1611  float genHEPMCweight;
1612  float wgt=1;
1613 
1614  TString cinput_main;
1615  if (sqrts==13) cinput_main = inputdir_13TeV;
1616  else return;
1617 
1618  vector<TString> strSamples = constructSamplesList("JJVBF", sqrts);
1619 
1620  unordered_map<TTree*, pair<float, float>> nGenMap;
1621  vector<TFile*> finputList;
1622  vector<TTree*> treeList;
1623  int nEntries=0;
1624  for (unsigned int is=0; is<strSamples.size(); is++){
1625  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples[is]).Data());
1626  TFile* finput = TFile::Open(cinput, "read");
1627  cout << "Opening file " << cinput << "..." << endl;
1628  TTree* tree=0;
1629  if (finput!=0){
1630  if (finput->IsOpen() && !finput->IsZombie()){
1631  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
1632  tree = (TTree*)finput->Get(TREE_NAME);
1633  if (tree!=0){
1634  cout << TREE_NAME << " is found." << endl;
1635  if (!recalculate && tree->GetBranchStatus("pConst_JJVBF_SIG_ghv1_1_JHUGen_JECNominal")==0) recalculate=true;
1636  tree->SetBranchStatus("*", 0);
1637  tree->SetBranchStatus("genHEPMCweight", 1); tree->SetBranchAddress("genHEPMCweight", &genHEPMCweight);
1638  tree->SetBranchStatus("nCleanedJetsPt30", 1); tree->SetBranchAddress("nCleanedJetsPt30", &NJets30);
1639  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
1640  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
1641  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
1642  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
1643  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
1644  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
1645  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
1646  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
1647  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
1648  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
1649  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
1650  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
1651  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
1652  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
1653  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
1654  tree->SetBranchStatus("LepLepId", 1); tree->SetBranchAddress("LepLepId", &LepLepId);
1655  tree->SetBranchStatus("LepPt", 1); tree->SetBranchAddress("LepPt", &LepPt);
1656  tree->SetBranchStatus("LepEta", 1); tree->SetBranchAddress("LepEta", &LepEta);
1657  tree->SetBranchStatus("LepPhi", 1); tree->SetBranchAddress("LepPhi", &LepPhi);
1658  if (!recalculate){
1659  tree->SetBranchStatus("p_JJVBF_SIG_ghv1_1_JHUGen_JECNominal", 1);
1660  tree->SetBranchAddress("p_JJVBF_SIG_ghv1_1_JHUGen_JECNominal", &mesq_calc);
1661  tree->SetBranchStatus("pConst_JJVBF_SIG_ghv1_1_JHUGen_JECNominal", 1);
1662  tree->SetBranchAddress("pConst_JJVBF_SIG_ghv1_1_JHUGen_JECNominal", &cconst_calc);
1663  }
1664 
1665  TH1F* htmp = (TH1F*)finput->Get(COUNTERS_NAME);
1666  pair<float, float> nsum(htmp->GetBinContent(1), htmp->GetBinContent(41)); // No PU reweighting
1667  nGenMap[tree]=nsum;
1668 
1669  nEntries += tree->GetEntries();
1670  treeList.push_back(tree);
1671  finputList.push_back(finput);
1672  }
1673  else if (finput->IsOpen()) finput->Close();
1674  }
1675  }
1676  }
1677  cout << "NEntries = " << nEntries << endl;
1678 
1679  vector<pair<float, int>> index;
1680  unsigned int ev_acc=0;
1681  for (int ev=0; ev<nEntries; ev++){
1682  getEntry(treeList, ev);
1683  if (NJets30<2) continue;
1684  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
1685  addByLowest(index, mzz, ev);
1686  ev_acc++;
1687  }
1688 
1689  float firstVal=index.at(0).first;
1690  float lastVal=index.at(index.size()-1).first;
1691  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
1692  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
1693  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
1694 
1695  float divisor=85000;
1696  int nbins = index.size()/divisor;
1697  const int nbins_th=10/*50*/;
1698  while (nbins<nbins_th){
1699  if (divisor>1000) divisor -= 1000;
1700  else if (divisor>100) divisor -= 100;
1701  else break;
1702  nbins=index.size()/divisor;
1703  }
1704  cout << "nbins=" << nbins << endl;
1705  if (nbins<3) cerr << "Not enough bins!" << endl;
1706  vector<ExtBin> binList;
1707  float* binning = new float[nbins+1];
1708  binning[0]=infimum;
1709  binning[nbins]=supremum;
1710  int ev_stepsize = index.size()/nbins;
1711  cout << "Event step size: " << ev_stepsize << endl;
1712  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
1713  for (int ix=1; ix<nbins; ix++){
1714  binning[ix]=(index[ix*ev_stepsize-1].first+index[ix*ev_stepsize].first)*0.5;
1715  ExtBin tmpbin;
1716  tmpbin.binlow = binning[ix-1];
1717  tmpbin.binhigh = binning[ix];
1718  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[(ix-1)*ev_stepsize+bin].second);
1719  binList.push_back(tmpbin);
1720  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
1721  }
1722  ExtBin tmpbin;
1723  tmpbin.binlow = binning[nbins-1];
1724  tmpbin.binhigh = binning[nbins];
1725  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index.size(); bin++) tmpbin.events.push_back(index[bin].second);
1726  binList.push_back(tmpbin);
1727  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
1728  cout << "Bin list has the following bins:" << endl;
1729  for (unsigned int ib=0; ib<binList.size(); ib++){
1730  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
1731  /*
1732  cout << "\tEvents: ";
1733  for (unsigned int ev=0; ev<binList.at(ib).events.size(); ev++){
1734  cout << binList.at(ib).events.at(ev) << " ";
1735  }
1736  cout << endl;
1737  */
1738  }
1739  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
1740 
1741  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
1742  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
1743  TProfile* hmesq_jetPtoEScale = new TProfile("P_MomentumToEnergy", "", nbins, binning); hmesq_jetPtoEScale->Sumw2();
1744 
1745 
1746  TTree* newtree=0;
1747  if (writeFinalTree){
1748  newtree = new TTree("FinalTree", "");
1749  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
1750  newtree->Branch("mesq_jetPtoEScale", &mesq_jetPtoEScale);
1751  newtree->Branch("ZZMass", &mzz);
1752  }
1753 
1754  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
1755 
1756  for (unsigned int bin=0; bin<binList.size(); bin++){
1757  cout << "Bin " << bin << " is now being scrutinized..." << endl;
1758  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
1759  int getEv = binList.at(bin).events.at(ev);
1760  getEntry(treeList, getEv);
1761  if (ev%1000==0) cout << "Doing event " << getEv << endl;
1762  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
1763  cerr << "Jet array sizes are less than Njets!" << endl;
1764  continue;
1765  }
1766  TTree* tree = findTree(treeList, getEv);
1767  wgt = fabs(genHEPMCweight*nGenMap[tree].first/nGenMap[tree].second);
1768 
1769  TLorentzVector jet[2], higgs;
1770  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(JetPt->at(ij), JetEta->at(ij), JetPhi->at(ij), JetMass->at(ij));
1771  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
1772  TVector3 boostH = higgs.BoostVector();
1773 
1775  associated.push_back(SimpleParticle_t(0, jet[0]));
1776  associated.push_back(SimpleParticle_t(0, jet[1]));
1777 
1779  for (int id=0; id<4; id++){
1780  double mass=0;
1781  if (abs(LepLepId->at(id))==13) mass = 0.105658;
1782  else if (abs(LepLepId->at(id))==11) mass = 0.000511;
1783  TLorentzVector pDaughter;
1784  pDaughter.SetPtEtaPhiM(LepPt->at(id), LepEta->at(id), LepPhi->at(id), mass);
1785  daughters.push_back(SimpleParticle_t(LepLepId->at(id), pDaughter));
1786  }
1787  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
1788 
1789  mela.setProcess(proc, me, prod);
1790 
1791  if (recalculate){
1793  mela.computeProdP(mesq_conserveDifermMass, false);
1794  }
1795  else{
1796  mesq_calc /= cconst_calc;
1797  mesq_conserveDifermMass=mesq_calc;
1798  }
1800  mela.computeProdP(mesq_jetPtoEScale, false);
1801 
1802  bool doFill = !(
1803  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
1804  ||
1805  isnan(mesq_jetPtoEScale) || isinf(mesq_jetPtoEScale)
1806  );
1807 
1808  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, mesq_jetPtoEScale, wgt);
1809 
1810  mela.resetInputEvent();
1811  }
1812 
1813  binList.at(bin).sift(); binList.at(bin).adjustWeights();
1814 
1815  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
1816  mzz = binList.at(bin).masses.at(ev);
1817  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
1818  mesq_jetPtoEScale = binList.at(bin).me2vals.at(ev);
1819  wgt = binList.at(bin).weights.at(ev);
1820  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass, wgt);
1821  hmesq_jetPtoEScale->Fill(mzz, mesq_jetPtoEScale, wgt);
1822  hvar->Fill(mzz, mzz, wgt);
1823  if (writeFinalTree) newtree->Fill();
1824  }
1825  }
1826 
1827  double* xexyey[2][4];
1828  for (int inorm=0; inorm<2; inorm++){
1829  for (int ix=0; ix<4; ix++) xexyey[inorm][ix] = new double[nbins];
1830  for (int bin=0; bin<nbins; bin++){
1831  xexyey[inorm][0][bin] = hvar->GetBinContent(bin+1);
1832  xexyey[inorm][1][bin] = hvar->GetBinError(bin+1);
1833 
1834  if (inorm==0) cout << "Bin " << bin << " x-center: " << xexyey[inorm][0][bin] << " +- " << xexyey[inorm][1][bin] << endl;
1835 
1836  if (inorm==0){
1837  xexyey[inorm][2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
1838  xexyey[inorm][3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
1839  }
1840  else{
1841  xexyey[inorm][2][bin] = hmesq_jetPtoEScale->GetBinContent(bin+1);
1842  xexyey[inorm][3][bin] = hmesq_jetPtoEScale->GetBinError(bin+1);
1843  }
1844  xexyey[inorm][3][bin] = log10(xexyey[inorm][3][bin])/xexyey[inorm][2][bin];
1845  xexyey[inorm][2][bin] = log10(xexyey[inorm][2][bin]);
1846  }
1847  }
1848 
1849  for (int inorm=0; inorm<2; inorm++){
1850  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[inorm][0], xexyey[inorm][2], xexyey[inorm][1], xexyey[inorm][3]);
1851  if (inorm==0) tg->SetName("tg_P_ConserveDifermionMass");
1852  else tg->SetName("tg_P_MomentumToEnergy");
1853  foutput->WriteTObject(tg);
1854  delete tg;
1855  }
1856 
1857  for (int inorm=0; inorm<2; inorm++){
1858  for (int ix=0; ix<4; ix++) delete[] xexyey[inorm][ix];
1859  }
1860  foutput->WriteTObject(hmesq_jetPtoEScale);
1861  foutput->WriteTObject(hmesq_conserveDifermMass);
1862  foutput->WriteTObject(hvar);
1863  if (writeFinalTree) foutput->WriteTObject(newtree);
1864  if (writeFinalTree) delete newtree;
1865  delete hmesq_conserveDifermMass;
1866  delete hmesq_jetPtoEScale;
1867  delete hvar;
1868  foutput->Close();
1869  delete[] binning;
1870  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
1871 }
1872 
1873 
1874 /* SPECIFIC COMMENT: OUTPUT ME DIVIDED BY ALPHAS(MZ)**4 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION */
1875 void get_PAvgProfile_JHUGen_JJQCD_HSMHiggs_13TeV(int sqrts=13, bool recalculate = true){
1876  int erg_tev=sqrts;
1877  float mPOLE=125.;
1878  TString TREE_NAME = "ZZTree/candTree";
1879  TString COUNTERS_NAME = "ZZTree/Counters";
1880  bool writeFinalTree=true;
1881 
1885 
1886  TString strproc = ProcessName(proc);
1887  TString strme = MatrixElementName(me);
1888  TString strprod = ProductionName(prod);
1889 
1890  TVar::VerbosityLevel verbosity = TVar::ERROR;
1891  Mela mela(erg_tev, mPOLE, verbosity);
1892 
1893  std::vector<short>* LepLepId=0;
1894  std::vector<float>* LepPt=0;
1895  std::vector<float>* LepEta=0;
1896  std::vector<float>* LepPhi=0;
1897 
1898  short NJets30;
1899  std::vector<float>* JetPt=0;
1900  std::vector<float>* JetEta=0;
1901  std::vector<float>* JetPhi=0;
1902  std::vector<float>* JetMass=0;
1903  std::vector<float> myJetPt;
1904  std::vector<float> myJetEta;
1905  std::vector<float> myJetPhi;
1906  std::vector<float> myJetMass;
1907 
1908  float mesq_calc=0., cconst_calc=1.;
1909  float mesq_conserveDifermMass=0;
1910  float mesq_jetPtoEScale=0;
1911  float mzz = 126.;
1912  float m1 = 91.471450;
1913  float m2 = 12.139782;
1914  float h1 = 0.2682896;
1915  float h2 = 0.1679779;
1916  float phi = 1.5969792;
1917  float hs = -0.727181;
1918  float phi1 = 1.8828257;
1919  float ZZPt, ZZPhi, ZZEta;
1920  float genHEPMCweight;
1921  float wgt=1;
1922 
1923  TString cinput_main;
1924  if (sqrts==13) cinput_main = inputdir_13TeV;
1925  else return;
1926 
1927  vector<TString> strSamples = constructSamplesList("JJQCD", sqrts);
1928 
1929  unordered_map<TTree*, pair<float, float>> nGenMap;
1930  vector<TFile*> finputList;
1931  vector<TTree*> treeList;
1932  int nEntries=0;
1933  for (unsigned int is=0; is<strSamples.size(); is++){
1934  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples[is]).Data());
1935  TFile* finput = TFile::Open(cinput, "read");
1936  cout << "Opening file " << cinput << "..." << endl;
1937  TTree* tree=0;
1938  if (finput!=0){
1939  if (finput->IsOpen() && !finput->IsZombie()){
1940  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
1941  tree = (TTree*)finput->Get(TREE_NAME);
1942  if (tree!=0){
1943  cout << TREE_NAME << " is found." << endl;
1944  if (!recalculate && tree->GetBranchStatus("pConst_JJQCD_SIG_ghg2_1_JHUGen_JECNominal")==0) recalculate=true;
1945  tree->SetBranchStatus("*", 0);
1946  tree->SetBranchStatus("genHEPMCweight", 1); tree->SetBranchAddress("genHEPMCweight", &genHEPMCweight);
1947  tree->SetBranchStatus("nCleanedJetsPt30", 1); tree->SetBranchAddress("nCleanedJetsPt30", &NJets30);
1948  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
1949  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
1950  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
1951  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
1952  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
1953  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
1954  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
1955  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
1956  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
1957  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
1958  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
1959  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
1960  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
1961  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
1962  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
1963  tree->SetBranchStatus("LepLepId", 1); tree->SetBranchAddress("LepLepId", &LepLepId);
1964  tree->SetBranchStatus("LepPt", 1); tree->SetBranchAddress("LepPt", &LepPt);
1965  tree->SetBranchStatus("LepEta", 1); tree->SetBranchAddress("LepEta", &LepEta);
1966  tree->SetBranchStatus("LepPhi", 1); tree->SetBranchAddress("LepPhi", &LepPhi);
1967  if (!recalculate){
1968  tree->SetBranchStatus("p_JJQCD_SIG_ghg2_1_JHUGen_JECNominal", 1);
1969  tree->SetBranchAddress("p_JJQCD_SIG_ghg2_1_JHUGen_JECNominal", &mesq_calc);
1970  tree->SetBranchStatus("pConst_JJQCD_SIG_ghg2_1_JHUGen_JECNominal", 1);
1971  tree->SetBranchAddress("pConst_JJQCD_SIG_ghg2_1_JHUGen_JECNominal", &cconst_calc);
1972  }
1973 
1974  TH1F* htmp = (TH1F*)finput->Get(COUNTERS_NAME);
1975  pair<float, float> nsum(htmp->GetBinContent(1), htmp->GetBinContent(41)); // No PU reweighting
1976  nGenMap[tree]=nsum;
1977 
1978  nEntries += tree->GetEntries();
1979  treeList.push_back(tree);
1980  finputList.push_back(finput);
1981  }
1982  else if (finput->IsOpen()) finput->Close();
1983  }
1984  }
1985  }
1986  cout << "NEntries = " << nEntries << endl;
1987 
1988  vector<pair<float, int>> index;
1989  unsigned int ev_acc=0;
1990  for (int ev=0; ev<nEntries; ev++){
1991  getEntry(treeList, ev);
1992  if (NJets30<2) continue;
1993  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
1994  addByLowest(index, mzz, ev);
1995  ev_acc++;
1996  }
1997 
1998  float firstVal=index.at(0).first;
1999  float lastVal=index.at(index.size()-1).first;
2000  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
2001  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
2002  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
2003 
2004  float divisor=85000;
2005  int nbins = index.size()/divisor;
2006  const int nbins_th=10/*50*/;
2007  while (nbins<nbins_th){
2008  if (divisor>1000) divisor -= 1000;
2009  else if (divisor>100) divisor -= 100;
2010  else break;
2011  nbins=index.size()/divisor;
2012  }
2013  cout << "nbins=" << nbins << endl;
2014  if (nbins<3) cerr << "Not enough bins!" << endl;
2015  vector<ExtBin> binList;
2016  float* binning = new float[nbins+1];
2017  binning[0]=infimum;
2018  binning[nbins]=supremum;
2019  int ev_stepsize = index.size()/nbins;
2020  cout << "Event step size: " << ev_stepsize << endl;
2021  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
2022  for (int ix=1; ix<nbins; ix++){
2023  binning[ix]=(index[ix*ev_stepsize-1].first+index[ix*ev_stepsize].first)*0.5;
2024  ExtBin tmpbin;
2025  tmpbin.binlow = binning[ix-1];
2026  tmpbin.binhigh = binning[ix];
2027  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[(ix-1)*ev_stepsize+bin].second);
2028  binList.push_back(tmpbin);
2029  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
2030  }
2031  ExtBin tmpbin;
2032  tmpbin.binlow = binning[nbins-1];
2033  tmpbin.binhigh = binning[nbins];
2034  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index.size(); bin++) tmpbin.events.push_back(index[bin].second);
2035  binList.push_back(tmpbin);
2036  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
2037  cout << "Bin list has the following bins:" << endl;
2038  for (unsigned int ib=0; ib<binList.size(); ib++){
2039  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
2040  /*
2041  cout << "\tEvents: ";
2042  for (unsigned int ev=0; ev<binList.at(ib).events.size(); ev++){
2043  cout << binList.at(ib).events.at(ev) << " ";
2044  }
2045  cout << endl;
2046  */
2047  }
2048  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
2049 
2050  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
2051  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
2052  TProfile* hmesq_jetPtoEScale = new TProfile("P_MomentumToEnergy", "", nbins, binning); hmesq_jetPtoEScale->Sumw2();
2053 
2054 
2055  TTree* newtree=0;
2056  if (writeFinalTree){
2057  newtree = new TTree("FinalTree", "");
2058  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
2059  newtree->Branch("mesq_jetPtoEScale", &mesq_jetPtoEScale);
2060  newtree->Branch("ZZMass", &mzz);
2061  }
2062 
2063  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
2064 
2065  for (unsigned int bin=0; bin<binList.size(); bin++){
2066  cout << "Bin " << bin << " is now being scrutinized..." << endl;
2067  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
2068  int getEv = binList.at(bin).events.at(ev);
2069  getEntry(treeList, getEv);
2070  if (ev%1000==0) cout << "Doing event " << getEv << endl;
2071  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
2072  cerr << "Jet array sizes are less than Njets!" << endl;
2073  continue;
2074  }
2075  TTree* tree = findTree(treeList, getEv);
2076  wgt = fabs(genHEPMCweight*nGenMap[tree].first/nGenMap[tree].second);
2077 
2078  TLorentzVector jet[2], higgs;
2079  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(JetPt->at(ij), JetEta->at(ij), JetPhi->at(ij), JetMass->at(ij));
2080  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
2081  TVector3 boostH = higgs.BoostVector();
2082 
2084  associated.push_back(SimpleParticle_t(0, jet[0]));
2085  associated.push_back(SimpleParticle_t(0, jet[1]));
2086 
2088  for (int id=0; id<4; id++){
2089  double mass=0;
2090  if (abs(LepLepId->at(id))==13) mass = 0.105658;
2091  else if (abs(LepLepId->at(id))==11) mass = 0.000511;
2092  TLorentzVector pDaughter;
2093  pDaughter.SetPtEtaPhiM(LepPt->at(id), LepEta->at(id), LepPhi->at(id), mass);
2094  daughters.push_back(SimpleParticle_t(LepLepId->at(id), pDaughter));
2095  }
2096  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
2097 
2098  mela.setProcess(proc, me, prod);
2099 
2100  double alphasVal;
2101  if (recalculate){
2103  mela.computeProdP(mesq_conserveDifermMass, false);
2104  alphasVal = mela.getIORecord()->getAlphaSatMZ();
2105  mesq_conserveDifermMass /= pow(alphasVal, 4);
2106  }
2107  else{
2108  mesq_calc /= cconst_calc;
2109  mesq_conserveDifermMass=mesq_calc;
2110  }
2112  mela.computeProdP(mesq_jetPtoEScale, false);
2113  alphasVal = mela.getIORecord()->getAlphaSatMZ();
2114  mesq_jetPtoEScale /= pow(alphasVal, 4);
2115 
2116  bool doFill = !(
2117  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
2118  ||
2119  isnan(mesq_jetPtoEScale) || isinf(mesq_jetPtoEScale)
2120  );
2121 
2122  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, mesq_jetPtoEScale, wgt);
2123 
2124  mela.resetInputEvent();
2125  }
2126 
2127  binList.at(bin).sift(); binList.at(bin).adjustWeights();
2128 
2129  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
2130  mzz = binList.at(bin).masses.at(ev);
2131  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
2132  mesq_jetPtoEScale = binList.at(bin).me2vals.at(ev);
2133  wgt = binList.at(bin).weights.at(ev);
2134  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass, wgt);
2135  hmesq_jetPtoEScale->Fill(mzz, mesq_jetPtoEScale, wgt);
2136  hvar->Fill(mzz, mzz, wgt);
2137  if (writeFinalTree) newtree->Fill();
2138  }
2139  }
2140 
2141  double* xexyey[2][4];
2142  for (int inorm=0; inorm<2; inorm++){
2143  for (int ix=0; ix<4; ix++) xexyey[inorm][ix] = new double[nbins];
2144  for (int bin=0; bin<nbins; bin++){
2145  xexyey[inorm][0][bin] = hvar->GetBinContent(bin+1);
2146  xexyey[inorm][1][bin] = hvar->GetBinError(bin+1);
2147 
2148  if (inorm==0) cout << "Bin " << bin << " x-center: " << xexyey[inorm][0][bin] << " +- " << xexyey[inorm][1][bin] << endl;
2149 
2150  if (inorm==0){
2151  xexyey[inorm][2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
2152  xexyey[inorm][3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
2153  }
2154  else{
2155  xexyey[inorm][2][bin] = hmesq_jetPtoEScale->GetBinContent(bin+1);
2156  xexyey[inorm][3][bin] = hmesq_jetPtoEScale->GetBinError(bin+1);
2157  }
2158  xexyey[inorm][3][bin] = log10(xexyey[inorm][3][bin])/xexyey[inorm][2][bin];
2159  xexyey[inorm][2][bin] = log10(xexyey[inorm][2][bin]);
2160  }
2161  }
2162 
2163  for (int inorm=0; inorm<2; inorm++){
2164  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[inorm][0], xexyey[inorm][2], xexyey[inorm][1], xexyey[inorm][3]);
2165  if (inorm==0) tg->SetName("tg_P_ConserveDifermionMass");
2166  else tg->SetName("tg_P_MomentumToEnergy");
2167  foutput->WriteTObject(tg);
2168  delete tg;
2169  }
2170 
2171  for (int inorm=0; inorm<2; inorm++){
2172  for (int ix=0; ix<4; ix++) delete[] xexyey[inorm][ix];
2173  }
2174  foutput->WriteTObject(hmesq_jetPtoEScale);
2175  foutput->WriteTObject(hmesq_conserveDifermMass);
2176  foutput->WriteTObject(hvar);
2177  if (writeFinalTree) foutput->WriteTObject(newtree);
2178  if (writeFinalTree) delete newtree;
2179  delete hmesq_conserveDifermMass;
2180  delete hmesq_jetPtoEScale;
2181  delete hvar;
2182  foutput->Close();
2183  delete[] binning;
2184  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
2185 }
2186 
2187 
2188 /* SPECIFIC COMMENT: OUTPUT ME DIVIDED BY ALPHAS(MZ)**3 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION */
2189 void get_PAvgProfile_JHUGen_JQCD_HSMHiggs_13TeV(int sqrts=13, bool recalculate = true){
2190  int erg_tev=sqrts;
2191  float mPOLE=125.;
2192  TString TREE_NAME = "ZZTree/candTree";
2193  TString COUNTERS_NAME = "ZZTree/Counters";
2194  bool writeFinalTree=true;
2195 
2199 
2200  TString strproc = ProcessName(proc);
2201  TString strme = MatrixElementName(me);
2202  TString strprod = ProductionName(prod);
2203 
2204  TVar::VerbosityLevel verbosity = TVar::ERROR;
2205  Mela mela(erg_tev, mPOLE, verbosity);
2206 
2207  std::vector<short>* LepLepId=0;
2208  std::vector<float>* LepPt=0;
2209  std::vector<float>* LepEta=0;
2210  std::vector<float>* LepPhi=0;
2211 
2212  short NJets30;
2213  std::vector<float>* JetPt=0;
2214  std::vector<float>* JetEta=0;
2215  std::vector<float>* JetPhi=0;
2216  std::vector<float>* JetMass=0;
2217  std::vector<float> myJetPt;
2218  std::vector<float> myJetEta;
2219  std::vector<float> myJetPhi;
2220  std::vector<float> myJetMass;
2221 
2222  float mesq_calc=0., cconst_calc=1.;
2223  float mesq_conserveDifermMass=0;
2224  float mesq_jetPtoEScale=0;
2225  float mzz = 126.;
2226  float m1 = 91.471450;
2227  float m2 = 12.139782;
2228  float h1 = 0.2682896;
2229  float h2 = 0.1679779;
2230  float phi = 1.5969792;
2231  float hs = -0.727181;
2232  float phi1 = 1.8828257;
2233  float ZZPt, ZZPhi, ZZEta;
2234  float genHEPMCweight;
2235  float wgt=1;
2236 
2237  TString cinput_main;
2238  if (sqrts==13) cinput_main = inputdir_13TeV;
2239  else return;
2240 
2241  vector<TString> strSamples = constructSamplesList("JJQCD", sqrts);
2242 
2243  unordered_map<TTree*, pair<float, float>> nGenMap;
2244  vector<TFile*> finputList;
2245  vector<TTree*> treeList;
2246  int nEntries=0;
2247  for (unsigned int is=0; is<strSamples.size(); is++){
2248  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples[is]).Data());
2249  TFile* finput = TFile::Open(cinput, "read");
2250  cout << "Opening file " << cinput << "..." << endl;
2251  TTree* tree=0;
2252  if (finput!=0){
2253  if (finput->IsOpen() && !finput->IsZombie()){
2254  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
2255  tree = (TTree*)finput->Get(TREE_NAME);
2256  if (tree!=0){
2257  cout << TREE_NAME << " is found." << endl;
2258  if (!recalculate && tree->GetBranchStatus("pConst_JQCD_SIG_ghg2_1_JHUGen_JECNominal")==0) recalculate=true;
2259  tree->SetBranchStatus("*", 0);
2260  tree->SetBranchStatus("genHEPMCweight", 1); tree->SetBranchAddress("genHEPMCweight", &genHEPMCweight);
2261  tree->SetBranchStatus("nCleanedJetsPt30", 1); tree->SetBranchAddress("nCleanedJetsPt30", &NJets30);
2262  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
2263  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
2264  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
2265  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
2266  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
2267  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
2268  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
2269  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
2270  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
2271  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
2272  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
2273  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
2274  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
2275  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
2276  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
2277  tree->SetBranchStatus("LepLepId", 1); tree->SetBranchAddress("LepLepId", &LepLepId);
2278  tree->SetBranchStatus("LepPt", 1); tree->SetBranchAddress("LepPt", &LepPt);
2279  tree->SetBranchStatus("LepEta", 1); tree->SetBranchAddress("LepEta", &LepEta);
2280  tree->SetBranchStatus("LepPhi", 1); tree->SetBranchAddress("LepPhi", &LepPhi);
2281  if (!recalculate){
2282  tree->SetBranchStatus("p_JQCD_SIG_ghg2_1_JHUGen_JECNominal", 1);
2283  tree->SetBranchAddress("p_JQCD_SIG_ghg2_1_JHUGen_JECNominal", &mesq_calc);
2284  tree->SetBranchStatus("pConst_JQCD_SIG_ghg2_1_JHUGen_JECNominal", 1);
2285  tree->SetBranchAddress("pConst_JQCD_SIG_ghg2_1_JHUGen_JECNominal", &cconst_calc);
2286  }
2287 
2288  TH1F* htmp = (TH1F*)finput->Get(COUNTERS_NAME);
2289  pair<float, float> nsum(htmp->GetBinContent(1), htmp->GetBinContent(41)); // No PU reweighting
2290  nGenMap[tree]=nsum;
2291 
2292  nEntries += tree->GetEntries();
2293  treeList.push_back(tree);
2294  finputList.push_back(finput);
2295  }
2296  else if (finput->IsOpen()) finput->Close();
2297  }
2298  }
2299  }
2300  cout << "NEntries = " << nEntries << endl;
2301 
2302  vector<pair<float, int>> index;
2303  unsigned int ev_acc=0;
2304  for (int ev=0; ev<nEntries; ev++){
2305  getEntry(treeList, ev);
2306  if (NJets30!=1) continue;
2307  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
2308  addByLowest(index, mzz, ev);
2309  ev_acc++;
2310  }
2311 
2312  float firstVal=index.at(0).first;
2313  float lastVal=index.at(index.size()-1).first;
2314  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
2315  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
2316  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
2317 
2318  float divisor=85000;
2319  int nbins = index.size()/divisor;
2320  const int nbins_th=10/*50*/;
2321  while (nbins<nbins_th){
2322  if (divisor>1000) divisor -= 1000;
2323  else if (divisor>100) divisor -= 100;
2324  else break;
2325  nbins=index.size()/divisor;
2326  }
2327  cout << "nbins=" << nbins << endl;
2328  if (nbins<3) cerr << "Not enough bins!" << endl;
2329  vector<ExtBin> binList;
2330  float* binning = new float[nbins+1];
2331  binning[0]=infimum;
2332  binning[nbins]=supremum;
2333  int ev_stepsize = index.size()/nbins;
2334  cout << "Event step size: " << ev_stepsize << endl;
2335  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
2336  for (int ix=1; ix<nbins; ix++){
2337  binning[ix]=(index[ix*ev_stepsize-1].first+index[ix*ev_stepsize].first)*0.5;
2338  ExtBin tmpbin;
2339  tmpbin.binlow = binning[ix-1];
2340  tmpbin.binhigh = binning[ix];
2341  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[(ix-1)*ev_stepsize+bin].second);
2342  binList.push_back(tmpbin);
2343  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
2344  }
2345  ExtBin tmpbin;
2346  tmpbin.binlow = binning[nbins-1];
2347  tmpbin.binhigh = binning[nbins];
2348  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index.size(); bin++) tmpbin.events.push_back(index[bin].second);
2349  binList.push_back(tmpbin);
2350  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
2351  cout << "Bin list has the following bins:" << endl;
2352  for (unsigned int ib=0; ib<binList.size(); ib++){
2353  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
2354  }
2355  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
2356 
2357  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
2358  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
2359  TProfile* hmesq_jetPtoEScale = new TProfile("P_MomentumToEnergy", "", nbins, binning); hmesq_jetPtoEScale->Sumw2();
2360 
2361 
2362  TTree* newtree=0;
2363  if (writeFinalTree){
2364  newtree = new TTree("FinalTree", "");
2365  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
2366  newtree->Branch("mesq_jetPtoEScale", &mesq_jetPtoEScale);
2367  newtree->Branch("ZZMass", &mzz);
2368  }
2369 
2370  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
2371 
2372  for (unsigned int bin=0; bin<binList.size(); bin++){
2373  cout << "Bin " << bin << " is now being scrutinized..." << endl;
2374  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
2375  int getEv = binList.at(bin).events.at(ev);
2376  getEntry(treeList, getEv);
2377  if (ev%1000==0) cout << "Doing event " << getEv << endl;
2378  if (JetPt->size()<1 || JetEta->size()<1 || JetPhi->size()<1 || JetMass->size()<1){
2379  cerr << "Jet array sizes are less than Njets!" << endl;
2380  continue;
2381  }
2382  TTree* tree = findTree(treeList, getEv);
2383  wgt = fabs(genHEPMCweight*nGenMap[tree].first/nGenMap[tree].second);
2384 
2385  TLorentzVector jet, higgs;
2386  jet.SetPtEtaPhiM(JetPt->at(0), JetEta->at(0), JetPhi->at(0), JetMass->at(0));
2387  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
2388  TVector3 boostH = higgs.BoostVector();
2389 
2391  associated.push_back(SimpleParticle_t(0, jet));
2392 
2394  for (int id=0; id<4; id++){
2395  double mass=0;
2396  if (abs(LepLepId->at(id))==13) mass = 0.105658;
2397  else if (abs(LepLepId->at(id))==11) mass = 0.000511;
2398  TLorentzVector pDaughter;
2399  pDaughter.SetPtEtaPhiM(LepPt->at(id), LepEta->at(id), LepPhi->at(id), mass);
2400  daughters.push_back(SimpleParticle_t(LepLepId->at(id), pDaughter));
2401  }
2402  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
2403 
2404  mela.setProcess(proc, me, prod);
2405 
2406  double alphasVal;
2407  if (recalculate){
2409  mela.computeProdP(mesq_conserveDifermMass, false);
2410  alphasVal = mela.getIORecord()->getAlphaSatMZ();
2411  mesq_conserveDifermMass /= pow(alphasVal, 3);
2412  }
2413  else{
2414  mesq_calc /= cconst_calc;
2415  mesq_conserveDifermMass=mesq_calc;
2416  }
2418  mela.computeProdP(mesq_jetPtoEScale, false);
2419  alphasVal = mela.getIORecord()->getAlphaSatMZ();
2420  mesq_jetPtoEScale /= pow(alphasVal, 3);
2421 
2422  bool doFill = !(
2423  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
2424  ||
2425  isnan(mesq_jetPtoEScale) || isinf(mesq_jetPtoEScale)
2426  );
2427 
2428  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, mesq_jetPtoEScale, wgt);
2429 
2430  mela.resetInputEvent();
2431  }
2432 
2433  binList.at(bin).sift(); binList.at(bin).adjustWeights();
2434 
2435  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
2436  mzz = binList.at(bin).masses.at(ev);
2437  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
2438  mesq_jetPtoEScale = binList.at(bin).me2vals.at(ev);
2439  wgt = binList.at(bin).weights.at(ev);
2440  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass, wgt);
2441  hmesq_jetPtoEScale->Fill(mzz, mesq_jetPtoEScale, wgt);
2442  hvar->Fill(mzz, mzz, wgt);
2443  if (writeFinalTree) newtree->Fill();
2444  }
2445  }
2446 
2447  double* xexyey[2][4];
2448  for (int inorm=0; inorm<2; inorm++){
2449  for (int ix=0; ix<4; ix++) xexyey[inorm][ix] = new double[nbins];
2450  for (int bin=0; bin<nbins; bin++){
2451  xexyey[inorm][0][bin] = hvar->GetBinContent(bin+1);
2452  xexyey[inorm][1][bin] = hvar->GetBinError(bin+1);
2453 
2454  if (inorm==0) cout << "Bin " << bin << " x-center: " << xexyey[inorm][0][bin] << " +- " << xexyey[inorm][1][bin] << endl;
2455 
2456  if (inorm==0){
2457  xexyey[inorm][2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
2458  xexyey[inorm][3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
2459  }
2460  else{
2461  xexyey[inorm][2][bin] = hmesq_jetPtoEScale->GetBinContent(bin+1);
2462  xexyey[inorm][3][bin] = hmesq_jetPtoEScale->GetBinError(bin+1);
2463  }
2464  xexyey[inorm][3][bin] = log10(xexyey[inorm][3][bin])/xexyey[inorm][2][bin];
2465  xexyey[inorm][2][bin] = log10(xexyey[inorm][2][bin]);
2466  }
2467  }
2468 
2469  for (int inorm=0; inorm<2; inorm++){
2470  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[inorm][0], xexyey[inorm][2], xexyey[inorm][1], xexyey[inorm][3]);
2471  if (inorm==0) tg->SetName("tg_P_ConserveDifermionMass");
2472  else tg->SetName("tg_P_MomentumToEnergy");
2473  foutput->WriteTObject(tg);
2474  delete tg;
2475  }
2476 
2477  for (int inorm=0; inorm<2; inorm++){
2478  for (int ix=0; ix<4; ix++) delete[] xexyey[inorm][ix];
2479  }
2480  foutput->WriteTObject(hmesq_jetPtoEScale);
2481  foutput->WriteTObject(hmesq_conserveDifermMass);
2482  foutput->WriteTObject(hvar);
2483  if (writeFinalTree) foutput->WriteTObject(newtree);
2484  if (writeFinalTree) delete newtree;
2485  delete hmesq_conserveDifermMass;
2486  delete hmesq_jetPtoEScale;
2487  delete hvar;
2488  foutput->Close();
2489  delete[] binning;
2490  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
2491 }
2492 
2493 /*
2494 SPECIFIC COMMENTS:
2495 OUTPUT ME DIVIDED BY
2496 - H(1) PROPAGATOR
2497 - Ideal mJJ propagator is taken out in WH or ZH.
2498 - Reco mJJ propagator is multiplied.
2499 */
2500 void get_PAvgProfile_JHUGen_HadVH_HSMHiggs_13TeV(TString strprod, int sqrts=13, bool recalculate = true){
2501  if (!(strprod == "Had_ZH" || strprod == "Had_WH")) return;
2502  int erg_tev=sqrts;
2503  float mPOLE=125.;
2504  TString TREE_NAME = "ZZTree/candTree";
2505  TString COUNTERS_NAME = "ZZTree/Counters";
2506  bool writeFinalTree=true;
2507 
2511  for (int iprod=(int)TVar::JJVBF; iprod<(int)TVar::nProductions; iprod++){
2512  prod = (TVar::Production)iprod;
2513  if (TVar::ProductionName(prod)==strprod) break;
2514  }
2515  TString strsamples="";
2516  //TString strsamples2="";
2517  if (strprod == "Had_ZH") strsamples="ZH";
2518  else if (strprod == "Had_WH") strsamples="WH";
2519  //if (strprod == "Had_ZH") strsamples2="WH";
2520  //else if (strprod == "Had_WH") strsamples2="ZH";
2521 
2522  TString strproc = ProcessName(proc);
2523  TString strme = MatrixElementName(me);
2524 
2525  TVar::VerbosityLevel verbosity = TVar::ERROR;
2526  Mela mela(erg_tev, mPOLE, verbosity);
2527 
2528  std::vector<short>* LepLepId=0;
2529  std::vector<float>* LepPt=0;
2530  std::vector<float>* LepEta=0;
2531  std::vector<float>* LepPhi=0;
2532 
2533  short NJets30;
2534  std::vector<float>* JetPt=0;
2535  std::vector<float>* JetEta=0;
2536  std::vector<float>* JetPhi=0;
2537  std::vector<float>* JetMass=0;
2538  std::vector<float> myJetPt;
2539  std::vector<float> myJetEta;
2540  std::vector<float> myJetPhi;
2541  std::vector<float> myJetMass;
2542 
2543  float mesq_calc=0., cconst_calc=1., pmavjj=1.;
2544  float mesq_conserveDifermMass=0;
2545  float mzz = 126.;
2546  float m1 = 91.471450;
2547  float m2 = 12.139782;
2548  float h1 = 0.2682896;
2549  float h2 = 0.1679779;
2550  float phi = 1.5969792;
2551  float hs = -0.727181;
2552  float phi1 = 1.8828257;
2553  float ZZPt, ZZPhi, ZZEta;
2554  short Z1Flav, Z2Flav;
2555  float genxsec=0, genBR=0, ngen=0, genHEPMCweight=0;
2556  float wgt=1;
2557  float rewgt=1;
2558  bool doRewgt=false;
2559  bool recomputePmavjj=false;
2560  TString strrewgtbranch="";
2561  TString strrecalcbranch="";
2562  TString strrecalcconstbranch="";
2563  TString strpmavjjbranch="";
2564 
2565  TString cinput_main;
2566  if (sqrts==13) cinput_main = inputdir_13TeV;
2567  else return;
2568 
2569  vector<TString> strSamples = constructSamplesList(strsamples, sqrts);
2570  //vector<TString> strSamples2 = constructSamplesList(strsamples2, sqrts);
2571  //appendVector(strSamples, strSamples2);
2572 
2573  unordered_map<TTree*, pair<float, float>> nGenMap;
2574  unordered_map<TTree*, float> mass_map;
2575  unordered_map<float, vector<TTree*>> mass_sample_map;
2576 
2577  vector<TFile*> finputList;
2578  vector<TTree*> treeList;
2579  int nEntries=0;
2580  for (unsigned int is=0; is<strSamples.size(); is++){
2581  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples[is]).Data());
2582  TFile* finput = TFile::Open(cinput, "read");
2583  cout << "Opening file " << cinput << "..." << endl;
2584  TTree* tree=0;
2585  if (finput!=0){
2586  if (finput->IsOpen() && !finput->IsZombie()){
2587  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
2588  tree = (TTree*)finput->Get(TREE_NAME);
2589  if (tree!=0){
2590  cout << TREE_NAME << " is found." << endl;
2591  if (!recalculate){
2592  if (prod==TVar::Had_ZH && tree->GetBranchStatus("pConst_HadZH_SIG_ghz1_1_JHUGen_JECNominal")!=0){
2593  strrecalcbranch = "p_HadZH_SIG_ghz1_1_JHUGen_JECNominal";
2594  strrecalcconstbranch = "pConst_HadZH_SIG_ghz1_1_JHUGen_JECNominal";
2595  }
2596  else if (prod==TVar::Had_WH && tree->GetBranchStatus("pConst_HadWH_SIG_ghw1_1_JHUGen_JECNominal")!=0){
2597  strrecalcbranch = "p_HadWH_SIG_ghw1_1_JHUGen_JECNominal";
2598  strrecalcconstbranch = "pConst_HadWH_SIG_ghw1_1_JHUGen_JECNominal";
2599  }
2600  else recalculate=true;
2601  if (prod==TVar::Had_ZH && tree->GetBranchStatus("p_HadZH_mavjj_JECNominal")!=0) strpmavjjbranch="p_HadZH_mavjj_JECNominal";
2602  else if (prod==TVar::Had_WH && tree->GetBranchStatus("p_HadWH_mavjj_JECNominal")!=0) strpmavjjbranch="p_HadWH_mavjj_JECNominal";
2603  else recomputePmavjj=true;
2604  }
2605  tree->SetBranchStatus("*", 0);
2606  tree->SetBranchStatus("genxsec", 1); tree->SetBranchAddress("genxsec", &genxsec); tree->SetBranchStatus("genBR", 1); tree->SetBranchAddress("genBR", &genBR);
2607  tree->SetBranchStatus("genHEPMCweight", 1); tree->SetBranchAddress("genHEPMCweight", &genHEPMCweight);
2608  tree->SetBranchStatus("nCleanedJetsPt30", 1); tree->SetBranchAddress("nCleanedJetsPt30", &NJets30);
2609  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
2610  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
2611  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
2612  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
2613  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
2614  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
2615  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
2616  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
2617  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
2618  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
2619  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
2620  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
2621  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
2622  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
2623  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
2624  tree->SetBranchStatus("LepLepId", 1); tree->SetBranchAddress("LepLepId", &LepLepId);
2625  tree->SetBranchStatus("LepPt", 1); tree->SetBranchAddress("LepPt", &LepPt);
2626  tree->SetBranchStatus("LepEta", 1); tree->SetBranchAddress("LepEta", &LepEta);
2627  tree->SetBranchStatus("LepPhi", 1); tree->SetBranchAddress("LepPhi", &LepPhi);
2628  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
2629  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
2630  if (!recalculate){
2631  tree->SetBranchStatus(strrecalcbranch, 1); tree->SetBranchAddress(strrecalcbranch, &mesq_calc);
2632  tree->SetBranchStatus(strrecalcconstbranch, 1); tree->SetBranchAddress(strrecalcconstbranch, &cconst_calc);
2633  cout << "Extracting ME from " << strrecalcbranch << " and const from " << strrecalcconstbranch << endl;
2634  }
2635  else cout << "Recalculating the ME" << endl;
2636  if (!recomputePmavjj && strpmavjjbranch!=""){
2637  tree->SetBranchStatus(strpmavjjbranch, 1);
2638  tree->SetBranchAddress(strpmavjjbranch, &pmavjj);
2639  cout << "Extracting P_V(mJJ) from " << strpmavjjbranch << endl;
2640  }
2641  else if (recomputePmavjj) cout << "Recomputing P_V(mJJ)" << endl;
2642  else cout << "No valid P_V(mJJ)" << endl;
2643  tree->GetEntry(0);
2644  cout << "Cross section = " << genxsec*genBR << endl;
2645 
2646  TH1F* htmp = (TH1F*)finput->Get(COUNTERS_NAME);
2647  pair<float, float> nsum(htmp->GetBinContent(1), htmp->GetBinContent(41)); // No PU reweighting
2648  nGenMap[tree]=nsum;
2649 
2650  nEntries += tree->GetEntries();
2651  treeList.push_back(tree);
2652  finputList.push_back(finput);
2653 
2654  float polemass = findPoleMass(strSamples[is]);
2655  cout << "Pole mass = " << polemass << endl;
2656  mass_map[tree]=polemass;
2657  if (mass_sample_map.find(polemass)==mass_sample_map.end()){
2658  cout << "Inserting new pole mass sample array" << endl;
2659  vector<TTree*> dumarr;
2660  mass_sample_map[polemass] = dumarr;
2661  }
2662  mass_sample_map[polemass].push_back(tree);
2663  }
2664  else finput->Close();
2665  }
2666  else if (finput->IsOpen()) finput->Close();
2667  }
2668  }
2669  cout << "NEntries = " << nEntries << endl;
2670 
2671  for (auto it = mass_sample_map.begin(); it != mass_sample_map.end(); ++it){
2672  float sum_ngen=0;
2673  float sum_xsec=0;
2674  for (unsigned int ix=0; ix<it->second.size(); ix++){
2675  it->second.at(ix)->GetEntry(0);
2676  sum_ngen += nGenMap[it->second.at(ix)].first;
2677  sum_xsec += genxsec*genBR;
2678  }
2679  float overallWeight = sum_ngen/sum_xsec;
2680  for (unsigned int ix=0; ix<it->second.size(); ix++){
2681  cout << "Sum Hep MC weights in tree " << ix << " / " << it->second.size() << " was " << nGenMap[it->second.at(ix)].second << " over " << nGenMap[it->second.at(ix)].first << " total gen. events." << endl;
2682  nGenMap[it->second.at(ix)].first = overallWeight/nGenMap[it->second.at(ix)].second;
2683  cout << "Event scale for tree " << ix << " / " << it->second.size() << " at pole mass " << it->first << " = " << nGenMap[it->second.at(ix)].first << endl;
2684  }
2685  }
2686 
2687  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
2688 
2689  vector<pair<float, int>> index;
2690  unsigned int ev_acc=0;
2691  for (int ev=0; ev<nEntries; ev++){
2692  getEntry(treeList, ev);
2693  bool doProcess=NJets30>=2;
2694  if (!doProcess) continue;
2695  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
2696  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
2697  addByLowest(index, mzz, ev);
2698  ev_acc++;
2699  }
2700 
2701  float firstVal=index.at(0).first;
2702  float lastVal=index.at(index.size()-1).first;
2703  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
2704  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
2705  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
2706 
2707  float divisor=30000;
2708  //float divisor=60000;
2709  if (prod==TVar::Had_WH) divisor=45000;
2710  int nbins = index.size()/divisor;
2711  const int nbins_th=8/*50*/;
2712  while (nbins<nbins_th){
2713  if (divisor>1000) divisor -= 1000;
2714  else if (divisor>100) divisor -= 100;
2715  else break;
2716  nbins=index.size()/divisor;
2717  }
2718  cout << "nbins=" << nbins << endl;
2719  if (nbins<3) cerr << "Not enough bins!" << endl;
2720  vector<ExtBin> binList;
2721  float* binning = new float[nbins+1];
2722  binning[0]=infimum;
2723  binning[nbins]=supremum;
2724  int ev_stepsize = index.size()/nbins;
2725  cout << "Event step size: " << ev_stepsize << endl;
2726  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
2727  for (int ix=1; ix<nbins; ix++){
2728  binning[ix]=(index[ix*ev_stepsize-1].first+index[ix*ev_stepsize].first)*0.5;
2729  ExtBin tmpbin;
2730  tmpbin.binlow = binning[ix-1];
2731  tmpbin.binhigh = binning[ix];
2732  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[(ix-1)*ev_stepsize+bin].second);
2733  binList.push_back(tmpbin);
2734  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
2735  }
2736  ExtBin tmpbin;
2737  tmpbin.binlow = binning[nbins-1];
2738  tmpbin.binhigh = binning[nbins];
2739  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index.size(); bin++) tmpbin.events.push_back(index[bin].second);
2740  binList.push_back(tmpbin);
2741  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
2742  cout << "Bin list has the following bins:" << endl;
2743  for (unsigned int ib=0; ib<binList.size(); ib++){
2744  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
2745  }
2746 
2747  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
2748  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
2749 
2750  TTree* newtree=0;
2751  if (writeFinalTree){
2752  newtree = new TTree("FinalTree", "");
2753  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
2754  newtree->Branch("ZZMass", &mzz);
2755  newtree->Branch("weight", &wgt);
2756  }
2757 
2758  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
2759 
2760  double aL1=0, aR1=0, aL2=0, aR2=0;
2761  double mh=0, gah=0;
2762  double mv=0, gav=0;
2763  ev_acc=0;
2764  for (unsigned int bin=0; bin<binList.size(); bin++){
2765  cout << "Bin " << bin << " is now being scrutinized..." << endl;
2766  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
2767  int getEv = binList.at(bin).events.at(ev);
2768  getEntry(treeList, getEv);
2769  if (ev%1000==0) cout << "Doing event " << getEv << endl;
2770  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
2771  cerr << "Jet array sizes are less than Njets!" << endl;
2772  continue;
2773  }
2774  TTree* tree = findTree(treeList, getEv);
2775  wgt = fabs(genxsec*genBR*genHEPMCweight*nGenMap[tree].first*rewgt);
2776 
2777  TLorentzVector jet[2], higgs;
2778  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(JetPt->at(ij), JetEta->at(ij), JetPhi->at(ij), JetMass->at(ij));
2779  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
2780  TVector3 boostH = higgs.BoostVector();
2781 
2783  associated.push_back(SimpleParticle_t(0, jet[0]));
2784  associated.push_back(SimpleParticle_t(0, jet[1]));
2785 
2787  for (int id=0; id<4; id++){
2788  double mass=0;
2789  if (abs(LepLepId->at(id))==13) mass = 0.105658;
2790  else if (abs(LepLepId->at(id))==11) mass = 0.000511;
2791  TLorentzVector pDaughter;
2792  pDaughter.SetPtEtaPhiM(LepPt->at(id), LepEta->at(id), LepPhi->at(id), mass);
2793  daughters.push_back(SimpleParticle_t(LepLepId->at(id), pDaughter));
2794  }
2795  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
2796 
2797  mela.setProcess(proc, me, prod);
2798 
2799  double propagator=1;
2800  double propagatorV=1;
2801  if (recalculate || ev_acc==0){
2803  mela.computeProdP(mesq_conserveDifermMass, false);
2804  if (!recalculate){
2805  mesq_calc /= cconst_calc;
2806  mesq_conserveDifermMass=mesq_calc;
2807 
2808  mh = mass_map[tree];
2809  gah = mela.getHiggsWidthAtPoleMass(mh);
2810  }
2811  else{
2812  mela.getIORecord()->getHiggsMassWidth(mh, gah, 0);
2813  }
2814  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
2815  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
2816  if (prod==TVar::Had_ZH){
2817  mv = mela.getPrimaryMass(23);
2818  gav = mela.getPrimaryWidth(23);
2819  }
2820  else if (prod==TVar::Had_WH){
2821  mv = mela.getPrimaryMass(24);
2822  gav = mela.getPrimaryWidth(24);
2823  }
2824  }
2825  else{
2826  mesq_calc /= cconst_calc;
2827  mesq_conserveDifermMass=mesq_calc;
2828  }
2829  propagator = 1./(pow(pow(mzz, 2)-pow(mh, 2), 2) + pow(mh*gah, 2));
2830  mesq_conserveDifermMass /= propagator;
2831  if (prod==TVar::Had_ZH || prod==TVar::Had_WH){
2832  double mjj = (jet[0] + jet[1]).M();
2833  propagatorV = 1./(pow(pow(mjj, 2)-pow(mv, 2), 2) + pow(mv*gav, 2));
2834  mesq_conserveDifermMass /= propagatorV;
2835  }
2836  if (recomputePmavjj) mela.computeDijetConvBW(pmavjj, false);
2837  mesq_conserveDifermMass *= pmavjj;
2838 
2839  bool doFill = !(
2840  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
2841  );
2842 
2843  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0, wgt);
2844 
2845  mela.resetInputEvent();
2846  ev_acc++;
2847  }
2848 
2849  binList.at(bin).sift(); binList.at(bin).adjustWeights(10);
2850 
2851  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
2852  mzz = binList.at(bin).masses.at(ev);
2853  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
2854  wgt = binList.at(bin).weights.at(ev);
2855  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
2856  hvar->Fill(mzz, mzz);
2857  if (writeFinalTree) newtree->Fill();
2858  }
2859  }
2860  cout << "Total events accumulated: " << ev_acc << endl;
2861 
2862  double* xexyey[4];
2863  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
2864  for (int bin=0; bin<nbins; bin++){
2865  xexyey[0][bin] = hvar->GetBinContent(bin+1);
2866  xexyey[1][bin] = hvar->GetBinError(bin+1);
2867 
2868  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
2869 
2870  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
2871  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
2872  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
2873  xexyey[2][bin] = log10(xexyey[2][bin]);
2874  }
2875 
2876  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
2877  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
2878  foutput->WriteTObject(tg);
2879  delete tg;
2880 
2881  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
2882 
2883  foutput->WriteTObject(hmesq_conserveDifermMass);
2884  foutput->WriteTObject(hvar);
2885  if (writeFinalTree) foutput->WriteTObject(newtree);
2886  if (writeFinalTree) delete newtree;
2887  delete hmesq_conserveDifermMass;
2888  delete hvar;
2889  delete[] binning;
2890 
2891  foutput->Close();
2892  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
2893 }
2894 
2895 
2896 /*
2897 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
2898 - H(1) PROPAGATOR
2899 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
2900 - Ideal mJJ propagator is taken out in WH or ZH.
2901 - Reco mJJ propagator is multiplied.
2902 */
2903 void get_PAvgProfile_MCFM_JJPROD_S_HSMHiggs_13TeV(TString strprod, int sqrts=13, bool recalculate = true){
2904  if (!(strprod == "Had_ZH_S" || strprod == "Had_WH_S" || strprod == "JJVBF_S")) return;
2905  int erg_tev=sqrts;
2906  float mPOLE=125.;
2907  TString TREE_NAME = "ZZTree/candTree";
2908  TString COUNTERS_NAME = "ZZTree/Counters";
2909  bool writeFinalTree=true;
2910 
2914  for (int iprod=(int) TVar::ZZGG; iprod<(int) TVar::nProductions; iprod++){
2915  prod = (TVar::Production)iprod;
2916  if (TVar::ProductionName(prod)==strprod) break;
2917  }
2918  TString strsamples;
2919  if (strprod == "Had_ZH_S") strsamples="ZH";
2920  else if (strprod == "Had_WH_S") strsamples="WH";
2921  else if (strprod == "JJVBF_S") strsamples="JJVBF";
2922 
2923  TString strproc = ProcessName(proc);
2924  TString strme = MatrixElementName(me);
2925 
2926  TVar::VerbosityLevel verbosity = TVar::ERROR;
2927  Mela mela(erg_tev, mPOLE, verbosity);
2928 
2929  std::vector<short>* LepLepId=0;
2930  std::vector<float>* LepPt=0;
2931  std::vector<float>* LepEta=0;
2932  std::vector<float>* LepPhi=0;
2933 
2934  short NJets30;
2935  std::vector<float>* JetPt=0;
2936  std::vector<float>* JetEta=0;
2937  std::vector<float>* JetPhi=0;
2938  std::vector<float>* JetMass=0;
2939  std::vector<float> myJetPt;
2940  std::vector<float> myJetEta;
2941  std::vector<float> myJetPhi;
2942  std::vector<float> myJetMass;
2943 
2944  float mesq_calc=0., cconst_calc=1., pmavjj=1.;
2945  float mesq_conserveDifermMass=0;
2946  float mzz = 126.;
2947  float m1 = 91.471450;
2948  float m2 = 12.139782;
2949  float h1 = 0.2682896;
2950  float h2 = 0.1679779;
2951  float phi = 1.5969792;
2952  float hs = -0.727181;
2953  float phi1 = 1.8828257;
2954  float ZZPt, ZZPhi, ZZEta;
2955  short Z1Flav, Z2Flav;
2956  float genxsec=0, genBR=0, ngen=0, genHEPMCweight=0;
2957  float wgt=1;
2958  float rewgt=1;
2959  bool doRewgt=false;
2960  bool recomputePmavjj=false;
2961  TString strrewgtbranch="";
2962  TString strrecalcbranch="";
2963  TString strrecalcconstbranch="";
2964  TString strpmavjjbranch="";
2965 
2966  TString cinput_main;
2967  if (sqrts==13) cinput_main = inputdir_13TeV;
2968  else return;
2969 
2970  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
2971  vector<TString> strSamples = constructSamplesList(strsamples, sqrts);
2972 
2973  unordered_map<TTree*, pair<float, float>> nGenMap;
2974  unordered_map<TTree*, float> mass_map;
2975  unordered_map<float, vector<TTree*>> mass_sample_map;
2976 
2977  vector<TFile*> finputList;
2978  vector<TTree*> treeList;
2979  int nEntries=0;
2980  for (unsigned int is=0; is<strSamples.size(); is++){
2981  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples[is]).Data());
2982  TFile* finput = TFile::Open(cinput, "read");
2983  cout << "Opening file " << cinput << "..." << endl;
2984  TTree* tree=0;
2985  if (finput!=0){
2986  if (finput->IsOpen() && !finput->IsZombie()){
2987  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
2988  tree = (TTree*)finput->Get(TREE_NAME);
2989  if (tree!=0){
2990  cout << TREE_NAME << " is found." << endl;
2991  if (!recalculate){
2992  if (prod==TVar::Had_ZH_S && tree->GetBranchStatus("pConst_HadZH_S_SIG_ghz1_1_MCFM_JECNominal")!=0){
2993  strrecalcbranch = "p_HadZH_S_SIG_ghz1_1_MCFM_JECNominal";
2994  strrecalcconstbranch = "pConst_HadZH_S_SIG_ghz1_1_MCFM_JECNominal";
2995  }
2996  else if (prod==TVar::Had_WH_S && tree->GetBranchStatus("pConst_HadWH_S_SIG_ghv1_1_MCFM_JECNominal")!=0){
2997  strrecalcbranch = "p_HadWH_S_SIG_ghv1_1_MCFM_JECNominal";
2998  strrecalcconstbranch = "pConst_HadWH_S_SIG_ghv1_1_MCFM_JECNominal";
2999  }
3000  else if (prod==TVar::JJVBF_S && tree->GetBranchStatus("pConst_JJVBF_S_SIG_ghv1_1_MCFM_JECNominal")!=0){
3001  strrecalcbranch = "p_JJVBF_S_SIG_ghv1_1_MCFM_JECNominal";
3002  strrecalcconstbranch = "pConst_JJVBF_S_SIG_ghv1_1_MCFM_JECNominal";
3003  }
3004  else recalculate=true;
3005  if (prod==TVar::Had_ZH_S && tree->GetBranchStatus("p_HadZH_mavjj_JECNominal")!=0) strpmavjjbranch="p_HadZH_mavjj_JECNominal";
3006  else if (prod==TVar::Had_WH_S && tree->GetBranchStatus("p_HadWH_mavjj_JECNominal")!=0) strpmavjjbranch="p_HadWH_mavjj_JECNominal";
3007  else if (prod!=TVar::JJVBF_S) recomputePmavjj=true;
3008  }
3009  tree->SetBranchStatus("*", 0);
3010  tree->SetBranchStatus("genxsec", 1); tree->SetBranchAddress("genxsec", &genxsec); tree->SetBranchStatus("genBR", 1); tree->SetBranchAddress("genBR", &genBR);
3011  tree->SetBranchStatus("genHEPMCweight", 1); tree->SetBranchAddress("genHEPMCweight", &genHEPMCweight);
3012  tree->SetBranchStatus("nCleanedJetsPt30", 1); tree->SetBranchAddress("nCleanedJetsPt30", &NJets30);
3013  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
3014  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
3015  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
3016  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
3017  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
3018  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
3019  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
3020  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
3021  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
3022  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
3023  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
3024  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
3025  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
3026  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
3027  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
3028  tree->SetBranchStatus("LepLepId", 1); tree->SetBranchAddress("LepLepId", &LepLepId);
3029  tree->SetBranchStatus("LepPt", 1); tree->SetBranchAddress("LepPt", &LepPt);
3030  tree->SetBranchStatus("LepEta", 1); tree->SetBranchAddress("LepEta", &LepEta);
3031  tree->SetBranchStatus("LepPhi", 1); tree->SetBranchAddress("LepPhi", &LepPhi);
3032  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
3033  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
3034  if (!recalculate){
3035  tree->SetBranchStatus(strrecalcbranch, 1); tree->SetBranchAddress(strrecalcbranch, &mesq_calc);
3036  tree->SetBranchStatus(strrecalcconstbranch, 1); tree->SetBranchAddress(strrecalcconstbranch, &cconst_calc);
3037  cout << "Extracting ME from " << strrecalcbranch << " and const from " << strrecalcconstbranch << endl;
3038  }
3039  else cout << "Recalculating the ME" << endl;
3040  if (!recomputePmavjj && strpmavjjbranch!=""){
3041  tree->SetBranchStatus(strpmavjjbranch, 1);
3042  tree->SetBranchAddress(strpmavjjbranch, &pmavjj);
3043  cout << "Extracting P_V(mJJ) from " << strpmavjjbranch << endl;
3044  }
3045  else if (recomputePmavjj) cout << "Recomputing P_V(mJJ)" << endl;
3046  else cout << "No valid P_V(mJJ)" << endl;
3047 
3048  tree->GetEntry(0);
3049  cout << "Cross section = " << genxsec*genBR << endl;
3050 
3051  TH1F* htmp = (TH1F*)finput->Get(COUNTERS_NAME);
3052  pair<float, float> nsum(htmp->GetBinContent(1), htmp->GetBinContent(41)); // No PU reweighting
3053  nGenMap[tree]=nsum;
3054 
3055  nEntries += tree->GetEntries();
3056  treeList.push_back(tree);
3057  finputList.push_back(finput);
3058 
3059  float polemass = findPoleMass(strSamples[is]);
3060  cout << "Pole mass = " << polemass << endl;
3061  mass_map[tree]=polemass;
3062  if (mass_sample_map.find(polemass)==mass_sample_map.end()){
3063  cout << "Inserting new pole mass sample array" << endl;
3064  vector<TTree*> dumarr;
3065  mass_sample_map[polemass] = dumarr;
3066  }
3067  mass_sample_map[polemass].push_back(tree);
3068  }
3069  else finput->Close();
3070  }
3071  else if (finput->IsOpen()) finput->Close();
3072  }
3073  }
3074  cout << "NEntries = " << nEntries << endl;
3075 
3076  for (auto it = mass_sample_map.begin(); it != mass_sample_map.end(); ++it){
3077  float sum_ngen=0;
3078  float sum_xsec=0;
3079  for (unsigned int ix=0; ix<it->second.size(); ix++){
3080  it->second.at(ix)->GetEntry(0);
3081  sum_ngen += nGenMap[it->second.at(ix)].first;
3082  sum_xsec += genxsec*genBR;
3083  }
3084  float overallWeight = sum_ngen/sum_xsec;
3085  for (unsigned int ix=0; ix<it->second.size(); ix++){
3086  cout << "Sum Hep MC weights in tree " << ix << " / " << it->second.size() << " was " << nGenMap[it->second.at(ix)].second << " over " << nGenMap[it->second.at(ix)].first << " total gen. events." << endl;
3087  nGenMap[it->second.at(ix)].first = overallWeight/nGenMap[it->second.at(ix)].second;
3088  cout << "Event scale for tree " << ix << " / " << it->second.size() << " at pole mass " << it->first << " = " << nGenMap[it->second.at(ix)].first << endl;
3089  }
3090  }
3091 
3092  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
3093 
3094  vector<pair<float, int>> index[3];
3095  unsigned int ev_acc=0;
3096  for (int ev=0; ev<nEntries; ev++){
3097  getEntry(treeList, ev);
3098  bool doProcess=
3099  NJets30>=2 && (
3100  Z1Flav*Z2Flav==pow(13, 4)
3101  ||
3102  Z1Flav*Z2Flav==pow(11, 4)
3103  ||
3104  Z1Flav*Z2Flav==pow(11*13, 2)
3105  )
3106  ;
3107  if (!doProcess) continue;
3108  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
3109  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
3110  addByLowest(index[ic], mzz, ev);
3111  if (ic<2) addByLowest(index[1-ic], mzz, ev);
3112  ev_acc++;
3113  }
3114 
3115  for (unsigned int ic=0; ic<3; ic++){
3116  float firstVal=index[ic].at(0).first;
3117  float lastVal=index[ic].at(index[ic].size()-1).first;
3118  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
3119  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
3120  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
3121 
3122  float divisor=15000;
3123  if (prod==TVar::Had_ZH_S || prod==TVar::Had_WH_S) divisor=12000;
3124  int nbins = index[ic].size()/divisor;
3125  const int nbins_th=10/*50*/;
3126  while (nbins<nbins_th){
3127  if (divisor>1000) divisor -= 1000;
3128  else if (divisor>100) divisor -= 100;
3129  else break;
3130  nbins=index[ic].size()/divisor;
3131  }
3132  cout << "nbins=" << nbins << endl;
3133  if (nbins<3) cerr << "Not enough bins!" << endl;
3134  vector<ExtBin> binList;
3135  float* binning = new float[nbins+1];
3136  binning[0]=infimum;
3137  binning[nbins]=supremum;
3138  int ev_stepsize = index[ic].size()/nbins;
3139  cout << "Event step size: " << ev_stepsize << endl;
3140  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
3141  for (int ix=1; ix<nbins; ix++){
3142  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
3143  ExtBin tmpbin;
3144  tmpbin.binlow = binning[ix-1];
3145  tmpbin.binhigh = binning[ix];
3146  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[ic][(ix-1)*ev_stepsize+bin].second);
3147  binList.push_back(tmpbin);
3148  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
3149  }
3150  ExtBin tmpbin;
3151  tmpbin.binlow = binning[nbins-1];
3152  tmpbin.binhigh = binning[nbins];
3153  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++) tmpbin.events.push_back(index[ic][bin].second);
3154  binList.push_back(tmpbin);
3155  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
3156  cout << "Bin list has the following bins:" << endl;
3157  for (unsigned int ib=0; ib<binList.size(); ib++){
3158  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
3159  }
3160 
3161  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
3162  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
3163 
3164  TTree* newtree=0;
3165  if (writeFinalTree){
3166  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
3167  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
3168  newtree->Branch("ZZMass", &mzz);
3169  newtree->Branch("weight", &wgt);
3170  }
3171 
3172  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
3173 
3174  double aL1=0, aR1=0, aL2=0, aR2=0;
3175  double mh=0, gah=0;
3176  double mv=0, gav=0;
3177  unsigned int ev_acc=0;
3178  for (unsigned int bin=0; bin<binList.size(); bin++){
3179  cout << "Bin " << bin << " is now being scrutinized..." << endl;
3180  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
3181  int getEv = binList.at(bin).events.at(ev);
3182  getEntry(treeList, getEv);
3183  if (ev%1000==0) cout << "Doing event " << getEv << endl;
3184  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
3185  cerr << "Jet array sizes are less than Njets!" << endl;
3186  continue;
3187  }
3188  TTree* tree = findTree(treeList, getEv);
3189  wgt = fabs(genxsec*genBR*genHEPMCweight*nGenMap[tree].first*rewgt);
3190 
3191  TLorentzVector jet[2], higgs;
3192  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(JetPt->at(ij), JetEta->at(ij), JetPhi->at(ij), JetMass->at(ij));
3193  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
3194  TVector3 boostH = higgs.BoostVector();
3195 
3197  associated.push_back(SimpleParticle_t(0, jet[0]));
3198  associated.push_back(SimpleParticle_t(0, jet[1]));
3199 
3201  for (int id=0; id<4; id++){
3202  double mass=0;
3203  if (abs(LepLepId->at(id))==13) mass = 0.105658;
3204  else if (abs(LepLepId->at(id))==11) mass = 0.000511;
3205  TLorentzVector pDaughter;
3206  pDaughter.SetPtEtaPhiM(LepPt->at(id), LepEta->at(id), LepPhi->at(id), mass);
3207  daughters.push_back(SimpleParticle_t(LepLepId->at(id), pDaughter));
3208  }
3209  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
3210 
3211  mela.setProcess(proc, me, prod);
3212 
3213  double propagator=1;
3214  double propagatorV=1;
3215  if (recalculate || ev_acc==0){
3217  mela.computeProdP(mesq_conserveDifermMass, false);
3218  if (!recalculate){
3219  mesq_calc /= cconst_calc;
3220  mesq_conserveDifermMass=mesq_calc;
3221 
3222  mh = mPOLE;
3223  gah = mela.getHiggsWidthAtPoleMass(mh);
3224  }
3225  else{
3226  mela.getIORecord()->getHiggsMassWidth(mh, gah, 0);
3227  }
3228  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
3229  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
3230  if (prod==TVar::Had_ZH_S){
3231  mv = mela.getPrimaryMass(23);
3232  gav = mela.getPrimaryWidth(23);
3233  }
3234  else if (prod==TVar::Had_WH_S){
3235  mv = mela.getPrimaryMass(24);
3236  gav = mela.getPrimaryWidth(24);
3237  }
3238  }
3239  else{
3240  mesq_calc /= cconst_calc;
3241  mesq_conserveDifermMass=mesq_calc;
3242  }
3243  propagator = 1./(pow(pow(mzz, 2)-pow(mh, 2), 2) + pow(mh*gah, 2));
3244  mesq_conserveDifermMass /= propagator;
3245  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
3246  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
3248  double mjj = (jet[0] + jet[1]).M();
3249  propagatorV = 1./(pow(pow(mjj, 2)-pow(mv, 2), 2) + pow(mv*gav, 2));
3250  mesq_conserveDifermMass /= propagatorV;
3251  if (recomputePmavjj){
3253  mela.computeDijetConvBW(pmavjj, false);
3254  }
3255  mesq_conserveDifermMass *= pmavjj;
3256  }
3257 
3258  bool doFill = !(
3259  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
3260  );
3261 
3262  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0, wgt);
3263 
3264  mela.resetInputEvent();
3265  ev_acc++;
3266  }
3267 
3268  binList.at(bin).sift(); binList.at(bin).adjustWeights(10);
3269 
3270  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
3271  mzz = binList.at(bin).masses.at(ev);
3272  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
3273  wgt = binList.at(bin).weights.at(ev);
3274  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
3275  hvar->Fill(mzz, mzz);
3276  if (writeFinalTree) newtree->Fill();
3277  }
3278  }
3279  cout << "Total events accumulated: " << ev_acc << endl;
3280 
3281  double* xexyey[4];
3282  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
3283  for (int bin=0; bin<nbins; bin++){
3284  xexyey[0][bin] = hvar->GetBinContent(bin+1);
3285  xexyey[1][bin] = hvar->GetBinError(bin+1);
3286 
3287  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
3288 
3289  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
3290  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
3291  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
3292  xexyey[2][bin] = log10(xexyey[2][bin]);
3293  }
3294 
3295  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
3296  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
3297  foutput->WriteTObject(tg);
3298  delete tg;
3299 
3300  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
3301 
3302  foutput->WriteTObject(hmesq_conserveDifermMass);
3303  foutput->WriteTObject(hvar);
3304  if (writeFinalTree) foutput->WriteTObject(newtree);
3305  if (writeFinalTree) delete newtree;
3306  delete hmesq_conserveDifermMass;
3307  delete hvar;
3308  delete[] binning;
3309  }
3310  foutput->Close();
3311  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
3312 }
3313 
3314 /*
3315 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
3316 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
3317 - PROP(Z)/LINE_PROP(Z) AROUND M4L~=MZ
3318 - Ideal mJJ propagator is taken out in WZZ or ZZZ.
3319 - Reco mJJ propagator is multiplied.
3320 */
3321 void get_PAvgProfile_MCFM_JJPROD_bkgZZ_13TeV(TString strprod, int sqrts=13, bool recalculate = true){
3322  if (!(strprod == "Had_ZH" || strprod == "Had_WH" || strprod == "JJVBF")) return;
3323  int erg_tev=sqrts;
3324  float mPOLE=125.;
3325  TString TREE_NAME = "ZZTree/candTree";
3326  TString COUNTERS_NAME = "ZZTree/Counters";
3327  bool writeFinalTree=true;
3328 
3329  TVar::Process proc = TVar::bkgZZ;
3332  for (int iprod=(int) TVar::ZZGG; iprod<(int) TVar::nProductions; iprod++){
3333  prod = (TVar::Production)iprod;
3334  if (TVar::ProductionName(prod)==strprod) break;
3335  }
3336  TString strsamples;
3337  if (strprod == "Had_ZH") strsamples="ZH";
3338  else if (strprod == "Had_WH") strsamples="WH";
3339  else if (strprod == "JJVBF") strsamples="JJVBF";
3340 
3341  TString strproc = ProcessName(proc);
3342  TString strme = MatrixElementName(me);
3343 
3344  TVar::VerbosityLevel verbosity = TVar::ERROR;
3345  Mela mela(erg_tev, mPOLE, verbosity);
3346 
3347  std::vector<short>* LepLepId=0;
3348  std::vector<float>* LepPt=0;
3349  std::vector<float>* LepEta=0;
3350  std::vector<float>* LepPhi=0;
3351 
3352  short NJets30;
3353  std::vector<float>* JetPt=0;
3354  std::vector<float>* JetEta=0;
3355  std::vector<float>* JetPhi=0;
3356  std::vector<float>* JetMass=0;
3357  std::vector<float> myJetPt;
3358  std::vector<float> myJetEta;
3359  std::vector<float> myJetPhi;
3360  std::vector<float> myJetMass;
3361 
3362  float GenHMass=-1;
3363  std::vector<float>* LHEMotherPz=0;
3364  std::vector<float>* LHEMotherE=0;
3365  std::vector<short>* LHEMotherId=0;
3366  std::vector<float>* LHEDaughterPt=0;
3367  std::vector<float>* LHEDaughterEta=0;
3368  std::vector<float>* LHEDaughterPhi=0;
3369  std::vector<float>* LHEDaughterMass=0;
3370  std::vector<short>* LHEDaughterId=0;
3371  std::vector<float>* LHEAssociatedParticlePt=0;
3372  std::vector<float>* LHEAssociatedParticleEta=0;
3373  std::vector<float>* LHEAssociatedParticlePhi=0;
3374  std::vector<float>* LHEAssociatedParticleMass=0;
3375  std::vector<short>* LHEAssociatedParticleId=0;
3376 
3377  float mesq_calc=0., cconst_calc=1., pmavjj=1.;
3378  float mesq_conserveDifermMass=0;
3379  float mzz = 126.;
3380  float m1 = 91.471450;
3381  float m2 = 12.139782;
3382  float h1 = 0.2682896;
3383  float h2 = 0.1679779;
3384  float phi = 1.5969792;
3385  float hs = -0.727181;
3386  float phi1 = 1.8828257;
3387  float ZZPt, ZZPhi, ZZEta;
3388  short Z1Flav, Z2Flav;
3389  float genxsec=0, genBR=0, ngen=0, genHEPMCweight=0;
3390  float wgt=1;
3391  float rewgt=1;
3392  bool doRewgt=false;
3393  bool recomputePmavjj=false;
3394  TString strrewgtbranch="";
3395  TString strrecalcbranch="";
3396  TString strrecalcconstbranch="";
3397  TString strpmavjjbranch="";
3398 
3399  TString cinput_main;
3400  if (sqrts==13) cinput_main = inputdir_13TeV;
3401  else return;
3402 
3403  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
3404  vector<TString> strSamples = constructSamplesList(strsamples, sqrts);
3405 
3406  unordered_map<TTree*, pair<float, float>> nGenMap;
3407  unordered_map<TTree*, float> mass_map;
3408  unordered_map<float, vector<TTree*>> mass_sample_map;
3409 
3410  vector<TFile*> finputList;
3411  vector<TTree*> treeList;
3412  int nEntries=0;
3413  for (unsigned int is=0; is<strSamples.size(); is++){
3414  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples[is]).Data());
3415  TFile* finput = TFile::Open(cinput, "read");
3416  cout << "Opening file " << cinput << "..." << endl;
3417  TTree* tree=0;
3418  if (finput!=0){
3419  if (finput->IsOpen() && !finput->IsZombie()){
3420  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
3421  tree = (TTree*)finput->Get(TREE_NAME);
3422  if (tree!=0){
3423  cout << TREE_NAME << " is found." << endl;
3424  if (!recalculate){
3425  if (prod==TVar::Had_ZH && tree->GetBranchStatus("pConst_HadZH_BKG_MCFM_JECNominal")!=0){
3426  strrecalcbranch = "p_HadZH_BKG_MCFM_JECNominal";
3427  strrecalcconstbranch = "pConst_HadZH_BKG_MCFM_JECNominal";
3428  }
3429  else if (prod==TVar::Had_WH && tree->GetBranchStatus("pConst_HadWH_BKG_MCFM_JECNominal")!=0){
3430  strrecalcbranch = "p_HadWH_BKG_MCFM_JECNominal";
3431  strrecalcconstbranch = "pConst_HadWH_BKG_MCFM_JECNominal";
3432  }
3433  else if (prod==TVar::JJVBF && tree->GetBranchStatus("pConst_JJVBF_BKG_MCFM_JECNominal")!=0){
3434  strrecalcbranch = "p_JJVBF_BKG_MCFM_JECNominal";
3435  strrecalcconstbranch = "pConst_JJVBF_BKG_MCFM_JECNominal";
3436  }
3437  else recalculate=true;
3438  if (prod==TVar::Had_ZH && tree->GetBranchStatus("p_HadZH_mavjj_JECNominal")!=0) strpmavjjbranch="p_HadZH_mavjj_JECNominal";
3439  else if (prod==TVar::Had_WH && tree->GetBranchStatus("p_HadWH_mavjj_JECNominal")!=0) strpmavjjbranch="p_HadWH_mavjj_JECNominal";
3440  else if (prod!=TVar::JJVBF) recomputePmavjj=true;
3441  }
3442  if (tree->GetBranchStatus("p_Gen_JJEW_BKG_MCFM")!=0) strrewgtbranch = "p_Gen_JJEW_BKG_MCFM";
3443  tree->SetBranchStatus("*", 0);
3444  tree->SetBranchStatus("genxsec", 1); tree->SetBranchAddress("genxsec", &genxsec); tree->SetBranchStatus("genBR", 1); tree->SetBranchAddress("genBR", &genBR);
3445  tree->SetBranchStatus("genHEPMCweight", 1); tree->SetBranchAddress("genHEPMCweight", &genHEPMCweight);
3446  tree->SetBranchStatus("GenHMass", 1); tree->SetBranchAddress("GenHMass", &GenHMass);
3447  tree->SetBranchStatus("nCleanedJetsPt30", 1); tree->SetBranchAddress("nCleanedJetsPt30", &NJets30);
3448  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
3449  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
3450  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
3451  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
3452  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
3453  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
3454  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
3455  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
3456  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
3457  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
3458  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
3459  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
3460  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
3461  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
3462  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
3463  tree->SetBranchStatus("LepLepId", 1); tree->SetBranchAddress("LepLepId", &LepLepId);
3464  tree->SetBranchStatus("LepPt", 1); tree->SetBranchAddress("LepPt", &LepPt);
3465  tree->SetBranchStatus("LepEta", 1); tree->SetBranchAddress("LepEta", &LepEta);
3466  tree->SetBranchStatus("LepPhi", 1); tree->SetBranchAddress("LepPhi", &LepPhi);
3467  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
3468  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
3469  // Do reweight signal to bkg.
3470  if (strrewgtbranch!=""){
3471  tree->SetBranchStatus(strrewgtbranch, 1); tree->SetBranchAddress(strrewgtbranch, &rewgt);
3472  cout << "Reweighting is read from branch " << strrewgtbranch << endl;
3473  }
3474  else{
3475  doRewgt=true;
3476  cout << "Reweighting needs to be computed." << endl;
3477  cerr << "NOT YET IMPLEMENTED!" << endl;
3478  assert(0);
3479  }
3480  if (!recalculate){
3481  tree->SetBranchStatus(strrecalcbranch, 1); tree->SetBranchAddress(strrecalcbranch, &mesq_calc);
3482  tree->SetBranchStatus(strrecalcconstbranch, 1); tree->SetBranchAddress(strrecalcconstbranch, &cconst_calc);
3483  cout << "Extracting ME from " << strrecalcbranch << " and const from " << strrecalcconstbranch << endl;
3484  }
3485  else cout << "Recalculating the ME" << endl;
3486  if (!recomputePmavjj && strpmavjjbranch!=""){
3487  tree->SetBranchStatus(strpmavjjbranch, 1);
3488  tree->SetBranchAddress(strpmavjjbranch, &pmavjj);
3489  cout << "Extracting P_V(mJJ) from " << strpmavjjbranch << endl;
3490  }
3491  else if (recomputePmavjj) cout << "Recomputing P_V(mJJ)" << endl;
3492  else cout << "No valid P_V(mJJ)" << endl;
3493  if (doRewgt){
3494  tree->SetBranchStatus("LHEMotherPz", 1); tree->SetBranchAddress("LHEMotherPz", &LHEMotherPz);
3495  tree->SetBranchStatus("LHEMotherE", 1); tree->SetBranchAddress("LHEMotherE", &LHEMotherE);
3496  tree->SetBranchStatus("LHEMotherId", 1); tree->SetBranchAddress("LHEMotherId", &LHEMotherId);
3497 
3498  tree->SetBranchStatus("LHEDaughterPt", 1); tree->SetBranchAddress("LHEDaughterPt", &LHEDaughterPt);
3499  tree->SetBranchStatus("LHEDaughterEta", 1); tree->SetBranchAddress("LHEDaughterEta", &LHEDaughterEta);
3500  tree->SetBranchStatus("LHEDaughterPhi", 1); tree->SetBranchAddress("LHEDaughterPhi", &LHEDaughterPhi);
3501  tree->SetBranchStatus("LHEDaughterMass", 1); tree->SetBranchAddress("LHEDaughterMass", &LHEDaughterMass);
3502  tree->SetBranchStatus("LHEDaughterId", 1); tree->SetBranchAddress("LHEDaughterId", &LHEDaughterId);
3503 
3504  tree->SetBranchStatus("LHEAssociatedParticlePt", 1); tree->SetBranchAddress("LHEAssociatedParticlePt", &LHEAssociatedParticlePt);
3505  tree->SetBranchStatus("LHEAssociatedParticleEta", 1); tree->SetBranchAddress("LHEAssociatedParticleEta", &LHEAssociatedParticleEta);
3506  tree->SetBranchStatus("LHEAssociatedParticlePhi", 1); tree->SetBranchAddress("LHEAssociatedParticlePhi", &LHEAssociatedParticlePhi);
3507  tree->SetBranchStatus("LHEAssociatedParticleMass", 1); tree->SetBranchAddress("LHEAssociatedParticleMass", &LHEAssociatedParticleMass);
3508  tree->SetBranchStatus("LHEAssociatedParticleId", 1); tree->SetBranchAddress("LHEAssociatedParticleId", &LHEAssociatedParticleId);
3509  }
3510  tree->GetEntry(0);
3511  cout << "Cross section = " << genxsec*genBR << endl;
3512 
3513  TH1F* htmp = (TH1F*)finput->Get(COUNTERS_NAME);
3514  pair<float, float> nsum(htmp->GetBinContent(1), htmp->GetBinContent(41)); // No PU reweighting
3515  nGenMap[tree]=nsum;
3516 
3517  nEntries += tree->GetEntries();
3518  treeList.push_back(tree);
3519  finputList.push_back(finput);
3520 
3521  float polemass = findPoleMass(strSamples[is]);
3522  cout << "Pole mass = " << polemass << endl;
3523  mass_map[tree]=polemass;
3524  if (mass_sample_map.find(polemass)==mass_sample_map.end()){
3525  cout << "Inserting new pole mass sample array" << endl;
3526  vector<TTree*> dumarr;
3527  mass_sample_map[polemass] = dumarr;
3528  }
3529  mass_sample_map[polemass].push_back(tree);
3530  }
3531  else finput->Close();
3532  }
3533  else if (finput->IsOpen()) finput->Close();
3534  }
3535  }
3536  cout << "NEntries = " << nEntries << endl;
3537 
3538  for (auto it = mass_sample_map.begin(); it != mass_sample_map.end(); ++it){
3539  float sum_ngen=0;
3540  float sum_xsec=0;
3541  for (unsigned int ix=0; ix<it->second.size(); ix++){
3542  it->second.at(ix)->GetEntry(0);
3543  sum_ngen += nGenMap[it->second.at(ix)].first;
3544  sum_xsec += genxsec*genBR;
3545  }
3546  float overallWeight = sum_ngen/sum_xsec;
3547  for (unsigned int ix=0; ix<it->second.size(); ix++){
3548  cout << "Sum Hep MC weights in tree " << ix << " / " << it->second.size() << " was " << nGenMap[it->second.at(ix)].second << " over " << nGenMap[it->second.at(ix)].first << " total gen. events." << endl;
3549  nGenMap[it->second.at(ix)].first = overallWeight/nGenMap[it->second.at(ix)].second;
3550  cout << "Event scale for tree " << ix << " / " << it->second.size() << " at pole mass " << it->first << " = " << nGenMap[it->second.at(ix)].first << endl;
3551  }
3552  }
3553 
3554  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
3555 
3556  vector<pair<float, int>> index[3];
3557  unsigned int ev_acc=0;
3558  for (int ev=0; ev<nEntries; ev++){
3559  getEntry(treeList, ev);
3560  bool doProcess=
3561  NJets30>=2 && (
3562  Z1Flav*Z2Flav==pow(13, 4)
3563  ||
3564  Z1Flav*Z2Flav==pow(11, 4)
3565  ||
3566  Z1Flav*Z2Flav==pow(11*13, 2)
3567  )
3568  ;
3569  if (!doProcess) continue;
3570  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
3571  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
3572  addByLowest(index[ic], mzz, ev);
3573  if (ic<2) addByLowest(index[1-ic], mzz, ev);
3574  ev_acc++;
3575  }
3576 
3577  for (unsigned int ic=0; ic<3; ic++){
3578  float firstVal=index[ic].at(0).first;
3579  float lastVal=index[ic].at(index[ic].size()-1).first;
3580  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
3581  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
3582  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
3583 
3584  float divisor=25000*(ic==2)+40000*(ic<2);
3585  if (prod==TVar::Had_ZH) divisor=15000;
3586  if (prod==TVar::Had_WH) divisor=15000;
3587  int nbins = index[ic].size()/divisor;
3588  int nbins_th=10/*50*/;
3589  if (prod==TVar::Had_WH) nbins_th=8;
3590  while (nbins<nbins_th){
3591  if (divisor>1000) divisor -= 1000;
3592  else if (divisor>100) divisor -= 100;
3593  else break;
3594  nbins=index[ic].size()/divisor;
3595  }
3596  cout << "nbins=" << nbins << endl;
3597  if (nbins<3) cerr << "Not enough bins!" << endl;
3598  vector<ExtBin> binList;
3599  float* binning = new float[nbins+1];
3600  binning[0]=infimum;
3601  binning[nbins]=supremum;
3602  int ev_stepsize = index[ic].size()/nbins;
3603  cout << "Event step size: " << ev_stepsize << endl;
3604  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
3605  for (int ix=1; ix<nbins; ix++){
3606  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
3607  ExtBin tmpbin;
3608  tmpbin.binlow = binning[ix-1];
3609  tmpbin.binhigh = binning[ix];
3610  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[ic][(ix-1)*ev_stepsize+bin].second);
3611  binList.push_back(tmpbin);
3612  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
3613  }
3614  ExtBin tmpbin;
3615  tmpbin.binlow = binning[nbins-1];
3616  tmpbin.binhigh = binning[nbins];
3617  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++) tmpbin.events.push_back(index[ic][bin].second);
3618  binList.push_back(tmpbin);
3619  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
3620  cout << "Bin list has the following bins:" << endl;
3621  for (unsigned int ib=0; ib<binList.size(); ib++){
3622  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
3623  }
3624 
3625  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
3626  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
3627 
3628  TTree* newtree=0;
3629  if (writeFinalTree){
3630  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
3631  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
3632  newtree->Branch("ZZMass", &mzz);
3633  newtree->Branch("weight", &wgt);
3634  }
3635 
3636  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
3637 
3638  double aL1=0, aR1=0, aL2=0, aR2=0;
3639  double mv=0, gav=0;
3640  unsigned int ev_acc=0;
3641  for (unsigned int bin=0; bin<binList.size(); bin++){
3642  cout << "Bin " << bin << " is now being scrutinized..." << endl;
3643  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
3644  int getEv = binList.at(bin).events.at(ev);
3645  getEntry(treeList, getEv);
3646  if (ev%1000==0) cout << "Doing event " << getEv << endl;
3647  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
3648  cerr << "Jet array sizes are less than Njets!" << endl;
3649  continue;
3650  }
3651  TTree* tree = findTree(treeList, getEv);
3652 
3653  // Divide out the signal H propagator in the reweighting denominator = Multiply with the H BW
3654  // Why? So that reweighting is not affected by the reweighting out of the BW
3655  {
3656  float& samplePoleMass = mass_map[tree];
3657  float samplePoleWidth = 0;
3658  if (samplePoleMass>0.){
3659  samplePoleWidth = mela.getHiggsWidthAtPoleMass(samplePoleMass);
3660  rewgt /= pow(pow(GenHMass, 2)-pow(samplePoleMass, 2), 2) + pow(samplePoleMass*samplePoleWidth, 2);
3661  }
3662  }
3663  wgt = fabs(genxsec*genBR*genHEPMCweight*nGenMap[tree].first*rewgt);
3664 
3665  TLorentzVector jet[2], higgs;
3666  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(JetPt->at(ij), JetEta->at(ij), JetPhi->at(ij), JetMass->at(ij));
3667  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
3668  TVector3 boostH = higgs.BoostVector();
3669 
3671  associated.push_back(SimpleParticle_t(0, jet[0]));
3672  associated.push_back(SimpleParticle_t(0, jet[1]));
3673 
3675  for (int id=0; id<4; id++){
3676  double mass=0;
3677  if (abs(LepLepId->at(id))==13) mass = 0.105658;
3678  else if (abs(LepLepId->at(id))==11) mass = 0.000511;
3679  TLorentzVector pDaughter;
3680  pDaughter.SetPtEtaPhiM(LepPt->at(id), LepEta->at(id), LepPhi->at(id), mass);
3681  daughters.push_back(SimpleParticle_t(LepLepId->at(id), pDaughter));
3682  }
3683  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
3684 
3685  mela.setProcess(proc, me, prod);
3686 
3687  double propagatorV=1;
3688  if (recalculate || ev_acc==0){
3690  mela.computeProdP(mesq_conserveDifermMass, false);
3691  if (!recalculate){
3692  mesq_calc /= cconst_calc;
3693  mesq_conserveDifermMass=mesq_calc;
3694  }
3695  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
3696  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
3697  if (prod==TVar::Had_ZH){
3698  mv = mela.getPrimaryMass(23);
3699  gav = mela.getPrimaryWidth(23);
3700  }
3701  else if (prod==TVar::Had_WH){
3702  mv = mela.getPrimaryMass(24);
3703  gav = mela.getPrimaryWidth(24);
3704  }
3705  }
3706  else{
3707  mesq_calc /= cconst_calc;
3708  mesq_conserveDifermMass=mesq_calc;
3709  }
3710  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
3711  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
3712  if (prod==TVar::Had_ZH || prod==TVar::Had_WH){
3713  double mjj = (jet[0] + jet[1]).M();
3714  propagatorV = 1./(pow(pow(mjj, 2)-pow(mv, 2), 2) + pow(mv*gav, 2));
3715  mesq_conserveDifermMass /= propagatorV;
3716  if (recomputePmavjj){
3718  mela.computeDijetConvBW(pmavjj, false);
3719  }
3720  mesq_conserveDifermMass *= pmavjj;
3721  }
3722 
3723  double mz, gaz, propagator;
3724  mz = mela.getPrimaryMass(23);
3725  gaz = mela.getPrimaryWidth(23);
3726  if (fabs(mzz-mz)<=4.*gaz){
3727  double sh = pow(mzz, 2);
3728  double shdn = pow(mz-4.*gaz, 2);
3729  double shup = pow(mz+4.*gaz, 2);
3730  double prop_sh = 1./(pow(sh-pow(mz, 2), 2) + pow(mz*gaz, 2));
3731  double prop_shdn = 1./(pow(shdn-pow(mz, 2), 2) + pow(mz*gaz, 2));
3732  double prop_shup = 1./(pow(shup-pow(mz, 2), 2) + pow(mz*gaz, 2));
3733  double fsh = (sh-shdn)/(shup-shdn);
3734  propagator = prop_sh / (prop_shdn*(1.-fsh) + prop_shup*fsh);
3735  }
3736  else propagator=1.;
3737  mesq_conserveDifermMass /= propagator;
3738 
3739  mela.resetInputEvent();
3740  if (doRewgt){
3741  // Reweighting of NLO is too complex to implement here
3742  exit(1);
3743  }
3744 
3745  bool doFill = !(
3746  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
3747  );
3748 
3749  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0, wgt);
3750 
3751  mela.resetInputEvent();
3752  ev_acc++;
3753  }
3754 
3755  binList.at(bin).sift(); binList.at(bin).adjustWeights(10);
3756 
3757  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
3758  mzz = binList.at(bin).masses.at(ev);
3759  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
3760  wgt = binList.at(bin).weights.at(ev);
3761  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
3762  hvar->Fill(mzz, mzz);
3763  if (writeFinalTree) newtree->Fill();
3764  }
3765  }
3766  cout << "Total events accumulated: " << ev_acc << endl;
3767 
3768  double* xexyey[4];
3769  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
3770  for (int bin=0; bin<nbins; bin++){
3771  xexyey[0][bin] = hvar->GetBinContent(bin+1);
3772  xexyey[1][bin] = hvar->GetBinError(bin+1);
3773 
3774  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
3775 
3776  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
3777  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
3778  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
3779  xexyey[2][bin] = log10(xexyey[2][bin]);
3780  }
3781 
3782  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
3783  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
3784  foutput->WriteTObject(tg);
3785  delete tg;
3786 
3787  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
3788 
3789  foutput->WriteTObject(hmesq_conserveDifermMass);
3790  foutput->WriteTObject(hvar);
3791  if (writeFinalTree) foutput->WriteTObject(newtree);
3792  if (writeFinalTree) delete newtree;
3793  delete hmesq_conserveDifermMass;
3794  delete hvar;
3795  delete[] binning;
3796  }
3797  foutput->Close();
3798  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
3799 }
3800 
3801 /*
3802 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
3803 - ALPHAS(MZ)**2 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION
3804 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
3805 - PROP(Z)/LINE_PROP(Z) AROUND M4L~=MZ
3806 */
3807 void get_PAvgProfile_MCFM_JJQCD_bkgZZ_13TeV(int sqrts=13, bool recalculate = true){
3808  int erg_tev=sqrts;
3809  float mPOLE=125.;
3810  TString TREE_NAME = "ZZTree/candTree";
3811  TString COUNTERS_NAME = "ZZTree/Counters";
3812  bool writeFinalTree=true;
3813 
3814  TVar::Process proc = TVar::bkgZZ;
3817 
3818  TString strproc = ProcessName(proc);
3819  TString strme = MatrixElementName(me);
3820  TString strprod = ProductionName(prod);
3821 
3822  TVar::VerbosityLevel verbosity = TVar::ERROR;
3823  Mela mela(erg_tev, mPOLE, verbosity);
3824 
3825  std::vector<short>* LepLepId=0;
3826  std::vector<float>* LepPt=0;
3827  std::vector<float>* LepEta=0;
3828  std::vector<float>* LepPhi=0;
3829 
3830  short NJets30;
3831  std::vector<float>* JetPt=0;
3832  std::vector<float>* JetEta=0;
3833  std::vector<float>* JetPhi=0;
3834  std::vector<float>* JetMass=0;
3835  std::vector<float> myJetPt;
3836  std::vector<float> myJetEta;
3837  std::vector<float> myJetPhi;
3838  std::vector<float> myJetMass;
3839 
3840  float mesq_calc=0., cconst_calc=1.;
3841  float mesq_conserveDifermMass=0;
3842  float mzz = 126.;
3843  float m1 = 91.471450;
3844  float m2 = 12.139782;
3845  float h1 = 0.2682896;
3846  float h2 = 0.1679779;
3847  float phi = 1.5969792;
3848  float hs = -0.727181;
3849  float phi1 = 1.8828257;
3850  float ZZPt, ZZPhi, ZZEta;
3851  short Z1Flav, Z2Flav;
3852  float genxsec=0, genBR=0, ngen=0, genHEPMCweight=0;
3853  float wgt=1;
3854 
3855  TString cinput_main;
3856  if (sqrts==13) cinput_main = inputdir_13TeV;
3857  else return;
3858 
3859  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
3860 
3861  vector<TString> strSamples = constructSamplesList("qq_Bkg", sqrts);
3862 
3863  unordered_map<TTree*, pair<float, float>> nGenMap;
3864  unordered_map<float, vector<TTree*>> mass_sample_map;
3865 
3866  vector<TFile*> finputList;
3867  vector<TTree*> treeList;
3868  int nEntries=0;
3869  for (unsigned int is=0; is<strSamples.size(); is++){
3870  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples[is]).Data());
3871  TFile* finput = TFile::Open(cinput, "read");
3872  cout << "Opening file " << cinput << "..." << endl;
3873  TTree* tree=0;
3874  if (finput!=0){
3875  if (finput->IsOpen() && !finput->IsZombie()){
3876  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
3877  tree = (TTree*)finput->Get(TREE_NAME);
3878  if (tree!=0){
3879  cout << TREE_NAME << " is found." << endl;
3880  if (!recalculate && tree->GetBranchStatus("pConst_JJQCD_BKG_MCFM_JECNominal")==0) recalculate=true;
3881  tree->SetBranchStatus("*", 0);
3882  tree->SetBranchStatus("genxsec", 1); tree->SetBranchAddress("genxsec", &genxsec); tree->SetBranchStatus("genBR", 1); tree->SetBranchAddress("genBR", &genBR);
3883  tree->SetBranchStatus("genHEPMCweight", 1); tree->SetBranchAddress("genHEPMCweight", &genHEPMCweight);
3884  tree->SetBranchStatus("nCleanedJetsPt30", 1); tree->SetBranchAddress("nCleanedJetsPt30", &NJets30);
3885  tree->SetBranchStatus("JetPt", 1); tree->SetBranchAddress("JetPt", &JetPt);
3886  tree->SetBranchStatus("JetEta", 1); tree->SetBranchAddress("JetEta", &JetEta);
3887  tree->SetBranchStatus("JetPhi", 1); tree->SetBranchAddress("JetPhi", &JetPhi);
3888  tree->SetBranchStatus("JetMass", 1); tree->SetBranchAddress("JetMass", &JetMass);
3889  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
3890  tree->SetBranchStatus("ZZPt", 1); tree->SetBranchAddress("ZZPt", &ZZPt);
3891  tree->SetBranchStatus("ZZEta", 1); tree->SetBranchAddress("ZZEta", &ZZEta);
3892  tree->SetBranchStatus("ZZPhi", 1); tree->SetBranchAddress("ZZPhi", &ZZPhi);
3893  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
3894  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
3895  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
3896  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
3897  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
3898  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
3899  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
3900  tree->SetBranchStatus("LepLepId", 1); tree->SetBranchAddress("LepLepId", &LepLepId);
3901  tree->SetBranchStatus("LepPt", 1); tree->SetBranchAddress("LepPt", &LepPt);
3902  tree->SetBranchStatus("LepEta", 1); tree->SetBranchAddress("LepEta", &LepEta);
3903  tree->SetBranchStatus("LepPhi", 1); tree->SetBranchAddress("LepPhi", &LepPhi);
3904  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
3905  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
3906  if (!recalculate){
3907  tree->SetBranchStatus("p_JJQCD_BKG_MCFM_JECNominal", 1);
3908  tree->SetBranchAddress("p_JJQCD_BKG_MCFM_JECNominal", &mesq_calc);
3909  tree->SetBranchStatus("pConst_JJQCD_BKG_MCFM_JECNominal", 1);
3910  tree->SetBranchAddress("pConst_JJQCD_BKG_MCFM_JECNominal", &cconst_calc);
3911  }
3912  else cout << "ME needs to be recalculated!" << endl;
3913  tree->GetEntry(0);
3914  cout << "Cross section = " << genxsec*genBR << endl;
3915 
3916  TH1F* htmp = (TH1F*)finput->Get(COUNTERS_NAME);
3917  pair<float, float> nsum(htmp->GetBinContent(1), htmp->GetBinContent(41)); // No PU reweighting
3918  nGenMap[tree]=nsum;
3919 
3920  nEntries += tree->GetEntries();
3921  treeList.push_back(tree);
3922  finputList.push_back(finput);
3923 
3924  float polemass = findPoleMass(strSamples[is]);
3925  cout << "Pole mass = " << polemass << endl;
3926  if (mass_sample_map.find(polemass)==mass_sample_map.end()){
3927  cout << "Inserting new pole mass sample array" << endl;
3928  vector<TTree*> dumarr;
3929  mass_sample_map[polemass] = dumarr;
3930  }
3931  mass_sample_map[polemass].push_back(tree);
3932  }
3933  else if (finput->IsOpen()) finput->Close();
3934  }
3935  }
3936  }
3937  cout << "NEntries = " << nEntries << endl;
3938 
3939  for (auto it = mass_sample_map.begin(); it != mass_sample_map.end(); ++it){
3940  float sum_ngen=0;
3941  float sum_xsec=0;
3942  for (unsigned int ix=0; ix<it->second.size(); ix++){
3943  it->second.at(ix)->GetEntry(0);
3944  sum_ngen += nGenMap[it->second.at(ix)].first;
3945  sum_xsec += genxsec*genBR;
3946  }
3947  float overallWeight = sum_ngen/sum_xsec;
3948  for (unsigned int ix=0; ix<it->second.size(); ix++){
3949  cout << "Sum Hep MC weights in tree " << ix << " / " << it->second.size() << " was " << nGenMap[it->second.at(ix)].second << " over " << nGenMap[it->second.at(ix)].first << " total gen. events." << endl;
3950  nGenMap[it->second.at(ix)].first = overallWeight/nGenMap[it->second.at(ix)].second;
3951  cout << "Event scale for tree " << ix << " / " << it->second.size() << " at pole mass " << it->first << " = " << nGenMap[it->second.at(ix)].first << endl;
3952  }
3953  }
3954 
3955  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
3956 
3957  vector<pair<float, int>> index[3];
3958  unsigned int ev_acc=0;
3959  for (int ev=0; ev<nEntries; ev++){
3960  getEntry(treeList, ev);
3961  bool doProcess=
3962  NJets30>=2 && (
3963  Z1Flav*Z2Flav==pow(13, 4)
3964  ||
3965  Z1Flav*Z2Flav==pow(11, 4)
3966  ||
3967  Z1Flav*Z2Flav==pow(11*13, 2)
3968  )
3969  ;
3970  if (!doProcess) continue;
3971  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
3972  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
3973  addByLowest(index[ic], mzz, ev);
3974  if (ic<2) addByLowest(index[1-ic], mzz, ev);
3975  ev_acc++;
3976  }
3977 
3978  for (unsigned int ic=0; ic<3; ic++){
3979  float firstVal=index[ic].at(0).first;
3980  float lastVal=index[ic].at(index[ic].size()-1).first;
3981  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
3982  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
3983  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
3984 
3985  float divisor=10000;
3986  int nbins = index[ic].size()/divisor;
3987  const int nbins_th=8/*50*/;
3988  while (nbins<nbins_th){
3989  if (divisor>1000) divisor -= 1000;
3990  else if (divisor>100) divisor -= 100;
3991  else break;
3992  nbins=index[ic].size()/divisor;
3993  }
3994  cout << "nbins=" << nbins << endl;
3995  if (nbins<3) cerr << "Not enough bins!" << endl;
3996  vector<ExtBin> binList;
3997  float* binning = new float[nbins+1];
3998  binning[0]=infimum;
3999  binning[nbins]=supremum;
4000  int ev_stepsize = index[ic].size()/nbins;
4001  cout << "Event step size: " << ev_stepsize << endl;
4002  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
4003  for (int ix=1; ix<nbins; ix++){
4004  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
4005  ExtBin tmpbin;
4006  tmpbin.binlow = binning[ix-1];
4007  tmpbin.binhigh = binning[ix];
4008  for (int bin=0; bin<ev_stepsize; bin++) tmpbin.events.push_back(index[ic][(ix-1)*ev_stepsize+bin].second);
4009  binList.push_back(tmpbin);
4010  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
4011  }
4012  ExtBin tmpbin;
4013  tmpbin.binlow = binning[nbins-1];
4014  tmpbin.binhigh = binning[nbins];
4015  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++) tmpbin.events.push_back(index[ic][bin].second);
4016  binList.push_back(tmpbin);
4017  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
4018  cout << "Bin list has the following bins:" << endl;
4019  for (unsigned int ib=0; ib<binList.size(); ib++){
4020  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
4021  }
4022 
4023  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
4024  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
4025 
4026  TTree* newtree=0;
4027  if (writeFinalTree){
4028  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
4029  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
4030  newtree->Branch("ZZMass", &mzz);
4031  newtree->Branch("weight", &wgt);
4032  }
4033 
4034  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
4035 
4036  double aL1=0, aR1=0, aL2=0, aR2=0;
4037  double alphasVal=0;
4038  unsigned int ev_acc=0;
4039  for (unsigned int bin=0; bin<binList.size(); bin++){
4040  cout << "Bin " << bin << " is now being scrutinized..." << endl;
4041  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
4042  int getEv = binList.at(bin).events.at(ev);
4043  getEntry(treeList, getEv);
4044  if (ev%1000==0) cout << "Doing event " << getEv << endl;
4045  if (JetPt->size()<2 || JetEta->size()<2 || JetPhi->size()<2 || JetMass->size()<2){
4046  cerr << "Jet array sizes are less than Njets!" << endl;
4047  continue;
4048  }
4049  TTree* tree = findTree(treeList, getEv);
4050  wgt = fabs(genxsec*genBR*genHEPMCweight*nGenMap[tree].first);
4051 
4052  TLorentzVector jet[2], higgs;
4053  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(JetPt->at(ij), JetEta->at(ij), JetPhi->at(ij), JetMass->at(ij));
4054  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
4055  TVector3 boostH = higgs.BoostVector();
4056 
4058  associated.push_back(SimpleParticle_t(0, jet[0]));
4059  associated.push_back(SimpleParticle_t(0, jet[1]));
4060 
4062  for (int id=0; id<4; id++){
4063  double mass=0;
4064  if (abs(LepLepId->at(id))==13) mass = 0.105658;
4065  else if (abs(LepLepId->at(id))==11) mass = 0.000511;
4066  TLorentzVector pDaughter;
4067  pDaughter.SetPtEtaPhiM(LepPt->at(id), LepEta->at(id), LepPhi->at(id), mass);
4068  daughters.push_back(SimpleParticle_t(LepLepId->at(id), pDaughter));
4069  }
4070  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
4071 
4072  mela.setProcess(proc, me, prod);
4073 
4074  if (recalculate || ev_acc==0){
4076  mela.computeProdP(mesq_conserveDifermMass, false);
4077  if (!recalculate){
4078  mesq_calc /= cconst_calc;
4079  mesq_conserveDifermMass=mesq_calc;
4080  }
4081  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
4082  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
4083  alphasVal = mela.getIORecord()->getAlphaSatMZ();
4084  if (ev_acc==0 && !recalculate){
4085  cout << "\taL1, aR1, aL2, aR2 = " << aL1 << " , " << aR1 << " , " << aL2 << " , " << aR2 << endl;
4086  cout << "\talphaS(mZ) = " << alphasVal << endl;
4087  }
4088  }
4089  else{
4090  mesq_calc /= cconst_calc;
4091  mesq_conserveDifermMass=mesq_calc;
4092  }
4093  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
4094  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
4095  mesq_conserveDifermMass /= pow(alphasVal, 2);
4096 
4097  double mz, gaz, propagator;
4098  mz = mela.getPrimaryMass(23);
4099  gaz = mela.getPrimaryWidth(23);
4100  if (fabs(mzz-mz)<=4.*gaz){
4101  double sh = pow(mzz, 2);
4102  double shdn = pow(mz-4.*gaz, 2);
4103  double shup = pow(mz+4.*gaz, 2);
4104  double prop_sh = 1./(pow(sh-pow(mz, 2), 2) + pow(mz*gaz, 2));
4105  double prop_shdn = 1./(pow(shdn-pow(mz, 2), 2) + pow(mz*gaz, 2));
4106  double prop_shup = 1./(pow(shup-pow(mz, 2), 2) + pow(mz*gaz, 2));
4107  double fsh = (sh-shdn)/(shup-shdn);
4108  propagator = prop_sh / (prop_shdn*(1.-fsh) + prop_shup*fsh);
4109  }
4110  else propagator=1.;
4111  mesq_conserveDifermMass /= propagator;
4112 
4113  bool doFill = !(
4114  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
4115  );
4116 
4117  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0, wgt);
4118 
4119  mela.resetInputEvent();
4120  ev_acc++;
4121  }
4122 
4123  binList.at(bin).sift(); binList.at(bin).adjustWeights(-1);
4124 
4125  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
4126  mzz = binList.at(bin).masses.at(ev);
4127  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
4128  wgt = binList.at(bin).weights.at(ev);
4129  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
4130  hvar->Fill(mzz, mzz);
4131  if (writeFinalTree) newtree->Fill();
4132  }
4133  }
4134 
4135  double* xexyey[4];
4136  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
4137  for (int bin=0; bin<nbins; bin++){
4138  xexyey[0][bin] = hvar->GetBinContent(bin+1);
4139  xexyey[1][bin] = hvar->GetBinError(bin+1);
4140 
4141  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
4142 
4143  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
4144  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
4145  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
4146  xexyey[2][bin] = log10(xexyey[2][bin]);
4147  }
4148 
4149  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
4150  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
4151  foutput->WriteTObject(tg);
4152  delete tg;
4153 
4154  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
4155 
4156  foutput->WriteTObject(hmesq_conserveDifermMass);
4157  foutput->WriteTObject(hvar);
4158  if (writeFinalTree) foutput->WriteTObject(newtree);
4159  if (writeFinalTree) delete newtree;
4160  delete hmesq_conserveDifermMass;
4161  delete hvar;
4162  delete[] binning;
4163  }
4164  foutput->Close();
4165  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
4166 }
4167 
4168 
4169 /* SPECIFIC COMMENT: OUTPUT ME DIVIDED BY (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL */
4171  int erg_tev=8;
4172  float mPOLE=125.;
4173  TString TREE_NAME;
4174  const bool writeFinalTree=false;
4175  const TString strMEBranchname = "p_ZZINDEPENDENT_SIG_ghz1_1_JHUGen";
4176  const TString strConstBranchname = "pConst_ZZINDEPENDENT_SIG_ghz1_1_JHUGen";
4177 
4178  TVar::VerbosityLevel verbosity = TVar::ERROR;
4179  Mela mela(erg_tev, mPOLE, verbosity);
4180 
4184 
4185  TString strproc = ProcessName(proc);
4186  TString strme = MatrixElementName(me);
4187  TString strprod = ProductionName(prod);
4188 
4189  std::vector<short>* LepLepId=0;
4190  std::vector<float>* LepPt=0;
4191  std::vector<float>* LepEta=0;
4192  std::vector<float>* LepPhi=0;
4193 
4194  float mesq_calc=0., cconst_calc=1.;
4195  float mesq_conserveDifermMass=0;
4196  float mzz = 126.;
4197  float m1 = 91.471450;
4198  float m2 = 12.139782;
4199  float h1 = 0.2682896;
4200  float h2 = 0.1679779;
4201  float phi = 1.5969792;
4202  float hs = -0.727181;
4203  float phi1 = 1.8828257;
4204  float ZZPt, ZZPhi, ZZEta;
4205  short Z1Flav, Z2Flav;
4206  int LepID[4];
4207 
4208  vector<pair<float, int>> index[3];
4209  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
4210  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s.root", strme.Data(), strprod.Data(), strproc.Data()), "recreate");
4211 
4212  vector<TString> dumappend;
4213  vector<TString> strSamples_13TeV = constructSamplesList("JJQCD", 13.);
4214  dumappend = constructSamplesList("JJVBF", 13.);
4215  appendVector<TString>(strSamples_13TeV, dumappend);
4216  dumappend = constructSamplesList("gg_Sig_JHUGen", 13.);
4217  appendVector<TString>(strSamples_13TeV, dumappend);
4218  dumappend = constructSamplesList("gg_Sig_MCFM", 13.);
4219  appendVector<TString>(strSamples_13TeV, dumappend);
4220  dumappend = constructSamplesList("gg_Sig_ggVV", 13.);
4221  appendVector<TString>(strSamples_13TeV, dumappend);
4222 
4223  vector<TString> strSamples_8TeV = constructSamplesList("JJQCD", 8.);
4224  dumappend = constructSamplesList("JJVBF", 8.);
4225  appendVector<TString>(strSamples_8TeV, dumappend);
4226  dumappend = constructSamplesList("gg_Sig_JHUGen", 8.);
4227  appendVector<TString>(strSamples_8TeV, dumappend);
4228  dumappend = constructSamplesList("gg_Sig_MCFM", 8.);
4229  appendVector<TString>(strSamples_8TeV, dumappend);
4230  dumappend = constructSamplesList("gg_Sig_ggVV", 8.);
4231  appendVector<TString>(strSamples_8TeV, dumappend);
4232 
4233  vector<TString> strSamples_7TeV = constructSamplesList("JJQCD", 7.);
4234  dumappend = constructSamplesList("JJVBF", 7.);
4235  appendVector<TString>(strSamples_7TeV, dumappend);
4236  dumappend = constructSamplesList("gg_Sig_JHUGen", 7.);
4237  appendVector<TString>(strSamples_7TeV, dumappend);
4238  dumappend = constructSamplesList("gg_Sig_MCFM", 7.);
4239  appendVector<TString>(strSamples_7TeV, dumappend);
4240  dumappend = constructSamplesList("gg_Sig_ggVV", 7.);
4241  appendVector<TString>(strSamples_7TeV, dumappend);
4242 
4243  vector<TFile*> finputList;
4244  vector<TTree*> treeList;
4245  int nEntries=0;
4246  TString cinput_main;
4247 
4248  TREE_NAME = "ZZTree/candTree";
4249  cinput_main = inputdir_13TeV;
4250  //for (int is=0; is<2; is++){
4251  for (int is=0; is<(int)strSamples_13TeV.size(); is++){
4252  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples_13TeV[is]).Data());
4253  TFile* finput = TFile::Open(cinput, "read");
4254  cout << "Opening file " << cinput << "..." << endl;
4255  TTree* tree=0;
4256  if (finput!=0){
4257  if (finput->IsOpen() && !finput->IsZombie()){
4258  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
4259  tree = (TTree*)finput->Get(TREE_NAME);
4260  if (tree!=0){
4261  cout << TREE_NAME << " is found." << endl;
4262 
4263  bool doRecalculate = recalculate;
4264  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0) doRecalculate = true;
4265  tree->SetBranchStatus("*", 0);
4266  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
4267  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
4268  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
4269  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
4270  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
4271  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
4272  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
4273  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
4274  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
4275  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
4276  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0){
4277  tree->SetBranchStatus(strMEBranchname, 1); tree->SetBranchAddress(strMEBranchname, &mesq_calc);
4278  tree->SetBranchStatus(strConstBranchname, 1); tree->SetBranchAddress(strConstBranchname, &cconst_calc);
4279  }
4280  nEntries += tree->GetEntries();
4281  treeList.push_back(tree);
4282  finputList.push_back(finput);
4283  }
4284  else finput->Close();
4285  }
4286  else if (finput->IsOpen()) finput->Close();
4287  }
4288  }
4289 
4290  for (int ic=0; ic<3; ic++){
4291  TREE_NAME = "SelectedTree";
4292  cinput_main = inputdir_8TeV;
4293  //for (int is=0; is<2; is++){
4294  for (int is=0; is<(int)strSamples_8TeV.size(); is++){
4295  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_8TeV[is]).Data());
4296  TFile* finput = TFile::Open(cinput, "read");
4297  cout << "Opening file " << cinput << "..." << endl;
4298  TTree* tree=0;
4299  if (finput!=0){
4300  if (finput->IsOpen() && !finput->IsZombie()){
4301  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
4302  tree = (TTree*)finput->Get(TREE_NAME);
4303  if (tree!=0){
4304  cout << TREE_NAME << " is found." << endl;
4305  tree->SetBranchStatus("*", 0);
4306  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
4307  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
4308  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
4309  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
4310  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
4311  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
4312  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
4313  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
4314  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
4315  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
4316  nEntries += tree->GetEntries();
4317  treeList.push_back(tree);
4318  finputList.push_back(finput);
4319  }
4320  else finput->Close();
4321  }
4322  else if (finput->IsOpen()) finput->Close();
4323  }
4324  }
4325  cinput_main = inputdir_7TeV;
4326  //for (int is=0; is<2; is++){
4327  for (int is=0; is<(int)strSamples_7TeV.size(); is++){
4328  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_7TeV[is]).Data());
4329  TFile* finput = TFile::Open(cinput, "read");
4330  cout << "Opening file " << cinput << "..." << endl;
4331  TTree* tree=0;
4332  if (finput!=0){
4333  if (finput->IsOpen() && !finput->IsZombie()){
4334  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
4335  tree = (TTree*)finput->Get(TREE_NAME);
4336  if (tree!=0){
4337  cout << TREE_NAME << " is found." << endl;
4338  tree->SetBranchStatus("*", 0);
4339  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
4340  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
4341  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
4342  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
4343  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
4344  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
4345  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
4346  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
4347  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
4348  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
4349  nEntries += tree->GetEntries();
4350  treeList.push_back(tree);
4351  finputList.push_back(finput);
4352  }
4353  else finput->Close();
4354  }
4355  else if (finput->IsOpen()) finput->Close();
4356  }
4357  }
4358  }
4359 
4360  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
4361 
4362  unsigned int ev_acc=0;
4363  for (int ev=0; ev<nEntries; ev++){
4364  getEntry(treeList, ev);
4365  bool doProcess=
4366  (
4367  Z1Flav*Z2Flav==pow(13, 4)
4368  ||
4369  Z1Flav*Z2Flav==pow(11, 4)
4370  ||
4371  Z1Flav*Z2Flav==pow(11*13, 2)
4372  )
4373  ;
4374  if (!doProcess) continue;
4375  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
4376  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
4377  addByLowest(index[ic], mzz, ev);
4378  ev_acc++;
4379  }
4380  cout << "Number of valid entries: " << ev_acc << endl;
4381 
4382  for (unsigned int ic=0; ic<3; ic++){
4383  float firstVal=index[ic].at(0).first;
4384  float lastVal=index[ic].at(index[ic].size()-1).first;
4385  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
4386  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
4387  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
4388 
4389  float divisor=95000;
4390  int nbins = index[ic].size()/divisor;
4391  const int nbins_th=10/*50*/;
4392  while (nbins<nbins_th){
4393  if (divisor>1000) divisor -= 1000;
4394  else if (divisor>100) divisor -= 100;
4395  else break;
4396  nbins=index[ic].size()/divisor;
4397  }
4398  cout << "nbins=" << nbins << endl;
4399  if (nbins<3) cerr << "Not enough bins!" << endl;
4400  vector<ExtBin> binList;
4401  float* binning = new float[nbins+1];
4402  binning[0]=infimum;
4403  binning[nbins]=supremum;
4404  int ev_stepsize = index[ic].size()/nbins;
4405  cout << "Event step size: " << ev_stepsize << endl;
4406  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
4407  for (int ix=1; ix<nbins; ix++){
4408  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
4409  ExtBin tmpbin;
4410  tmpbin.binlow = binning[ix-1]; tmpbin.binhigh = binning[ix];
4411  for (int bin=0; bin<ev_stepsize; bin++){
4412  int evid = index[ic][(ix-1)*ev_stepsize+bin].second;
4413  tmpbin.events.push_back(evid);
4414  }
4415  binList.push_back(tmpbin);
4416  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
4417  }
4418  ExtBin tmpbin;
4419  tmpbin.binlow = binning[nbins-1]; tmpbin.binhigh = binning[nbins];
4420  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++){
4421  int evid = index[ic][bin].second;
4422  tmpbin.events.push_back(evid);
4423  }
4424  binList.push_back(tmpbin);
4425  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
4426  cout << "Bin list has the following bins:" << endl;
4427  for (unsigned int ib=0; ib<binList.size(); ib++){
4428  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
4429  }
4430 
4431  foutput->cd();
4432 
4433  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
4434  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
4435 
4436  TTree* newtree=0;
4437  if (writeFinalTree){
4438  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
4439  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
4440  newtree->Branch("ZZMass", &mzz);
4441  }
4442 
4443  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
4444 
4445  if (ic==1){
4446  LepID[0]=11;
4447  LepID[1]=-11;
4448  }
4449  else{
4450  LepID[0]=13;
4451  LepID[1]=-13;
4452  }
4453  if (ic==0){
4454  LepID[2]=13;
4455  LepID[3]=-13;
4456  }
4457  else{
4458  LepID[2]=11;
4459  LepID[3]=-11;
4460  }
4461 
4462  double aL1=0, aR1=0, aL2=0, aR2=0;
4463  unsigned int ctr=0;
4464  for (unsigned int bin=0; bin<binList.size(); bin++){
4465  cout << "Bin " << bin << " is now being scrutinized..." << endl;
4466  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
4467  int getEv = binList.at(bin).events.at(ev);
4468  getEntry(treeList, getEv);
4469  TTree* tree = findTree(treeList, getEv);
4470 
4471  if (ev%1000==0) cout << "Doing event " << getEv << endl;
4472 
4473  TLorentzVector pDaughters[4];
4474  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
4475  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); }
4477  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
4478  mela.setInputEvent(&daughters, (SimpleParticleCollection_t*)0, (SimpleParticleCollection_t*)0, false);
4479 
4480  mela.setProcess(proc, me, prod);
4482 
4483  bool hasConst = (tree->GetBranchStatus(strConstBranchname)==1);
4484  bool doCalc=(!hasConst || ctr==0);
4485  if (doCalc){
4486  mela.computeP(mesq_conserveDifermMass, false);
4487  if (hasConst) mesq_conserveDifermMass = mesq_calc / cconst_calc;
4488 
4489  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
4490  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
4491  }
4492  else mesq_conserveDifermMass = mesq_calc / cconst_calc;
4493 
4494  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
4495  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
4496 
4497  bool doFill = !(
4498  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
4499  );
4500 
4501  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0);
4502 
4503  mela.resetInputEvent();
4504  ctr++;
4505  }
4506 
4507  binList.at(bin).sift(); binList.at(bin).adjustWeights();
4508 
4509  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
4510  mzz = binList.at(bin).masses.at(ev);
4511  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
4512  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
4513  hvar->Fill(mzz, mzz);
4514  if (writeFinalTree) newtree->Fill();
4515  }
4516  }
4517 
4518  double* xexyey[4];
4519  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
4520  for (int bin=0; bin<nbins; bin++){
4521  xexyey[0][bin] = hvar->GetBinContent(bin+1);
4522  xexyey[1][bin] = hvar->GetBinError(bin+1);
4523 
4524  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
4525  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
4526  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
4527  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
4528  xexyey[2][bin] = log10(xexyey[2][bin]);
4529  }
4530 
4531  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
4532  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
4533  foutput->WriteTObject(tg);
4534  delete tg;
4535 
4536  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
4537  foutput->WriteTObject(hmesq_conserveDifermMass);
4538  foutput->WriteTObject(hvar);
4539  if (writeFinalTree) foutput->WriteTObject(newtree);
4540  if (writeFinalTree) delete newtree;
4541  delete hmesq_conserveDifermMass;
4542  delete hvar;
4543  delete[] binning;
4544  }
4545  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
4546  foutput->Close();
4547 }
4548 
4549 /*
4550 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
4551 - ALPHAS(MZ)**2 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION
4552 - H(1) PROPAGATOR
4553 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
4554 */
4555 void get_PAvgProfile_JHUGen_ZZGG_HSMHiggs(bool recalculate=false){
4556  int erg_tev=8;
4557  float mPOLE=125.;
4558  TString TREE_NAME;
4559  const bool writeFinalTree=false;
4560  const TString strMEBranchname = "p_GG_SIG_ghg2_1_ghz1_1_JHUGen";
4561  const TString strConstBranchname = "pConst_GG_SIG_ghg2_1_ghz1_1_JHUGen";
4562 
4563  TVar::VerbosityLevel verbosity = TVar::ERROR;
4564  Mela mela(erg_tev, mPOLE, verbosity);
4565 
4569 
4570  TString strproc = ProcessName(proc);
4571  TString strme = MatrixElementName(me);
4572  TString strprod = ProductionName(prod);
4573 
4574  std::vector<short>* LepLepId=0;
4575  std::vector<float>* LepPt=0;
4576  std::vector<float>* LepEta=0;
4577  std::vector<float>* LepPhi=0;
4578 
4579  float mesq_calc=0., cconst_calc=1.;
4580  float mesq_conserveDifermMass=0;
4581  float mzz = 126.;
4582  float m1 = 91.471450;
4583  float m2 = 12.139782;
4584  float h1 = 0.2682896;
4585  float h2 = 0.1679779;
4586  float phi = 1.5969792;
4587  float hs = -0.727181;
4588  float phi1 = 1.8828257;
4589  float ZZPt, ZZPhi, ZZEta;
4590  short Z1Flav, Z2Flav;
4591  int LepID[4];
4592 
4593  vector<pair<float, int>> index[3];
4594  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
4595  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s.root", strme.Data(), strprod.Data(), strproc.Data()), "recreate");
4596 
4597  vector<TString> dumappend;
4598  vector<TString> strSamples_13TeV = constructSamplesList("JJQCD", 13.);
4599  dumappend = constructSamplesList("JJVBF", 13.);
4600  appendVector<TString>(strSamples_13TeV, dumappend);
4601  dumappend = constructSamplesList("gg_Sig_JHUGen", 13.);
4602  appendVector<TString>(strSamples_13TeV, dumappend);
4603  dumappend = constructSamplesList("gg_Sig_MCFM", 13.);
4604  appendVector<TString>(strSamples_13TeV, dumappend);
4605  dumappend = constructSamplesList("gg_Sig_ggVV", 13.);
4606  appendVector<TString>(strSamples_13TeV, dumappend);
4607 
4608  vector<TString> strSamples_8TeV = constructSamplesList("JJQCD", 8.);
4609  dumappend = constructSamplesList("JJVBF", 8.);
4610  appendVector<TString>(strSamples_8TeV, dumappend);
4611  dumappend = constructSamplesList("gg_Sig_JHUGen", 8.);
4612  appendVector<TString>(strSamples_8TeV, dumappend);
4613  dumappend = constructSamplesList("gg_Sig_MCFM", 8.);
4614  appendVector<TString>(strSamples_8TeV, dumappend);
4615  dumappend = constructSamplesList("gg_Sig_ggVV", 8.);
4616  appendVector<TString>(strSamples_8TeV, dumappend);
4617 
4618  vector<TString> strSamples_7TeV = constructSamplesList("JJQCD", 7.);
4619  dumappend = constructSamplesList("JJVBF", 7.);
4620  appendVector<TString>(strSamples_7TeV, dumappend);
4621  dumappend = constructSamplesList("gg_Sig_JHUGen", 7.);
4622  appendVector<TString>(strSamples_7TeV, dumappend);
4623  dumappend = constructSamplesList("gg_Sig_MCFM", 7.);
4624  appendVector<TString>(strSamples_7TeV, dumappend);
4625  dumappend = constructSamplesList("gg_Sig_ggVV", 7.);
4626  appendVector<TString>(strSamples_7TeV, dumappend);
4627 
4628  vector<TFile*> finputList;
4629  vector<TTree*> treeList;
4630  int nEntries=0;
4631  TString cinput_main;
4632 
4633  TREE_NAME = "ZZTree/candTree";
4634  cinput_main = inputdir_13TeV;
4635  //for (int is=0; is<2; is++){
4636  for (int is=0; is<(int)strSamples_13TeV.size(); is++){
4637  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples_13TeV[is]).Data());
4638  TFile* finput = TFile::Open(cinput, "read");
4639  cout << "Opening file " << cinput << "..." << endl;
4640  TTree* tree=0;
4641  if (finput!=0){
4642  if (finput->IsOpen() && !finput->IsZombie()){
4643  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
4644  tree = (TTree*)finput->Get(TREE_NAME);
4645  if (tree!=0){
4646  cout << TREE_NAME << " is found." << endl;
4647 
4648  bool doRecalculate = recalculate;
4649  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0) doRecalculate = true;
4650  tree->SetBranchStatus("*", 0);
4651  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
4652  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
4653  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
4654  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
4655  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
4656  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
4657  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
4658  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
4659  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
4660  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
4661  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0){
4662  tree->SetBranchStatus(strMEBranchname, 1); tree->SetBranchAddress(strMEBranchname, &mesq_calc);
4663  tree->SetBranchStatus(strConstBranchname, 1); tree->SetBranchAddress(strConstBranchname, &cconst_calc);
4664  }
4665  nEntries += tree->GetEntries();
4666  treeList.push_back(tree);
4667  finputList.push_back(finput);
4668  }
4669  else finput->Close();
4670  }
4671  else if (finput->IsOpen()) finput->Close();
4672  }
4673  }
4674 
4675  for (int ic=0; ic<3; ic++){
4676  TREE_NAME = "SelectedTree";
4677  cinput_main = inputdir_8TeV;
4678  //for (int is=0; is<2; is++){
4679  for (int is=0; is<(int)strSamples_8TeV.size(); is++){
4680  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_8TeV[is]).Data());
4681  TFile* finput = TFile::Open(cinput, "read");
4682  cout << "Opening file " << cinput << "..." << endl;
4683  TTree* tree=0;
4684  if (finput!=0){
4685  if (finput->IsOpen() && !finput->IsZombie()){
4686  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
4687  tree = (TTree*)finput->Get(TREE_NAME);
4688  if (tree!=0){
4689  cout << TREE_NAME << " is found." << endl;
4690  tree->SetBranchStatus("*", 0);
4691  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
4692  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
4693  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
4694  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
4695  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
4696  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
4697  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
4698  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
4699  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
4700  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
4701  nEntries += tree->GetEntries();
4702  treeList.push_back(tree);
4703  finputList.push_back(finput);
4704  }
4705  else finput->Close();
4706  }
4707  else if (finput->IsOpen()) finput->Close();
4708  }
4709  }
4710  cinput_main = inputdir_7TeV;
4711  //for (int is=0; is<2; is++){
4712  for (int is=0; is<(int)strSamples_7TeV.size(); is++){
4713  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_7TeV[is]).Data());
4714  TFile* finput = TFile::Open(cinput, "read");
4715  cout << "Opening file " << cinput << "..." << endl;
4716  TTree* tree=0;
4717  if (finput!=0){
4718  if (finput->IsOpen() && !finput->IsZombie()){
4719  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
4720  tree = (TTree*)finput->Get(TREE_NAME);
4721  if (tree!=0){
4722  cout << TREE_NAME << " is found." << endl;
4723  tree->SetBranchStatus("*", 0);
4724  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
4725  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
4726  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
4727  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
4728  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
4729  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
4730  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
4731  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
4732  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
4733  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
4734  nEntries += tree->GetEntries();
4735  treeList.push_back(tree);
4736  finputList.push_back(finput);
4737  }
4738  else finput->Close();
4739  }
4740  else if (finput->IsOpen()) finput->Close();
4741  }
4742  }
4743  }
4744 
4745  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
4746 
4747  unsigned int ev_acc=0;
4748  for (int ev=0; ev<nEntries; ev++){
4749  getEntry(treeList, ev);
4750  bool doProcess=
4751  (
4752  Z1Flav*Z2Flav==pow(13, 4)
4753  ||
4754  Z1Flav*Z2Flav==pow(11, 4)
4755  ||
4756  Z1Flav*Z2Flav==pow(11*13, 2)
4757  )
4758  ;
4759  if (!doProcess) continue;
4760  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
4761  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
4762  addByLowest(index[ic], mzz, ev);
4763  ev_acc++;
4764  }
4765  cout << "Number of valid entries: " << ev_acc << endl;
4766 
4767  for (unsigned int ic=0; ic<3; ic++){
4768  float firstVal=index[ic].at(0).first;
4769  float lastVal=index[ic].at(index[ic].size()-1).first;
4770  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
4771  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
4772  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
4773 
4774  float divisor=95000;
4775  int nbins = index[ic].size()/divisor;
4776  const int nbins_th=10/*50*/;
4777  while (nbins<nbins_th){
4778  if (divisor>1000) divisor -= 1000;
4779  else if (divisor>100) divisor -= 100;
4780  else break;
4781  nbins=index[ic].size()/divisor;
4782  }
4783  cout << "nbins=" << nbins << endl;
4784  if (nbins<3) cerr << "Not enough bins!" << endl;
4785  vector<ExtBin> binList;
4786  float* binning = new float[nbins+1];
4787  binning[0]=infimum;
4788  binning[nbins]=supremum;
4789  int ev_stepsize = index[ic].size()/nbins;
4790  cout << "Event step size: " << ev_stepsize << endl;
4791  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
4792  for (int ix=1; ix<nbins; ix++){
4793  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
4794  ExtBin tmpbin;
4795  tmpbin.binlow = binning[ix-1]; tmpbin.binhigh = binning[ix];
4796  for (int bin=0; bin<ev_stepsize; bin++){
4797  int evid = index[ic][(ix-1)*ev_stepsize+bin].second;
4798  tmpbin.events.push_back(evid);
4799  }
4800  binList.push_back(tmpbin);
4801  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
4802  }
4803  ExtBin tmpbin;
4804  tmpbin.binlow = binning[nbins-1]; tmpbin.binhigh = binning[nbins];
4805  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++){
4806  int evid = index[ic][bin].second;
4807  tmpbin.events.push_back(evid);
4808  }
4809  binList.push_back(tmpbin);
4810  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
4811  cout << "Bin list has the following bins:" << endl;
4812  for (unsigned int ib=0; ib<binList.size(); ib++){
4813  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
4814  }
4815 
4816  foutput->cd();
4817 
4818  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
4819  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
4820 
4821  TTree* newtree=0;
4822  if (writeFinalTree){
4823  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
4824  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
4825  newtree->Branch("ZZMass", &mzz);
4826  }
4827 
4828  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
4829 
4830  if (ic==1){
4831  LepID[0]=11;
4832  LepID[1]=-11;
4833  }
4834  else{
4835  LepID[0]=13;
4836  LepID[1]=-13;
4837  }
4838  if (ic==0){
4839  LepID[2]=13;
4840  LepID[3]=-13;
4841  }
4842  else{
4843  LepID[2]=11;
4844  LepID[3]=-11;
4845  }
4846 
4847  double alphasVal=0, mh=0, gah=0, aL1=0, aR1=0, aL2=0, aR2=0;
4848  unsigned int ctr=0;
4849  for (unsigned int bin=0; bin<binList.size(); bin++){
4850  cout << "Bin " << bin << " is now being scrutinized..." << endl;
4851  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
4852  int getEv = binList.at(bin).events.at(ev);
4853  getEntry(treeList, getEv);
4854  TTree* tree = findTree(treeList, getEv);
4855 
4856  if (ev%1000==0) cout << "Doing event " << getEv << endl;
4857 
4858  TLorentzVector pDaughters[4];
4859  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
4860  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); }
4862  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
4863  mela.setInputEvent(&daughters, (SimpleParticleCollection_t*)0, (SimpleParticleCollection_t*)0, false);
4864 
4865  mela.setProcess(proc, me, prod);
4867 
4868  bool hasConst = (tree->GetBranchStatus(strConstBranchname)==1);
4869  bool doCalc=(!hasConst || ctr==0);
4870  if (doCalc){
4871  mela.computeP(mesq_conserveDifermMass, false);
4872  if (hasConst) mesq_conserveDifermMass = mesq_calc / cconst_calc;
4873 
4874  alphasVal = mela.getIORecord()->getAlphaSatMZ();
4875  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
4876  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
4877  mela.getIORecord()->getHiggsMassWidth(mh, gah, 0);
4878  }
4879  else{
4880  mesq_conserveDifermMass = mesq_calc / cconst_calc;
4881  mh=mzz;
4882  gah = mela.getHiggsWidthAtPoleMass(mh);
4883  }
4884 
4885  double propagator = 1./(pow(pow(mzz, 2)-pow(mh, 2), 2) + pow(mh*gah, 2));
4886  mesq_conserveDifermMass /= propagator;
4887  mesq_conserveDifermMass /= pow(alphasVal, 2);
4888  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
4889  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
4890 
4891  bool doFill = !(
4892  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
4893  );
4894 
4895  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0);
4896 
4897  mela.resetInputEvent();
4898  ctr++;
4899  }
4900 
4901  binList.at(bin).sift(); binList.at(bin).adjustWeights();
4902 
4903  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
4904  mzz = binList.at(bin).masses.at(ev);
4905  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
4906  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
4907  hvar->Fill(mzz, mzz);
4908  if (writeFinalTree) newtree->Fill();
4909  }
4910  }
4911 
4912  double* xexyey[4];
4913  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
4914  for (int bin=0; bin<nbins; bin++){
4915  xexyey[0][bin] = hvar->GetBinContent(bin+1);
4916  xexyey[1][bin] = hvar->GetBinError(bin+1);
4917 
4918  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
4919  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
4920  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
4921  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
4922  xexyey[2][bin] = log10(xexyey[2][bin]);
4923  }
4924 
4925 
4926  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
4927  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
4928  foutput->WriteTObject(tg);
4929  delete tg;
4930 
4931  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
4932  foutput->WriteTObject(hmesq_conserveDifermMass);
4933  foutput->WriteTObject(hvar);
4934  if (writeFinalTree) foutput->WriteTObject(newtree);
4935  if (writeFinalTree) delete newtree;
4936  delete hmesq_conserveDifermMass;
4937  delete hvar;
4938  delete[] binning;
4939  }
4940  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
4941  foutput->Close();
4942 }
4943 
4944 /*
4945 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
4946 - ALPHAS(MZ)**2 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION
4947 - H(1) PROPAGATOR
4948 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
4949 */
4950 void get_PAvgProfile_MCFM_ZZGG_HSMHiggs(bool recalculate=false){
4951  int erg_tev=8;
4952  float mPOLE=125.;
4953  TString TREE_NAME;
4954  const bool writeFinalTree=false;
4955  const TString strMEBranchname = "p_GG_SIG_kappaTopBot_1_ghz1_1_MCFM";
4956  const TString strConstBranchname = "pConst_GG_SIG_kappaTopBot_1_ghz1_1_MCFM";
4957 
4958  TVar::VerbosityLevel verbosity = TVar::ERROR;
4959  Mela mela(erg_tev, mPOLE, verbosity);
4960 
4964 
4965  TString strproc = ProcessName(proc);
4966  TString strme = MatrixElementName(me);
4967  TString strprod = ProductionName(prod);
4968 
4969  std::vector<short>* LepLepId=0;
4970  std::vector<float>* LepPt=0;
4971  std::vector<float>* LepEta=0;
4972  std::vector<float>* LepPhi=0;
4973 
4974  float mesq_calc=0., cconst_calc=1.;
4975  float mesq_conserveDifermMass=0;
4976  float mzz = 126.;
4977  float m1 = 91.471450;
4978  float m2 = 12.139782;
4979  float h1 = 0.2682896;
4980  float h2 = 0.1679779;
4981  float phi = 1.5969792;
4982  float hs = -0.727181;
4983  float phi1 = 1.8828257;
4984  float ZZPt, ZZPhi, ZZEta;
4985  short Z1Flav, Z2Flav;
4986  int LepID[4];
4987 
4988  vector<pair<float, int>> index[3];
4989  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
4990  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s.root", strme.Data(), strprod.Data(), strproc.Data()), "recreate");
4991 
4992  vector<TString> dumappend;
4993  vector<TString> strSamples_13TeV = constructSamplesList("JJQCD", 13.);
4994  dumappend = constructSamplesList("JJVBF", 13.);
4995  appendVector<TString>(strSamples_13TeV, dumappend);
4996  dumappend = constructSamplesList("gg_Sig_JHUGen", 13.);
4997  appendVector<TString>(strSamples_13TeV, dumappend);
4998  dumappend = constructSamplesList("gg_Sig_MCFM", 13.);
4999  appendVector<TString>(strSamples_13TeV, dumappend);
5000  dumappend = constructSamplesList("gg_Sig_ggVV", 13.);
5001  appendVector<TString>(strSamples_13TeV, dumappend);
5002 
5003  vector<TString> strSamples_8TeV = constructSamplesList("JJQCD", 8.);
5004  dumappend = constructSamplesList("JJVBF", 8.);
5005  appendVector<TString>(strSamples_8TeV, dumappend);
5006  dumappend = constructSamplesList("gg_Sig_JHUGen", 8.);
5007  appendVector<TString>(strSamples_8TeV, dumappend);
5008  dumappend = constructSamplesList("gg_Sig_MCFM", 8.);
5009  appendVector<TString>(strSamples_8TeV, dumappend);
5010  dumappend = constructSamplesList("gg_Sig_ggVV", 8.);
5011  appendVector<TString>(strSamples_8TeV, dumappend);
5012 
5013  vector<TString> strSamples_7TeV = constructSamplesList("JJQCD", 7.);
5014  dumappend = constructSamplesList("JJVBF", 7.);
5015  appendVector<TString>(strSamples_7TeV, dumappend);
5016  dumappend = constructSamplesList("gg_Sig_JHUGen", 7.);
5017  appendVector<TString>(strSamples_7TeV, dumappend);
5018  dumappend = constructSamplesList("gg_Sig_MCFM", 7.);
5019  appendVector<TString>(strSamples_7TeV, dumappend);
5020  dumappend = constructSamplesList("gg_Sig_ggVV", 7.);
5021  appendVector<TString>(strSamples_7TeV, dumappend);
5022 
5023  vector<TFile*> finputList;
5024  vector<TTree*> treeList;
5025  int nEntries=0;
5026  TString cinput_main;
5027 
5028  TREE_NAME = "ZZTree/candTree";
5029  cinput_main = inputdir_13TeV;
5030  //for (int is=0; is<2; is++){
5031  for (int is=0; is<(int)strSamples_13TeV.size(); is++){
5032  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples_13TeV[is]).Data());
5033  TFile* finput = TFile::Open(cinput, "read");
5034  cout << "Opening file " << cinput << "..." << endl;
5035  TTree* tree=0;
5036  if (finput!=0){
5037  if (finput->IsOpen() && !finput->IsZombie()){
5038  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5039  tree = (TTree*)finput->Get(TREE_NAME);
5040  if (tree!=0){
5041  cout << TREE_NAME << " is found." << endl;
5042 
5043  bool doRecalculate = recalculate;
5044  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0) doRecalculate = true;
5045  tree->SetBranchStatus("*", 0);
5046  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5047  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5048  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5049  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5050  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5051  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5052  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5053  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5054  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
5055  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
5056  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0){
5057  tree->SetBranchStatus(strMEBranchname, 1); tree->SetBranchAddress(strMEBranchname, &mesq_calc);
5058  tree->SetBranchStatus(strConstBranchname, 1); tree->SetBranchAddress(strConstBranchname, &cconst_calc);
5059  }
5060  nEntries += tree->GetEntries();
5061  treeList.push_back(tree);
5062  finputList.push_back(finput);
5063  }
5064  else finput->Close();
5065  }
5066  else if (finput->IsOpen()) finput->Close();
5067  }
5068  }
5069 
5070  for (int ic=0; ic<3; ic++){
5071  TREE_NAME = "SelectedTree";
5072  cinput_main = inputdir_8TeV;
5073  //for (int is=0; is<2; is++){
5074  for (int is=0; is<(int)strSamples_8TeV.size(); is++){
5075  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_8TeV[is]).Data());
5076  TFile* finput = TFile::Open(cinput, "read");
5077  cout << "Opening file " << cinput << "..." << endl;
5078  TTree* tree=0;
5079  if (finput!=0){
5080  if (finput->IsOpen() && !finput->IsZombie()){
5081  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5082  tree = (TTree*)finput->Get(TREE_NAME);
5083  if (tree!=0){
5084  cout << TREE_NAME << " is found." << endl;
5085  tree->SetBranchStatus("*", 0);
5086  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5087  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5088  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5089  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5090  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5091  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5092  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5093  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5094  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
5095  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
5096  nEntries += tree->GetEntries();
5097  treeList.push_back(tree);
5098  finputList.push_back(finput);
5099  }
5100  else finput->Close();
5101  }
5102  else if (finput->IsOpen()) finput->Close();
5103  }
5104  }
5105  cinput_main = inputdir_7TeV;
5106  //for (int is=0; is<2; is++){
5107  for (int is=0; is<(int)strSamples_7TeV.size(); is++){
5108  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_7TeV[is]).Data());
5109  TFile* finput = TFile::Open(cinput, "read");
5110  cout << "Opening file " << cinput << "..." << endl;
5111  TTree* tree=0;
5112  if (finput!=0){
5113  if (finput->IsOpen() && !finput->IsZombie()){
5114  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5115  tree = (TTree*)finput->Get(TREE_NAME);
5116  if (tree!=0){
5117  cout << TREE_NAME << " is found." << endl;
5118  tree->SetBranchStatus("*", 0);
5119  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5120  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5121  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5122  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5123  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5124  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5125  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5126  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5127  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
5128  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
5129  nEntries += tree->GetEntries();
5130  treeList.push_back(tree);
5131  finputList.push_back(finput);
5132  }
5133  else finput->Close();
5134  }
5135  else if (finput->IsOpen()) finput->Close();
5136  }
5137  }
5138  }
5139 
5140  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
5141 
5142  unsigned int ev_acc=0;
5143  for (int ev=0; ev<nEntries; ev++){
5144  getEntry(treeList, ev);
5145  bool doProcess=
5146  (
5147  Z1Flav*Z2Flav==pow(13, 4)
5148  ||
5149  Z1Flav*Z2Flav==pow(11, 4)
5150  ||
5151  Z1Flav*Z2Flav==pow(11*13, 2)
5152  )
5153  ;
5154  if (!doProcess) continue;
5155  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
5156  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
5157  addByLowest(index[ic], mzz, ev);
5158  ev_acc++;
5159  }
5160  cout << "Number of valid entries: " << ev_acc << endl;
5161 
5162  for (unsigned int ic=0; ic<3; ic++){
5163  float firstVal=index[ic].at(0).first;
5164  float lastVal=index[ic].at(index[ic].size()-1).first;
5165  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
5166  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
5167  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
5168 
5169  float divisor=95000;
5170  int nbins = index[ic].size()/divisor;
5171  const int nbins_th=10/*50*/;
5172  while (nbins<nbins_th){
5173  if (divisor>1000) divisor -= 1000;
5174  else if (divisor>100) divisor -= 100;
5175  else break;
5176  nbins=index[ic].size()/divisor;
5177  }
5178  cout << "nbins=" << nbins << endl;
5179  if (nbins<3) cerr << "Not enough bins!" << endl;
5180  vector<ExtBin> binList;
5181  float* binning = new float[nbins+1];
5182  binning[0]=infimum;
5183  binning[nbins]=supremum;
5184  int ev_stepsize = index[ic].size()/nbins;
5185  cout << "Event step size: " << ev_stepsize << endl;
5186  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
5187  for (int ix=1; ix<nbins; ix++){
5188  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
5189  ExtBin tmpbin;
5190  tmpbin.binlow = binning[ix-1]; tmpbin.binhigh = binning[ix];
5191  for (int bin=0; bin<ev_stepsize; bin++){
5192  int evid = index[ic][(ix-1)*ev_stepsize+bin].second;
5193  tmpbin.events.push_back(evid);
5194  }
5195  binList.push_back(tmpbin);
5196  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
5197  }
5198  ExtBin tmpbin;
5199  tmpbin.binlow = binning[nbins-1]; tmpbin.binhigh = binning[nbins];
5200  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++){
5201  int evid = index[ic][bin].second;
5202  tmpbin.events.push_back(evid);
5203  }
5204  binList.push_back(tmpbin);
5205  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
5206  cout << "Bin list has the following bins:" << endl;
5207  for (unsigned int ib=0; ib<binList.size(); ib++){
5208  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
5209  }
5210 
5211  foutput->cd();
5212 
5213  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
5214  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
5215 
5216  TTree* newtree=0;
5217  if (writeFinalTree){
5218  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
5219  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
5220  newtree->Branch("ZZMass", &mzz);
5221  }
5222 
5223  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
5224 
5225  if (ic==1){
5226  LepID[0]=11;
5227  LepID[1]=-11;
5228  }
5229  else{
5230  LepID[0]=13;
5231  LepID[1]=-13;
5232  }
5233  if (ic==0){
5234  LepID[2]=13;
5235  LepID[3]=-13;
5236  }
5237  else{
5238  LepID[2]=11;
5239  LepID[3]=-11;
5240  }
5241 
5242  double alphasVal=0, mh=0, gah=0, aL1=0, aR1=0, aL2=0, aR2=0;
5243  unsigned int ctr=0;
5244  for (unsigned int bin=0; bin<binList.size(); bin++){
5245  cout << "Bin " << bin << " is now being scrutinized..." << endl;
5246  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
5247  int getEv = binList.at(bin).events.at(ev);
5248  getEntry(treeList, getEv);
5249  TTree* tree = findTree(treeList, getEv);
5250 
5251  if (ev%1000==0) cout << "Doing event " << getEv << endl;
5252 
5253  TLorentzVector pDaughters[4];
5254  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
5255  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); }
5257  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
5258  mela.setInputEvent(&daughters, (SimpleParticleCollection_t*)0, (SimpleParticleCollection_t*)0, false);
5259 
5260  mela.setProcess(proc, me, prod);
5262 
5263  bool hasConst = (tree->GetBranchStatus(strConstBranchname)==1);
5264  bool doCalc=(!hasConst || ctr==0);
5265  if (doCalc){
5266  mela.computeP(mesq_conserveDifermMass, false);
5267  if (hasConst) mesq_conserveDifermMass = mesq_calc / cconst_calc;
5268 
5269  alphasVal = mela.getIORecord()->getAlphaSatMZ();
5270  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
5271  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
5272  mela.getIORecord()->getHiggsMassWidth(mh, gah, 0);
5273  }
5274  else mesq_conserveDifermMass = mesq_calc / cconst_calc;
5275 
5276  double propagator = 1./(pow(pow(mzz, 2)-pow(mh, 2), 2) + pow(mh*gah, 2));
5277  mesq_conserveDifermMass /= propagator;
5278  mesq_conserveDifermMass /= pow(alphasVal, 2);
5279  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
5280  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
5281 
5282  bool doFill = !(
5283  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
5284  );
5285 
5286  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0);
5287 
5288  mela.resetInputEvent();
5289  ctr++;
5290  }
5291 
5292  binList.at(bin).sift(); binList.at(bin).adjustWeights();
5293 
5294  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
5295  mzz = binList.at(bin).masses.at(ev);
5296  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
5297  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
5298  hvar->Fill(mzz, mzz);
5299  if (writeFinalTree) newtree->Fill();
5300  }
5301  }
5302 
5303  double* xexyey[4];
5304  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
5305  for (int bin=0; bin<nbins; bin++){
5306  xexyey[0][bin] = hvar->GetBinContent(bin+1);
5307  xexyey[1][bin] = hvar->GetBinError(bin+1);
5308 
5309  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
5310  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
5311  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
5312  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
5313  xexyey[2][bin] = log10(xexyey[2][bin]);
5314  }
5315 
5316 
5317  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
5318  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
5319  foutput->WriteTObject(tg);
5320  delete tg;
5321 
5322  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
5323  foutput->WriteTObject(hmesq_conserveDifermMass);
5324  foutput->WriteTObject(hvar);
5325  if (writeFinalTree) foutput->WriteTObject(newtree);
5326  if (writeFinalTree) delete newtree;
5327  delete hmesq_conserveDifermMass;
5328  delete hvar;
5329  delete[] binning;
5330  }
5331  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
5332  foutput->Close();
5333 }
5334 
5335 /*
5336 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
5337 - ALPHAS(MZ)**2 TO REMAIN INDEPENDENT OF PDF CHOICE TO FIRST APPROXIMATION
5338 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
5339 */
5340 void get_PAvgProfile_MCFM_ZZGG_bkgZZ(bool recalculate=false){
5341  int erg_tev=8;
5342  float mPOLE=125.;
5343  TString TREE_NAME;
5344  const bool writeFinalTree=true;
5345  const TString strMEBranchname = "p_GG_BKG_MCFM";
5346  const TString strConstBranchname = "pConst_GG_BKG_MCFM";
5347 
5348  TVar::VerbosityLevel verbosity = TVar::ERROR;
5349  Mela mela(erg_tev, mPOLE, verbosity);
5350 
5351  TVar::Process proc = TVar::bkgZZ;
5354 
5355  TString strproc = ProcessName(proc);
5356  TString strme = MatrixElementName(me);
5357  TString strprod = ProductionName(prod);
5358 
5359  std::vector<short>* LepLepId=0;
5360  std::vector<float>* LepPt=0;
5361  std::vector<float>* LepEta=0;
5362  std::vector<float>* LepPhi=0;
5363 
5364  float mesq_calc=0., cconst_calc=1.;
5365  float mesq_conserveDifermMass=0;
5366  float mzz = 126.;
5367  float m1 = 91.471450;
5368  float m2 = 12.139782;
5369  float h1 = 0.2682896;
5370  float h2 = 0.1679779;
5371  float phi = 1.5969792;
5372  float hs = -0.727181;
5373  float phi1 = 1.8828257;
5374  float ZZPt, ZZPhi, ZZEta;
5375  float wgt=1.;
5376  float GenHMass;
5377  short Z1Flav, Z2Flav;
5378  int LepID[4];
5379  short useWeighted;
5380 
5381  vector<pair<float, int>> index[3];
5382  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
5383  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s.root", strme.Data(), strprod.Data(), strproc.Data()), "recreate");
5384 
5385  vector<TString> dumappend;
5386  vector<TString> strSamples_13TeV = constructSamplesList("gg_Bkg_MCFM", 13.);
5387  dumappend = constructSamplesList("gg_Bkg_ggVV", 13.);
5388  appendVector<TString>(strSamples_13TeV, dumappend);
5389 
5390  vector<TString> strSamples_8TeV = constructSamplesList("gg_Bkg_MCFM", 8.);
5391  dumappend = constructSamplesList("gg_Bkg_ggVV", 8.);
5392  appendVector<TString>(strSamples_8TeV, dumappend);
5393 
5394  vector<TString> strSamples_7TeV = constructSamplesList("gg_Bkg_MCFM", 7.);
5395  dumappend = constructSamplesList("gg_Bkg_ggVV", 7.);
5396  appendVector<TString>(strSamples_7TeV, dumappend);
5397 
5398  // Consider if weights exist
5399  unordered_map<TTree*, pair<float, float>> samplePoleMasses;
5400 
5401  vector<TString> strSamples_weighted_13TeV = constructSamplesList("JJQCD", 13.);
5402  dumappend = constructSamplesList("gg_Sig_JHUGen", 13.);
5403  appendVector<TString>(strSamples_weighted_13TeV, dumappend);
5404  dumappend = constructSamplesList("gg_Sig_MCFM", 13.);
5405  appendVector<TString>(strSamples_weighted_13TeV, dumappend);
5406  dumappend = constructSamplesList("gg_Sig_ggVV", 13.);
5407  appendVector<TString>(strSamples_weighted_13TeV, dumappend);
5408 
5409  vector<TFile*> finputList;
5410  vector<TTree*> treeList;
5411  int nEntries=0;
5412  TString cinput_main;
5413 
5414  TREE_NAME = "ZZTree/candTree";
5415  cinput_main = inputdir_13TeV;
5416  //for (int is=0; is<1; is++){
5417  for (int is=0; is<(int)strSamples_weighted_13TeV.size(); is++){
5418  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples_weighted_13TeV[is]).Data());
5419  TFile* finput = TFile::Open(cinput, "read");
5420  cout << "Opening file " << cinput << "..." << endl;
5421  TTree* tree=0;
5422  if (finput!=0){
5423  if (finput->IsOpen() && !finput->IsZombie()){
5424  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5425  tree = (TTree*)finput->Get(TREE_NAME);
5426  if (tree!=0){
5427  cout << TREE_NAME << " is found." << endl;
5428  if (tree->GetBranchStatus("p_Gen_GG_BKG_MCFM")==1){
5429  bool doRecalculate = recalculate;
5430  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0) doRecalculate = true;
5431  tree->SetBranchStatus("*", 0);
5432  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5433  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5434  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5435  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5436  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5437  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5438  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5439  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5440  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
5441  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
5442  tree->SetBranchStatus("p_Gen_GG_BKG_MCFM", 1); tree->SetBranchAddress("p_Gen_GG_BKG_MCFM", &wgt);
5443  tree->SetBranchStatus("GenHMass", 1); tree->SetBranchAddress("GenHMass", &GenHMass);
5444  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0){
5445  tree->SetBranchStatus(strMEBranchname, 1); tree->SetBranchAddress(strMEBranchname, &mesq_calc);
5446  tree->SetBranchStatus(strConstBranchname, 1); tree->SetBranchAddress(strConstBranchname, &cconst_calc);
5447  }
5448  nEntries += tree->GetEntries();
5449  treeList.push_back(tree);
5450  finputList.push_back(finput);
5451 
5452  float polemass = findPoleMass(strSamples_weighted_13TeV[is]);
5453  float polewidth = mela.getHiggsWidthAtPoleMass(polemass);
5454  cout << strSamples_weighted_13TeV[is] << " pole mass = " << polemass << ", pole width = " << polewidth << endl;
5455  samplePoleMasses[tree] = pair<float, float>(polemass, polewidth);
5456  }
5457  else{
5458  cerr << TREE_NAME << " in " << cinput << " does not have weight p_Gen_GG_BKG_MCFM." << endl;
5459  finput->Close();
5460  }
5461  }
5462  else finput->Close();
5463  }
5464  else if (finput->IsOpen()) finput->Close();
5465  }
5466  }
5467 
5468  TREE_NAME = "ZZTree/candTree";
5469  cinput_main = inputdir_13TeV;
5470  //for (int is=0; is<1; is++){
5471  for (int is=0; is<(int)strSamples_13TeV.size(); is++){
5472  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples_13TeV[is]).Data());
5473  TFile* finput = TFile::Open(cinput, "read");
5474  cout << "Opening file " << cinput << "..." << endl;
5475  TTree* tree=0;
5476  if (finput!=0){
5477  if (finput->IsOpen() && !finput->IsZombie()){
5478  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5479  tree = (TTree*)finput->Get(TREE_NAME);
5480  if (tree!=0){
5481  cout << TREE_NAME << " is found." << endl;
5482  bool doRecalculate = recalculate;
5483  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0) doRecalculate = true;
5484  tree->SetBranchStatus("*", 0);
5485  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5486  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5487  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5488  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5489  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5490  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5491  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5492  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5493  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
5494  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
5495  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0){
5496  tree->SetBranchStatus(strMEBranchname, 1); tree->SetBranchAddress(strMEBranchname, &mesq_calc);
5497  tree->SetBranchStatus(strConstBranchname, 1); tree->SetBranchAddress(strConstBranchname, &cconst_calc);
5498  }
5499  nEntries += tree->GetEntries();
5500  treeList.push_back(tree);
5501  finputList.push_back(finput);
5502  }
5503  else finput->Close();
5504  }
5505  else if (finput->IsOpen()) finput->Close();
5506  }
5507  }
5508 
5509  TREE_NAME = "SelectedTree";
5510  for (int ic=0; ic<3; ic++){
5511  cinput_main = inputdir_8TeV;
5512  //for (int is=0; is<0; is++){
5513  for (int is=0; is<(int)strSamples_8TeV.size(); is++){
5514  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_8TeV[is]).Data());
5515  TFile* finput = TFile::Open(cinput, "read");
5516  cout << "Opening file " << cinput << "..." << endl;
5517  TTree* tree=0;
5518  if (finput!=0){
5519  if (finput->IsOpen() && !finput->IsZombie()){
5520  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5521  tree = (TTree*)finput->Get(TREE_NAME);
5522  if (tree!=0){
5523  cout << TREE_NAME << " is found." << endl;
5524  tree->SetBranchStatus("*", 0);
5525  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5526  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5527  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5528  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5529  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5530  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5531  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5532  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5533  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
5534  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
5535  nEntries += tree->GetEntries();
5536  treeList.push_back(tree);
5537  finputList.push_back(finput);
5538  }
5539  else finput->Close();
5540  }
5541  else if (finput->IsOpen()) finput->Close();
5542  }
5543  }
5544  cinput_main = inputdir_7TeV;
5545  //for (int is=0; is<0; is++){
5546  for (int is=0; is<(int)strSamples_7TeV.size(); is++){
5547  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_7TeV[is]).Data());
5548  TFile* finput = TFile::Open(cinput, "read");
5549  cout << "Opening file " << cinput << "..." << endl;
5550  TTree* tree=0;
5551  if (finput!=0){
5552  if (finput->IsOpen() && !finput->IsZombie()){
5553  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5554  tree = (TTree*)finput->Get(TREE_NAME);
5555  if (tree!=0){
5556  cout << TREE_NAME << " is found." << endl;
5557  tree->SetBranchStatus("*", 0);
5558  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5559  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5560  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5561  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5562  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5563  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5564  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5565  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5566  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
5567  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
5568  nEntries += tree->GetEntries();
5569  treeList.push_back(tree);
5570  finputList.push_back(finput);
5571  }
5572  else finput->Close();
5573  }
5574  else if (finput->IsOpen()) finput->Close();
5575  }
5576  }
5577  }
5578 
5579  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
5580 
5581  unsigned int ev_acc=0;
5582  for (int ev=0; ev<nEntries; ev++){
5583  getEntry(treeList, ev);
5584  bool doProcess=
5585  (
5586  Z1Flav*Z2Flav==pow(13, 4)
5587  ||
5588  Z1Flav*Z2Flav==pow(11, 4)
5589  ||
5590  Z1Flav*Z2Flav==pow(11*13, 2)
5591  )
5592  ;
5593  if (!doProcess) continue;
5594  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
5595  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
5596  addByLowest(index[ic], mzz, ev);
5597  ev_acc++;
5598  }
5599  cout << "Number of valid entries: " << ev_acc << endl;
5600 
5601  for (unsigned int ic=0; ic<3; ic++){
5602  float firstVal=index[ic].at(0).first;
5603  float lastVal=index[ic].at(index[ic].size()-1).first;
5604  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
5605  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
5606  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
5607 
5608  float divisor=100000;
5609  if (ic!=2) divisor=130000;
5610  int nbins = index[ic].size()/divisor;
5611  const int nbins_th=10/*50*/;
5612  while (nbins<nbins_th){
5613  if (divisor>1000) divisor -= 1000;
5614  else if (divisor>100) divisor -= 100;
5615  else break;
5616  nbins=index[ic].size()/divisor;
5617  }
5618  cout << "nbins=" << nbins << endl;
5619  if (nbins<3) cerr << "Not enough bins!" << endl;
5620  vector<ExtBin> binList;
5621  vector<ExtBin> weightedBinList;
5622  float* binning = new float[nbins+1];
5623  binning[0]=infimum;
5624  binning[nbins]=supremum;
5625  int ev_stepsize = index[ic].size()/nbins;
5626  cout << "Event step size: " << ev_stepsize << endl;
5627  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
5628  for (int ix=1; ix<nbins; ix++){
5629  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
5630  ExtBin tmpbin, tmpbin_weighted;
5631  tmpbin.binlow = binning[ix-1]; tmpbin.binhigh = binning[ix];
5632  tmpbin_weighted.binlow = binning[ix-1]; tmpbin_weighted.binhigh = binning[ix];
5633  for (int bin=0; bin<ev_stepsize; bin++){
5634  int evid = index[ic][(ix-1)*ev_stepsize+bin].second;
5635  TTree* tree = findTree(treeList, evid);
5636  if (tree->GetBranchStatus("p_Gen_GG_BKG_MCFM")==1) tmpbin_weighted.events.push_back(evid);
5637  else tmpbin.events.push_back(evid);
5638  }
5639  binList.push_back(tmpbin);
5640  weightedBinList.push_back(tmpbin_weighted);
5641  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
5642  }
5643  ExtBin tmpbin, tmpbin_weighted;
5644  tmpbin.binlow = binning[nbins-1]; tmpbin.binhigh = binning[nbins];
5645  tmpbin_weighted.binlow = binning[nbins-1]; tmpbin_weighted.binhigh = binning[nbins];
5646  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++){
5647  int evid = index[ic][bin].second;
5648  TTree* tree = findTree(treeList, evid);
5649  if (tree->GetBranchStatus("p_Gen_GG_BKG_MCFM")==1) tmpbin_weighted.events.push_back(evid);
5650  else tmpbin.events.push_back(evid);
5651  }
5652  binList.push_back(tmpbin);
5653  weightedBinList.push_back(tmpbin_weighted);
5654  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
5655  cout << "Bin list has the following bins:" << endl;
5656  for (unsigned int ib=0; ib<binList.size(); ib++){
5657  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
5658  }
5659 
5660  foutput->cd();
5661  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
5662  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
5663 
5664  TTree* newtree=0;
5665  if (writeFinalTree){
5666  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
5667  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
5668  newtree->Branch("ZZMass", &mzz);
5669  newtree->Branch("weight", &wgt);
5670  newtree->Branch("isWeighted", &useWeighted);
5671  }
5672 
5673  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
5674 
5675  if (ic==1){
5676  LepID[0]=11;
5677  LepID[1]=-11;
5678  }
5679  else{
5680  LepID[0]=13;
5681  LepID[1]=-13;
5682  }
5683  if (ic==0){
5684  LepID[2]=13;
5685  LepID[3]=-13;
5686  }
5687  else{
5688  LepID[2]=11;
5689  LepID[3]=-11;
5690  }
5691 
5692  double alphasVal=0, aL1=0, aR1=0, aL2=0, aR2=0;
5693  unsigned int ctr=0;
5694  for (unsigned int bin=0; bin<binList.size(); bin++){
5695  cout << "Bin " << bin << " is now being scrutinized..." << endl;
5696  unsigned int nweighted = weightedBinList.at(bin).events.size();
5697  unsigned int nunweighted = binList.at(bin).events.size();
5698  unsigned int ntotal = nweighted + nunweighted;
5699  for (unsigned int ev = 0; ev < ntotal; ev++){
5700  int getEv;
5701  bool isweighted = (ev<nweighted);
5702  if (isweighted) getEv = weightedBinList.at(bin).events.at(ev);
5703  else getEv = binList.at(bin).events.at(ev-nweighted);
5704  wgt=1;
5705  getEntry(treeList, getEv);
5706  TTree* tree = findTree(treeList, getEv);
5707 
5708  if (ev%1000==0) cout << "Doing event " << getEv << endl;
5709 
5710  TLorentzVector pDaughters[4];
5711  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
5712  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); }
5714  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
5715  mela.setInputEvent(&daughters, (SimpleParticleCollection_t*)0, (SimpleParticleCollection_t*)0, false);
5716 
5717  mela.setProcess(proc, me, prod);
5719 
5720  bool hasConst = (tree->GetBranchStatus(strConstBranchname)==1);
5721  bool doCalc=(!hasConst || ctr==0);
5722  if (doCalc){
5723  mela.computeP(mesq_conserveDifermMass, false);
5724  if (hasConst) mesq_conserveDifermMass = mesq_calc / cconst_calc;
5725 
5726  alphasVal = mela.getIORecord()->getAlphaSatMZ();
5727  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
5728  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
5729  }
5730  else mesq_conserveDifermMass = mesq_calc / cconst_calc;
5731 
5732  mesq_conserveDifermMass /= pow(alphasVal, 2);
5733  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
5734  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
5735 
5736  bool doFill = !(
5737  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
5738  );
5739 
5740  if (doFill){
5741  if (isweighted){
5742  float mh = samplePoleMasses[tree].first;
5743  float gh = samplePoleMasses[tree].second;
5744  float prop = 1./(pow(pow(GenHMass, 2) - pow(mh, 2), 2) + pow(mh*gh, 2));
5745  wgt *= prop;
5746  weightedBinList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0, wgt);
5747  }
5748  else binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0, 1.);
5749  }
5750 
5751  mela.resetInputEvent();
5752  ctr++;
5753  }
5754 
5755  cout << "Adjusting weights in weighted samples" << endl;
5756  weightedBinList.at(bin).adjustWeights();
5757  cout << "Merging event info. from weighted samples" << endl;
5758  binList.at(bin).mergeBin(weightedBinList.at(bin));
5759  cout << "Sifting" << endl;
5760  binList.at(bin).sift();
5761 
5762  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
5763  mzz = binList.at(bin).masses.at(ev);
5764  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
5765  wgt = binList.at(bin).weights.at(ev);
5766  useWeighted = (wgt!=1.);
5767  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass, wgt);
5768  hvar->Fill(mzz, mzz, wgt);
5769  if (writeFinalTree) newtree->Fill();
5770  }
5771  }
5772 
5773  double* xexyey[4];
5774  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
5775  for (int bin=0; bin<nbins; bin++){
5776  xexyey[0][bin] = hvar->GetBinContent(bin+1);
5777  xexyey[1][bin] = hvar->GetBinError(bin+1);
5778 
5779  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
5780  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
5781  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
5782  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
5783  xexyey[2][bin] = log10(xexyey[2][bin]);
5784  }
5785 
5786 
5787  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
5788  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
5789  foutput->WriteTObject(tg);
5790  delete tg;
5791 
5792  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
5793  foutput->WriteTObject(hmesq_conserveDifermMass);
5794  foutput->WriteTObject(hvar);
5795  if (writeFinalTree) foutput->WriteTObject(newtree);
5796  if (writeFinalTree) delete newtree;
5797  delete hmesq_conserveDifermMass;
5798  delete hvar;
5799  delete[] binning;
5800  }
5801  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
5802  foutput->Close();
5803 }
5804 
5805 /*
5806 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
5807 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
5808 */
5809 void get_PAvgProfile_MCFM_ZZQQB_bkgZZ(bool recalculate=false){
5810  int erg_tev=8;
5811  float mPOLE=125.;
5812  TString TREE_NAME;
5813  const bool writeFinalTree=true;
5814  const TString strMEBranchname = "p_QQB_BKG_MCFM";
5815  const TString strConstBranchname = "pConst_QQB_BKG_MCFM";
5816 
5817  TVar::VerbosityLevel verbosity = TVar::ERROR;
5818  Mela mela(erg_tev, mPOLE, verbosity);
5819 
5820  TVar::Process proc = TVar::bkgZZ;
5823 
5824  TString strproc = ProcessName(proc);
5825  TString strme = MatrixElementName(me);
5826  TString strprod = ProductionName(prod);
5827 
5828  std::vector<short>* LepLepId=0;
5829  std::vector<float>* LepPt=0;
5830  std::vector<float>* LepEta=0;
5831  std::vector<float>* LepPhi=0;
5832 
5833  float mesq_calc=0., cconst_calc=1.;
5834  float mesq_conserveDifermMass=0;
5835  float mzz = 126.;
5836  float m1 = 91.471450;
5837  float m2 = 12.139782;
5838  float h1 = 0.2682896;
5839  float h2 = 0.1679779;
5840  float phi = 1.5969792;
5841  float hs = -0.727181;
5842  float phi1 = 1.8828257;
5843  float ZZPt, ZZPhi, ZZEta;
5844  short Z1Flav, Z2Flav;
5845  int LepID[4];
5846 
5847  vector<pair<float, int>> index[3];
5848  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
5849  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s.root", strme.Data(), strprod.Data(), strproc.Data()), "recreate");
5850 
5851  vector<TString> strSamples_13TeV = constructSamplesList("qq_Bkg", 13.);
5852  vector<TString> strSamples_8TeV = constructSamplesList("qq_Bkg", 8.);
5853  vector<TString> strSamples_7TeV = constructSamplesList("qq_Bkg", 7.);
5854 
5855  vector<TFile*> finputList;
5856  vector<TTree*> treeList;
5857  int nEntries=0;
5858  TString cinput_main;
5859 
5860  TREE_NAME = "ZZTree/candTree";
5861  cinput_main = inputdir_13TeV;
5862  //for (int is=0; is<1; is++){
5863  for (int is=0; is<(int)strSamples_13TeV.size(); is++){
5864  TString cinput = Form("%s/%s/ZZ4lAnalysis.root", cinput_main.Data(), (strSamples_13TeV[is]).Data());
5865  TFile* finput = TFile::Open(cinput, "read");
5866  cout << "Opening file " << cinput << "..." << endl;
5867  TTree* tree=0;
5868  if (finput!=0){
5869  if (finput->IsOpen() && !finput->IsZombie()){
5870  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5871  tree = (TTree*)finput->Get(TREE_NAME);
5872  if (tree!=0){
5873  cout << TREE_NAME << " is found with " << tree->GetEntries() << " events." << endl;
5874  bool doRecalculate = recalculate;
5875  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0) doRecalculate = true;
5876  tree->SetBranchStatus("*", 0);
5877  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5878  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5879  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5880  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5881  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5882  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5883  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5884  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5885  tree->SetBranchStatus("Z1Flav", 1); tree->SetBranchAddress("Z1Flav", &Z1Flav);
5886  tree->SetBranchStatus("Z2Flav", 1); tree->SetBranchAddress("Z2Flav", &Z2Flav);
5887  if (!doRecalculate && tree->GetBranchStatus(strConstBranchname)==0){
5888  tree->SetBranchStatus(strMEBranchname, 1); tree->SetBranchAddress(strMEBranchname, &mesq_calc);
5889  tree->SetBranchStatus(strConstBranchname, 1); tree->SetBranchAddress(strConstBranchname, &cconst_calc);
5890  }
5891  nEntries += tree->GetEntries();
5892  treeList.push_back(tree);
5893  finputList.push_back(finput);
5894  }
5895  else finput->Close();
5896  }
5897  else if (finput->IsOpen()) finput->Close();
5898  }
5899  }
5900 
5901  for (int ic=0; ic<3; ic++){
5902  TREE_NAME = "SelectedTree";
5903  cinput_main = inputdir_8TeV;
5904  //for (int is=0; is<2; is++){
5905  for (int is=0; is<(int)strSamples_8TeV.size(); is++){
5906  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_8TeV[is]).Data());
5907  TFile* finput = TFile::Open(cinput, "read");
5908  cout << "Opening file " << cinput << "..." << endl;
5909  TTree* tree=0;
5910  if (finput!=0){
5911  if (finput->IsOpen() && !finput->IsZombie()){
5912  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5913  tree = (TTree*)finput->Get(TREE_NAME);
5914  if (tree!=0){
5915  cout << TREE_NAME << " is found with " << tree->GetEntries() << " events." << endl;
5916  tree->SetBranchStatus("*", 0);
5917  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5918  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5919  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5920  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5921  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5922  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5923  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5924  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5925  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
5926  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
5927  nEntries += tree->GetEntries();
5928  treeList.push_back(tree);
5929  finputList.push_back(finput);
5930  }
5931  else finput->Close();
5932  }
5933  else if (finput->IsOpen()) finput->Close();
5934  }
5935  }
5936  cinput_main = inputdir_7TeV;
5937  //for (int is=0; is<0; is++){
5938  for (int is=0; is<(int)strSamples_7TeV.size(); is++){
5939  TString cinput = Form("%s/%s/%s", cinput_main.Data(), strchannel[ic].Data(), (strSamples_7TeV[is]).Data());
5940  TFile* finput = TFile::Open(cinput, "read");
5941  cout << "Opening file " << cinput << "..." << endl;
5942  TTree* tree=0;
5943  if (finput!=0){
5944  if (finput->IsOpen() && !finput->IsZombie()){
5945  cout << cinput << " opened. Extracting tree " << TREE_NAME << "..." << endl;
5946  tree = (TTree*)finput->Get(TREE_NAME);
5947  if (tree!=0){
5948  cout << TREE_NAME << " is found with " << tree->GetEntries() << " events." << endl;
5949  tree->SetBranchStatus("*", 0);
5950  tree->SetBranchStatus("ZZMass", 1); tree->SetBranchAddress("ZZMass", &mzz);
5951  tree->SetBranchStatus("Z1Mass", 1); tree->SetBranchAddress("Z1Mass", &m1);
5952  tree->SetBranchStatus("Z2Mass", 1); tree->SetBranchAddress("Z2Mass", &m2);
5953  tree->SetBranchStatus("helcosthetaZ1", 1); tree->SetBranchAddress("helcosthetaZ1", &h1);
5954  tree->SetBranchStatus("helcosthetaZ2", 1); tree->SetBranchAddress("helcosthetaZ2", &h2);
5955  tree->SetBranchStatus("helphi", 1); tree->SetBranchAddress("helphi", &phi);
5956  tree->SetBranchStatus("costhetastar", 1); tree->SetBranchAddress("costhetastar", &hs);
5957  tree->SetBranchStatus("phistarZ1", 1); tree->SetBranchAddress("phistarZ1", &phi1);
5958  tree->SetBranchStatus("Z1ids", 1); tree->SetBranchAddress("Z1ids", &Z1Flav);
5959  tree->SetBranchStatus("Z2ids", 1); tree->SetBranchAddress("Z2ids", &Z2Flav);
5960  nEntries += tree->GetEntries();
5961  treeList.push_back(tree);
5962  finputList.push_back(finput);
5963  }
5964  else finput->Close();
5965  }
5966  else if (finput->IsOpen()) finput->Close();
5967  }
5968  }
5969  }
5970 
5971  cout << "NEntries = " << nEntries << " over " << treeList.size() << " trees." << endl;
5972 
5973  unsigned int ev_acc=0;
5974  for (int ev=0; ev<nEntries; ev++){
5975  getEntry(treeList, ev);
5976  bool doProcess=
5977  (
5978  Z1Flav*Z2Flav==pow(13, 4)
5979  ||
5980  Z1Flav*Z2Flav==pow(11, 4)
5981  ||
5982  Z1Flav*Z2Flav==pow(11*13, 2)
5983  )
5984  ;
5985  if (!doProcess) continue;
5986  if (ev_acc%10000==0) cout << "Pre-processing event " << ev << endl;
5987  unsigned int ic = (Z1Flav*Z2Flav==pow(13, 4))*0 + (Z1Flav*Z2Flav==pow(11, 4))*1 + (Z1Flav*Z2Flav==pow(11*13, 2))*2;
5988  addByLowest(index[ic], mzz, ev);
5989  ev_acc++;
5990  }
5991  cout << "Number of valid entries: " << ev_acc << endl;
5992 
5993  for (unsigned int ic=0; ic<3; ic++){
5994  float firstVal=index[ic].at(0).first;
5995  float lastVal=index[ic].at(index[ic].size()-1).first;
5996  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
5997  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
5998  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
5999 
6000  float divisor=55000;
6001  int nbins = index[ic].size()/divisor;
6002  const int nbins_th=10/*50*/;
6003  while (nbins<nbins_th){
6004  if (divisor>1000) divisor -= 1000;
6005  else if (divisor>100) divisor -= 100;
6006  else break;
6007  nbins=index[ic].size()/divisor;
6008  }
6009  cout << "nbins=" << nbins << endl;
6010  if (nbins<3) cerr << "Not enough bins!" << endl;
6011  vector<ExtBin> binList;
6012  float* binning = new float[nbins+1];
6013  binning[0]=infimum;
6014  binning[nbins]=supremum;
6015  int ev_stepsize = index[ic].size()/nbins;
6016  cout << "Event step size: " << ev_stepsize << endl;
6017  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
6018  for (int ix=1; ix<nbins; ix++){
6019  binning[ix]=(index[ic][ix*ev_stepsize-1].first+index[ic][ix*ev_stepsize].first)*0.5;
6020  ExtBin tmpbin;
6021  tmpbin.binlow = binning[ix-1]; tmpbin.binhigh = binning[ix];
6022  for (int bin=0; bin<ev_stepsize; bin++){
6023  int evid = index[ic][(ix-1)*ev_stepsize+bin].second;
6024  tmpbin.events.push_back(evid);
6025  }
6026  binList.push_back(tmpbin);
6027  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << index[ic][ix*ev_stepsize].second << ", step " << ix*ev_stepsize << "]" << endl;
6028  }
6029  ExtBin tmpbin;
6030  tmpbin.binlow = binning[nbins-1]; tmpbin.binhigh = binning[nbins];
6031  for (unsigned int bin=(nbins-1)*ev_stepsize; bin<index[ic].size(); bin++){
6032  int evid = index[ic][bin].second;
6033  tmpbin.events.push_back(evid);
6034  }
6035  binList.push_back(tmpbin);
6036  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
6037  cout << "Bin list has the following bins:" << endl;
6038  for (unsigned int ib=0; ib<binList.size(); ib++){
6039  cout << ib << " / " << binList.size() << ": [" << binList.at(ib).binlow << "," << binList.at(ib).binhigh << "]" << endl;
6040  }
6041 
6042  foutput->cd();
6043 
6044  TProfile* hvar = new TProfile(Form("candMass_%s", strchannel[ic].Data()), "", nbins, binning); hvar->Sumw2();
6045  TProfile* hmesq_conserveDifermMass = new TProfile(Form("P_ConserveDifermionMass_%s", strchannel[ic].Data()), "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
6046 
6047  TTree* newtree=0;
6048  if (writeFinalTree){
6049  newtree = new TTree(Form("FinalTree_%s", strchannel[ic].Data()), "");
6050  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
6051  newtree->Branch("ZZMass", &mzz);
6052  }
6053 
6054  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
6055 
6056  if (ic==1){
6057  LepID[0]=11;
6058  LepID[1]=-11;
6059  }
6060  else{
6061  LepID[0]=13;
6062  LepID[1]=-13;
6063  }
6064  if (ic==0){
6065  LepID[2]=13;
6066  LepID[3]=-13;
6067  }
6068  else{
6069  LepID[2]=11;
6070  LepID[3]=-11;
6071  }
6072 
6073  double aL1=0, aR1=0, aL2=0, aR2=0, mz=0, gaz=0;
6074  unsigned int ctr=0;
6075  for (unsigned int bin=0; bin<binList.size(); bin++){
6076  cout << "Bin " << bin << " is now being scrutinized..." << endl;
6077  for (unsigned int ev = 0; ev < binList.at(bin).events.size(); ev++){
6078  int getEv = binList.at(bin).events.at(ev);
6079  getEntry(treeList, getEv);
6080  TTree* tree = findTree(treeList, getEv);
6081 
6082  if (ev%1000==0) cout << "Doing event " << getEv << endl;
6083 
6084  TLorentzVector pDaughters[4];
6085  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
6086  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); }
6088  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
6089  mela.setInputEvent(&daughters, (SimpleParticleCollection_t*)0, (SimpleParticleCollection_t*)0, false);
6090 
6091  mela.setProcess(proc, me, prod);
6093 
6094  bool hasConst = (tree->GetBranchStatus(strConstBranchname)==1);
6095  bool doCalc=(!hasConst || ctr==0);
6096  if (doCalc){
6097  mela.computeP(mesq_conserveDifermMass, false);
6098  if (hasConst) mesq_conserveDifermMass = mesq_calc / cconst_calc;
6099 
6100  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
6101  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
6102  mz = mela.getPrimaryMass(23);
6103  gaz = mela.getPrimaryWidth(23);
6104  }
6105  else mesq_conserveDifermMass = mesq_calc / cconst_calc;
6106 
6107  double propagator;
6108  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
6109  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
6110  if (fabs(mzz-mz)<=4.*gaz){
6111  double sh = pow(mzz, 2);
6112  double shdn = pow(mz-4.*gaz, 2);
6113  double shup = pow(mz+4.*gaz, 2);
6114  double prop_sh = 1./(pow(sh-pow(mz, 2), 2) + pow(mz*gaz, 2));
6115  double prop_shdn = 1./(pow(shdn-pow(mz, 2), 2) + pow(mz*gaz, 2));
6116  double prop_shup = 1./(pow(shup-pow(mz, 2), 2) + pow(mz*gaz, 2));
6117  double fsh = (sh-shdn)/(shup-shdn);
6118  propagator = prop_sh / (prop_shdn*(1.-fsh) + prop_shup*fsh);
6119  }
6120  else propagator=1.;
6121  mesq_conserveDifermMass /= propagator;
6122 
6123  bool doFill = !(
6124  isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)
6125  );
6126 
6127  if (doFill) binList.at(bin).addEvent(mzz, mesq_conserveDifermMass, 0);
6128 
6129  mela.resetInputEvent();
6130  ctr++;
6131  }
6132 
6133  binList.at(bin).sift(); binList.at(bin).adjustWeights();
6134 
6135  for (unsigned int ev=0; ev<binList.at(bin).masses.size(); ev++){
6136  mzz = binList.at(bin).masses.at(ev);
6137  mesq_conserveDifermMass = binList.at(bin).mevals.at(ev);
6138  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
6139  hvar->Fill(mzz, mzz);
6140  if (writeFinalTree) newtree->Fill();
6141  }
6142  }
6143 
6144  double* xexyey[4];
6145  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
6146  for (int bin=0; bin<nbins; bin++){
6147  xexyey[0][bin] = hvar->GetBinContent(bin+1);
6148  xexyey[1][bin] = hvar->GetBinError(bin+1);
6149 
6150  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
6151  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
6152  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
6153  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
6154  xexyey[2][bin] = log10(xexyey[2][bin]);
6155  }
6156 
6157 
6158  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
6159  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
6160  foutput->WriteTObject(tg);
6161  delete tg;
6162 
6163  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
6164  foutput->WriteTObject(hmesq_conserveDifermMass);
6165  foutput->WriteTObject(hvar);
6166  if (writeFinalTree) foutput->WriteTObject(newtree);
6167  if (writeFinalTree) delete newtree;
6168  delete hmesq_conserveDifermMass;
6169  delete hvar;
6170  delete[] binning;
6171  }
6172  for (unsigned int f=0; f<finputList.size(); f++) finputList.at(f)->Close();
6173  foutput->Close();
6174 }
6175 
6177  const int erg_tev=13;
6178  const float mPOLE=125.;
6179 
6180  TVar::VerbosityLevel verbosity = TVar::ERROR;
6181  Mela mela(erg_tev, mPOLE, verbosity);
6182 
6183  TVar::Process proc = TVar::bkgZZ;
6186 
6187  TString strproc = ProcessName(proc);
6188  TString strme = MatrixElementName(me);
6189  TString strprod = ProductionName(prod);
6190 
6191  double m1_low=40;
6192  double m2_low=12;
6193 
6194  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s.root", strme.Data(), strprod.Data(), strproc.Data()), "recreate");
6195 
6196  cout << "Acquiring Mela measurables and pdf..." << endl;
6197  RooSpin::modelMeasurables measurables = mela.getMeasurablesRRV();
6198  const double xrange[2]={ max(57., (double)((int)(m1_low+m2_low+0.5))), 15000. };
6199  vector<double> masses;
6200  double massmin=xrange[0];
6201  double mass=massmin;
6202  while (mass<=xrange[1]){
6203  masses.push_back(mass);
6204  double massinc;
6205  if (mass<90.) massinc=1;
6206  else if (mass<122.) massinc=4;
6207  else if (mass<194.) massinc=2;
6208  else if (mass<200.) massinc=3;
6209  else if (mass<600.) massinc=20.;
6210  else if (mass<1500.) massinc=100.;
6211  else if (mass<3000.) massinc=250.;
6212  else if (mass<10000.) massinc=500.;
6213  else massinc=1000.;
6214  mass += massinc;
6215  }
6216  const unsigned int npoints=masses.size();
6217  ((RooRealVar*)measurables.m12)->setRange(xrange[0], xrange[1]);
6218  ((RooRealVar*)measurables.m1)->setRange(m1_low, 120); ((RooRealVar*)measurables.m1)->setVal(m1_low);
6219  ((RooRealVar*)measurables.m2)->setRange(m2_low, 120); ((RooRealVar*)measurables.m2)->setVal(m2_low);
6220  double* xy[2];
6221  for (int i=0; i<2; i++) xy[i] = new double[npoints];
6222 
6223  mela.upFrac_rrv->setVal(1.);
6224  mela.upFrac_rrv->setConstant(true);
6225 
6226  RooAbsPdf* pdf = mela.qqZZmodel;
6227 
6228  pdf->defaultIntegratorConfig()->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D");
6229  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg", 100);;
6230  pdf->defaultIntegratorConfig()->method2D().setLabel("RooAdaptiveGaussKronrodIntegrator2D");
6231  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegrator2D").setRealValue("maxSeg", 100);;
6232  pdf->defaultIntegratorConfig()->methodND().setLabel("RooAdaptiveGaussKronrodIntegratorND");
6233  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegratorND").setRealValue("maxSeg", 100);;
6234  pdf->defaultIntegratorConfig()->setEpsAbs(1e-5);
6235  pdf->defaultIntegratorConfig()->setEpsRel(1e-5);
6236 
6237  cout << "Computing pdf integral as a function of " << measurables.m12->GetName() << endl;
6238  RooRealIntegral* pdfInt = new RooRealIntegral(
6239  "pdfInt", "", *pdf,
6240  RooArgSet(
6241  *measurables.h1, *measurables.h2, *measurables.Phi,
6242  *measurables.hs, *measurables.Phi1,
6243  *measurables.m1, *measurables.m2/*,
6244  *measurables.Y*/
6245  )
6246  );
6247  pdfInt->Print("v");
6248  for (unsigned int ix=0; ix<npoints; ix++){
6249  xy[0][ix]=masses.at(ix);
6250  ((RooRealVar*)measurables.m12)->setVal(xy[0][ix]);
6251  xy[1][ix]=pdfInt->getVal();
6252  cout << "pdfInt(" << xy[0][ix] << ") = " << xy[1][ix] << endl;
6253  xy[1][ix] = log10(xy[1][ix]);
6254  }
6255  TGraph* tg = new TGraph(npoints, xy[0], xy[1]);
6256  tg->SetName("tg_anaPdfInt");
6257 
6258  foutput->WriteTObject(tg);
6259  delete tg;
6260  delete pdfInt;
6261  for (int i=0; i<2; i++) delete[] xy[i];
6262 
6263  foutput->Close();
6264 }
6265 
6267  const int erg_tev=13;
6268  const float mPOLE=125.;
6269 
6270  TVar::VerbosityLevel verbosity = TVar::ERROR;
6271  Mela mela(erg_tev, mPOLE, verbosity);
6272 
6276 
6277  TString strproc = ProcessName(proc);
6278  TString strme = MatrixElementName(me);
6279  TString strprod = ProductionName(prod);
6280 
6281  double m1_low=40;
6282  double m2_low=12;
6283 
6284  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s.root", strme.Data(), strprod.Data(), strproc.Data()), "recreate");
6285 
6286  cout << "Acquiring Mela measurables and pdf..." << endl;
6287  RooSpin::modelMeasurables measurables = mela.getMeasurablesRRV();
6288  const double xrange[2]={ max(57., (double)((int)(m1_low+m2_low+0.5))), 15000. };
6289  vector<double> masses;
6290  double massmin=xrange[0];
6291  double mass=massmin;
6292  while (mass<=xrange[1]){
6293  masses.push_back(mass);
6294  double massinc;
6295  if (mass<90.) massinc=1;
6296  else if (mass<194.) massinc=2;
6297  else if (mass<200.) massinc=3;
6298  else if (mass<600.) massinc=20.;
6299  else if (mass<1500.) massinc=100.;
6300  else if (mass<3000.) massinc=250.;
6301  else if (mass<10000.) massinc=500.;
6302  else massinc=1000.;
6303  mass += massinc;
6304  }
6305  const unsigned int npoints=masses.size();
6306  ((RooRealVar*)measurables.m12)->setRange(xrange[0], xrange[1]);
6307  ((RooRealVar*)measurables.m1)->setRange(m1_low, 120); ((RooRealVar*)measurables.m1)->setVal(m1_low);
6308  ((RooRealVar*)measurables.m2)->setRange(m2_low, 120); ((RooRealVar*)measurables.m2)->setVal(m2_low);
6309  double* xy[2];
6310  for (int i=0; i<2; i++) xy[i] = new double[npoints];
6311 
6312  mela.upFrac_rrv->setVal(1.);
6313  mela.upFrac_rrv->setConstant(true);
6314 
6315  RooAbsPdf* pdf = mela.ggSpin0Model->getPDF();
6316  pdf->defaultIntegratorConfig()->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D");
6317  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg", 100);;
6318  pdf->defaultIntegratorConfig()->method2D().setLabel("RooAdaptiveGaussKronrodIntegrator2D");
6319  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegrator2D").setRealValue("maxSeg", 100);;
6320  pdf->defaultIntegratorConfig()->methodND().setLabel("RooAdaptiveGaussKronrodIntegratorND");
6321  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegratorND").setRealValue("maxSeg", 100);;
6322  pdf->defaultIntegratorConfig()->setEpsAbs(1e-5);
6323  pdf->defaultIntegratorConfig()->setEpsRel(1e-5);
6324 
6325  cout << "Computing pdf integral as a function of " << measurables.m12->GetName() << endl;
6326  RooRealIntegral* pdfInt = new RooRealIntegral(
6327  "pdfInt", "", *pdf,
6328  RooArgSet(
6329  *measurables.h1, *measurables.h2, *measurables.Phi,
6330  *measurables.hs, *measurables.Phi1,
6331  *measurables.m1, *measurables.m2/*,
6332  *measurables.Y*/
6333  )
6334  );
6335  pdfInt->Print("v");
6336  for (unsigned int ix=0; ix<npoints; ix++){
6337  xy[0][ix]=masses.at(ix);
6338  ((RooRealVar*)measurables.m12)->setVal(xy[0][ix]);
6339  xy[1][ix]=pdfInt->getVal();
6340  cout << "pdfInt(" << xy[0][ix] << ") = " << xy[1][ix] << endl;
6341  xy[1][ix] = log10(xy[1][ix]);
6342  }
6343  TGraph* tg = new TGraph(npoints, xy[0], xy[1]);
6344  tg->SetName("tg_anaPdfInt");
6345 
6346  foutput->WriteTObject(tg);
6347  delete tg;
6348  delete pdfInt;
6349  for (int i=0; i<2; i++) delete[] xy[i];
6350 
6351  foutput->Close();
6352 }
6353 
6355  if (!(strprod == "Had_ZH" || strprod == "Had_WH")) return;
6356  int erg_tev=sqrts;
6357  float mPOLE=125.;
6358  float mVPOLE;
6359 
6360  TVar::VerbosityLevel verbosity = TVar::ERROR;
6361  Mela mela(erg_tev, mPOLE, verbosity);
6362 
6366  for (int iprod=(int)TVar::JJVBF; iprod<(int)TVar::nProductions; iprod++){
6367  prod = (TVar::Production)iprod;
6368  if (TVar::ProductionName(prod)==strprod) break;
6369  }
6370  RooSpin::VdecayType pmode, dmode;
6371  if (prod==TVar::Had_ZH){
6373  dmode=pmode;
6374  mVPOLE=mela.getPrimaryMass(23);
6375  }
6376  else{
6378  dmode=pmode;
6379  mVPOLE=mela.getPrimaryMass(24);
6380  }
6381 
6382  TString strproc = ProcessName(proc);
6383  TString strme = MatrixElementName(me);
6384 
6385 
6386  cout << "Acquiring measurables and pdf..." << endl;
6387  const double xrange[2]={ 57, sqrts*1000.-1. };
6388  RooRealVar* m12 = new RooRealVar("m12", "m_{H} (GeV)", mPOLE, xrange[0], xrange[1]);
6389  RooRealVar* m1 = new RooRealVar("m1", "m1", 460, (mVPOLE)-10., sqrts*1000.);
6390  RooRealVar* m2 = new RooRealVar("m2", "m2", mVPOLE, mVPOLE-0.05, mVPOLE+0.05);
6391  RooRealVar* h1 = new RooRealVar("h1", "cos#theta_{V*}", -1, 1);
6392  RooRealVar* h2 = new RooRealVar("h2", "cos#theta_{V}", -1, 1);
6393  RooRealVar* Phi1 = new RooRealVar("Phi1", "#Phi_{V*}", -TMath::Pi(), TMath::Pi());
6394  RooRealVar* hs = new RooRealVar("hs", "cos#theta^{*}", -1, 1);
6395  RooRealVar* Phi = new RooRealVar("Phi", "#Phi", -TMath::Pi(), TMath::Pi());
6396  RooRealVar* Y = new RooRealVar("Y", "Y", 0, -4, 4);
6397 
6398  RooSpin::modelMeasurables measurables;
6399  measurables.h1 = h1;
6400  measurables.h2 = h2;
6401  measurables.Phi = Phi;
6402  measurables.m1 = m1;
6403  measurables.m2 = m2;
6404  measurables.m12 = m12;
6405  measurables.hs = hs;
6406  measurables.Phi1 = Phi1;
6407  measurables.Y = Y;
6408 
6409  ScalarPdfFactory_VH fac(measurables, sqrts, pmode, dmode, false);
6410  cout << "m2 is fixed to " << measurables.m2->getVal() << endl;
6411  fac.makeParamsConst(false);
6412  RooRealVar* g1List[8][2];
6413  RooRealVar* g2List[8][2];
6414  //RooRealVar* g3List[8][2];
6415  RooRealVar* g4List[8][2];
6416  for (int gg=0; gg<8; gg++){
6417  for (int im=0; im<2; im++){
6418  g1List[gg][im] = (RooRealVar*)fac.couplings.g1List[gg][im];
6419  g2List[gg][im] = (RooRealVar*)fac.couplings.g2List[gg][im];
6420  //g3List[gg][im] = (RooRealVar*)fac.couplings.g3List[gg][im];
6421  g4List[gg][im] = (RooRealVar*)fac.couplings.g4List[gg][im];
6422  }
6423  }
6424  g1List[0][0]->setVal(1);
6425 
6426  TFile* foutput = TFile::Open(Form("pAvgLinToLog_%s_%s_%s_%iTeV.root", strme.Data(), strprod.Data(), strproc.Data(), sqrts), "recreate");
6427 
6428  m12->setConstant(false);
6429  vector<double> masses;
6430  double massmin=xrange[0];
6431  double mass=massmin;
6432  while (mass<=xrange[1]){
6433  masses.push_back(mass);
6434  double massinc;
6435  if (mass<90.) massinc=1;
6436  else if (mass<194.) massinc=2;
6437  else if (mass<200.) massinc=3;
6438  else if (mass<600.) massinc=20.;
6439  else if (mass<1500.) massinc=100.;
6440  else if (mass<3000.) massinc=250.;
6441  else if (mass<10000.) massinc=500.;
6442  else massinc=1000.;
6443  mass += massinc;
6444  }
6445  const unsigned int npoints=masses.size();
6446  m12->setRange(xrange[0], xrange[1]);
6447 
6448  double* xy[2];
6449  for (int i=0; i<2; i++) xy[i] = new double[npoints];
6450 
6451  RooAbsPdf* pdf = fac.getPDF();
6452  /*
6453  pdf->defaultIntegratorConfig()->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D");
6454  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg", 100);;
6455  pdf->defaultIntegratorConfig()->method2D().setLabel("RooAdaptiveGaussKronrodIntegrator2D");
6456  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegrator2D").setRealValue("maxSeg", 100);;
6457  pdf->defaultIntegratorConfig()->methodND().setLabel("RooAdaptiveGaussKronrodIntegratorND");
6458  pdf->defaultIntegratorConfig()->getConfigSection("RooAdaptiveGaussKronrodIntegratorND").setRealValue("maxSeg", 100);;
6459  pdf->defaultIntegratorConfig()->setEpsAbs(1e-5);
6460  pdf->defaultIntegratorConfig()->setEpsRel(1e-5);
6461  */
6462 
6463  cout << "Computing pdf integral as a function of " << measurables.m12->GetName() << endl;
6464  for (unsigned int ix=0; ix<npoints; ix++){
6465  xy[0][ix]=masses.at(ix);
6466  m12->setVal(xy[0][ix]);
6467  ((RooRealVar*)fac.parameters.mX)->setVal(m12->getVal());
6468  ((RooRealVar*)fac.parameters.gamX)->setVal(1./m12->getVal());
6469 
6470  float m1_low = xy[0][ix] + measurables.m2->getVal();
6471  m1->setRange(m1_low, 1000.*sqrts);
6472  m1->setVal((m1->getMin()+m1->getMax())/2.);
6473  cout << "m1 min, max, val = " << m1->getMin() << " " << m1->getMax() << " " << m1->getVal() << endl;
6474 
6475  RooRealIntegral* pdfInt = new RooRealIntegral(
6476  "pdfInt", "", *pdf,
6477  RooArgSet(
6478  *measurables.h1, *measurables.h2, *measurables.Phi,
6479  *measurables.hs, *measurables.Phi1//,
6480  //*measurables.m1//,
6482  //*measurables.Y
6483  )
6484  );
6485  if (ix==0) pdfInt->Print("v");
6486  xy[1][ix]=pdfInt->getVal();
6487  delete pdfInt;
6488 
6489  cout << "pdfInt(" << xy[0][ix] << ") = " << xy[1][ix] << endl;
6490  xy[1][ix] = log10(xy[1][ix]);
6491  }
6492  TGraph* tg = new TGraph(npoints, xy[0], xy[1]);
6493  tg->SetName("tg_anaPdfInt");
6494 
6495  foutput->WriteTObject(tg);
6496  delete tg;
6497  for (int i=0; i<2; i++) delete[] xy[i];
6498 
6499  foutput->Close();
6500 
6501  delete m12;
6502  delete m1;
6503  delete m2;
6504  delete hs;
6505  delete h1;
6506  delete h2;
6507  delete Phi;
6508  delete Phi1;
6509  delete Y;
6510 }
6511 
6512 
6513 /*
6514 SPECIFIC COMMENT: OUTPUT ME DIVIDED BY
6515 - (aL1**2+aR1**2)*(aL2**2+aR2**2) TO REMAIN INDEPENDENT OF CHANNEL
6516 */
6518  int erg_tev=13;
6519  float mPOLE=125.;
6520  TString TREE_NAME = "ZZTree/candTree";
6521  const bool writeFinalTree=false;
6522 
6523  TVar::VerbosityLevel verbosity = TVar::ERROR;
6524  Mela mela(erg_tev, mPOLE, verbosity);
6525 
6526  float mesq_conserveDifermMass=0;
6527  float mzz;
6528  float m1;
6529  float m2;
6530  float h1;
6531  float h2;
6532  float phi;
6533  float hs;
6534  float phi1;
6535  vector<float>* mzz_array=0;
6536  vector<float>* m1_array=0;
6537  vector<float>* m2_array=0;
6538  vector<float>* h1_array=0;
6539  vector<float>* h2_array=0;
6540  vector<float>* phi_array=0;
6541  vector<float>* hs_array=0;
6542  vector<float>* phi1_array=0;
6543  vector<short>* ZZsel=0;
6544  vector<short>* ZZCandType=0;
6545  int LepID[4]={ 0, 0, 11, -11 };
6546 
6547  TString cinput_main = "/scratch0/hep/usarical/CJLST/LHC_13TeV/2l2q/";
6548  const int nSamples = 2;
6549  TString strSamples[nSamples]={
6550  "ZZ2l2qAnalysis_DY2JetsToLL.root",
6551  "ZZ2l2qAnalysis_DYJetsToLL.root"
6552  };
6553 
6554  TFile* foutput = new TFile("pAvgLinToLog_MCFM_JJQCD_bkgZJets_13TeV_2l2q.root", "recreate");
6555 
6556  gROOT->cd();
6557  TChain* tree = new TChain(TREE_NAME, "");
6558  for (int is=0; is<nSamples; is++) tree->Add(Form("%s/%s", cinput_main.Data(), (strSamples[is]).Data()));
6559  tree->SetBranchAddress("ZZCandType", &ZZCandType);
6560  tree->SetBranchAddress("ZZsel", &ZZsel);
6561  tree->SetBranchAddress("ZZMass", &mzz_array);
6562  tree->SetBranchAddress("Z1Mass", &m1_array);
6563  tree->SetBranchAddress("Z2Mass", &m2_array);
6564  tree->SetBranchAddress("helcosthetaZ1", &h1_array);
6565  tree->SetBranchAddress("helcosthetaZ2", &h2_array);
6566  tree->SetBranchAddress("helphi", &phi_array);
6567  tree->SetBranchAddress("costhetastar", &hs_array);
6568  tree->SetBranchAddress("phistarZ1", &phi1_array);
6569 
6570  int nTotalEntries = tree->GetEntries();
6571 
6572  TTree* tmptree = new TTree("IntermediateTree", "");
6573  tmptree->Branch("ZZMass", &mzz);
6574  tmptree->Branch("Z1Mass", &m1);
6575  tmptree->Branch("Z2Mass", &m2);
6576  tmptree->Branch("helcosthetaZ1", &h1);
6577  tmptree->Branch("helcosthetaZ2", &h2);
6578  tmptree->Branch("helphi", &phi);
6579  tmptree->Branch("costhetastar", &hs);
6580  tmptree->Branch("phistarZ1", &phi1);
6581 
6582  unsigned int ctr=0;
6583  cout << "Nrawentries = " << nTotalEntries << endl;
6584  for (int ev = 0; ev < nTotalEntries; ev++){
6585  tree->GetEntry(ev);
6586  for (unsigned int j=0; j < ZZsel->size(); j++){
6587  if (ZZsel->at(j)>=90 && (ZZCandType->at(j)==2 || ZZCandType->at(j)==1)){
6588  if (ctr%100==0) cout << "Event " << ctr << " being recorded at actual event " << ev << "." << endl;
6589  mzz=mzz_array->at(j);
6590  m1=m1_array->at(j);
6591  h1=h1_array->at(j);
6592  m2=m2_array->at(j);
6593  h2=h2_array->at(j);
6594  hs=hs_array->at(j);
6595  phi=phi_array->at(j);
6596  phi1=phi1_array->at(j);
6597  tmptree->Fill();
6598  ctr++;
6599  }
6600  }
6601  }
6602 
6603  nTotalEntries=tmptree->GetEntries();
6604  cout << "Ntotalentries = " << nTotalEntries << endl;
6605 
6606  TTree* tmptree2 = new TTree("IntermediateTree2", "");
6607  tmptree2->Branch("ZZMass", &mzz);
6608  tmptree2->Branch("Z1Mass", &m1);
6609  tmptree2->Branch("Z2Mass", &m2);
6610  tmptree2->Branch("helcosthetaZ1", &h1);
6611  tmptree2->Branch("helcosthetaZ2", &h2);
6612  tmptree2->Branch("helphi", &phi);
6613  tmptree2->Branch("costhetastar", &hs);
6614  tmptree2->Branch("phistarZ1", &phi1);
6615 
6616  TRandom3 randomthrow(1234567);
6617  double portion_to_keep = 1;
6618  if (nTotalEntries>1000000) portion_to_keep = 9.95e5/nTotalEntries;
6619  for (int ev = 0; ev < nTotalEntries; ev++){
6620  tmptree->GetEntry(ev);
6621  double rndnum = randomthrow.Uniform();
6622  if (rndnum<=portion_to_keep) tmptree2->Fill();
6623  }
6624  delete tmptree;
6625  tmptree=tmptree2;
6626 
6627  const int nEntries = tmptree->GetEntries();
6628  if (nEntries>=1000000){
6629  cerr << "TMath::Sort will experience problems. Aborting!" << endl;
6630  delete tmptree;
6631  delete tree;
6632  assert(0);
6633  }
6634  int* index = new int[nEntries];
6635  tmptree->Draw("ZZMass", "", "goff");
6636  TMath::Sort(nEntries, tmptree->GetV1(), index, false);
6637 
6638  tmptree->GetEntry(index[0]);
6639  float firstVal=mzz;
6640  tmptree->GetEntry(index[nEntries-1]);
6641  float lastVal=mzz;
6642  float infimum = (float)((int)firstVal); infimum -= (float)(((int)infimum)%10);
6643  float supremum = (float)((int)(lastVal+0.5)); supremum += (float)(10-((int)supremum)%10);
6644  cout << "Nentries = " << nEntries << " | mzz = " << firstVal << " - " << lastVal << "(" << infimum << ", " << supremum << ")" << endl;
6645 
6646  int nbins=0;
6647  int divisor=11000;
6648  while (nbins<50){
6649  if (divisor>1000) divisor -= 1000;
6650  else if (divisor>100) divisor -= 100;
6651  else break;
6652  nbins=nEntries/divisor+1;
6653  }
6654  cout << "nbins=" << nbins << endl;
6655  if (nbins<3) cerr << "Not enough bins!" << endl;
6656  float* binning = new float[nbins+1];
6657  binning[0]=infimum;
6658  binning[nbins]=supremum;
6659  int ev_stepsize = nEntries/nbins;
6660  cout << "Event step size: " << ev_stepsize << endl;
6661  cout << "Boundary (" << 0 << ") = " << binning[0] << endl;
6662  for (int ix=1; ix<nbins; ix++){
6663  int ev = index[ix*ev_stepsize];
6664  tmptree->GetEntry(ev);
6665  float bhigh = mzz;
6666  ev = index[ix*ev_stepsize-1];
6667  float blow = mzz;
6668  binning[ix]=(bhigh+blow)*0.5;
6669  cout << "Boundary (" << ix << ")= " << binning[ix] << " [event " << ev << ", step " << ix*ev_stepsize << "]" << endl;
6670  }
6671  cout << "Boundary (" << nbins << ") = " << binning[nbins] << endl;
6672  delete[] index;
6673 
6674  foutput->cd();
6675  TProfile* hvar = new TProfile("candMass", "", nbins, binning); hvar->Sumw2();
6676  TProfile* hmesq_conserveDifermMass = new TProfile("P_ConserveDifermionMass", "", nbins, binning); hmesq_conserveDifermMass->Sumw2();
6677 
6678  TTree* newtree=0;
6679  if (writeFinalTree){
6680  newtree = new TTree("FinalTree", "");
6681  newtree->Branch("mesq_conserveDifermMass", &mesq_conserveDifermMass);
6682  newtree->Branch("ZZMass", &mzz);
6683  }
6684 
6685  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
6686 
6687  for (int ev = 0; ev < nEntries; ev++){
6688  tmptree->GetEntry(ev); // No need for ordering anymore
6689  if (ev%10000==0) cout << "Doing event " << ev << endl;
6690 
6691  TLorentzVector pDaughters[4];
6692  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
6693  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); }
6695  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
6696  mela.setInputEvent(&daughters, (SimpleParticleCollection_t*)0, (SimpleParticleCollection_t*)0, false);
6697 
6698  //double alphasVal;
6699  bool doFill=true;
6700  mela.setProcess(TVar::bkgZJets, TVar::MCFM, TVar::JJQCD);
6701 
6703  mela.computeP(mesq_conserveDifermMass, false);
6704  double aL1, aR1, aL2, aR2;
6705  mela.getIORecord()->getVDaughterCouplings(aL1, aR1, 0);
6706  mela.getIORecord()->getVDaughterCouplings(aL2, aR2, 1);
6707  if (fabs(aL1)>0. || fabs(aR1)>0.) mesq_conserveDifermMass /= pow(aL1, 2)+pow(aR1, 2);
6708  if (fabs(aL2)>0. || fabs(aR2)>0.) mesq_conserveDifermMass /= pow(aL2, 2)+pow(aR2, 2);
6709  cout << "aL1: " << aL1 << '\t';
6710  cout << "aR1: " << aR1 << '\t';
6711  cout << "aL2: " << aL2 << '\t';
6712  cout << "aR2: " << aR2 << endl;
6713  //mesq_conserveDifermMass = log10(mesq_conserveDifermMass);
6714  if (isnan(mesq_conserveDifermMass) || isinf(mesq_conserveDifermMass)) doFill=false;
6715 
6716  if (doFill){
6717  hmesq_conserveDifermMass->Fill(mzz, mesq_conserveDifermMass);
6718  hvar->Fill(mzz, mzz);
6719  }
6720 
6721  if (writeFinalTree) newtree->Fill();
6722  mela.resetInputEvent();
6723  }
6724 
6725  double* xexyey[4];
6726  for (int ix=0; ix<4; ix++) xexyey[ix] = new double[nbins];
6727  for (int bin=0; bin<nbins; bin++){
6728  xexyey[0][bin] = hvar->GetBinContent(bin+1);
6729  xexyey[1][bin] = hvar->GetBinError(bin+1);
6730 
6731  cout << "Bin " << bin << " x-center: " << xexyey[0][bin] << " +- " << xexyey[1][bin] << endl;
6732  xexyey[2][bin] = hmesq_conserveDifermMass->GetBinContent(bin+1);
6733  xexyey[3][bin] = hmesq_conserveDifermMass->GetBinError(bin+1);
6734  xexyey[3][bin] = log10(xexyey[3][bin])/xexyey[2][bin];
6735  xexyey[2][bin] = log10(xexyey[2][bin]);
6736  }
6737 
6738 
6739  TGraphErrors* tg = new TGraphErrors(nbins, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
6740  tg->SetName(Form("tg_%s", hmesq_conserveDifermMass->GetName()));
6741  foutput->WriteTObject(tg);
6742  delete tg;
6743 
6744  for (int ix=0; ix<4; ix++) delete[] xexyey[ix];
6745  foutput->WriteTObject(hmesq_conserveDifermMass);
6746  foutput->WriteTObject(hvar);
6747  if (writeFinalTree) foutput->WriteTObject(newtree);
6748  if (writeFinalTree) delete newtree;
6749  delete hmesq_conserveDifermMass;
6750  delete hvar;
6751  delete[] binning;
6752  delete tmptree;
6753  delete tree;
6754 
6755  foutput->Close();
6756 }
6757 
6758 /* SPECIFIC COMMENT: Convert a TGraph to a TSpline3 */
6759 TSpline3* convertGraphToSpline3(TGraph* tg, double* dfirst=0, double* dlast=0){
6760  unsigned int nbins = tg->GetN();
6761  double* xy[2]={
6762  tg->GetX(),
6763  tg->GetY()
6764  };
6765  TSpline3* spline = new TSpline3("spline", tg, "b2e2", 0, 0);
6766  spline->SetName(Form("sp_%s", tg->GetName()));
6767  if (dfirst!=0) *dfirst = spline->Derivative(xy[0][0]);
6768  if (dlast!=0) *dlast = spline->Derivative(xy[0][nbins-1]);
6769  return spline;
6770 }
6771 
6772 /* SPECIFIC COMMENT: Convert a TGraph to a TSpline3 */
6773 TSpline3* convertGraphToSpline3_FaithfulSlopes(TGraph* tg, double* dfirst=0, double* dlast=0, bool faithfulLow=true, bool faithfulHigh=true){
6774  if (!faithfulLow&&!faithfulHigh) return convertGraphToSpline3(tg, dfirst, dlast);
6775  else{
6776  unsigned int nbins = tg->GetN();
6777  double* xy[2]={
6778  tg->GetX(),
6779  tg->GetY()
6780  };
6781  double derivative_first = (xy[1][1]-xy[1][0])/(xy[0][1]-xy[0][0]);
6782  double derivative_last = (xy[1][nbins-1]-xy[1][nbins-2])/(xy[0][nbins-1]-xy[0][nbins-2]);
6783  TSpline3* spline;
6784  if (faithfulLow && faithfulHigh) spline = new TSpline3("spline", tg, "b1e1", derivative_first, derivative_last);
6785  else if (!faithfulLow && faithfulHigh) spline = new TSpline3("spline", tg, "b2e1", 0, derivative_last);
6786  else if (faithfulLow && !faithfulHigh) spline = new TSpline3("spline", tg, "b1e2", derivative_first, 0);
6787  else return nullptr; // Should never reach here, just to avoid warnings
6788  spline->SetName(Form("sp_%s", tg->GetName()));
6789  if (dfirst!=0) *dfirst = spline->Derivative(xy[0][0]);
6790  if (dlast!=0) *dlast = spline->Derivative(xy[0][nbins-1]);
6791  return spline;
6792  }
6793 }
6794 /* SPECIFIC COMMENT: Convert a TGraph to a TSpline5 */
6795 TSpline5* convertGraphToSpline5(TGraph* tg, double* dfirst=0, double* dlast=0){
6796  unsigned int nbins = tg->GetN();
6797  double* xy[2]={
6798  tg->GetX(),
6799  tg->GetY()
6800  };
6801  double derivative_first = (xy[1][1]-xy[1][0])/(xy[0][1]-xy[0][0]);
6802  double derivative_last = (xy[1][nbins-1]-xy[1][nbins-2])/(xy[0][nbins-1]-xy[0][nbins-2]);
6803  TSpline5* spline = new TSpline5("spline", tg, "b1e1", derivative_first, derivative_last);
6804  spline->SetName(Form("sp_%s", tg->GetName()));
6805  if (dfirst!=0) *dfirst=derivative_first;
6806  if (dlast!=0) *dlast=derivative_last;
6807  return spline;
6808 }
6809 TSpline3* convertTSpline5ToTspline3(TSpline5* sp){
6810  double xmin = sp->GetXmin();
6811  double xmax = sp->GetXmax();
6812  const int nbins=500;
6813  double xyval[2][nbins+1];
6814  double interval = (xmax-xmin)/nbins;
6815  for (int ix=0; ix<=nbins; ix++){
6816  xyval[0][ix] = xmin + ix*interval;
6817  xyval[1][ix] = sp->Eval(xyval[0][ix]);
6818  }
6819  double dfirst = sp->Derivative(xmin);
6820  double dlast = sp->Derivative(xmax);
6821  TSpline3* sp_new = new TSpline3("spline", xyval[0], xyval[1], nbins+1, "b1e1", dfirst, dlast);
6822  sp_new->SetName(sp->GetName());
6823  sp_new->SetTitle(sp->GetTitle());
6824  return sp_new;
6825 }
6826 
6827 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0+a1*exp(x) */
6828 TF1* getFcn_a0(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6829  double x, y, s;
6830  if (useLowBound) x = sp->GetXmin();
6831  else x = sp->GetXmax();
6832  y = sp->Eval(x);
6833 
6834  double a0;
6835  a0 = y;
6836 
6837  TString fcnName;
6838  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
6839  else fcnName = Form("highFcn_%s", sp->GetName());
6840  TF1* fcn = new TF1(fcnName, "[0]", xmin, xmax);
6841  fcn->SetParameter(0, a0);
6842 
6843  return fcn;
6844 }
6845 
6846 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0+a1*exp(x) */
6847 TF1* getFcn_a0plusa1expX(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6848  double x, y, s;
6849  if (useLowBound) x = sp->GetXmin();
6850  else x = sp->GetXmax();
6851  y = sp->Eval(x);
6852  s = sp->Derivative(x);
6853 
6854  double a0, a1;
6855  a0 = y-s;
6856  a1 = s*exp(-x);
6857 
6858  TString fcnName;
6859  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
6860  else fcnName = Form("highFcn_%s", sp->GetName());
6861  TF1* fcn = new TF1(fcnName, "[0]+[1]*exp(x)", xmin, xmax);
6862  fcn->SetParameter(0, a0);
6863  fcn->SetParameter(1, a1);
6864 
6865  return fcn;
6866 }
6867 
6868 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0+a1/x */
6869 TF1* getFcn_a0plusa1overX(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6870  double x, y, s;
6871  if (useLowBound) x = sp->GetXmin();
6872  else x = sp->GetXmax();
6873  y = sp->Eval(x);
6874  s = sp->Derivative(x);
6875 
6876  double a0, a1;
6877  a0 = y+s*x;
6878  a1 = -s*pow(x, 2);
6879 
6880  TString fcnName;
6881  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
6882  else fcnName = Form("highFcn_%s", sp->GetName());
6883  TF1* fcn = new TF1(fcnName, "[0]+[1]/x", xmin, xmax);
6884  fcn->SetParameter(0, a0);
6885  fcn->SetParameter(1, a1);
6886 
6887  return fcn;
6888 }
6889 
6890 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0/x**2-a1/x */
6891 TF1* getFcn_a0overX2minusa1overX(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6892  double x, y, s;
6893  if (useLowBound) x = sp->GetXmin();
6894  else x = sp->GetXmax();
6895  y = sp->Eval(x);
6896  s = sp->Derivative(x);
6897 
6898  double a0, a1;
6899  a0 = -y*pow(x, 2)-s*pow(x, 3);
6900  a1 = -2.*y*x-s*pow(x, 2);
6901 
6902  TString fcnName;
6903  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
6904  else fcnName = Form("highFcn_%s", sp->GetName());
6905  TF1* fcn = new TF1(fcnName, "[0]/x/x-[1]/x", xmin, xmax);
6906  fcn->SetParameter(0, a0);
6907  fcn->SetParameter(1, a1);
6908 
6909  return fcn;
6910 }
6911 
6912 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0+a1/x-(a1/x)**2 */
6913 TF1* getFcn_a0plusXPinvminusXpsqinv(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6914  double x, y, s;
6915  if (useLowBound) x = sp->GetXmin();
6916  else x = sp->GetXmax();
6917  y = sp->Eval(x);
6918  s = sp->Derivative(x);
6919 
6920  double a0, a1;
6921  double disc = 1.+8.*x*s;
6922  if (disc>0.){
6923  a1 = x/4.*(1.+sqrt(disc));
6924  a0 = y-a1/x+pow(a1/x, 2);
6925  cout << x << '\t' << a0 << '\t' << a1 << '\t' << s << '\t' << y << endl;
6926 
6927  TString fcnName;
6928  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
6929  else fcnName = Form("highFcn_%s", sp->GetName());
6930  TF1* fcn = new TF1(fcnName, "[0]+[1]/x-pow([1]/x, 2)", xmin, xmax);
6931  fcn->SetParameter(0, a0);
6932  fcn->SetParameter(1, a1);
6933  return fcn;
6934  }
6935  else return getFcn_a0plusa1overX(sp, xmin, xmax, useLowBound);
6936 }
6937 
6938 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0+a1*x */
6939 TF1* getFcn_a0plusa1timesX(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6940  double x, y, s;
6941  if (useLowBound) x = sp->GetXmin();
6942  else x = sp->GetXmax();
6943  y = sp->Eval(x);
6944  s = sp->Derivative(x);
6945 
6946  double a0, a1;
6947  a0 = y-s*x;
6948  a1 = s;
6949 
6950  TString fcnName;
6951  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
6952  else fcnName = Form("highFcn_%s", sp->GetName());
6953  TF1* fcn = new TF1(fcnName, "[0]+[1]*x", xmin, xmax);
6954  fcn->SetParameter(0, a0);
6955  fcn->SetParameter(1, a1);
6956 
6957  return fcn;
6958 }
6959 
6960 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0+a1*x+a2*x**2 */
6961 TF1* getFcn_a0plusa1timesXplusa2overX2(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6962  double x, y, s, d;
6963  double xh, sh;
6964  if (useLowBound){
6965  x = sp->GetXmin();
6966  xh = x+0.001;
6967  }
6968  else{
6969  x = sp->GetXmax();
6970  xh = x-0.001;
6971  }
6972  y = sp->Eval(x);
6973  s = sp->Derivative(x);
6974  sh = sp->Derivative(xh);
6975  d = (sh-s)/(xh-x);
6976 
6977  double a0, a1, a2;
6978  a2 = d*pow(x, 4)/6.;
6979  a1 = s+2.*a2/pow(x, 3);
6980  a0 = y-a1*x-a2/pow(x, 2);
6981 
6982  TString fcnName;
6983  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
6984  else fcnName = Form("highFcn_%s", sp->GetName());
6985  TF1* fcn = new TF1(fcnName, "[0]+[1]*x+[2]/pow(x, 2)", xmin, xmax);
6986  fcn->SetParameter(0, a0);
6987  fcn->SetParameter(1, a1);
6988  fcn->SetParameter(2, a2);
6989 
6990  return fcn;
6991 }
6992 
6993 /* SPECIFIC COMMENT: Get a1 and a2 as well as a TF1 object for the formula a0+a1*x+a2/x**2 */
6994 TF1* getFcn_a0plusa1timesXplusa2timesX2(TSpline3* sp, double xmin, double xmax, bool useLowBound){
6995  double x, y, s, d;
6996  double xh, sh;
6997  if (useLowBound){
6998  x = sp->GetXmin();
6999  xh = x+0.001;
7000  }
7001  else{
7002  x = sp->GetXmax();
7003  xh = x-0.001;
7004  }
7005  y = sp->Eval(x);
7006  s = sp->Derivative(x);
7007  sh = sp->Derivative(xh);
7008  d = (sh-s)/(xh-x);
7009 
7010  double a0, a1, a2;
7011  a2 = d/2.;
7012  a1 = s-2.*a2*x;
7013  a0 = y-a1*x-a2*pow(x, 2);
7014 
7015  TString fcnName;
7016  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
7017  else fcnName = Form("highFcn_%s", sp->GetName());
7018  TF1* fcn = new TF1(fcnName, "[0]+[1]*x+[2]*pow(x, 2)", xmin, xmax);
7019  fcn->SetParameter(0, a0);
7020  fcn->SetParameter(1, a1);
7021  fcn->SetParameter(2, a2);
7022 
7023  return fcn;
7024 }
7025 
7026 /* SPECIFIC COMMENT: Get external input added by a0 and multiplied by a1 */
7027 TGraph* getPatch_a0a1(TSpline3* sp, TSpline3* sppatch, double xmin, double xmax, bool useLowBound, bool forceOutput=true){
7028  double x, y, s;
7029  double y_patch, s_patch;
7030  if (useLowBound) x = sp->GetXmin();
7031  else x = sp->GetXmax();
7032  y = sp->Eval(x);
7033  s = sp->Derivative(x);
7034  y_patch = sppatch->Eval(x);
7035  s_patch = sppatch->Derivative(x);
7036 
7037  double a0, a1;
7038  a1 = s/s_patch;
7039  a0 = y-a1*y_patch;
7040 
7041  int n = (xmax-xmin+0.5);
7042  cout << "Patcher n: " << n << endl;
7043  double xwidth = (xmax-xmin)/n;
7044  double* xy[2];
7045  for (unsigned int i=0; i<2; i++) xy[i]=new double[n+1];
7046  for (int ip=0; ip<=n; ip++){
7047  double xval = xmin+xwidth*ip;
7048  double yval = sppatch->Eval(xval);
7049  yval = a0+a1*yval;
7050  xy[0][ip]=xval;
7051  xy[1][ip]=yval;
7052  }
7053  TString fcnName;
7054  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
7055  else fcnName = Form("highFcn_%s", sp->GetName());
7056  TGraph* fcn = new TGraph(n+1, xy[0], xy[1]);
7057  fcn->SetName(fcnName);
7058 
7059  for (unsigned int i=0; i<2; i++) delete[] xy[i];
7060  return fcn;
7061 }
7062 
7063 /* SPECIFIC COMMENT: Get external input added by a0+a1/x */
7064 TGraph* getPatch_a0plusa1overX(TSpline3* sp, TSpline3* sppatch, double xmin, double xmax, bool useLowBound, bool forceOutput=true){
7065  double x, y, s;
7066  if (useLowBound) x = sp->GetXmin();
7067  else x = sp->GetXmax();
7068  y = sp->Eval(x);
7069  s = sp->Derivative(x);
7070  y -= sppatch->Eval(x);
7071  s -= sppatch->Derivative(x);
7072 
7073  double a0, a1;
7074  a0 = y+s*x;
7075  a1 = -s*pow(x, 2);
7076 
7077  cout << "getPatch_a0plusa1overX: Checking if patch will flip: " << endl;
7078  double xext;
7079  if (useLowBound) xext = x;
7080  else xext = x;
7081  if (1.-a1/pow(xext, 2)/sppatch->Derivative(xext)<0.){
7082  cout << "Sign flips at " << xext << endl;
7083  if (!forceOutput) return 0;
7084  }
7085  else cout << "Sign does not flip at " << xext << endl;
7086 
7087  int n = (xmax-xmin+0.5);
7088  cout << "Patcher n: " << n << endl;
7089  double xwidth = (xmax-xmin)/n;
7090  double* xy[2];
7091  for (unsigned int i=0; i<2; i++) xy[i]=new double[n+1];
7092  for (int ip=0; ip<=n; ip++){
7093  double xval = xmin+xwidth*ip;
7094  if (xval==0.) xval += xwidth*0.5;
7095  double yval = sppatch->Eval(xval);
7096  yval += (a0+a1/xval);
7097  xy[0][ip]=xval;
7098  xy[1][ip]=yval;
7099  }
7100  TString fcnName;
7101  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
7102  else fcnName = Form("highFcn_%s", sp->GetName());
7103  TGraph* fcn = new TGraph(n+1, xy[0], xy[1]);
7104  fcn->SetName(fcnName);
7105 
7106  for (unsigned int i=0; i<2; i++) delete[] xy[i];
7107  return fcn;
7108 }
7109 
7110 /* SPECIFIC COMMENT: Get external input added by a0+a1*x */
7111 TGraph* getPatch_a0plusa1timesX(TSpline3* sp, TSpline3* sppatch, double xmin, double xmax, bool useLowBound, bool forceOutput=true){
7112  double x, y, s;
7113  if (useLowBound) x = sp->GetXmin();
7114  else x = sp->GetXmax();
7115  y = sp->Eval(x);
7116  s = sp->Derivative(x);
7117  y -= sppatch->Eval(x);
7118  s -= sppatch->Derivative(x);
7119 
7120  double a0, a1;
7121  a1 = s;
7122  a0 = y-a1*x;
7123 
7124  cout << "getPatch_a0plusa1timesX: Checking if patch will flip: " << endl;
7125  double xext;
7126  if (useLowBound) xext = x;
7127  else xext = x;
7128  if (1.+a1/sppatch->Derivative(xext)<0.){
7129  cout << "Sign flips at " << xext << endl;
7130  if (!forceOutput) return 0;
7131  }
7132  else cout << "Sign does not flip at " << xext << endl;
7133 
7134  int n = (xmax-xmin+0.5);
7135  cout << "Patcher n: " << n << endl;
7136  double xwidth = (xmax-xmin)/n;
7137  double* xy[2];
7138  for (unsigned int i=0; i<2; i++) xy[i]=new double[n+1];
7139  for (int ip=0; ip<=n; ip++){
7140  double xval = xmin+xwidth*ip;
7141  double yval = sppatch->Eval(xval);
7142  yval += (a0+a1*xval);
7143  xy[0][ip]=xval;
7144  xy[1][ip]=yval;
7145  }
7146  TString fcnName;
7147  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
7148  else fcnName = Form("highFcn_%s", sp->GetName());
7149  TGraph* fcn = new TGraph(n+1, xy[0], xy[1]);
7150  fcn->SetName(fcnName);
7151 
7152  for (unsigned int i=0; i<2; i++) delete[] xy[i];
7153  return fcn;
7154 }
7155 
7156 /* SPECIFIC COMMENT: Get external input added by a0*exp(a1*x) */
7157 TGraph* getPatch_a0expa1timesX(TSpline3* sp, TSpline3* sppatch, double xmin, double xmax, bool useLowBound){
7158  double x, y, s;
7159  if (useLowBound) x = sp->GetXmin();
7160  else x = sp->GetXmax();
7161  y = sp->Eval(x);
7162  s = sp->Derivative(x);
7163  y -= sppatch->Eval(x);
7164  s -= sppatch->Derivative(x);
7165 
7166  double a0, a1;
7167  a1 = s/y;
7168  a0 = y/exp(a1*x);
7169 
7170  int n = (xmax-xmin+0.5);
7171  cout << "Patcher n: " << n << endl;
7172  double xwidth = (xmax-xmin)/n;
7173  double* xy[2];
7174  for (unsigned int i=0; i<2; i++) xy[i]=new double[n+1];
7175  for (int ip=0; ip<=n; ip++){
7176  double xval = xmin+xwidth*ip;
7177  double yval = sppatch->Eval(xval);
7178  yval += (a0*exp(a1*xval));
7179  xy[0][ip]=xval;
7180  xy[1][ip]=yval;
7181  }
7182  TString fcnName;
7183  if (useLowBound) fcnName = Form("lowFcn_%s", sp->GetName());
7184  else fcnName = Form("highFcn_%s", sp->GetName());
7185  TGraph* fcn = new TGraph(n+1, xy[0], xy[1]);
7186  fcn->SetName(fcnName);
7187 
7188  for (unsigned int i=0; i<2; i++) delete[] xy[i];
7189  return fcn;
7190 }
7191 
7192 /* SPECIFIC COMMENT: THIS FUNCTION TAKES A TGRAPHERRORS AND MODIFIES IT DIRECTLY. THE OPTIONAL STD::VECTOR IS FOR FIXING <P> FOR CERTAIN X-VALUES. */
7193 void regularizeSlice(TGraphErrors* tgSlice, std::vector<double>* fixedX=0, double omitbelow=0., int nIter_=-1, double threshold_ = -1){
7194  unsigned int nbins_slice = tgSlice->GetN();
7195  double* xexyey_slice[4]={
7196  tgSlice->GetX(),
7197  tgSlice->GetEX(),
7198  tgSlice->GetY(),
7199  tgSlice->GetEY()
7200  };
7201 
7202  double* xexyey_linear[4];
7203  for (unsigned int ix=0; ix<4; ix++){
7204  xexyey_linear[ix] = new double[nbins_slice];
7205  for (unsigned int iy=0; iy<nbins_slice; iy++){
7206  if (ix<2) xexyey_linear[ix][iy] = xexyey_slice[ix][iy];
7207  else if (ix==3) xexyey_linear[ix][iy] = exp(xexyey_slice[ix][iy]);
7208  else xexyey_linear[ix][iy] = xexyey_slice[ix][iy]*xexyey_linear[ix-1][iy];
7209  }
7210  }
7211  TGraphErrors* tgSlice_linear = new TGraphErrors(nbins_slice, xexyey_linear[0], xexyey_linear[2], xexyey_linear[1], xexyey_linear[3]);
7212  double integral_in=tgSlice_linear->Integral();
7213  delete tgSlice_linear;
7214  for (unsigned int ix=0; ix<4; ix++) delete[] xexyey_linear[ix];
7215 
7216 
7217  double* xexyey_mod[4];
7218  for (unsigned int ix=0; ix<4; ix++){
7219  xexyey_mod[ix] = new double[nbins_slice];
7220  for (unsigned int iy=0; iy<nbins_slice; iy++) xexyey_mod[ix][iy] = xexyey_slice[ix][iy];
7221  }
7222  unsigned int bin_first = 2, bin_last = nbins_slice-1;
7223 
7224  std::vector<int> fixedBins;
7225  if (fixedX!=0){
7226  for (unsigned int ifx=0; ifx<fixedX->size(); ifx++){
7227  double requestedVal = fixedX->at(ifx);
7228  double distance=1e15;
7229  int bin_to_fix=-1;
7230  for (unsigned int bin=0; bin<nbins_slice; bin++){ if (distance>fabs(xexyey_mod[0][bin]-requestedVal)){ bin_to_fix = bin; distance = fabs(xexyey_mod[0][bin]-requestedVal); } }
7231  if (bin_to_fix>=0) fixedBins.push_back(bin_to_fix);
7232  cout << "Requested to fix bin " << bin_to_fix << endl;
7233  }
7234  }
7235  if (omitbelow>0.){
7236  for (unsigned int bin=0; bin<nbins_slice; bin++){
7237  if (xexyey_mod[0][bin]<omitbelow) fixedBins.push_back(bin);
7238  cout << "Requested to fix bin " << bin << endl;
7239  }
7240  }
7241 
7242  double* xx_second;
7243  double* yy_second;
7244 
7245  int nIter = (nIter_<0 ? 1000 : nIter_);
7246  for (int it=0; it<nIter; it++){
7247  double threshold = (threshold_<0. ? 0.01 : threshold_);
7248  for (unsigned int binIt = bin_first; binIt<=bin_last; binIt++){
7249  bool doFix=false;
7250  for (unsigned int ifx=0; ifx<fixedBins.size(); ifx++){
7251  if ((int)(binIt-1)==fixedBins.at(ifx)){ doFix=true; /*cout << "Iteration " << it << " is fixing bin " << (binIt-1) << endl; */break; }
7252  }
7253  if (doFix) continue;
7254 
7255  int ctr = 0;
7256  int nbins_second = nbins_slice-1;
7257  xx_second = new double[nbins_second];
7258  yy_second = new double[nbins_second];
7259  for (unsigned int bin = 1; bin<=nbins_slice; bin++){
7260  if (bin==binIt) continue;
7261  xx_second[ctr] = xexyey_mod[0][bin-1];
7262  yy_second[ctr] = xexyey_mod[2][bin-1];
7263  ctr++;
7264  }
7265 
7266  TGraph* interpolator = new TGraph(nbins_second, xx_second, yy_second);
7267  double derivative_first = (yy_second[1]-yy_second[0])/(xx_second[1]-xx_second[0]);
7268  double derivative_last = (yy_second[nbins_second-1]-yy_second[nbins_second-2])/(xx_second[nbins_second-1]-xx_second[nbins_second-2]);
7269  TSpline3* spline = new TSpline3("spline", interpolator, "b1e1", derivative_first, derivative_last);
7270 
7271  double center = xexyey_mod[0][binIt-1];
7272  double val = spline->Eval(center);
7273  if (fabs(xexyey_mod[2][binIt-1]-val)>threshold*val) xexyey_mod[2][binIt-1]=val;
7274 
7275  delete spline;
7276  delete interpolator;
7277 
7278  delete[] yy_second;
7279  delete[] xx_second;
7280  }
7281  }
7282 
7283  for (unsigned int ix=0; ix<4; ix++){
7284  xexyey_linear[ix] = new double[nbins_slice];
7285  for (unsigned int iy=0; iy<nbins_slice; iy++){
7286  if (ix<2) xexyey_linear[ix][iy] = xexyey_mod[ix][iy];
7287  else if (ix==3) xexyey_linear[ix][iy] = exp(xexyey_mod[ix][iy]);
7288  else xexyey_linear[ix][iy] = xexyey_mod[ix][iy]*xexyey_linear[ix-1][iy];
7289  }
7290  }
7291  tgSlice_linear = new TGraphErrors(nbins_slice, xexyey_linear[0], xexyey_linear[2], xexyey_linear[1], xexyey_linear[3]);
7292  double integral_out=tgSlice_linear->Integral();
7293  delete tgSlice_linear;
7294  for (unsigned int ix=0; ix<4; ix++) delete[] xexyey_linear[ix];
7295 
7296  double scale = integral_out / integral_in;
7297  for (unsigned int iy=0; iy<nbins_slice; iy++){
7298  xexyey_slice[2][iy] = xexyey_mod[2][iy]*scale;
7299  xexyey_slice[3][iy] *= xexyey_mod[2][iy]/xexyey_slice[2][iy];
7300  }
7301 
7302  for (unsigned int ix=0; ix<4; ix++) delete[] xexyey_mod[ix];
7303 }
7304 
7305 TGraphErrors* removePointsBetween(TGraphErrors* tgSlice, double xmin, double xmax){
7306  const unsigned int nbins_slice = tgSlice->GetN();
7307  double* xexyey_slice[4]={
7308  tgSlice->GetX(),
7309  tgSlice->GetEX(),
7310  tgSlice->GetY(),
7311  tgSlice->GetEY()
7312  };
7313 
7314  double xexyey[4][nbins_slice];
7315  unsigned int ctr=0;
7316  for (unsigned int iy=0; iy<nbins_slice; iy++){
7317  if (xexyey_slice[0][iy]<=xmax && xexyey_slice[0][iy]>=xmin){
7318  cout << "Point to remove with X: " << xexyey_slice[0][iy] << endl;
7319  continue;
7320  }
7321  for (unsigned int ix=0; ix<4; ix++) xexyey[ix][ctr] = xexyey_slice[ix][iy];
7322  cout << "Point " << ctr << " X: " << xexyey[0][ctr] << endl;
7323  ctr++;
7324  }
7325  cout << "removePointsBetween: " << "Starting number of points " << nbins_slice << " final number " << ctr << endl;
7326  TGraphErrors* tgSlice_new = new TGraphErrors(ctr, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
7327  tgSlice_new->SetName(tgSlice->GetName());
7328  tgSlice_new->SetTitle(tgSlice->GetTitle());
7329  return tgSlice_new;
7330 }
7331 
7332 TGraphErrors* replacePointsBetween(TGraphErrors* tgSlice, double xmin, double xmax){
7333  const unsigned int nbins_slice = tgSlice->GetN();
7334  double* xexyey_slice[4]={
7335  tgSlice->GetX(),
7336  tgSlice->GetEX(),
7337  tgSlice->GetY(),
7338  tgSlice->GetEY()
7339  };
7340 
7341  double xexyey[4][nbins_slice];
7342  unsigned int lowbin=0, highbin=0;
7343  for (unsigned int iy=0; iy<nbins_slice; iy++){
7344  if (xexyey_slice[0][iy]<xmin) lowbin=iy;
7345  if (xexyey_slice[0][iy]>xmax){ highbin=iy; break; }
7346  }
7347  cout << "Low bin " << lowbin << " at " << xexyey_slice[0][lowbin] << endl;
7348  cout << "High bin " << highbin << " at " << xexyey_slice[0][highbin] << endl;
7349 
7350  for (unsigned int iy=0; iy<nbins_slice; iy++){
7351  if (xexyey_slice[0][iy]<=xmax && xexyey_slice[0][iy]>=xmin){
7352  for (unsigned int ix=0; ix<4; ix++){
7353  double xlow = xexyey_slice[0][lowbin];
7354  double xhigh = xexyey_slice[0][highbin];
7355  double ylow = xexyey_slice[ix][lowbin];
7356  double yhigh = xexyey_slice[ix][highbin];
7357  double val;
7358  if (ix==0) val = xexyey_slice[0][iy];
7359  else if (ix==2) val = ylow + (yhigh-ylow)/(xhigh-xlow)*(xexyey_slice[0][iy]-xlow);
7360  else val = xexyey_slice[ix][iy]*(xexyey[ix-1][iy]/xexyey_slice[ix-1][iy]);
7361  xexyey[ix][iy] = val;
7362  }
7363  }
7364  else{
7365  for (unsigned int ix=0; ix<4; ix++) xexyey[ix][iy] = xexyey_slice[ix][iy];
7366  }
7367  cout << "Point " << iy << " X: " << xexyey[0][iy] << endl;
7368  }
7369  TGraphErrors* tgSlice_new = new TGraphErrors(nbins_slice, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
7370  tgSlice_new->SetName(tgSlice->GetName());
7371  tgSlice_new->SetTitle(tgSlice->GetTitle());
7372  return tgSlice_new;
7373 }
7374 
7375 void addPoint(TGraphErrors*& tg, double x, double y, double ex, double ey){
7376  TString strname = tg->GetName();
7377  TString strtitle = tg->GetTitle();
7378  TString strxtitle = tg->GetXaxis()->GetTitle();
7379  TString strytitle = tg->GetYaxis()->GetTitle();
7380 
7381  vector<double> xarray;
7382  vector<double> yarray;
7383  vector<double> exarray;
7384  vector<double> eyarray;
7385  xarray.push_back(x);
7386  yarray.push_back(y);
7387  exarray.push_back(ex);
7388  eyarray.push_back(ey);
7389  for (int ip=0; ip<tg->GetN(); ip++){
7390  if (tg->GetX()[ip]!=x){
7391  xarray.push_back(tg->GetX()[ip]);
7392  yarray.push_back(tg->GetY()[ip]);
7393  exarray.push_back(tg->GetEX()[ip]);
7394  eyarray.push_back(tg->GetEY()[ip]);
7395  }
7396  }
7397  vector<pair<double, int>> xorder;
7398  for (unsigned int ip=0; ip<xarray.size(); ip++) addByLowest<double, int>(xorder, xarray.at(ip), ip);
7399 
7400  double* xynew[4];
7401  for (unsigned int i=0; i<4; i++) xynew[i] = new double[xorder.size()];
7402  for (unsigned int ip=0; ip<xarray.size(); ip++){
7403  unsigned int pos = xorder[ip].second;
7404  xynew[0][ip] = xarray[pos];
7405  xynew[1][ip] = yarray[pos];
7406  xynew[2][ip] = exarray[pos];
7407  xynew[3][ip] = eyarray[pos];
7408  }
7409 
7410  delete tg;
7411 
7412  tg = new TGraphErrors(xorder.size(), xynew[0], xynew[1], xynew[2], xynew[3]);
7413  tg->SetName(strname);
7414  tg->SetTitle(strtitle);
7415  tg->GetXaxis()->SetTitle(strxtitle);
7416  tg->GetYaxis()->SetTitle(strytitle);
7417  for (unsigned int i=0; i<4; i++) delete[] xynew[i];
7418 }
7419 
7420 TGraphErrors* addPoint(TGraphErrors* tgSlice, double x){
7421  const unsigned int nbins_slice = tgSlice->GetN();
7422  double* xexyey_slice[4]={
7423  tgSlice->GetX(),
7424  tgSlice->GetEX(),
7425  tgSlice->GetY(),
7426  tgSlice->GetEY()
7427  };
7428 
7429  double xexyey[4][nbins_slice+1];
7430  unsigned int lowbin=0, highbin=0;
7431  for (unsigned int iy=0; iy<nbins_slice; iy++){
7432  if (xexyey_slice[0][iy]<x) lowbin=iy;
7433  if (xexyey_slice[0][iy]>x){ highbin=iy; break; }
7434  }
7435  cout << "Low bin " << lowbin << " at " << xexyey_slice[0][lowbin] << endl;
7436  cout << "High bin " << highbin << " at " << xexyey_slice[0][highbin] << endl;
7437 
7438  int ctr=0;
7439  for (unsigned int iy=0; iy<nbins_slice; iy++){
7440  for (unsigned int ix=0; ix<4; ix++) xexyey[ix][ctr] = xexyey_slice[ix][iy];
7441  ctr++;
7442  if (iy==lowbin){
7443  for (unsigned int ix=0; ix<4; ix++){
7444  double ylow = xexyey_slice[ix][lowbin];
7445  double yhigh = xexyey_slice[ix][highbin];
7446  double val = (yhigh+ylow)*0.5;
7447  xexyey[ix][ctr] = val;
7448  }
7449  ctr++;
7450  }
7451  }
7452  TGraphErrors* tgSlice_new = new TGraphErrors(ctr, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
7453  tgSlice_new->SetName(tgSlice->GetName());
7454  tgSlice_new->SetTitle(tgSlice->GetTitle());
7455  return tgSlice_new;
7456 }
7457 
7458 TGraphErrors* addPointAfterBin(TGraphErrors* tgSlice, int abin){
7459  const unsigned int nbins_slice = tgSlice->GetN();
7460  double* xexyey_slice[4]={
7461  tgSlice->GetX(),
7462  tgSlice->GetEX(),
7463  tgSlice->GetY(),
7464  tgSlice->GetEY()
7465  };
7466 
7467  double xexyey[4][nbins_slice+1];
7468  unsigned int lowbin=abin, highbin=abin+1;
7469  cout << "Low bin " << lowbin << " at " << xexyey_slice[0][lowbin] << endl;
7470  cout << "High bin " << highbin << " at " << xexyey_slice[0][highbin] << endl;
7471 
7472  int ctr=0;
7473  for (unsigned int iy=0; iy<nbins_slice; iy++){
7474  for (unsigned int ix=0; ix<4; ix++) xexyey[ix][ctr] = xexyey_slice[ix][iy];
7475  ctr++;
7476  if (iy==lowbin){
7477  for (unsigned int ix=0; ix<4; ix++){
7478  double ylow = xexyey_slice[ix][lowbin];
7479  double yhigh = xexyey_slice[ix][highbin];
7480  double val = (yhigh+ylow)*0.5;
7481  xexyey[ix][ctr] = val;
7482  }
7483  ctr++;
7484  cout << "Adding additional point at " << xexyey[0][ctr-1] << '\t' << xexyey[2][ctr-1] << endl;
7485  }
7486  }
7487  TGraphErrors* tgSlice_new = new TGraphErrors(ctr, xexyey[0], xexyey[2], xexyey[1], xexyey[3]);
7488  tgSlice_new->SetName(tgSlice->GetName());
7489  tgSlice_new->SetTitle(tgSlice->GetTitle());
7490  return tgSlice_new;
7491 }
7492 
7494  unsigned int npoints;
7495  double xlow;
7496  double xhigh;
7497  PointRedivision(unsigned int n, double xl, double xh) : npoints(n), xlow(xl), xhigh(xh){}
7498 };
7499 
7501  TGraphErrors*& tg, TTree*& tree, TProfile*& pmass,
7502  PointRedivision& rediv,
7503  float& ZZMass, float& mesq, float& weight
7504 ){
7505  cout << "Old set of boundaries: " << endl;
7506  for (int ibin=1; ibin<=pmass->GetNbinsX()+1; ibin++) cout << pmass->GetXaxis()->GetBinLowEdge(ibin) << " ";
7507  cout << endl;
7508 
7509  int binlow=pmass->GetXaxis()->FindBin(rediv.xlow);
7510  int binhigh=pmass->GetXaxis()->FindBin(rediv.xhigh);
7511  float redivlow, redivhigh;
7512  if (binlow==0) binlow=1; redivlow=pmass->GetXaxis()->GetBinLowEdge(binlow);
7513  if (binhigh==0) binhigh=1; redivhigh=pmass->GetXaxis()->GetBinUpEdge(binhigh);
7514  cout << "Rebinning between " << redivlow << " and " << redivhigh << endl;
7515 
7516  vector<float> massvals;
7517  for (int ev=0; ev<tree->GetEntries(); ev++){
7518  tree->GetEntry(ev);
7519  if (ZZMass<redivlow || ZZMass>=redivhigh) continue;
7520  massvals.push_back(ZZMass);
7521  }
7522  std::sort(massvals.begin(), massvals.end());
7523  cout << massvals.size() << " events in this range" << endl;
7524  vector<float> xboundaries;
7525  unsigned int nevts_collected=massvals.size();
7526  addByLowest(xboundaries, redivlow, true);
7527  addByLowest(xboundaries, redivhigh, true);
7528  for (unsigned int ip=1; ip<rediv.npoints; ip++) addByLowest(xboundaries, float((massvals.at(nevts_collected/rediv.npoints*ip) + massvals.at(nevts_collected/rediv.npoints*ip+1))*0.5), true);
7529  cout << "Additional x boundaries: ";
7530  for (unsigned int ip=0; ip<xboundaries.size(); ip++) cout << xboundaries.at(ip) << " ";
7531  cout << endl;
7532 
7533  TProfile* hvar = new TProfile("mass", "", rediv.npoints, xboundaries.data()); hvar->Sumw2();
7534  TProfile* hmesq = new TProfile("mesq", "", rediv.npoints, xboundaries.data()); hmesq->Sumw2();
7535  for (int ev=0; ev<tree->GetEntries(); ev++){
7536  tree->GetEntry(ev);
7537  if (ZZMass<redivlow || ZZMass>=redivhigh) continue;
7538  hvar->Fill(ZZMass, ZZMass, weight);
7539  hmesq->Fill(ZZMass, mesq, weight);
7540  }
7541 
7542  { TGraphErrors* tgnew = removePointsBetween(tg, redivlow, redivhigh); delete tg; tg=tgnew; }
7543  for (int ibin=1; ibin<=hvar->GetNbinsX(); ibin++){
7544  double xadd=hvar->GetBinContent(ibin);
7545  double exadd=hvar->GetBinError(ibin);
7546  double yadd=hmesq->GetBinContent(ibin);
7547  double eyadd=hmesq->GetBinError(ibin);
7548  eyadd=log10(eyadd)/yadd;
7549  yadd=log10(yadd);
7550  addPoint(tg, xadd, yadd, exadd, eyadd);
7551  cout << "Added point " << xadd << ", " << yadd << " for range [ " << hvar->GetXaxis()->GetBinLowEdge(ibin) << ", " << hvar->GetXaxis()->GetBinUpEdge(ibin) << " ]" << endl;
7552  }
7553  delete hmesq;
7554  delete hvar;
7555 
7556  TString pmassname=pmass->GetName();
7557  TString pmasstitle=pmass->GetTitle();
7558  TString pmassxtitle=pmass->GetXaxis()->GetTitle();
7559  TString pmassytitle=pmass->GetYaxis()->GetTitle();
7560  vector<float> pmassboundaries;
7561  for (int ibin=1; ibin<=pmass->GetNbinsX()+1; ibin++){
7562  if (ibin>=binlow && ibin<=binhigh+1) continue;
7563  addByLowest(pmassboundaries, float(pmass->GetXaxis()->GetBinLowEdge(ibin)), true);
7564  }
7565  for (float& bb:xboundaries) addByLowest(pmassboundaries, bb, true);
7566  assert((int) pmassboundaries.size()==tg->GetN()+1);
7567  delete pmass;
7568  pmass = new TProfile(pmassname, pmasstitle, pmassboundaries.size()-1, pmassboundaries.data());
7569  for (int ibin=1; ibin<=tg->GetN(); ibin++){
7570  pmass->SetBinContent(ibin, tg->GetX()[ibin-1]);
7571  pmass->SetBinError(ibin, tg->GetEX()[ibin-1]);
7572  }
7573 
7574  cout << "New set of boundaries: " << endl;
7575  for (int ibin=1; ibin<=pmass->GetNbinsX()+1; ibin++) cout << pmass->GetXaxis()->GetBinLowEdge(ibin) << " ";
7576  cout << endl;
7577 }
7578 
7579 
7580 /* SPECIFIC COMMENT: NONE */
7583  TF1* (*lowf)(TSpline3*, double, double, bool),
7584  TF1* (*highf)(TSpline3*, double, double, bool),
7585  int sqrts=-1,
7586  bool useFaithfulLowSlopes=false,
7587  bool useFaithfulHighSlopes=false,
7588  vector<PointRedivision>* redivision=nullptr
7589 ){
7590  TString strme = MatrixElementName(me);
7591  TString strprod = ProductionName(prod);
7592  TString strproc = ProcessName(proc);
7593  TString strsqrts="";
7594  if (sqrts>0) strsqrts = Form("_%iTeV", sqrts);
7595 
7596  TString strappend = Form("%s_%s_%s%s", strme.Data(), strprod.Data(), strproc.Data(), strsqrts.Data());
7597 
7598  TFile* finput = TFile::Open(Form("pAvgLinToLog_%s%s", strappend.Data(), ".root"), "read");
7599  TFile* foutput = TFile::Open(Form("pAvgSmooth_%s%s", strappend.Data(), ".root"), "recreate");
7600  const unsigned int ngraphs=2;
7601  TString strtg[ngraphs]={
7602  "tg_P_ConserveDifermionMass",
7603  "tg_P_MomentumToEnergy"
7604  };
7605  const double xmin=0;
7606  const double xmax=(sqrts>0 ? (double) sqrts*1000. : 15000.);
7607 
7608  for (unsigned int ig=0; ig<ngraphs; ig++){
7609  finput->cd();
7610  TGraphErrors* tgin = 0;
7611  tgin = (TGraphErrors*) finput->Get(strtg[ig]);
7612  if (tgin==0){ cerr << strtg[ig] << " does not exist." << endl; continue; }
7613  TGraphErrors* tg = new TGraphErrors(*tgin);
7614  if (redivision){
7615  TTree* tree = (TTree*) finput->Get("FinalTree");
7616  if (!tree) cerr << "Final tree does not exist, so cannot apply redivision" << endl;
7617  else{
7618  float ZZMass, weight, mesq;
7619  tree->SetBranchAddress("ZZMass", &ZZMass);
7620  tree->SetBranchAddress("weight", &weight);
7621  if (ig==0) tree->SetBranchAddress("mesq_conserveDifermMass", &mesq);
7622  else tree->SetBranchAddress("mesq_jetPtoEScale", &mesq);
7623  TProfile* pmass = (TProfile*) finput->Get("candMass");
7624  for (PointRedivision& rediv:*redivision){
7626  tg, tree, pmass,
7627  rediv,
7628  ZZMass, mesq, weight
7629  );
7630  }
7631  }
7632  }
7633 
7634  foutput->WriteTObject(tg);
7635 
7636  int n = tg->GetN();
7637  double* xx = tg->GetX();
7638  double* ex = tg->GetEX();
7639  double* yy = tg->GetY();
7640  double* ey = tg->GetEY();
7641 
7642  TSpline3* sp = convertGraphToSpline3_FaithfulSlopes(tg, (double*)nullptr, (double*)nullptr, useFaithfulLowSlopes, useFaithfulHighSlopes);
7643  double tglow = xx[0];
7644  double tghigh = xx[tg->GetN()-1];
7645  TF1* lowFcn = lowf(sp, xmin, tglow, true);
7646  TF1* highFcn = highf(sp, tghigh, xmax, false);
7647  lowFcn->SetNpx((int) (tglow-xmin)*5);
7648  highFcn->SetNpx((int) (xmax-tghigh)*5);
7649 
7650  vector<pair<double, double>> points;
7651  for (double xval=xmin; xval<tglow; xval+=1){
7652  double yval = lowFcn->Eval(xval);
7653  addByLowest<double, double>(points, xval, yval);
7654  }
7655  for (int ix=0; ix<n; ix++){
7656  addByLowest<double, double>(points, xx[ix], yy[ix]);
7657  }
7658  int tghigh_int = ((int) ((tghigh+1.)/100.+0.5))*100;
7659  if (tghigh>=(double) tghigh_int) tghigh_int+=100;
7660  for (double xval=tghigh_int; xval<=xmax; xval+=100){
7661  double yval = highFcn->Eval(xval);
7662  addByLowest<double, double>(points, xval, yval);
7663  }
7664 
7665  int nn_new = points.size();
7666  cout << "Number of new points: " << nn_new-n << endl;
7667  double* xy_new[2];
7668  for (unsigned int i=0; i<2; i++) xy_new[i] = new double[nn_new];
7669  for (int ix=0; ix<nn_new; ix++){
7670  xy_new[0][ix] = points.at(ix).first;
7671  xy_new[1][ix] = points.at(ix).second;
7672  }
7673 
7674  delete highFcn;
7675  delete lowFcn;
7676  delete sp;
7677 
7678  TGraph* tg_updated = new TGraph(nn_new, xy_new[0], xy_new[1]);
7679  tg_updated->SetName(Form("%s_Smooth", tg->GetName()));
7680  foutput->WriteTObject(tg_updated);
7681 
7682  sp = convertGraphToSpline3(tg_updated);
7683  foutput->WriteTObject(sp);
7684 
7685  TCanvas* ctest = new TCanvas(Form("test_%s", strtg[ig].Data()), "", 8, 30, 800, 800);
7686  ctest->cd();
7687  tg->GetXaxis()->SetRangeUser(xmin, xmax);
7688  tg->Draw("ae1p");
7689  sp->Draw("csame");
7690  ctest->RedrawAxis();
7691  ctest->Modified();
7692  ctest->Update();
7693  foutput->WriteTObject(ctest);
7694  ctest->SaveAs(Form("%s%s", ctest->GetName(), ".pdf"));
7695  ctest->Close();
7696 
7697  delete sp;
7698  delete tg_updated;
7699  for (unsigned int i=0; i<2; i++) delete[] xy_new[i];
7700  delete tg;
7701  }
7702  foutput->Close();
7703  finput->Close();
7704 }
7705 
7706 /* SPECIFIC COMMENT: NONE */
7709  TF1* (*lowf)(TSpline3*, double, double, bool),
7710  TF1* (*highf)(TSpline3*, double, double, bool),
7711  int sqrts=-1,
7712  bool useFaithfulLowSlopes=false,
7713  bool useFaithfulHighSlopes=false,
7714  vector<PointRedivision>* redivision=nullptr
7715  ){
7716  TString strme = MatrixElementName(me);
7717  TString strprod = ProductionName(prod);
7718  TString strproc = ProcessName(proc);
7719  TString strsqrts="";
7720  if (sqrts>0) strsqrts = Form("_%iTeV", sqrts);
7721 
7722  TString strappend = Form("%s_%s_%s%s", strme.Data(), strprod.Data(), strproc.Data(), strsqrts.Data());
7723 
7724  TFile* finput = TFile::Open(Form("pAvgLinToLog_%s%s", strappend.Data(), ".root"), "read");
7725  TFile* foutput = TFile::Open(Form("pAvgSmooth_%s%s", strappend.Data(), ".root"), "recreate");
7726  const unsigned int ngraphs=3;
7727  TString strtg[ngraphs]={
7728  "tg_P_ConserveDifermionMass_4mu",
7729  "tg_P_ConserveDifermionMass_4e",
7730  "tg_P_ConserveDifermionMass_2mu2e"
7731  };
7732  const double xmin=0;
7733  const double xmax=(sqrts>0 ? (double)sqrts*1000. : 15000.);
7734 
7735  for (unsigned int ig=0; ig<ngraphs; ig++){
7736  finput->cd();
7737  TGraphErrors* tgin = 0;
7738  tgin = (TGraphErrors*) finput->Get(strtg[ig]);
7739  if (tgin==0){ cerr << strtg[ig] << " does not exist." << endl; continue; }
7740  TGraphErrors* tg = new TGraphErrors(*tgin);
7741  if (redivision){
7742  TTree* tree;
7743  if (ig==0) tree = (TTree*) finput->Get("FinalTree_4mu");
7744  else if (ig==1) tree = (TTree*) finput->Get("FinalTree_4e");
7745  else tree = (TTree*) finput->Get("FinalTree_2mu2e");
7746  if (!tree) cerr << "Final tree does not exist, so cannot apply redivision" << endl;
7747  else{
7748  float ZZMass, weight, mesq;
7749  tree->SetBranchAddress("ZZMass", &ZZMass);
7750  tree->SetBranchAddress("weight", &weight);
7751  tree->SetBranchAddress("mesq_conserveDifermMass", &mesq);
7752  TProfile* pmass;
7753  if (ig==0) pmass = (TProfile*) finput->Get("candMass_4mu");
7754  else if (ig==1) pmass = (TProfile*) finput->Get("candMass_4e");
7755  else pmass = (TProfile*) finput->Get("candMass_2mu2e");
7756  for (PointRedivision& rediv:*redivision){
7758  tg, tree, pmass,
7759  rediv,
7760  ZZMass, mesq, weight
7761  );
7762  }
7763  }
7764  }
7765 
7766  foutput->cd();
7767  foutput->WriteTObject(tg);
7768 
7769  int n = tg->GetN();
7770  double* xx = tg->GetX();
7771  double* ex = tg->GetEX();
7772  double* yy = tg->GetY();
7773  double* ey = tg->GetEY();
7774 
7775  TSpline3* sp = convertGraphToSpline3_FaithfulSlopes(tg, (double*)nullptr, (double*)nullptr, useFaithfulLowSlopes, useFaithfulHighSlopes);
7776  double tglow = xx[0];
7777  double tghigh = xx[tg->GetN()-1];
7778  TF1* lowFcn = lowf(sp, xmin, tglow, true);
7779  TF1* highFcn = highf(sp, tghigh, xmax, false);
7780  lowFcn->SetNpx((int)(tglow-xmin)*5);
7781  highFcn->SetNpx((int)(xmax-tghigh)*5);
7782 
7783  vector<pair<double, double>> points;
7784  for (double xval=xmin; xval<tglow; xval+=1){
7785  double yval = lowFcn->Eval(xval);
7786  addByLowest<double, double>(points, xval, yval);
7787  }
7788  for (int ix=0; ix<n; ix++){
7789  addByLowest<double, double>(points, xx[ix], yy[ix]);
7790  }
7791  int tghigh_int = ((int)((tghigh+1.)/100.+0.5))*100;
7792  if (tghigh>=(double)tghigh_int) tghigh_int+=100;
7793  for (double xval=tghigh_int; xval<=xmax; xval+=100){
7794  double yval = highFcn->Eval(xval);
7795  addByLowest<double, double>(points, xval, yval);
7796  }
7797 
7798  int nn_new = points.size();
7799  cout << "Number of new points: " << nn_new-n << endl;
7800  double* xy_new[2];
7801  for (unsigned int i=0; i<2; i++) xy_new[i] = new double[nn_new];
7802  for (int ix=0; ix<nn_new; ix++){
7803  xy_new[0][ix] = points.at(ix).first;
7804  xy_new[1][ix] = points.at(ix).second;
7805  }
7806 
7807  delete highFcn;
7808  delete lowFcn;
7809  delete sp;
7810 
7811  TGraph* tg_updated = new TGraph(nn_new, xy_new[0], xy_new[1]);
7812  tg_updated->SetName(Form("%s_Smooth", tg->GetName()));
7813  foutput->WriteTObject(tg_updated);
7814 
7815  sp = convertGraphToSpline3(tg_updated);
7816  foutput->WriteTObject(sp);
7817 
7818  TCanvas* ctest = new TCanvas(Form("test_%s", strtg[ig].Data()), "", 8, 30, 800, 800);
7819  ctest->cd();
7820  tg->GetXaxis()->SetRangeUser(xmin, xmax);
7821  tg->Draw("ae1p");
7822  sp->Draw("csame");
7823  ctest->RedrawAxis();
7824  ctest->Modified();
7825  ctest->Update();
7826  foutput->WriteTObject(ctest);
7827  ctest->SaveAs(Form("%s%s", ctest->GetName(), ".pdf"));
7828  ctest->Close();
7829 
7830  delete sp;
7831  delete tg_updated;
7832  for (unsigned int i=0; i<2; i++) delete[] xy_new[i];
7833  delete tg;
7834  }
7835  foutput->Close();
7836  finput->Close();
7837 }
7838 
7839  /* SPECIFIC COMMENT: NONE */
7842  TString strpatchpath, TString strpatchname,
7843  TGraph* (*lowpatcher)(TSpline3*, TSpline3*, double, double, bool, bool),
7844  TGraph* (*highpatcher)(TSpline3*, TSpline3*, double, double, bool, bool),
7845  int sqrts=-1,
7846  vector<PointRedivision>* redivision=nullptr
7847  ){
7848  TString strme = MatrixElementName(me);
7849  TString strprod = ProductionName(prod);
7850  TString strproc = ProcessName(proc);
7851  TString strsqrts="";
7852  if (sqrts>0) strsqrts = Form("_%iTeV", sqrts);
7853 
7854  TString strappend = Form("%s_%s_%s%s", strme.Data(), strprod.Data(), strproc.Data(), strsqrts.Data());
7855 
7856  TFile* finput = TFile::Open(Form("pAvgLinToLog_%s%s", strappend.Data(), ".root"), "read");
7857  TFile* finput_patch = TFile::Open(strpatchpath, "read");
7858  TFile* foutput = TFile::Open(Form("pAvgSmooth_%s%s", strappend.Data(), ".root"), "recreate");
7859  const unsigned int ngraphs=3;
7860  TString strtg[ngraphs]={
7861  "tg_P_ConserveDifermionMass_4mu",
7862  "tg_P_ConserveDifermionMass_4e",
7863  "tg_P_ConserveDifermionMass_2mu2e"
7864  };
7865 
7866  TGraph* tgpatch = (TGraph*)finput_patch->Get(strpatchname);
7867  TSpline3* sppatch = convertGraphToSpline3(tgpatch);
7868 
7869  const double xmin=max(0., (tgpatch->GetX())[0]);
7870  const double xmax=min((sqrts>0 ? (double)sqrts*1000. : 15000.), (tgpatch->GetX())[tgpatch->GetN()-1]);
7871 
7872  for (unsigned int ig=0; ig<ngraphs; ig++){
7873  finput->cd();
7874  TGraphErrors* tgin = 0;
7875  tgin = (TGraphErrors*) finput->Get(strtg[ig]);
7876  if (tgin==0){ cerr << strtg[ig] << " does not exist." << endl; continue; }
7877  TGraphErrors* tg = new TGraphErrors(*tgin);
7878  if (redivision){
7879  TTree* tree;
7880  if (ig==0) tree = (TTree*) finput->Get("FinalTree_4mu");
7881  else if (ig==1) tree = (TTree*) finput->Get("FinalTree_4e");
7882  else tree = (TTree*) finput->Get("FinalTree_2mu2e");
7883  if (!tree) cerr << "Final tree does not exist, so cannot apply redivision" << endl;
7884  else{
7885  float ZZMass, weight, mesq;
7886  tree->SetBranchAddress("ZZMass", &ZZMass);
7887  tree->SetBranchAddress("weight", &weight);
7888  tree->SetBranchAddress("mesq_conserveDifermMass", &mesq);
7889  TProfile* pmass;
7890  if (ig==0) pmass = (TProfile*) finput->Get("candMass_4mu");
7891  else if (ig==1) pmass = (TProfile*) finput->Get("candMass_4e");
7892  else pmass = (TProfile*) finput->Get("candMass_2mu2e");
7893  for (PointRedivision& rediv:*redivision){
7895  tg, tree, pmass,
7896  rediv,
7897  ZZMass, mesq, weight
7898  );
7899  }
7900  }
7901  }
7902 
7903  foutput->cd();
7904  foutput->WriteTObject(tg);
7905 
7906  int n = tg->GetN();
7907  double* xx = tg->GetX();
7908  double* ex = tg->GetEX();
7909  double* yy = tg->GetY();
7910  double* ey = tg->GetEY();
7911 
7912  TSpline3* sp = convertGraphToSpline3(tg);
7913  sp->SetName(Form("%s_initial", sp->GetName()));
7914  foutput->WriteTObject(sp);
7915  double tglow = xx[0];
7916  double tghigh = xx[tg->GetN()-1];
7917  cout << "Extracting low patch..." << endl;
7918  TGraph* lowGraph = lowpatcher(sp, sppatch, xmin, tglow, true, true);
7919  cout << "Extracting high patch..." << endl;
7920  TGraph* highGraph = highpatcher(sp, sppatch, tghigh, xmax, false, true);
7921  cout << "Converting patches to splines..." << endl;
7922  TSpline3* lowFcn = convertGraphToSpline3(lowGraph);
7923  TSpline3* highFcn = convertGraphToSpline3(highGraph);
7924 
7925  vector<pair<double, double>> points;
7926  for (double xval=xmin; xval<tglow; xval+=1){
7927  double yval = lowFcn->Eval(xval);
7928  addByLowest<double, double>(points, xval, yval);
7929  }
7930  for (int ix=0; ix<n; ix++){
7931  addByLowest<double, double>(points, xx[ix], yy[ix]);
7932  }
7933  int tghigh_int = ((int)((tghigh+1.)/100.+0.5))*100;
7934  if (tghigh>=(double)tghigh_int) tghigh_int+=100;
7935  for (double xval=tghigh_int; xval<=xmax; xval+=100){
7936  double yval = highFcn->Eval(xval);
7937  addByLowest<double, double>(points, xval, yval);
7938  }
7939 
7940  int nn_new = points.size();
7941  cout << "Number of new points: " << nn_new-n << endl;
7942  double* xy_new[2];
7943  for (unsigned int i=0; i<2; i++) xy_new[i] = new double[nn_new];
7944  for (int ix=0; ix<nn_new; ix++){
7945  xy_new[0][ix] = points.at(ix).first;
7946  xy_new[1][ix] = points.at(ix).second;
7947  }
7948 
7949  delete highFcn;
7950  delete lowFcn;
7951  delete highGraph;
7952  delete lowGraph;
7953  delete sp;
7954 
7955  TGraph* tg_updated = new TGraph(nn_new, xy_new[0], xy_new[1]);
7956  tg_updated->SetName(Form("%s_Smooth", tg->GetName()));
7957  foutput->WriteTObject(tg_updated);
7958 
7959  sp = convertGraphToSpline3(tg_updated);
7960  foutput->WriteTObject(sp);
7961 
7962  TCanvas* ctest = new TCanvas(Form("test_%s", strtg[ig].Data()), "", 8, 30, 800, 800);
7963  ctest->cd();
7964  tg->GetXaxis()->SetRangeUser(xmin, xmax);
7965  tg->Draw("ae1p");
7966  sp->Draw("csame");
7967  ctest->RedrawAxis();
7968  ctest->Modified();
7969  ctest->Update();
7970  foutput->WriteTObject(ctest);
7971  ctest->SaveAs(Form("%s%s", ctest->GetName(), ".pdf"));
7972  ctest->Close();
7973 
7974  delete sp;
7975  delete tg_updated;
7976  for (unsigned int i=0; i<2; i++) delete[] xy_new[i];
7977  delete tg;
7978  }
7979 
7980  delete sppatch;
7981  foutput->Close();
7982  finput_patch->Close();
7983  finput->Close();
7984 }
7985 
7988  TString strpatchpath, TString strpatchname,
7989  TString strpatchpath2, TString strpatchname2,
7990  TGraph* (*lowpatcher)(TSpline3*, TSpline3*, double, double, bool, bool),
7991  TGraph* (*highpatcher)(TSpline3*, TSpline3*, double, double, bool, bool),
7992  int sqrts=-1,
7993  vector<PointRedivision>* redivision=nullptr
7994  ){
7995  TString strme = MatrixElementName(me);
7996  TString strprod = ProductionName(prod);
7997  TString strproc = ProcessName(proc);
7998  TString strsqrts="";
7999  if (sqrts>0) strsqrts = Form("_%iTeV", sqrts);
8000 
8001  TString strappend = Form("%s_%s_%s%s", strme.Data(), strprod.Data(), strproc.Data(), strsqrts.Data());
8002 
8003  TFile* finput = TFile::Open(Form("pAvgLinToLog_%s%s", strappend.Data(), ".root"), "read");
8004  TFile* finput_patch = TFile::Open(strpatchpath, "read");
8005  TFile* finput_patch2 = TFile::Open(strpatchpath2, "read");
8006  TFile* foutput = TFile::Open(Form("pAvgSmooth_%s%s", strappend.Data(), ".root"), "recreate");
8007  const unsigned int ngraphs=3;
8008  TString strtg[ngraphs]={
8009  "tg_P_ConserveDifermionMass_4mu",
8010  "tg_P_ConserveDifermionMass_4e",
8011  "tg_P_ConserveDifermionMass_2mu2e"
8012  };
8013 
8014  TGraph* tgpatch = (TGraph*)finput_patch->Get(strpatchname);
8015  TSpline3* sppatch = convertGraphToSpline3(tgpatch);
8016 
8017  TGraph* tgpatch2 = (TGraph*)finput_patch2->Get(strpatchname2);
8018  TSpline3* sppatch2 = convertGraphToSpline3(tgpatch2);
8019 
8020  const double xmin=max(0., (tgpatch->GetX())[0]);
8021  const double xmax=min((sqrts>0 ? (double)sqrts*1000. : 15000.), (tgpatch->GetX())[tgpatch->GetN()-1]);
8022 
8023  for (unsigned int ig=0; ig<ngraphs; ig++){
8024  finput->cd();
8025  TGraphErrors* tgin = 0;
8026  tgin = (TGraphErrors*) finput->Get(strtg[ig]);
8027  if (tgin==0){ cerr << strtg[ig] << " does not exist." << endl; continue; }
8028  TGraphErrors* tg = new TGraphErrors(*tgin);
8029  if (redivision){
8030  TTree* tree;
8031  if (ig==0) tree = (TTree*) finput->Get("FinalTree_4mu");
8032  else if (ig==1) tree = (TTree*) finput->Get("FinalTree_4e");
8033  else tree = (TTree*) finput->Get("FinalTree_2mu2e");
8034  if (!tree) cerr << "Final tree does not exist, so cannot apply redivision" << endl;
8035  else{
8036  float ZZMass, weight, mesq;
8037  tree->SetBranchAddress("ZZMass", &ZZMass);
8038  tree->SetBranchAddress("weight", &weight);
8039  tree->SetBranchAddress("mesq_conserveDifermMass", &mesq);
8040  TProfile* pmass;
8041  if (ig==0) pmass = (TProfile*) finput->Get("candMass_4mu");
8042  else if (ig==1) pmass = (TProfile*) finput->Get("candMass_4e");
8043  else pmass = (TProfile*) finput->Get("candMass_2mu2e");
8044  for (PointRedivision& rediv:*redivision){
8046  tg, tree, pmass,
8047  rediv,
8048  ZZMass, mesq, weight
8049  );
8050  }
8051  }
8052  }
8053 
8054  foutput->cd();
8055  foutput->WriteTObject(tg);
8056 
8057  int n = tg->GetN();
8058  double* xx = tg->GetX();
8059  double* ex = tg->GetEX();
8060  double* yy = tg->GetY();
8061  double* ey = tg->GetEY();
8062 
8063  TSpline3* sp = convertGraphToSpline3(tg);
8064  sp->SetName(Form("%s_initial", sp->GetName()));
8065  foutput->WriteTObject(sp);
8066  double tglow = xx[0];
8067  double tghigh = xx[tg->GetN()-1];
8068  cout << "Extracting low patch..." << endl;
8069  TGraph* lowGraph = lowpatcher(sp, sppatch, xmin, tglow, true, false);
8070  if (lowGraph==0) lowGraph = lowpatcher(sp, sppatch2, xmin, tglow, true, true);
8071  cout << "Extracting high patch..." << endl;
8072  TGraph* highGraph = highpatcher(sp, sppatch, tghigh, xmax, false, false);
8073  if (highGraph==0) highGraph = highpatcher(sp, sppatch2, tghigh, xmax, false, true);
8074  cout << "Converting patches to splines..." << endl;
8075  TSpline3* lowFcn = convertGraphToSpline3(lowGraph);
8076  TSpline3* highFcn = convertGraphToSpline3(highGraph);
8077 
8078  vector<pair<double, double>> points;
8079  for (double xval=xmin; xval<tglow; xval+=1){
8080  double yval = lowFcn->Eval(xval);
8081  addByLowest<double, double>(points, xval, yval);
8082  }
8083  for (int ix=0; ix<n; ix++){
8084  addByLowest<double, double>(points, xx[ix], yy[ix]);
8085  }
8086  int tghigh_int = ((int)((tghigh+1.)/100.+0.5))*100;
8087  if (tghigh>=(double)tghigh_int) tghigh_int+=100;
8088  for (double xval=tghigh_int; xval<=xmax; xval+=100){
8089  double yval = highFcn->Eval(xval);
8090  addByLowest<double, double>(points, xval, yval);
8091  }
8092 
8093  int nn_new = points.size();
8094  cout << "Number of new points: " << nn_new-n << endl;
8095  double* xy_new[2];
8096  for (unsigned int i=0; i<2; i++) xy_new[i] = new double[nn_new];
8097  for (int ix=0; ix<nn_new; ix++){
8098  xy_new[0][ix] = points.at(ix).first;
8099  xy_new[1][ix] = points.at(ix).second;
8100  }
8101 
8102  delete highFcn;
8103  delete lowFcn;
8104  delete highGraph;
8105  delete lowGraph;
8106  delete sp;
8107 
8108  TGraph* tg_updated = new TGraph(nn_new, xy_new[0], xy_new[1]);
8109  tg_updated->SetName(Form("%s_Smooth", tg->GetName()));
8110  foutput->WriteTObject(tg_updated);
8111 
8112  sp = convertGraphToSpline3(tg_updated);
8113  foutput->WriteTObject(sp);
8114 
8115  TCanvas* ctest = new TCanvas(Form("test_%s", strtg[ig].Data()), "", 8, 30, 800, 800);
8116  ctest->cd();
8117  tg->GetXaxis()->SetRangeUser(xmin, xmax);
8118  tg->Draw("ae1p");
8119  sp->Draw("csame");
8120  ctest->RedrawAxis();
8121  ctest->Modified();
8122  ctest->Update();
8123  foutput->WriteTObject(ctest);
8124  ctest->SaveAs(Form("%s%s", ctest->GetName(), ".pdf"));
8125  ctest->Close();
8126 
8127  delete sp;
8128  delete tg_updated;
8129  for (unsigned int i=0; i<2; i++) delete[] xy_new[i];
8130  delete tg;
8131  }
8132 
8133  delete sppatch2;
8134  delete sppatch;
8135  foutput->Close();
8136  finput_patch2->Close();
8137  finput_patch->Close();
8138  finput->Close();
8139 }
8140 
8141 /* SPECIFIC COMMENT: NONE */
8144  TString strpatchpath, TString strpatchname,
8145  TF1* (*lowf)(TSpline3*, double, double, bool),
8146  TGraph* (*highpatcher)(TSpline3*, TSpline3*, double, double, bool, bool),
8147  int sqrts=-1,
8148  vector<PointRedivision>* redivision=nullptr
8149  ){
8150  TString strme = MatrixElementName(me);
8151  TString strprod = ProductionName(prod);
8152  TString strproc = ProcessName(proc);
8153  TString strsqrts="";
8154  if (sqrts>0) strsqrts = Form("_%iTeV", sqrts);
8155 
8156  TString strappend = Form("%s_%s_%s%s", strme.Data(), strprod.Data(), strproc.Data(), strsqrts.Data());
8157 
8158  TFile* finput = TFile::Open(Form("pAvgLinToLog_%s%s", strappend.Data(), ".root"), "read");
8159  TFile* finput_patch = TFile::Open(strpatchpath, "read");
8160  TFile* foutput = TFile::Open(Form("pAvgSmooth_%s%s", strappend.Data(), ".root"), "recreate");
8161  const unsigned int ngraphs=3;
8162  TString strtg[ngraphs]={
8163  "tg_P_ConserveDifermionMass_4mu",
8164  "tg_P_ConserveDifermionMass_4e",
8165  "tg_P_ConserveDifermionMass_2mu2e"
8166  };
8167 
8168  TGraph* tgpatch = (TGraph*)finput_patch->Get(strpatchname);
8169  TSpline3* sppatch = convertGraphToSpline3(tgpatch);
8170 
8171  const double xmin=max(0., (tgpatch->GetX())[0]);
8172  const double xmax=min((sqrts>0 ? (double)sqrts*1000. : 15000.), (tgpatch->GetX())[tgpatch->GetN()-1]);
8173 
8174  for (unsigned int ig=0; ig<ngraphs; ig++){
8175  finput->cd();
8176  TGraphErrors* tgin = 0;
8177  tgin = (TGraphErrors*) finput->Get(strtg[ig]);
8178  if (tgin==0){ cerr << strtg[ig] << " does not exist." << endl; continue; }
8179  TGraphErrors* tg = new TGraphErrors(*tgin);
8180  if (redivision){
8181  TTree* tree;
8182  if (ig==0) tree = (TTree*) finput->Get("FinalTree_4mu");
8183  else if (ig==1) tree = (TTree*) finput->Get("FinalTree_4e");
8184  else tree = (TTree*) finput->Get("FinalTree_2mu2e");
8185  if (!tree) cerr << "Final tree does not exist, so cannot apply redivision" << endl;
8186  else{
8187  float ZZMass, weight, mesq;
8188  tree->SetBranchAddress("ZZMass", &ZZMass);
8189  tree->SetBranchAddress("weight", &weight);
8190  tree->SetBranchAddress("mesq_conserveDifermMass", &mesq);
8191  TProfile* pmass;
8192  if (ig==0) pmass = (TProfile*) finput->Get("candMass_4mu");
8193  else if (ig==1) pmass = (TProfile*) finput->Get("candMass_4e");
8194  else pmass = (TProfile*) finput->Get("candMass_2mu2e");
8195  for (PointRedivision& rediv:*redivision){
8197  tg, tree, pmass,
8198  rediv,
8199  ZZMass, mesq, weight
8200  );
8201  }
8202  }
8203  }
8204 
8205  foutput->cd();
8206  foutput->WriteTObject(tg);
8207 
8208  int n = tg->GetN();
8209  double* xx = tg->GetX();
8210  double* ex = tg->GetEX();
8211  double* yy = tg->GetY();
8212  double* ey = tg->GetEY();
8213 
8214  TSpline3* sp = convertGraphToSpline3(tg);
8215  sp->SetName(Form("%s_initial", sp->GetName()));
8216  foutput->WriteTObject(sp);
8217  double tglow = xx[0];
8218  double tghigh = xx[tg->GetN()-1];
8219  cout << "Extracting high patch..." << endl;
8220  TGraph* highGraph = highpatcher(sp, sppatch, tghigh, xmax, false, true);
8221  cout << "Converting patches to splines..." << endl;
8222  TSpline3* highFcn = convertGraphToSpline3(highGraph);
8223 
8224  TF1* lowFcn = lowf(sp, xmin, tglow, true);
8225  lowFcn->SetNpx((int)(tglow-xmin)*5);
8226 
8227  vector<pair<double, double>> points;
8228  for (double xval=xmin; xval<tglow; xval+=1){
8229  double yval = lowFcn->Eval(xval);
8230  addByLowest<double, double>(points, xval, yval);
8231  }
8232  for (int ix=0; ix<n; ix++){
8233  addByLowest<double, double>(points, xx[ix], yy[ix]);
8234  }
8235  int tghigh_int = ((int)((tghigh+1.)/100.+0.5))*100;
8236  if (tghigh>=(double)tghigh_int) tghigh_int+=100;
8237  for (double xval=tghigh_int; xval<=xmax; xval+=100){
8238  double yval = highFcn->Eval(xval);
8239  addByLowest<double, double>(points, xval, yval);
8240  }
8241 
8242  int nn_new = points.size();
8243  cout << "Number of new points: " << nn_new-n << endl;
8244  double* xy_new[2];
8245  for (unsigned int i=0; i<2; i++) xy_new[i] = new double[nn_new];
8246  for (int ix=0; ix<nn_new; ix++){
8247  xy_new[0][ix] = points.at(ix).first;
8248  xy_new[1][ix] = points.at(ix).second;
8249  }
8250 
8251  delete lowFcn;
8252  delete highFcn;
8253  delete highGraph;
8254  delete sp;
8255 
8256  TGraph* tg_updated = new TGraph(nn_new, xy_new[0], xy_new[1]);
8257  tg_updated->SetName(Form("%s_Smooth", tg->GetName()));
8258  foutput->WriteTObject(tg_updated);
8259 
8260  sp = convertGraphToSpline3(tg_updated);
8261  foutput->WriteTObject(sp);
8262 
8263  TCanvas* ctest = new TCanvas(Form("test_%s", strtg[ig].Data()), "", 8, 30, 800, 800);
8264  ctest->cd();
8265  tg->GetXaxis()->SetRangeUser(xmin, xmax);
8266  tg->Draw("ae1p");
8267  sp->Draw("csame");
8268  ctest->RedrawAxis();
8269  ctest->Modified();
8270  ctest->Update();
8271  foutput->WriteTObject(ctest);
8272  ctest->SaveAs(Form("%s%s", ctest->GetName(), ".pdf"));
8273  ctest->Close();
8274 
8275  delete sp;
8276  delete tg_updated;
8277  for (unsigned int i=0; i<2; i++) delete[] xy_new[i];
8278  delete tg;
8279  }
8280 
8281  delete sppatch;
8282  foutput->Close();
8283  finput_patch->Close();
8284  finput->Close();
8285 }
8286 
8289  TString strpatchpath, TString strpatchname,
8290  TGraph* (*lowpatcher)(TSpline3*, TSpline3*, double, double, bool, bool),
8291  TF1* (*highf)(TSpline3*, double, double, bool),
8292  int sqrts=-1,
8293  vector<PointRedivision>* redivision=nullptr
8294  ){
8295  TString strme = MatrixElementName(me);
8296  TString strprod = ProductionName(prod);
8297  TString strproc = ProcessName(proc);
8298  TString strsqrts="";
8299  if (sqrts>0) strsqrts = Form("_%iTeV", sqrts);
8300 
8301  TString strappend = Form("%s_%s_%s%s", strme.Data(), strprod.Data(), strproc.Data(), strsqrts.Data());
8302 
8303  TFile* finput = TFile::Open(Form("pAvgLinToLog_%s%s", strappend.Data(), ".root"), "read");
8304  TFile* finput_patch = TFile::Open(strpatchpath, "read");
8305  TFile* foutput = TFile::Open(Form("pAvgSmooth_%s%s", strappend.Data(), ".root"), "recreate");
8306  const unsigned int ngraphs=3;
8307  TString strtg[ngraphs]={
8308  "tg_P_ConserveDifermionMass_4mu",
8309  "tg_P_ConserveDifermionMass_4e",
8310  "tg_P_ConserveDifermionMass_2mu2e"
8311  };
8312 
8313  TGraph* tgpatch = (TGraph*)finput_patch->Get(strpatchname);
8314  TSpline3* sppatch = convertGraphToSpline3(tgpatch);
8315 
8316  const double xmin=max(0., (tgpatch->GetX())[0]);
8317  const double xmax=min((sqrts>0 ? (double)sqrts*1000. : 15000.), (tgpatch->GetX())[tgpatch->GetN()-1]);
8318 
8319  for (unsigned int ig=0; ig<ngraphs; ig++){
8320  finput->cd();
8321  TGraphErrors* tgin = 0;
8322  tgin = (TGraphErrors*) finput->Get(strtg[ig]);
8323  if (tgin==0){ cerr << strtg[ig] << " does not exist." << endl; continue; }
8324  TGraphErrors* tg = new TGraphErrors(*tgin);
8325  if (redivision){
8326  TTree* tree;
8327  if (ig==0) tree = (TTree*) finput->Get("FinalTree_4mu");
8328  else if (ig==1) tree = (TTree*) finput->Get("FinalTree_4e");
8329  else tree = (TTree*) finput->Get("FinalTree_2mu2e");
8330  if (!tree) cerr << "Final tree does not exist, so cannot apply redivision" << endl;
8331  else{
8332  float ZZMass, weight, mesq;
8333  tree->SetBranchAddress("ZZMass", &ZZMass);
8334  tree->SetBranchAddress("weight", &weight);
8335  tree->SetBranchAddress("mesq_conserveDifermMass", &mesq);
8336  TProfile* pmass;
8337  if (ig==0) pmass = (TProfile*) finput->Get("candMass_4mu");
8338  else if (ig==1) pmass = (TProfile*) finput->Get("candMass_4e");
8339  else pmass = (TProfile*) finput->Get("candMass_2mu2e");
8340  for (PointRedivision& rediv:*redivision){
8342  tg, tree, pmass,
8343  rediv,
8344  ZZMass, mesq, weight
8345  );
8346  }
8347  }
8348  }
8349 
8350  foutput->cd();
8351  foutput->WriteTObject(tg);
8352 
8353  int n = tg->GetN();
8354  double* xx = tg->GetX();
8355  double* ex = tg->GetEX();
8356  double* yy = tg->GetY();
8357  double* ey = tg->GetEY();
8358 
8359  TSpline3* sp = convertGraphToSpline3(tg);
8360  sp->SetName(Form("%s_initial", sp->GetName()));
8361  foutput->WriteTObject(sp);
8362  double tglow = xx[0];
8363  double tghigh = xx[tg->GetN()-1];
8364  cout << "Extracting low patch..." << endl;
8365  TGraph* lowGraph = lowpatcher(sp, sppatch, tglow, xmax, true, true);
8366  cout << "Converting patches to splines..." << endl;
8367  TSpline3* lowFcn = convertGraphToSpline3(lowGraph);
8368 
8369  TF1* highFcn = highf(sp, tghigh, xmax, false);
8370  highFcn->SetNpx((int)(tglow-xmin)*5);
8371 
8372  vector<pair<double, double>> points;
8373  for (double xval=xmin; xval<tglow; xval+=1){
8374  double yval = lowFcn->Eval(xval);
8375  addByLowest<double, double>(points, xval, yval);
8376  }
8377  for (int ix=0; ix<n; ix++){
8378  addByLowest<double, double>(points, xx[ix], yy[ix]);
8379  }
8380  int tghigh_int = ((int)((tghigh+1.)/100.+0.5))*100;
8381  if (tghigh>=(double)tghigh_int) tghigh_int+=100;
8382  for (double xval=tghigh_int; xval<=xmax; xval+=100){
8383  double yval = highFcn->Eval(xval);
8384  addByLowest<double, double>(points, xval, yval);
8385  }
8386 
8387  int nn_new = points.size();
8388  cout << "Number of new points: " << nn_new-n << endl;
8389  double* xy_new[2];
8390  for (unsigned int i=0; i<2; i++) xy_new[i] = new double[nn_new];
8391  for (int ix=0; ix<nn_new; ix++){
8392  xy_new[0][ix] = points.at(ix).first;
8393  xy_new[1][ix] = points.at(ix).second;
8394  }
8395 
8396  delete lowFcn;
8397  delete highFcn;
8398  delete lowGraph;
8399  delete sp;
8400 
8401  TGraph* tg_updated = new TGraph(nn_new, xy_new[0], xy_new[1]);
8402  tg_updated->SetName(Form("%s_Smooth", tg->GetName()));
8403  foutput->WriteTObject(tg_updated);
8404 
8405  sp = convertGraphToSpline3(tg_updated);
8406  foutput->WriteTObject(sp);
8407 
8408  TCanvas* ctest = new TCanvas(Form("test_%s", strtg[ig].Data()), "", 8, 30, 800, 800);
8409  ctest->cd();
8410  tg->GetXaxis()->SetRangeUser(xmin, xmax);
8411  tg->Draw("ae1p");
8412  sp->Draw("csame");
8413  ctest->RedrawAxis();
8414  ctest->Modified();
8415  ctest->Update();
8416  foutput->WriteTObject(ctest);
8417  ctest->SaveAs(Form("%s%s", ctest->GetName(), ".pdf"));
8418  ctest->Close();
8419 
8420  delete sp;
8421  delete tg_updated;
8422  for (unsigned int i=0; i<2; i++) delete[] xy_new[i];
8423  delete tg;
8424  }
8425 
8426  delete sppatch;
8427  foutput->Close();
8428  finput_patch->Close();
8429  finput->Close();
8430 }
8431 
8432 /* SPECIFIC COMMENT: NONE */
8434  TFile* finput = new TFile(Form("pAvgLinToLog_MCFM_JJQCD_bkgZJets_%iTeV_2l2q.root", sqrts), "read");
8435  TFile* foutput = new TFile(Form("pAvgSmooth_MCFM_JJQCD_bkgZJets_%iTeV_2l2q.root", sqrts), "recreate");
8436  const unsigned int ngraphs=1;
8437  TString strtg[ngraphs]={
8438  "tg_P_ConserveDifermionMass"
8439  };
8440 
8441  for (unsigned int ig=0; ig<ngraphs; ig++){
8442  finput->cd();
8443  TGraphErrors* tg = 0;
8444  tg = (TGraphErrors*)finput->Get(strtg[ig].Data());
8445  if (tg==0){ cerr << strtg[ig] << " does not exist." << endl; continue; }
8446  tg->SetName(strtg[ig]);
8447  foutput->cd();
8448  foutput->WriteTObject(tg);
8449  tg->SetName(Form("%s_Smooth", tg->GetName()));
8450 
8451  TGraphErrors* tg_new = replacePointsBetween(tg, 510, 520); tg=tg_new;
8452  tg_new = replacePointsBetween(tg, 329.5, 330.5); delete tg; tg=tg_new;
8453  tg_new = replacePointsBetween(tg, 338, 340); delete tg; tg=tg_new;
8454 
8455  regularizeSlice(tg);
8456  foutput->WriteTObject(tg);
8457 
8458  TSpline3* sp = convertGraphToSpline3(tg);
8459  TF1* lowFcn = getFcn_a0plusa1overX(sp, 0, (tg->GetX())[0], true);
8460  TF1* highFcn = getFcn_a0plusa1timesX(sp, (tg->GetX())[tg->GetN()-1], 20000., false);
8461  lowFcn->SetNpx(1000);
8462  highFcn->SetNpx(10000);
8463 
8464  foutput->WriteTObject(sp);
8465  foutput->WriteTObject(lowFcn);
8466  foutput->WriteTObject(highFcn);
8467 
8468  TCanvas* ctest = new TCanvas("test", "", 8, 30, 800, 800);
8469  ctest->cd();
8470  tg->GetXaxis()->SetRangeUser(0, 20000);
8471  tg->Draw("ae1p");
8472  sp->Draw("csame");
8473  lowFcn->Draw("csame");
8474  highFcn->Draw("csame");
8475  ctest->RedrawAxis();
8476  ctest->Modified();
8477  ctest->Update();
8478  foutput->WriteTObject(ctest);
8479  ctest->Close();
8480 
8481  delete highFcn;
8482  delete lowFcn;
8483  delete sp;
8484  delete tg;
8485  }
8486  foutput->Close();
8487  finput->Close();
8488 }
8489 
8490 /* SPECIFIC COMMENT: NONE */
8496  me, prod, proc,
8499  sqrts
8500  );
8501 }
8502 
8503 /* SPECIFIC COMMENT: NONE */
8509  me, prod, proc,
8512  sqrts
8513  );
8514 }
8515 
8516 /* SPECIFIC COMMENT: NONE */
8522  me, prod, proc,
8525  sqrts
8526  );
8527 }
8528 
8529 void produce_PAvgSmooth_JHUGen_HadVH_HSMHiggs(TString strprod, int sqrts=13){
8530  if (!(strprod == "Had_ZH" || strprod == "Had_WH")) return;
8531 
8533  for (int iprod=(int)TVar::JJVBF; iprod<(int)TVar::nProductions; iprod++){
8534  prod = (TVar::Production)iprod;
8535  if (TVar::ProductionName(prod)==strprod) break;
8536  }
8539  vector<PointRedivision> redivs;
8540  redivs.emplace_back(2, 90, 90);
8541  redivs.emplace_back(2, 90, 90);
8542  redivs.emplace_back(2, 90, 90);
8543  redivs.emplace_back(4, 1600, 3000);
8545  me, prod, proc,
8548  sqrts,
8549  false, false,
8550  &redivs
8551  );
8552 }
8553 
8554 /* SPECIFIC COMMENT: PATCHING DONE USING ANALYTICAL ggH PDF */
8560  me, prod, proc,
8561  "../data/pAvgLinToLog_ANALYTICAL_ZZGG_HSMHiggs.root", "tg_anaPdfInt",
8564  -1
8565  );
8566 }
8567 
8568 /* SPECIFIC COMMENT: PATCHING DONE USING ANALYTICAL ggH PDF */
8574  me, prod, proc,
8575  "../data/pAvgLinToLog_ANALYTICAL_ZZGG_HSMHiggs.root", "tg_anaPdfInt",
8578  -1
8579  );
8580 }
8581 
8582 /* SPECIFIC COMMENT: PATCHING DONE USING ANALYTICAL QQZZ PDF */
8586  TVar::Process proc = TVar::bkgZZ;
8588  me, prod, proc,
8589  "../data/pAvgLinToLog_ANALYTICAL_ZZGG_HSMHiggs.root", "tg_anaPdfInt",
8590  "../data/pAvgLinToLog_ANALYTICAL_ZZQQB_bkgZZ.root", "tg_anaPdfInt",
8593  -1
8594  );
8595 }
8596 
8597 /* SPECIFIC COMMENT: PATCHING DONE USING ANALYTICAL QQZZ PDF */
8601  TVar::Process proc = TVar::bkgZZ;
8603  me, prod, proc,
8604  "../data/pAvgLinToLog_ANALYTICAL_ZZQQB_bkgZZ.root","tg_anaPdfInt",
8607  -1
8608  );
8609 }
8610 
8611 /* SPECIFIC COMMENT: PATCHING DONE USING ANALYTICAL ggH PDF */
8612 void produce_PAvgSmooth_MCFM_JJPROD_S_HSMHiggs(TString strprod, int sqrts=13){
8613  if (!(strprod == "Had_ZH_S" || strprod == "Had_WH_S" || strprod == "JJVBF_S")) return;
8614 
8616  for (int iprod=(int)TVar::JJVBF; iprod<(int)TVar::nProductions; iprod++){
8617  prod = (TVar::Production)iprod;
8618  if (TVar::ProductionName(prod)==strprod) break;
8619  }
8622  //generic_PAvgSmoothProducer_withDecay(
8623  // me, prod, proc,
8624  // "../data/pAvgLinToLog_ANALYTICAL_ZZGG_HSMHiggs.root", "tg_anaPdfInt",
8625  // &getPatch_a0plusa1timesX,
8626  // &getPatch_a0plusa1overX,
8627  // sqrts
8628  // );
8629  vector<PointRedivision> redivs;
8631  redivs.emplace_back(2, 90, 90);
8632  redivs.emplace_back(2, 90, 90);
8633  redivs.emplace_back(2, 90, 90);
8634 
8635  redivs.emplace_back(4, 1600, 3000);
8636 
8637  vector<float> redivmultiplesA{ 192.072, 210.622, 243.901, 272.591, 331.305, 392.596, 449.076, 504.783, 565.813, 640.583, 729.157, 822.262, 945.274 };
8638  vector<float> redivmultiplesB{ 195.798, 215.901, 249.163, 286.182, 346.839, 406.022, 464.092, 522.738, 588.661, 672.415, 764.014, 862.423, 1015.89 };
8639  for (unsigned int irm=0; irm<redivmultiplesA.size()-1; irm++){
8640  if (prod==TVar::Had_ZH_S && (irm==0 || irm==2)) continue;
8641  float xlow=std::max(redivmultiplesA.at(irm), redivmultiplesB.at(irm))+1e-2;
8642  float xhigh=std::min(redivmultiplesA.at(irm+1), redivmultiplesB.at(irm+1))-1e-2;
8643  redivs.emplace_back(1, xlow, xhigh);
8644  }
8645  }
8647  me, prod, proc,
8650  sqrts,
8651  true, false,
8652  &redivs
8653  );
8654 }
8655 
8656 /* SPECIFIC COMMENT: PATCHING DONE USING FUNCTIONS */
8657 void produce_PAvgSmooth_MCFM_JJPROD_bkgZZ(TString strprod, int sqrts=13){
8658  if (!(strprod == "Had_ZH" || strprod == "Had_WH" || strprod == "JJVBF")) return;
8659 
8661  for (int iprod=(int) TVar::JJVBF; iprod<(int) TVar::nProductions; iprod++){
8662  prod = (TVar::Production)iprod;
8663  if (TVar::ProductionName(prod)==strprod) break;
8664  }
8666  TVar::Process proc = TVar::bkgZZ;
8667  if (prod!=TVar::JJVBF){
8668  vector<PointRedivision> redivs;
8669  redivs.emplace_back(2, 90, 90);
8670  redivs.emplace_back(2, 90, 90);
8671  redivs.emplace_back(2, 90, 90);
8672  if (prod==TVar::Had_ZH) redivs.emplace_back(5, 125.3, 170);
8673  else{
8674  redivs.emplace_back(1, 105, 118);
8675  redivs.emplace_back(1, 120, 125);
8676  }
8678  me, prod, proc,
8679  //"../data/pAvgLinToLog_ANALYTICAL_ZZQQB_bkgZZ.root", "tg_anaPdfInt",
8682  sqrts,
8683  true, false,
8684  &redivs
8685  );
8686  }
8687  else{
8689  me, prod, proc,
8692  sqrts,
8693  false, false
8694  );
8695  }
8696 }
8697 
8698 /* SPECIFIC COMMENT: PATCHING DONE USING ANALYTICAL QQZZ PDF */
8702  TVar::Process proc = TVar::bkgZZ;
8704  me, prod, proc,
8705  "../data/pAvgLinToLog_ANALYTICAL_ZZQQB_bkgZZ.root", "tg_anaPdfInt",
8708  sqrts
8709  );
8710 }
8711 
8712 
8714  int erg_tev=sqrts;
8715  float mPOLE=125.;
8716  TString TREE_NAME = "SelectedTree";
8717 
8718  TVar::VerbosityLevel verbosity = TVar::ERROR;
8719  Mela mela(erg_tev, mPOLE, verbosity);
8720 
8721  short NJets30;
8722  std::vector<double>* JetPt=0;
8723  std::vector<double>* JetEta=0;
8724  std::vector<double>* JetPhi=0;
8725  std::vector<double>* JetMass=0;
8726  std::vector<double> myJetPt;
8727  std::vector<double> myJetEta;
8728  std::vector<double> myJetPhi;
8729  std::vector<double> myJetMass;
8730  TBranch* bJetPt=0;
8731  TBranch* bJetEta=0;
8732  TBranch* bJetPhi=0;
8733  TBranch* bJetMass=0;
8734  float jetptetaphimass[2][4];
8735 
8736  float mzz = 126.;
8737  float m1 = 91.471450;
8738  float m2 = 12.139782;
8739  float h1 = 0.2682896;
8740  float h2 = 0.1679779;
8741  float phi = 1.5969792;
8742  float hs = -0.727181;
8743  float phi1 = 1.8828257;
8744  float ZZPt, ZZPhi, ZZEta;
8745  int LepID[4]={ 13, -13, 11, -11 };
8746 
8747  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
8748  TString cinput_main;
8749  if (sqrts==8) cinput_main = "/scratch0/hep/ianderso/CJLST/140519/PRODFSR_8TeV";
8750  else if (sqrts==7) cinput_main = "/scratch0/hep/ianderso/CJLST/140519/PRODFSR";
8751  else return;
8752  const int nSamples_JJVBF = 42;
8753  TString strSamples_JJVBF[nSamples_JJVBF]={
8754  "HZZ4lTree_VBFH116.root",
8755  "HZZ4lTree_VBFH117.root",
8756  "HZZ4lTree_VBFH118.root",
8757  "HZZ4lTree_VBFH119.root",
8758  "HZZ4lTree_VBFH120.root",
8759  "HZZ4lTree_VBFH121.root",
8760  "HZZ4lTree_VBFH122.root",
8761  "HZZ4lTree_VBFH123.root",
8762  "HZZ4lTree_VBFH124.root",
8763  "HZZ4lTree_VBFH125.root",
8764  "HZZ4lTree_VBFH126.root",
8765  "HZZ4lTree_VBFH127.root",
8766  "HZZ4lTree_VBFH128.root",
8767  "HZZ4lTree_VBFH129.root",
8768  "HZZ4lTree_VBFH130.root",
8769  "HZZ4lTree_VBFH135.root",
8770  "HZZ4lTree_VBFH140.root",
8771  "HZZ4lTree_VBFH145.root",
8772  "HZZ4lTree_VBFH150.root",
8773  "HZZ4lTree_VBFH160.root",
8774  "HZZ4lTree_VBFH170.root",
8775  "HZZ4lTree_VBFH180.root",
8776  "HZZ4lTree_VBFH190.root",
8777  "HZZ4lTree_powheg15VBFH200.root",
8778  "HZZ4lTree_powheg15VBFH225.root",
8779  "HZZ4lTree_powheg15VBFH250.root",
8780  "HZZ4lTree_powheg15VBFH275.root",
8781  "HZZ4lTree_powheg15VBFH300.root",
8782  "HZZ4lTree_powheg15VBFH350.root",
8783  "HZZ4lTree_powheg15VBFH400.root",
8784  "HZZ4lTree_powheg15VBFH450.root",
8785  "HZZ4lTree_powheg15VBFH500.root",
8786  "HZZ4lTree_powheg15VBFH550.root",
8787  "HZZ4lTree_powheg15VBFH600.root",
8788  "HZZ4lTree_powheg15VBFH650.root",
8789  "HZZ4lTree_powheg15VBFH700.root",
8790  "HZZ4lTree_powheg15VBFH750.root",
8791  "HZZ4lTree_powheg15VBFH800.root",
8792  "HZZ4lTree_powheg15VBFH850.root",
8793  "HZZ4lTree_powheg15VBFH900.root",
8794  "HZZ4lTree_powheg15VBFH950.root",
8795  "HZZ4lTree_powheg15VBFH1000.root"
8796  };
8797  const int nSamples_JJQCD = 37;
8798  TString strSamples_JJQCD[nSamples_JJQCD]={
8799  "HZZ4lTree_minloH90.root",
8800  "HZZ4lTree_minloH95.root",
8801  "HZZ4lTree_minloH100.root",
8802  "HZZ4lTree_minloH105.root",
8803  "HZZ4lTree_minloH110.root",
8804  "HZZ4lTree_minloH115.root",
8805  "HZZ4lTree_minloH120.root",
8806  "HZZ4lTree_minloH124.root",
8807  "HZZ4lTree_minloH125.root",
8808  "HZZ4lTree_minloH126.root",
8809  "HZZ4lTree_minloH130.root",
8810  "HZZ4lTree_minloH135.root",
8811  "HZZ4lTree_minloH140.root",
8812  "HZZ4lTree_minloH145.root",
8813  "HZZ4lTree_minloH150.root",
8814  "HZZ4lTree_minloH155.root",
8815  "HZZ4lTree_minloH160.root",
8816  "HZZ4lTree_minloH170.root",
8817  "HZZ4lTree_minloH180.root",
8818  "HZZ4lTree_minloH190.root",
8819  "HZZ4lTree_minloH200.root",
8820  "HZZ4lTree_minloH250.root",
8821  "HZZ4lTree_minloH300.root",
8822  "HZZ4lTree_minloH350.root",
8823  "HZZ4lTree_minloH400.root",
8824  "HZZ4lTree_minloH450.root",
8825  "HZZ4lTree_minloH500.root",
8826  "HZZ4lTree_minloH550.root",
8827  "HZZ4lTree_minloH600.root",
8828  "HZZ4lTree_minloH650.root",
8829  "HZZ4lTree_minloH700.root",
8830  "HZZ4lTree_minloH750.root",
8831  "HZZ4lTree_minloH800.root",
8832  "HZZ4lTree_minloH850.root",
8833  "HZZ4lTree_minloH900.root",
8834  "HZZ4lTree_minloH950.root",
8835  "HZZ4lTree_minloH1000.root"
8836  };
8837 
8838  TChain* tree[2] ={
8839  new TChain(TREE_NAME, ""),
8840  new TChain(TREE_NAME, "")
8841  };
8842  for (int ic=0; ic<3; ic++){
8843  for (int is=0; is<nSamples_JJVBF; is++) tree[0]->Add(Form("%s/%s/%s", cinput_main.Data(), (strchannel[ic]).Data(), (strSamples_JJVBF[is]).Data()));
8844  for (int is=0; is<nSamples_JJQCD; is++) tree[1]->Add(Form("%s/%s/%s", cinput_main.Data(), (strchannel[ic]).Data(), (strSamples_JJQCD[is]).Data()));
8845  }
8846  for (int it=0; it<2; it++){
8847  tree[it]->SetBranchAddress("NJets30", &NJets30);
8848  tree[it]->SetBranchAddress("JetPt", &JetPt, &bJetPt);
8849  tree[it]->SetBranchAddress("JetEta", &JetEta, &bJetEta);
8850  tree[it]->SetBranchAddress("JetPhi", &JetPhi, &bJetPhi);
8851  tree[it]->SetBranchAddress("JetMass", &JetMass, &bJetMass);
8852  tree[it]->SetBranchAddress("ZZMass", &mzz);
8853  tree[it]->SetBranchAddress("ZZPt", &ZZPt);
8854  tree[it]->SetBranchAddress("ZZEta", &ZZEta);
8855  tree[it]->SetBranchAddress("ZZPhi", &ZZPhi);
8856  tree[it]->SetBranchAddress("Z1Mass", &m1);
8857  tree[it]->SetBranchAddress("Z2Mass", &m2);
8858  tree[it]->SetBranchAddress("helcosthetaZ1", &h1);
8859  tree[it]->SetBranchAddress("helcosthetaZ2", &h2);
8860  tree[it]->SetBranchAddress("helphi", &phi);
8861  tree[it]->SetBranchAddress("costhetastar", &hs);
8862  tree[it]->SetBranchAddress("phistarZ1", &phi1);
8863  }
8864 
8865  TFile* foutput = new TFile(Form("pJHUGen_JJVBF_JJQCD_HSMHiggs_Comparison_%iTeV.root", sqrts), "recreate");
8866 
8867  TH2F* hJJVBF = new TH2F("JJVBF", "", 50, 70, 1070, 10, 0, 1);
8868  TH2F* hJJQCD = new TH2F("JJQCD", "", 50, 70, 1070, 10, 0, 1);
8869 
8870  TProfile* prJJVBF = new TProfile("prJJVBF", "", 50, 70, 1070); prJJVBF->Sumw2();
8871  TProfile* prJJQCD = new TProfile("prJJQCD", "", 50, 70, 1070); prJJQCD->Sumw2();
8872 
8873  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
8874 
8875  int nTotalEntries = tree[0]->GetEntries();
8876  for (int ev = 0; ev < nTotalEntries; ev++){
8877  tree[0]->GetEntry(ev);
8878  if (ev%10000==0) cout << "Doing event " << ev << endl;
8879  if (NJets30>=2){
8880  for (int ij=0; ij<2; ij++){
8881  jetptetaphimass[ij][0]=JetPt->at(ij);
8882  jetptetaphimass[ij][1]=JetEta->at(ij);
8883  jetptetaphimass[ij][2]=JetPhi->at(ij);
8884  jetptetaphimass[ij][3]=JetMass->at(ij);
8885  }
8886 
8887  TLorentzVector jet[2], higgs;
8888  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
8889  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
8890  TVector3 boostH = higgs.BoostVector();
8891 
8893  associated.push_back(SimpleParticle_t(0, jet[0]));
8894  associated.push_back(SimpleParticle_t(0, jet[1]));
8895 
8896  TLorentzVector pDaughters[4];
8897  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
8898  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
8900  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
8901  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
8902 
8903  float mesq_jjvbf=0, mesq_jjqcd=0;
8906  mela.computeProdP(mesq_jjvbf, true);
8909  mela.computeProdP(mesq_jjqcd, true);
8910  float kd = mesq_jjvbf/(mesq_jjvbf+0.06*mesq_jjqcd);
8911 
8912  hJJVBF->Fill(mzz, kd);
8913  prJJVBF->Fill(mzz, mesq_jjvbf);
8914 
8915  mela.resetInputEvent();
8916  }
8917  }
8918  nTotalEntries = tree[1]->GetEntries();
8919  for (int ev = 0; ev < nTotalEntries; ev++){
8920  tree[1]->GetEntry(ev);
8921  if (ev%10000==0) cout << "Doing event " << ev << endl;
8922  if (NJets30>=2){
8923  for (int ij=0; ij<2; ij++){
8924  jetptetaphimass[ij][0]=JetPt->at(ij);
8925  jetptetaphimass[ij][1]=JetEta->at(ij);
8926  jetptetaphimass[ij][2]=JetPhi->at(ij);
8927  jetptetaphimass[ij][3]=JetMass->at(ij);
8928  }
8929 
8930  TLorentzVector jet[2], higgs;
8931  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
8932  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
8933  TVector3 boostH = higgs.BoostVector();
8934 
8936  associated.push_back(SimpleParticle_t(0, jet[0]));
8937  associated.push_back(SimpleParticle_t(0, jet[1]));
8938 
8939  TLorentzVector pDaughters[4];
8940  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
8941  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
8943  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
8944  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
8945 
8946  float mesq_jjvbf=0, mesq_jjqcd=0;
8949  mela.computeProdP(mesq_jjvbf, true);
8952  mela.computeProdP(mesq_jjqcd, true);
8953  float kd = mesq_jjvbf/(mesq_jjvbf+0.06*mesq_jjqcd);
8954 
8955  hJJQCD->Fill(mzz, kd);
8956  prJJQCD->Fill(mzz, mesq_jjqcd);
8957 
8958  mela.resetInputEvent();
8959  }
8960  }
8961 
8962  for (int ix=1; ix<=hJJVBF->GetNbinsX(); ix++){
8963  double integral = hJJVBF->Integral(ix, ix, 0, hJJVBF->GetNbinsY()+1);
8964  for (int iy=0; iy<=hJJVBF->GetNbinsY(); iy++){ if (integral!=0.) hJJVBF->SetBinContent(ix, iy, hJJVBF->GetBinContent(ix, iy)/integral); }
8965  }
8966  for (int ix=1; ix<=hJJQCD->GetNbinsX(); ix++){
8967  double integral = hJJQCD->Integral(ix, ix, 0, hJJQCD->GetNbinsY()+1);
8968  for (int iy=0; iy<=hJJQCD->GetNbinsY(); iy++){ if (integral!=0.) hJJQCD->SetBinContent(ix, iy, hJJQCD->GetBinContent(ix, iy)/integral); }
8969  }
8970 
8971  foutput->WriteTObject(hJJVBF);
8972  foutput->WriteTObject(hJJQCD);
8973  foutput->WriteTObject(prJJVBF);
8974  foutput->WriteTObject(prJJQCD);
8975  foutput->Close();
8976  for (int it=0; it<2; it++) delete tree[it];
8977 }
8978 
8980  int erg_tev=sqrts;
8981  float mPOLE=125.;
8982  TString TREE_NAME = "ZZTree/candTree";
8983 
8984  TVar::VerbosityLevel verbosity = TVar::ERROR;
8985  Mela mela(erg_tev, mPOLE, verbosity);
8986 
8987  short NJets30;
8988  std::vector<float>* JetPt=0;
8989  std::vector<float>* JetEta=0;
8990  std::vector<float>* JetPhi=0;
8991  std::vector<float>* JetMass=0;
8992  std::vector<float> myJetPt;
8993  std::vector<float> myJetEta;
8994  std::vector<float> myJetPhi;
8995  std::vector<float> myJetMass;
8996  TBranch* bJetPt=0;
8997  TBranch* bJetEta=0;
8998  TBranch* bJetPhi=0;
8999  TBranch* bJetMass=0;
9000  float jetptetaphimass[2][4];
9001 
9002  float mzz = 126.;
9003  float m1 = 91.471450;
9004  float m2 = 12.139782;
9005  float h1 = 0.2682896;
9006  float h2 = 0.1679779;
9007  float phi = 1.5969792;
9008  float hs = -0.727181;
9009  float phi1 = 1.8828257;
9010  float ZZPt, ZZPhi, ZZEta;
9011  int LepID[4]={ 13, -13, 11, -11 };
9012 
9013  TString cinput_main;
9014  if (sqrts==13) cinput_main = "/scratch0/hep/usarical/CJLST/LHC_13TeV/4l/160225";
9015  else return;
9016  const int nSamples_JJVBF = 33;
9017  TString strSamples_JJVBF[nSamples_JJVBF]={
9018  "VBFH115/ZZ4lAnalysis.root",
9019  "VBFH120/ZZ4lAnalysis.root",
9020  "VBFH124/ZZ4lAnalysis.root",
9021  "VBFH125/ZZ4lAnalysis.root",
9022  "VBFH126/ZZ4lAnalysis.root",
9023  "VBFH130/ZZ4lAnalysis.root",
9024  "VBFH135/ZZ4lAnalysis.root",
9025  "VBFH140/ZZ4lAnalysis.root",
9026  "VBFH145/ZZ4lAnalysis.root",
9027  "VBFH150/ZZ4lAnalysis.root",
9028  "VBFH155/ZZ4lAnalysis.root",
9029  "VBFH160/ZZ4lAnalysis.root",
9030  "VBFH165/ZZ4lAnalysis.root",
9031  "VBFH170/ZZ4lAnalysis.root",
9032  "VBFH175/ZZ4lAnalysis.root",
9033  "VBFH180/ZZ4lAnalysis.root",
9034  "VBFH190/ZZ4lAnalysis.root",
9035  "VBFH200/ZZ4lAnalysis.root",
9036  "VBFH210/ZZ4lAnalysis.root",
9037  "VBFH230/ZZ4lAnalysis.root",
9038  "VBFH250/ZZ4lAnalysis.root",
9039  "VBFH270/ZZ4lAnalysis.root",
9040  "VBFH300/ZZ4lAnalysis.root",
9041  "VBFH350/ZZ4lAnalysis.root",
9042  "VBFH400/ZZ4lAnalysis.root",
9043  "VBFH450/ZZ4lAnalysis.root",
9044  "VBFH500/ZZ4lAnalysis.root",
9045  "VBFH550/ZZ4lAnalysis.root",
9046  "VBFH600/ZZ4lAnalysis.root",
9047  "VBFH700/ZZ4lAnalysis.root",
9048  "VBFH750/ZZ4lAnalysis.root",
9049  "VBFH800/ZZ4lAnalysis.root",
9050  "VBFH900/ZZ4lAnalysis.root"
9051  };
9052  const int nSamples_JJQCD = 35;
9053  TString strSamples_JJQCD[nSamples_JJQCD]={
9054  "ggH91_GaZ/ZZ4lAnalysis.root",
9055  "ggH115/ZZ4lAnalysis.root",
9056  "ggH120/ZZ4lAnalysis.root",
9057  "ggH124/ZZ4lAnalysis.root",
9058  "ggH125/ZZ4lAnalysis.root",
9059  "ggH126/ZZ4lAnalysis.root",
9060  "ggH130/ZZ4lAnalysis.root",
9061  "ggH135/ZZ4lAnalysis.root",
9062  "ggH140/ZZ4lAnalysis.root",
9063  "ggH145/ZZ4lAnalysis.root",
9064  "ggH150/ZZ4lAnalysis.root",
9065  "ggH155/ZZ4lAnalysis.root",
9066  "ggH160/ZZ4lAnalysis.root",
9067  "ggH165/ZZ4lAnalysis.root",
9068  "ggH170/ZZ4lAnalysis.root",
9069  "ggH175/ZZ4lAnalysis.root",
9070  "ggH180/ZZ4lAnalysis.root",
9071  "ggH190/ZZ4lAnalysis.root",
9072  "ggH200/ZZ4lAnalysis.root",
9073  "ggH210/ZZ4lAnalysis.root",
9074  "ggH230/ZZ4lAnalysis.root",
9075  "ggH250/ZZ4lAnalysis.root",
9076  "ggH270/ZZ4lAnalysis.root",
9077  "ggH300/ZZ4lAnalysis.root",
9078  "ggH350/ZZ4lAnalysis.root",
9079  "ggH400/ZZ4lAnalysis.root",
9080  "ggH450/ZZ4lAnalysis.root",
9081  "ggH500/ZZ4lAnalysis.root",
9082  "ggH550/ZZ4lAnalysis.root",
9083  "ggH600/ZZ4lAnalysis.root",
9084  "ggH700/ZZ4lAnalysis.root",
9085  "ggH750/ZZ4lAnalysis.root",
9086  "ggH800/ZZ4lAnalysis.root",
9087  "ggH900/ZZ4lAnalysis.root",
9088  "ggH1000/ZZ4lAnalysis.root"
9089  };
9090 
9091  TChain* tree[2] ={
9092  new TChain(TREE_NAME, ""),
9093  new TChain(TREE_NAME, "")
9094  };
9095  for (int is=0; is<nSamples_JJVBF; is++) tree[0]->Add(Form("%s/%s", cinput_main.Data(), (strSamples_JJVBF[is]).Data()));
9096  for (int is=0; is<nSamples_JJQCD; is++) tree[1]->Add(Form("%s/%s", cinput_main.Data(), (strSamples_JJQCD[is]).Data()));
9097 
9098  for (int it=0; it<2; it++){
9099  tree[it]->SetBranchAddress("nCleanedJetsPt30", &NJets30);
9100  tree[it]->SetBranchAddress("JetPt", &JetPt, &bJetPt);
9101  tree[it]->SetBranchAddress("JetEta", &JetEta, &bJetEta);
9102  tree[it]->SetBranchAddress("JetPhi", &JetPhi, &bJetPhi);
9103  tree[it]->SetBranchAddress("JetMass", &JetMass, &bJetMass);
9104  tree[it]->SetBranchAddress("ZZMass", &mzz);
9105  tree[it]->SetBranchAddress("ZZPt", &ZZPt);
9106  tree[it]->SetBranchAddress("ZZEta", &ZZEta);
9107  tree[it]->SetBranchAddress("ZZPhi", &ZZPhi);
9108  tree[it]->SetBranchAddress("Z1Mass", &m1);
9109  tree[it]->SetBranchAddress("Z2Mass", &m2);
9110  tree[it]->SetBranchAddress("helcosthetaZ1", &h1);
9111  tree[it]->SetBranchAddress("helcosthetaZ2", &h2);
9112  tree[it]->SetBranchAddress("helphi", &phi);
9113  tree[it]->SetBranchAddress("costhetastar", &hs);
9114  tree[it]->SetBranchAddress("phistarZ1", &phi1);
9115  }
9116 
9117  TFile* foutput = new TFile(Form("pJHUGen_JJVBF_JJQCD_HSMHiggs_Comparison_%iTeV.root", sqrts), "recreate");
9118 
9119  TH2F* hJJVBF = new TH2F("JJVBF", "", 75, 70, 1570, 10, 0, 1);
9120  TH2F* hJJQCD = new TH2F("JJQCD", "", 75, 70, 1570, 10, 0, 1);
9121 
9122  TProfile* prJJVBF = new TProfile("prJJVBF", "", 75, 70, 1570); prJJVBF->Sumw2();
9123  TProfile* prJJQCD = new TProfile("prJJQCD", "", 75, 70, 1570); prJJQCD->Sumw2();
9124 
9125  TProfile* prRatioForJJVBF = new TProfile("prRatioForJJVBF", "", 75, 70, 1570); prRatioForJJVBF->Sumw2();
9126  TProfile* prRatioForJJQCD = new TProfile("prRatioForJJQCD", "", 75, 70, 1570); prRatioForJJQCD->Sumw2();
9127 
9128  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
9129 
9130  int nTotalEntries = tree[0]->GetEntries();
9131  for (int ev = 0; ev < nTotalEntries; ev++){
9132  tree[0]->GetEntry(ev);
9133  if (ev%10000==0) cout << "Doing event " << ev << endl;
9134  if (NJets30>=2){
9135  for (int ij=0; ij<2; ij++){
9136  jetptetaphimass[ij][0]=JetPt->at(ij);
9137  jetptetaphimass[ij][1]=JetEta->at(ij);
9138  jetptetaphimass[ij][2]=JetPhi->at(ij);
9139  jetptetaphimass[ij][3]=JetMass->at(ij);
9140  }
9141 
9142  TLorentzVector jet[2], higgs;
9143  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
9144  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
9145  TVector3 boostH = higgs.BoostVector();
9146 
9148  associated.push_back(SimpleParticle_t(0, jet[0]));
9149  associated.push_back(SimpleParticle_t(0, jet[1]));
9150 
9151  TLorentzVector pDaughters[4];
9152  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
9153  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
9155  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
9156  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
9157 
9158  float mesq_jjvbf=0, mesq_jjqcd=0;
9161  mela.computeProdP(mesq_jjvbf, true);
9164  mela.computeProdP(mesq_jjqcd, true);
9165  float kd = mesq_jjvbf/(mesq_jjvbf+0.06*mesq_jjqcd);
9166 
9167  hJJVBF->Fill(mzz, kd);
9168  prJJVBF->Fill(mzz, mesq_jjvbf);
9169  if (mesq_jjvbf>0.) prRatioForJJVBF->Fill(mzz, mesq_jjqcd/mesq_jjvbf);
9170 
9171  mela.resetInputEvent();
9172  }
9173  }
9174  nTotalEntries = tree[1]->GetEntries();
9175  for (int ev = 0; ev < nTotalEntries; ev++){
9176  tree[1]->GetEntry(ev);
9177  if (ev%10000==0) cout << "Doing event " << ev << endl;
9178  if (NJets30>=2){
9179  for (int ij=0; ij<2; ij++){
9180  jetptetaphimass[ij][0]=JetPt->at(ij);
9181  jetptetaphimass[ij][1]=JetEta->at(ij);
9182  jetptetaphimass[ij][2]=JetPhi->at(ij);
9183  jetptetaphimass[ij][3]=JetMass->at(ij);
9184  }
9185 
9186  TLorentzVector jet[2], higgs;
9187  for (int ij=0; ij<2; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
9188  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
9189  TVector3 boostH = higgs.BoostVector();
9190 
9192  associated.push_back(SimpleParticle_t(0, jet[0]));
9193  associated.push_back(SimpleParticle_t(0, jet[1]));
9194 
9195  TLorentzVector pDaughters[4];
9196  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
9197  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
9199  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
9200  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
9201 
9202  float mesq_jjvbf=0, mesq_jjqcd=0;
9205  mela.computeProdP(mesq_jjvbf, true);
9208  mela.computeProdP(mesq_jjqcd, true);
9209  float kd = mesq_jjvbf/(mesq_jjvbf+0.06*mesq_jjqcd);
9210 
9211  hJJQCD->Fill(mzz, kd);
9212  prJJQCD->Fill(mzz, mesq_jjqcd);
9213  if (mesq_jjvbf>0.) prRatioForJJQCD->Fill(mzz, mesq_jjqcd/mesq_jjvbf);
9214 
9215  mela.resetInputEvent();
9216  }
9217  }
9218 
9219  for (int ix=1; ix<=hJJVBF->GetNbinsX(); ix++){
9220  double integral = hJJVBF->Integral(ix, ix, 0, hJJVBF->GetNbinsY()+1);
9221  for (int iy=0; iy<=hJJVBF->GetNbinsY(); iy++){ if (integral!=0.) hJJVBF->SetBinContent(ix, iy, hJJVBF->GetBinContent(ix, iy)/integral); }
9222  }
9223  for (int ix=1; ix<=hJJQCD->GetNbinsX(); ix++){
9224  double integral = hJJQCD->Integral(ix, ix, 0, hJJQCD->GetNbinsY()+1);
9225  for (int iy=0; iy<=hJJQCD->GetNbinsY(); iy++){ if (integral!=0.) hJJQCD->SetBinContent(ix, iy, hJJQCD->GetBinContent(ix, iy)/integral); }
9226  }
9227 
9228  foutput->WriteTObject(hJJVBF);
9229  foutput->WriteTObject(hJJQCD);
9230  foutput->WriteTObject(prJJVBF);
9231  foutput->WriteTObject(prJJQCD);
9232  foutput->WriteTObject(prRatioForJJVBF);
9233  foutput->WriteTObject(prRatioForJJQCD);
9234  foutput->Close();
9235  for (int it=0; it<2; it++) delete tree[it];
9236 }
9237 
9238 
9240  int erg_tev=sqrts;
9241  float mPOLE=125.;
9242  TString TREE_NAME = "SelectedTree";
9243 
9244  TVar::VerbosityLevel verbosity = TVar::ERROR;
9245  Mela mela(erg_tev, mPOLE, verbosity);
9246 
9247  short NJets30;
9248  std::vector<double>* JetPt=0;
9249  std::vector<double>* JetEta=0;
9250  std::vector<double>* JetPhi=0;
9251  std::vector<double>* JetMass=0;
9252  std::vector<double> myJetPt;
9253  std::vector<double> myJetEta;
9254  std::vector<double> myJetPhi;
9255  std::vector<double> myJetMass;
9256  TBranch* bJetPt=0;
9257  TBranch* bJetEta=0;
9258  TBranch* bJetPhi=0;
9259  TBranch* bJetMass=0;
9260  float jetptetaphimass[2][4];
9261 
9262  float mzz = 126.;
9263  float m1 = 91.471450;
9264  float m2 = 12.139782;
9265  float h1 = 0.2682896;
9266  float h2 = 0.1679779;
9267  float phi = 1.5969792;
9268  float hs = -0.727181;
9269  float phi1 = 1.8828257;
9270  float ZZPt, ZZPhi, ZZEta;
9271  int LepID[4]={ 13, -13, 11, -11 };
9272 
9273  const int nMEs=1;
9274  TChain* tree[nMEs] ={
9275  new TChain(TREE_NAME, "")
9276  };
9277 
9278  TString strchannel[3]={ "4mu", "4e", "2mu2e" };
9279  TString cinput_main;
9280  if (sqrts==8) cinput_main = "/scratch0/hep/ianderso/CJLST/140519/PRODFSR_8TeV";
9281  else if (sqrts==7) cinput_main = "/scratch0/hep/ianderso/CJLST/140519/PRODFSR";
9282  else return;
9283  const int nSamples_JQCD = 37;
9284  TString strSamples_JQCD[nSamples_JQCD]={
9285  "HZZ4lTree_minloH90.root",
9286  "HZZ4lTree_minloH95.root",
9287  "HZZ4lTree_minloH100.root",
9288  "HZZ4lTree_minloH105.root",
9289  "HZZ4lTree_minloH110.root",
9290  "HZZ4lTree_minloH115.root",
9291  "HZZ4lTree_minloH120.root",
9292  "HZZ4lTree_minloH124.root",
9293  "HZZ4lTree_minloH125.root",
9294  "HZZ4lTree_minloH126.root",
9295  "HZZ4lTree_minloH130.root",
9296  "HZZ4lTree_minloH135.root",
9297  "HZZ4lTree_minloH140.root",
9298  "HZZ4lTree_minloH145.root",
9299  "HZZ4lTree_minloH150.root",
9300  "HZZ4lTree_minloH155.root",
9301  "HZZ4lTree_minloH160.root",
9302  "HZZ4lTree_minloH170.root",
9303  "HZZ4lTree_minloH180.root",
9304  "HZZ4lTree_minloH190.root",
9305  "HZZ4lTree_minloH200.root",
9306  "HZZ4lTree_minloH250.root",
9307  "HZZ4lTree_minloH300.root",
9308  "HZZ4lTree_minloH350.root",
9309  "HZZ4lTree_minloH400.root",
9310  "HZZ4lTree_minloH450.root",
9311  "HZZ4lTree_minloH500.root",
9312  "HZZ4lTree_minloH550.root",
9313  "HZZ4lTree_minloH600.root",
9314  "HZZ4lTree_minloH650.root",
9315  "HZZ4lTree_minloH700.root",
9316  "HZZ4lTree_minloH750.root",
9317  "HZZ4lTree_minloH800.root",
9318  "HZZ4lTree_minloH850.root",
9319  "HZZ4lTree_minloH900.root",
9320  "HZZ4lTree_minloH950.root",
9321  "HZZ4lTree_minloH1000.root"
9322  };
9323  for (int ic=0; ic<3; ic++){
9324  for (int is=0; is<nSamples_JQCD; is++) tree[0]->Add(Form("%s/%s/%s", cinput_main.Data(), (strchannel[ic]).Data(), (strSamples_JQCD[is]).Data()));
9325  }
9326  for (int it=0; it<nMEs; it++){
9327  tree[it]->SetBranchAddress("NJets30", &NJets30);
9328  tree[it]->SetBranchAddress("JetPt", &JetPt, &bJetPt);
9329  tree[it]->SetBranchAddress("JetEta", &JetEta, &bJetEta);
9330  tree[it]->SetBranchAddress("JetPhi", &JetPhi, &bJetPhi);
9331  tree[it]->SetBranchAddress("JetMass", &JetMass, &bJetMass);
9332  tree[it]->SetBranchAddress("ZZMass", &mzz);
9333  tree[it]->SetBranchAddress("ZZPt", &ZZPt);
9334  tree[it]->SetBranchAddress("ZZEta", &ZZEta);
9335  tree[it]->SetBranchAddress("ZZPhi", &ZZPhi);
9336  tree[it]->SetBranchAddress("Z1Mass", &m1);
9337  tree[it]->SetBranchAddress("Z2Mass", &m2);
9338  tree[it]->SetBranchAddress("helcosthetaZ1", &h1);
9339  tree[it]->SetBranchAddress("helcosthetaZ2", &h2);
9340  tree[it]->SetBranchAddress("helphi", &phi);
9341  tree[it]->SetBranchAddress("costhetastar", &hs);
9342  tree[it]->SetBranchAddress("phistarZ1", &phi1);
9343  }
9344 
9345  TFile* foutput = new TFile(Form("pJHUGen_JQCD_HSMHiggs_Comparison_%iTeV.root", sqrts), "recreate");
9346 
9347  TProfile* prJQCD = new TProfile("prJQCD", "", 75, 70, 1570); prJQCD->Sumw2();
9348 
9349  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
9350 
9351  int nTotalEntries = tree[0]->GetEntries();
9352  for (int ev = 0; ev < nTotalEntries; ev++){
9353  tree[0]->GetEntry(ev);
9354  if (ev%10000==0) cout << "Doing event " << ev << endl;
9355  if (NJets30==1){
9356  for (int ij=0; ij<1; ij++){
9357  jetptetaphimass[ij][0]=JetPt->at(ij);
9358  jetptetaphimass[ij][1]=JetEta->at(ij);
9359  jetptetaphimass[ij][2]=JetPhi->at(ij);
9360  jetptetaphimass[ij][3]=JetMass->at(ij);
9361  }
9362 
9363  TLorentzVector jet[1], higgs;
9364  for (int ij=0; ij<1; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
9365  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
9366  TVector3 boostH = higgs.BoostVector();
9367 
9369  associated.push_back(SimpleParticle_t(0, jet[0]));
9370 
9371  TLorentzVector pDaughters[4];
9372  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
9373  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
9375  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
9376  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
9377 
9378  float mesq_jqcd=0;
9380  mela.computeProdP(mesq_jqcd, true);
9381 
9382  prJQCD->Fill(mzz, mesq_jqcd);
9383 
9384  mela.resetInputEvent();
9385  }
9386  }
9387 
9388  foutput->WriteTObject(prJQCD);
9389  foutput->Close();
9390  for (int it=0; it<nMEs; it++) delete tree[it];
9391 }
9392 
9394  int erg_tev=sqrts;
9395  float mPOLE=125.;
9396  TString TREE_NAME = "ZZTree/candTree";
9397 
9398  TVar::VerbosityLevel verbosity = TVar::ERROR;
9399  Mela mela(erg_tev, mPOLE, verbosity);
9400 
9401  short NJets30;
9402  std::vector<float>* JetPt=0;
9403  std::vector<float>* JetEta=0;
9404  std::vector<float>* JetPhi=0;
9405  std::vector<float>* JetMass=0;
9406  std::vector<float> myJetPt;
9407  std::vector<float> myJetEta;
9408  std::vector<float> myJetPhi;
9409  std::vector<float> myJetMass;
9410  TBranch* bJetPt=0;
9411  TBranch* bJetEta=0;
9412  TBranch* bJetPhi=0;
9413  TBranch* bJetMass=0;
9414  float jetptetaphimass[2][4];
9415 
9416  float mzz = 126.;
9417  float m1 = 91.471450;
9418  float m2 = 12.139782;
9419  float h1 = 0.2682896;
9420  float h2 = 0.1679779;
9421  float phi = 1.5969792;
9422  float hs = -0.727181;
9423  float phi1 = 1.8828257;
9424  float ZZPt, ZZPhi, ZZEta;
9425  int LepID[4]={ 13, -13, 11, -11 };
9426 
9427  const int nMEs=1;
9428  TChain* tree[nMEs] ={
9429  new TChain(TREE_NAME, "")
9430  };
9431 
9432  TString cinput_main;
9433  if (sqrts==13) cinput_main = "/scratch0/hep/usarical/CJLST/LHC_13TeV/4l/160225";
9434  else return;
9435  const int nSamples_JQCD = 35;
9436  TString strSamples_JQCD[nSamples_JQCD]={
9437  "ggH91_GaZ/ZZ4lAnalysis.root",
9438  "ggH115/ZZ4lAnalysis.root",
9439  "ggH120/ZZ4lAnalysis.root",
9440  "ggH124/ZZ4lAnalysis.root",
9441  "ggH125/ZZ4lAnalysis.root",
9442  "ggH126/ZZ4lAnalysis.root",
9443  "ggH130/ZZ4lAnalysis.root",
9444  "ggH135/ZZ4lAnalysis.root",
9445  "ggH140/ZZ4lAnalysis.root",
9446  "ggH145/ZZ4lAnalysis.root",
9447  "ggH150/ZZ4lAnalysis.root",
9448  "ggH155/ZZ4lAnalysis.root",
9449  "ggH160/ZZ4lAnalysis.root",
9450  "ggH165/ZZ4lAnalysis.root",
9451  "ggH170/ZZ4lAnalysis.root",
9452  "ggH175/ZZ4lAnalysis.root",
9453  "ggH180/ZZ4lAnalysis.root",
9454  "ggH190/ZZ4lAnalysis.root",
9455  "ggH200/ZZ4lAnalysis.root",
9456  "ggH210/ZZ4lAnalysis.root",
9457  "ggH230/ZZ4lAnalysis.root",
9458  "ggH250/ZZ4lAnalysis.root",
9459  "ggH270/ZZ4lAnalysis.root",
9460  "ggH300/ZZ4lAnalysis.root",
9461  "ggH350/ZZ4lAnalysis.root",
9462  "ggH400/ZZ4lAnalysis.root",
9463  "ggH450/ZZ4lAnalysis.root",
9464  "ggH500/ZZ4lAnalysis.root",
9465  "ggH550/ZZ4lAnalysis.root",
9466  "ggH600/ZZ4lAnalysis.root",
9467  "ggH700/ZZ4lAnalysis.root",
9468  "ggH750/ZZ4lAnalysis.root",
9469  "ggH800/ZZ4lAnalysis.root",
9470  "ggH900/ZZ4lAnalysis.root",
9471  "ggH1000/ZZ4lAnalysis.root"
9472  };
9473  for (int is=0; is<nSamples_JQCD; is++) tree[0]->Add(Form("%s/%s", cinput_main.Data(), (strSamples_JQCD[is]).Data()));
9474  for (int it=0; it<nMEs; it++){
9475  tree[it]->SetBranchAddress("nCleanedJetsPt30", &NJets30);
9476  tree[it]->SetBranchAddress("JetPt", &JetPt, &bJetPt);
9477  tree[it]->SetBranchAddress("JetEta", &JetEta, &bJetEta);
9478  tree[it]->SetBranchAddress("JetPhi", &JetPhi, &bJetPhi);
9479  tree[it]->SetBranchAddress("JetMass", &JetMass, &bJetMass);
9480  tree[it]->SetBranchAddress("ZZMass", &mzz);
9481  tree[it]->SetBranchAddress("ZZPt", &ZZPt);
9482  tree[it]->SetBranchAddress("ZZEta", &ZZEta);
9483  tree[it]->SetBranchAddress("ZZPhi", &ZZPhi);
9484  tree[it]->SetBranchAddress("Z1Mass", &m1);
9485  tree[it]->SetBranchAddress("Z2Mass", &m2);
9486  tree[it]->SetBranchAddress("helcosthetaZ1", &h1);
9487  tree[it]->SetBranchAddress("helcosthetaZ2", &h2);
9488  tree[it]->SetBranchAddress("helphi", &phi);
9489  tree[it]->SetBranchAddress("costhetastar", &hs);
9490  tree[it]->SetBranchAddress("phistarZ1", &phi1);
9491  }
9492 
9493  TFile* foutput = new TFile(Form("pJHUGen_JQCD_HSMHiggs_Comparison_%iTeV.root", sqrts), "recreate");
9494 
9495  TProfile* prJQCD = new TProfile("prJQCD", "", 35, 70, 1575); prJQCD->Sumw2();
9496 
9497  mela.setCandidateDecayMode(TVar::CandidateDecay_ZZ);
9498 
9499  int nTotalEntries = tree[0]->GetEntries();
9500  for (int ev = 0; ev < nTotalEntries; ev++){
9501  tree[0]->GetEntry(ev);
9502  if (ev%10000==0) cout << "Doing event " << ev << endl;
9503  if (NJets30==1){
9504  for (int ij=0; ij<1; ij++){
9505  jetptetaphimass[ij][0]=JetPt->at(ij);
9506  jetptetaphimass[ij][1]=JetEta->at(ij);
9507  jetptetaphimass[ij][2]=JetPhi->at(ij);
9508  jetptetaphimass[ij][3]=JetMass->at(ij);
9509  }
9510 
9511  TLorentzVector jet[1], higgs;
9512  for (int ij=0; ij<1; ij++) jet[ij].SetPtEtaPhiM(jetptetaphimass[ij][0], jetptetaphimass[ij][1], jetptetaphimass[ij][2], jetptetaphimass[ij][3]);
9513  higgs.SetPtEtaPhiM(ZZPt, ZZEta, ZZPhi, mzz);
9514  TVector3 boostH = higgs.BoostVector();
9515 
9517  associated.push_back(SimpleParticle_t(0, jet[0]));
9518 
9519  TLorentzVector pDaughters[4];
9520  std::vector<TLorentzVector> daus = mela.calculate4Momentum(mzz, m1, m2, acos(hs), acos(h1), acos(h2), phi1, phi);
9521  for (int ip=0; ip<min(4, (int)daus.size()); ip++){ pDaughters[ip]=daus.at(ip); pDaughters[ip].Boost(boostH); }
9523  for (unsigned int idau=0; idau<4; idau++) daughters.push_back(SimpleParticle_t(LepID[idau], pDaughters[idau]));
9524  mela.setInputEvent(&daughters, &associated, (SimpleParticleCollection_t*)0, false);
9525 
9526  float mesq_jqcd=0;
9528  mela.computeProdP(mesq_jqcd, true);
9529 
9530  prJQCD->Fill(mzz, mesq_jqcd);
9531 
9532  mela.resetInputEvent();
9533  }
9534  }
9535 
9536  foutput->WriteTObject(prJQCD);
9537  foutput->Close();
9538  for (int it=0; it<nMEs; it++) delete tree[it];
9539 }
TVar::ERROR
@ ERROR
Definition: TVar.hh:49
RooSpin::modelMeasurables::h2
RooAbsReal * h2
Definition: RooSpin.h:52
SimpleParticle_t
std::pair< int, TLorentzVector > SimpleParticle_t
Definition: TVar.hh:24
TVar::JJVBF
@ JJVBF
Definition: TVar.hh:72
get_PAvgProfile_JHUGen_JJQCD_HSMHiggs_7or8TeV
void get_PAvgProfile_JHUGen_JJQCD_HSMHiggs_7or8TeV(int sqrts=8)
Definition: calcC_lintolog.c:965
TVar::Had_ZH_S
@ Had_ZH_S
Definition: TVar.hh:86
mela.daughters
string daughters
Definition: mela.py:767
value
pymela::gHIGGS_KAPPA value("gHIGGS_KAPPA_TILDE", pymela::gHIGGS_KAPPA_TILDE) .value("SIZE_HQQ"
get_PAvgProfile_JHUGen_JJQCD_HSMHiggs_13TeV
void get_PAvgProfile_JHUGen_JJQCD_HSMHiggs_13TeV(int sqrts=13, bool recalculate=true)
Definition: calcC_lintolog.c:1875
check_JQCD_7or8TeV
void check_JQCD_7or8TeV(int sqrts=8)
Definition: calcC_lintolog.c:9239
RooSpin::modelMeasurables::Y
RooAbsReal * Y
Definition: RooSpin.h:59
produce_PAvgSmooth_JHUGen_HadVH_HSMHiggs
void produce_PAvgSmooth_JHUGen_HadVH_HSMHiggs(TString strprod, int sqrts=13)
Definition: calcC_lintolog.c:8529
TVar::VerbosityLevel
VerbosityLevel
Definition: TVar.hh:47
inputdir_8TeV
TString inputdir_8TeV
Definition: calcC_lintolog.c:41
ExtBin::binlow
double binlow
Definition: calcC_lintolog.c:112
get_PAvgProfile_JHUGen_JQCD_HSMHiggs_13TeV
void get_PAvgProfile_JHUGen_JQCD_HSMHiggs_13TeV(int sqrts=13, bool recalculate=true)
Definition: calcC_lintolog.c:2189
mela
Definition: mela.py:1
get_PAvgProfile_MCFM_JJQCD_bkgZZ_13TeV
void get_PAvgProfile_MCFM_JJQCD_bkgZZ_13TeV(int sqrts=13, bool recalculate=true)
Definition: calcC_lintolog.c:3807
TVar::CandidateDecay_ZZ
@ CandidateDecay_ZZ
Definition: TVar.hh:41
hto_masses::mz
real *8, parameter mz
Definition: CALLING_cpHTO.f:76
RooSpin::modelMeasurables::Phi1
RooAbsReal * Phi1
Definition: RooSpin.h:55
ExtBin::masses
vector< float > masses
Definition: calcC_lintolog.c:115
getFcn_a0plusa1timesX
TF1 * getFcn_a0plusa1timesX(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6939
modparameters::a2
complex(8), public a2
Definition: mod_Parameters.F90:924
PointRedivision
Definition: calcC_lintolog.c:7493
get_PAvgProfile_MCFM_ZZQQB_bkgZZ
void get_PAvgProfile_MCFM_ZZQQB_bkgZZ(bool recalculate=false)
Definition: calcC_lintolog.c:5809
findPoleMass
float findPoleMass(TString samplename)
Definition: calcC_lintolog.c:214
TVar::ProductionName
TString ProductionName(TVar::Production temp)
Definition: TVar.cc:64
produce_PAvgSmooth_JHUGen_JJVBF_HSMHiggs
void produce_PAvgSmooth_JHUGen_JJVBF_HSMHiggs(int sqrts=8)
Definition: calcC_lintolog.c:8491
getPatch_a0plusa1timesX
TGraph * getPatch_a0plusa1timesX(TSpline3 *sp, TSpline3 *sppatch, double xmin, double xmax, bool useLowBound, bool forceOutput=true)
Definition: calcC_lintolog.c:7111
ScalarPdfFactory_VH.h
regularizeSlice
void regularizeSlice(TGraphErrors *tgSlice, std::vector< double > *fixedX=0, double omitbelow=0., int nIter_=-1, double threshold_=-1)
Definition: calcC_lintolog.c:7193
RooSpinZero::modelCouplings::g2List
RooAbsReal * g2List[8][2]
Definition: RooSpinZero.h:12
getFcn_a0overX2minusa1overX
TF1 * getFcn_a0overX2minusa1overX(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6891
RooSpin::modelMeasurables::m12
RooAbsReal * m12
Definition: RooSpin.h:58
getPatch_a0expa1timesX
TGraph * getPatch_a0expa1timesX(TSpline3 *sp, TSpline3 *sppatch, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:7157
produce_PAvgSmooth_MCFM_ZZQQB_bkgZZ
void produce_PAvgSmooth_MCFM_ZZQQB_bkgZZ()
Definition: calcC_lintolog.c:8598
TVar::bkgZJets
@ bkgZJets
Definition: TVar.hh:165
get_PAvgProfile_JHUGen_ZZINDEPENDENT_HSMHiggs
void get_PAvgProfile_JHUGen_ZZINDEPENDENT_HSMHiggs(bool recalculate=false)
Definition: calcC_lintolog.c:4170
TVar::ZZINDEPENDENT
@ ZZINDEPENDENT
Definition: TVar.hh:64
ExtBin::events
vector< int > events
Definition: calcC_lintolog.c:114
TVar::Process
Process
Definition: TVar.hh:125
TVar::JJQCD
@ JJQCD
Definition: TVar.hh:71
RooSpin::modelMeasurables::m1
RooAbsReal * m1
Definition: RooSpin.h:56
removePointsBetween
TGraphErrors * removePointsBetween(TGraphErrors *tgSlice, double xmin, double xmax)
Definition: calcC_lintolog.c:7305
produce_PAvgSmooth_MCFM_ZZGG_HSMHiggs
void produce_PAvgSmooth_MCFM_ZZGG_HSMHiggs()
Definition: calcC_lintolog.c:8569
mela.prod
prod
Definition: mela.py:806
TVar::ZZQQB
@ ZZQQB
Definition: TVar.hh:62
ExtBin::sift
void sift()
Definition: calcC_lintolog.c:126
modparameters::second
real(8), parameter, public second
Definition: mod_Parameters.F90:105
modmisc::isnan
logical function isnan(x)
Definition: mod_Misc.F90:380
ExtBin::mergeBin
void mergeBin(const ExtBin &other)
Definition: calcC_lintolog.c:190
PointRedivision::xlow
double xlow
Definition: calcC_lintolog.c:7495
addPoint
void addPoint(TGraphErrors *&tg, double x, double y, double ex, double ey)
Definition: calcC_lintolog.c:7375
convertGraphToSpline3
TSpline3 * convertGraphToSpline3(TGraph *tg, double *dfirst=0, double *dlast=0)
Definition: calcC_lintolog.c:6759
get_PAvgProfile_ANALYTICAL_HadVH_HSMHiggs
void get_PAvgProfile_ANALYTICAL_HadVH_HSMHiggs(TString strprod, int sqrts)
Definition: calcC_lintolog.c:6354
TVar::ZZGG
@ ZZGG
Definition: TVar.hh:61
TVar::ProcessName
TString ProcessName(TVar::Process temp)
Definition: TVar.cc:6
TVar::MomentumToEnergy
@ MomentumToEnergy
Definition: TVar.hh:114
TUtil::setLeptonMassScheme
void setLeptonMassScheme(TVar::FermionMassRemoval scheme=TVar::ConserveDifermionMass)
Definition: TUtil.cc:37
getPatch_a0a1
TGraph * getPatch_a0a1(TSpline3 *sp, TSpline3 *sppatch, double xmin, double xmax, bool useLowBound, bool forceOutput=true)
Definition: calcC_lintolog.c:7027
produce_PAvgSmooth_JHUGen_JJQCD_HSMHiggs
void produce_PAvgSmooth_JHUGen_JJQCD_HSMHiggs(int sqrts=8)
Definition: calcC_lintolog.c:8504
inputdir_13TeV
TString inputdir_13TeV
Definition: calcC_lintolog.c:42
getFcn_a0plusXPinvminusXpsqinv
TF1 * getFcn_a0plusXPinvminusXpsqinv(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6913
modparameters::ev
real(8), parameter, public ev
Definition: mod_Parameters.F90:97
get_PAvgProfile_ANALYTICAL_ZZGG_HSMHiggs
void get_PAvgProfile_ANALYTICAL_ZZGG_HSMHiggs()
Definition: calcC_lintolog.c:6266
testME_all.int
int
Definition: testME_all.py:13
SpinPdfFactory::parameters
RooSpin::modelParameters parameters
Definition: SpinPdfFactory.h:12
get_PAvgProfile_JHUGen_JJVBF_HSMHiggs_7or8TeV
void get_PAvgProfile_JHUGen_JJVBF_HSMHiggs_7or8TeV(int sqrts=8)
Definition: calcC_lintolog.c:673
TVar::HSMHiggs
@ HSMHiggs
Definition: TVar.hh:126
xl
double xl[ndims]
Definition: TMCFM.hh:46
addByLowest
void addByLowest(std::vector< T > &valArray, T val, bool unique)
Definition: calcC_lintolog.c:45
get_PAvgProfile_MCFM_JJPROD_S_HSMHiggs_13TeV
void get_PAvgProfile_MCFM_JJPROD_S_HSMHiggs_13TeV(TString strprod, int sqrts=13, bool recalculate=true)
Definition: calcC_lintolog.c:2903
ExtBin::me2vals
vector< float > me2vals
Definition: calcC_lintolog.c:117
get_PAvgProfile_MCFM_ZZGG_HSMHiggs
void get_PAvgProfile_MCFM_ZZGG_HSMHiggs(bool recalculate=false)
Definition: calcC_lintolog.c:4950
RooSpin::modelMeasurables::Phi
RooAbsReal * Phi
Definition: RooSpin.h:54
check_JJVBF_vs_JJQCD_7or8TeV
void check_JJVBF_vs_JJQCD_7or8TeV(int sqrts=8)
Definition: calcC_lintolog.c:8713
PointRedivision::npoints
unsigned int npoints
Definition: calcC_lintolog.c:7494
produce_PAvgSmooth_JHUGen_ZZGG_HSMHiggs
void produce_PAvgSmooth_JHUGen_ZZGG_HSMHiggs()
Definition: calcC_lintolog.c:8555
TVar::MCFM
@ MCFM
Definition: TVar.hh:56
RooSpin::modelParameters::mX
RooAbsReal * mX
Definition: RooSpin.h:62
produce_PAvgSmooth_MCFM_ZZGG_bkgZZ
void produce_PAvgSmooth_MCFM_ZZGG_bkgZZ()
Definition: calcC_lintolog.c:8583
getFcn_a0plusa1overX
TF1 * getFcn_a0plusa1overX(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6869
appendVector
void appendVector(std::vector< T > &a, std::vector< T > &b)
Definition: calcC_lintolog.c:79
mela.associated
string associated
Definition: mela.py:773
get_PAvgProfile_JHUGen_JJVBF_HSMHiggs_13TeV
void get_PAvgProfile_JHUGen_JJVBF_HSMHiggs_13TeV(int sqrts=13, bool recalculate=true)
Definition: calcC_lintolog.c:1566
get_PAvgProfile_ANALYTICAL_ZZQQB_bkgZZ
void get_PAvgProfile_ANALYTICAL_ZZQQB_bkgZZ()
Definition: calcC_lintolog.c:6176
get_PAvgProfile_JHUGen_JQCD_HSMHiggs_7or8TeV
void get_PAvgProfile_JHUGen_JQCD_HSMHiggs_7or8TeV(int sqrts=8)
Definition: calcC_lintolog.c:1262
TVar::ConserveDifermionMass
@ ConserveDifermionMass
Definition: TVar.hh:113
PointRedivision::PointRedivision
PointRedivision(unsigned int n, double xl, double xh)
Definition: calcC_lintolog.c:7497
RooSpin::modelMeasurables::m2
RooAbsReal * m2
Definition: RooSpin.h:57
TVar::MatrixElement
MatrixElement
Definition: TVar.hh:55
check_JQCD_13TeV
void check_JQCD_13TeV(int sqrts=13)
Definition: calcC_lintolog.c:9393
produce_PAvgSmooth_MCFM_JJPROD_S_HSMHiggs
void produce_PAvgSmooth_MCFM_JJPROD_S_HSMHiggs(TString strprod, int sqrts=13)
Definition: calcC_lintolog.c:8612
inputdir_7TeV
TString inputdir_7TeV
Definition: calcC_lintolog.c:40
RooSpin::modelMeasurables::h1
RooAbsReal * h1
Definition: RooSpin.h:51
check_JJVBF_vs_JJQCD_13TeV
void check_JJVBF_vs_JJQCD_13TeV(int sqrts=13)
Definition: calcC_lintolog.c:8979
TVar::JHUGen
@ JHUGen
Definition: TVar.hh:57
TVar::Had_ZH
@ Had_ZH
Definition: TVar.hh:75
getFcn_a0plusa1expX
TF1 * getFcn_a0plusa1expX(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6847
dd_global::cout
integer cout
Definition: DD_global.F90:21
RooSpin::modelParameters::gamX
RooAbsReal * gamX
Definition: RooSpin.h:63
get_PAvgProfile_MCFM_JJQCD_bkgZJets_13TeV_2l2q
void get_PAvgProfile_MCFM_JJQCD_bkgZJets_13TeV_2l2q()
Definition: calcC_lintolog.c:6517
SpinPdfFactory::makeParamsConst
virtual void makeParamsConst(bool yesNo)
Definition: SpinPdfFactory.cc:154
Mela
Definition: Mela.h:48
ScalarPdfFactory_VH::getPDF
RooSpinZero * getPDF()
Definition: ScalarPdfFactory_VH.cc:59
convertTSpline5ToTspline3
TSpline3 * convertTSpline5ToTspline3(TSpline5 *sp)
Definition: calcC_lintolog.c:6809
getFcn_a0plusa1timesXplusa2overX2
TF1 * getFcn_a0plusa1timesXplusa2overX2(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6961
TVar::Had_WH_S
@ Had_WH_S
Definition: TVar.hh:87
modparameters::a1
complex(8), public a1
Definition: mod_Parameters.F90:923
PointRedivision::xhigh
double xhigh
Definition: calcC_lintolog.c:7496
RooSpin::kVdecayType_Zud
@ kVdecayType_Zud
Definition: RooSpin.h:35
splitOption
void splitOption(const string rawoption, string &wish, string &value, char delimiter)
Definition: calcC_lintolog.c:88
TVar::bkgZZ
@ bkgZZ
Definition: TVar.hh:166
produce_PAvgSmooth_MCFM_JJQCD_bkgZZ
void produce_PAvgSmooth_MCFM_JJQCD_bkgZZ(int sqrts=13)
Definition: calcC_lintolog.c:8699
ExtBin::addEvent
void addEvent(float mass, float me, float me2, float weight=1)
Definition: calcC_lintolog.c:120
get_PAvgProfile_MCFM_JJPROD_bkgZZ_13TeV
void get_PAvgProfile_MCFM_JJPROD_bkgZZ_13TeV(TString strprod, int sqrts=13, bool recalculate=true)
Definition: calcC_lintolog.c:3321
constructSamplesList
vector< TString > constructSamplesList(TString strsample, float sqrts)
Definition: calcC_lintolog.c:253
get_PAvgProfile_JHUGen_ZZGG_HSMHiggs
void get_PAvgProfile_JHUGen_ZZGG_HSMHiggs(bool recalculate=false)
Definition: calcC_lintolog.c:4555
Mela.h
This is the "MELA" object that interfaces with the Fortran code in both MCFM-JHUGen and pure JHUGen.
ExtBin
Definition: calcC_lintolog.c:111
getPatch_a0plusa1overX
TGraph * getPatch_a0plusa1overX(TSpline3 *sp, TSpline3 *sppatch, double xmin, double xmax, bool useLowBound, bool forceOutput=true)
Definition: calcC_lintolog.c:7064
getFcn_a0plusa1timesXplusa2timesX2
TF1 * getFcn_a0plusa1timesXplusa2timesX2(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6994
checkListVariable
bool checkListVariable(const vector< T > &list, const T &var)
Definition: calcC_lintolog.c:81
produce_PAvgSmooth_MCFM_JJQCD_bkgZJets_2l2q
void produce_PAvgSmooth_MCFM_JJQCD_bkgZJets_2l2q(int sqrts=13)
Definition: calcC_lintolog.c:8433
ExtBin::mevals
vector< float > mevals
Definition: calcC_lintolog.c:116
ExtBin::binhigh
double binhigh
Definition: calcC_lintolog.c:113
getFcn_a0
TF1 * getFcn_a0(TSpline3 *sp, double xmin, double xmax, bool useLowBound)
Definition: calcC_lintolog.c:6828
generic_PAvgSmoothProducer_withDecay
void generic_PAvgSmoothProducer_withDecay(TVar::MatrixElement me, TVar::Production prod, TVar::Process proc, TF1 *(*lowf)(TSpline3 *, double, double, bool), TF1 *(*highf)(TSpline3 *, double, double, bool), int sqrts=-1, bool useFaithfulLowSlopes=false, bool useFaithfulHighSlopes=false, vector< PointRedivision > *redivision=nullptr)
Definition: calcC_lintolog.c:7707
TVar::JQCD
@ JQCD
Definition: TVar.hh:69
RooSpinZero::modelCouplings::g1List
RooAbsReal * g1List[8][2]
Definition: RooSpinZero.h:11
TVar::ANALYTICAL
@ ANALYTICAL
Definition: TVar.hh:58
ScalarPdfFactory::couplings
RooSpinZero::modelCouplings couplings
Definition: ScalarPdfFactory.h:12
get_PAvgProfile_MCFM_ZZGG_bkgZZ
void get_PAvgProfile_MCFM_ZZGG_bkgZZ(bool recalculate=false)
Definition: calcC_lintolog.c:5340
xmin
double xmin
Definition: TMCFM.hh:72
convertGraphToSpline5
TSpline5 * convertGraphToSpline5(TGraph *tg, double *dfirst=0, double *dlast=0)
Definition: calcC_lintolog.c:6795
SimpleParticleCollection_t
std::vector< SimpleParticle_t > SimpleParticleCollection_t
Definition: TVar.hh:25
RooSpin::kVdecayType_Wany
@ kVdecayType_Wany
Definition: RooSpin.h:29
RooSpinZero::modelCouplings::g4List
RooAbsReal * g4List[8][2]
Definition: RooSpinZero.h:14
globalc::f
double complex, dimension(2) f
Definition: reductionC.F90:50
modvhiggs::propagator
complex(8) function propagator(inv_mass, mass, width)
Definition: mod_VHiggs.F90:1357
TVar::Had_WH
@ Had_WH
Definition: TVar.hh:76
RooSpin::modelMeasurables
Definition: RooSpin.h:50
sqrts
double sqrts
Definition: TMCFM.hh:290
TVar::Production
Production
Definition: TVar.hh:60
splitOptionRecursive
void splitOptionRecursive(const string rawoption, vector< string > &splitoptions, char delimiter)
Definition: calcC_lintolog.c:100
hto_masses::me
real *8, parameter me
Definition: CALLING_cpHTO.f:77
ExtBin::adjustWeights
void adjustWeights(float refth=3.)
Definition: calcC_lintolog.c:158
TUtil::setJetMassScheme
void setJetMassScheme(TVar::FermionMassRemoval scheme=TVar::ConserveDifermionMass)
Definition: TUtil.cc:38
findTree
TTree * findTree(vector< TTree * > treeList, int evid)
Definition: calcC_lintolog.c:230
TVar::JJVBF_S
@ JJVBF_S
Definition: TVar.hh:83
get_PAvgProfile_JHUGen_HadVH_HSMHiggs_13TeV
void get_PAvgProfile_JHUGen_HadVH_HSMHiggs_13TeV(TString strprod, int sqrts=13, bool recalculate=true)
Definition: calcC_lintolog.c:2500
RooSpin::modelMeasurables::hs
RooAbsReal * hs
Definition: RooSpin.h:53
rebinAverageME
void rebinAverageME(TGraphErrors *&tg, TTree *&tree, TProfile *&pmass, PointRedivision &rediv, float &ZZMass, float &mesq, float &weight)
Definition: calcC_lintolog.c:7500
TVar::nProductions
@ nProductions
Definition: TVar.hh:104
TVar::MatrixElementName
TString MatrixElementName(TVar::MatrixElement temp)
Definition: TVar.cc:109
produce_PAvgSmooth_MCFM_JJPROD_bkgZZ
void produce_PAvgSmooth_MCFM_JJPROD_bkgZZ(TString strprod, int sqrts=13)
Definition: calcC_lintolog.c:8657
generic_PAvgSmoothProducer
void generic_PAvgSmoothProducer(TVar::MatrixElement me, TVar::Production prod, TVar::Process proc, TF1 *(*lowf)(TSpline3 *, double, double, bool), TF1 *(*highf)(TSpline3 *, double, double, bool), int sqrts=-1, bool useFaithfulLowSlopes=false, bool useFaithfulHighSlopes=false, vector< PointRedivision > *redivision=nullptr)
Definition: calcC_lintolog.c:7581
getEntry
void getEntry(vector< TTree * > treeList, int evid)
Definition: calcC_lintolog.c:241
addPointAfterBin
TGraphErrors * addPointAfterBin(TGraphErrors *tgSlice, int abin)
Definition: calcC_lintolog.c:7458
RooSpin::VdecayType
VdecayType
Definition: RooSpin.h:28
ExtBin::weights
vector< float > weights
Definition: calcC_lintolog.c:118
ScalarPdfFactory_VH
Definition: ScalarPdfFactory_VH.h:9
produce_PAvgSmooth_JHUGen_JQCD_HSMHiggs
void produce_PAvgSmooth_JHUGen_JQCD_HSMHiggs(int sqrts=8)
Definition: calcC_lintolog.c:8517
replacePointsBetween
TGraphErrors * replacePointsBetween(TGraphErrors *tgSlice, double xmin, double xmax)
Definition: calcC_lintolog.c:7332
scale
double scale
Definition: TMCFM.hh:55
convertGraphToSpline3_FaithfulSlopes
TSpline3 * convertGraphToSpline3_FaithfulSlopes(TGraph *tg, double *dfirst=0, double *dlast=0, bool faithfulLow=true, bool faithfulHigh=true)
Definition: calcC_lintolog.c:6773