-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameters.cc
1746 lines (1578 loc) · 66.1 KB
/
Parameters.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: Parameters.cc,v 1.63 2011/06/23 07:53:36 stadie Exp $
#include <fstream>
#include <cassert>
#include <pwd.h>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>
#include "Parameters.h"
#include "TFile.h"
#include "TH1.h"
#include "TH1D.h"
#include "TMath.h"
#include "TString.h"
using namespace std;
Parameters* Parameters::instance_ = 0;
std::vector<Parameters*> Parameters::clones_;
Parametrization* Parameters::p_ = 0;
ResolutionParametrization* Parameters::resParam_ = 0;
long long Parameters::ncalls_ = 0;
long long Parameters::ntries_ = 0;
long long Parameters::nfails_ = 0;
long long Parameters::nwarns_ = 0;
// -----------------------------------------------------------------
Parametrization* Parameters::createParametrization(const std::string& name, const ConfigFile& config) {
if(name == "StepParametrization") {
return new StepParametrization();
} else if(name == "StepParametrizationEnergy") {
return new StepParametrizationEnergy();
} else if(name == "StepEfracParametrization") {
return new StepEfracParametrization();
} else if(name == "StepJetParametrization") {
return new StepJetParametrization();
} else if(name == "MyParametrization") {
return new MyParametrization();
} else if(name == "JetMETParametrization") {
return new JetMETParametrization();
} else if(name == "GlobalScaleFactorParametrization") {
return new GlobalScaleFactorParametrization();
} else if(name == "SimpleParametrization") {
return new SimpleParametrization();
} else if(name == "ToyParametrization") {
return new ToyParametrization();
} else if(name == "ToyJetParametrization") {
return new ToyJetParametrization();
} else if(name == "ToyStepParametrization") {
return new ToyStepParametrization();
} else if(name == "ToyStepJetParametrization") {
return new ToyStepJetParametrization();
} else if(name == "TrackParametrization") {
return new TrackParametrization();
} else if(name == "L2L3JetParametrization") {
return new L2L3JetParametrization();
} else if(name == "L2L3JetParametrization2") {
return new L2L3JetParametrization2();
} else if(name == "L2L3JetTrackParametrization") {
return new L2L3JetTrackParametrization();
} else if(name == "ResidualJetParametrization") {
return new ResidualJetParametrization();
} else if(name == "ToySimpleInverseParametrization") {
return new ToySimpleInverseParametrization();
} else if(name == "GroomParametrization") {
return new GroomParametrization();
} else if(name == "EtaEtaParametrization") {
return new EtaEtaParametrization();
} else if(name == "PhiPhiParametrization") {
return new PhiPhiParametrization();
} else if(name == "BinnedEMFParametrization") {
return new BinnedEMFParametrization();
} else if(name == "BinnedPhiPhiParametrization") {
return new BinnedPhiPhiParametrization();
} else if(name == "BinnedPhiPhiParametrization2") {
return new BinnedPhiPhiParametrization2();
} else if(name == "BinnedScaledPhiPhiParametrization") {
return new BinnedScaledPhiPhiParametrization();
} else if(name == "BinnedScaledEtaEtaParametrization") {
return new BinnedScaledEtaEtaParametrization();
} else if(name == "SimplePhiPhiParametrization") {
return new SimplePhiPhiParametrization();
} else if(name == "MeanWidthParametrization") {
return new MeanWidthParametrization();
} else if(name == "MeanWidthParametrization_old") {
return new MeanWidthParametrization_old();
}
return 0;
}
// -----------------------------------------------------------------
ResolutionParametrization* Parameters::createResolutionParametrization(const std::string& name, const ConfigFile& config) {
std::vector<double> ptBinEdges = bag_of<double>(config.read<std::string>("PtAve bin edges","-2 -1"));
if( ptBinEdges.front() == -2. ) {
ptBinEdges.at(0) = config.read<double>("Et min cut on dijet",-1.);
ptBinEdges.at(1) = config.read<double>("Et max cut on dijet",-1.);
}
assert( ptBinEdges.size() > 1 && ptBinEdges.front() >= 0. );
unsigned int nPtBins = ptBinEdges.size()-1;
ResolutionParametrization *param = 0;
if(name == "ResolutionGaussAvePt") {
param = new ResolutionGaussAvePt(nPtBins);
} else if(name == "ResolutionGauss") {
// Read spectrum from file and split into pdfs for different pt bins
TH1 *hPtGen = 0;
std::string spectrum = config.read<string>("jet spectrum","default.root");
std::cout << "Getting spectrum from file '" << spectrum << "'\n";
TFile file(spectrum.c_str(),"READ");
file.GetObject("hPtGen",hPtGen);
if( !hPtGen ) {
std::cerr << "ERROR: No histogram 'hPtGen' found in file '" << file.GetName() << "'\n";
exit(1);
}
hPtGen->SetDirectory(0);
file.Close();
// Split into spectra per bin
std::vector<TH1*> spectra(nPtBins);
std::cout << "Creating underlying pdfs per bin\n";
for(unsigned int i = 0; i < nPtBins; ++i) {
double ptTrueMin = 0.4*ptBinEdges[i];
double ptTrueMax = 1.8*ptBinEdges[i+1];
if( nPtBins == 1 ) {
ptTrueMin = config.read<double>("Et genJet min",ptTrueMin);
ptTrueMax = config.read<double>("Et genJet max",ptTrueMax);
}
int binMin = hPtGen->FindBin(ptTrueMin);
int binMax = hPtGen->FindBin(ptTrueMax);
TString name = "spectrum";
name += i;
spectra[i] = new TH1D(name,"",1+binMax-binMin,hPtGen->GetXaxis()->GetBinLowEdge(binMin),hPtGen->GetXaxis()->GetBinUpEdge(binMax));
for(int xBin = 1; xBin <= spectra[i]->GetNbinsX(); ++xBin) {
spectra[i]->SetBinContent(xBin,hPtGen->GetBinContent(binMin+xBin-1));
}
if( spectra[i]->Integral("width") ) spectra[i]->Scale(1./spectra[i]->Integral("width"));
}
param = new ResolutionGauss(nPtBins,ptBinEdges,spectra);
} else {
param = new ResolutionEmpty(1);
}
return param;
}
// -----------------------------------------------------------------
Parameters* Parameters::createParameters(const ConfigFile& config)
{
static Cleaner cleanup;
if( instance_ != 0 )
{
delete instance_;
instance_ = 0;
}
string parclass = config.read<string>("Parametrization Class","");
//create Parameters
if(parclass == "TStepParameters") {
parclass = "StepParametrization";
} else if(parclass == "TMyParameters") {
parclass = "MyParametrization";
} else if(parclass == "TStepParametersEnergy") {
parclass = "StepParametrizationEnergy";
} else if(parclass == "TStepEfracParameters") {
parclass = "StepEfracParametrization";
} else if(parclass == "TJetMEParameters") {
parclass = "JetMETParametrization";
} else if(parclass == "TGlobalScaleFactorParameters") {
parclass = "GlobalScaleFactorParametrization";
} else if(parclass == "TSimpleParameters") {
parclass = "SimpleParametrization";
} else if(parclass == "TToyParameters") {
parclass = "ToyParametrization";
} else if(parclass == "TToyJetParameters") {
parclass = "ToyJetParametrization";
} else if(parclass == "TToyStepParametersEnergy") {
parclass = "ToyStepParametrizationEnergy";
} else if(parclass == "StepJetParametrization") {
parclass = "StepJetParametrization";
} else if(parclass == "TTrackParameters") {
parclass = "TrackParametrization";
}
int mode = config.read<int>("Mode",0);
if( mode == 0 ) {
Parametrization *param = createParametrization(parclass,config);
if(! param) {
cerr << "Parameters::createParameters: could not instantiate class " << parclass << '\n';
exit(1);
}
instance_ = new Parameters(param);
} else if( mode == 1 ) {
ResolutionParametrization *resParam = createResolutionParametrization(parclass,config);
if( !resParam ) {
cerr << "Parameters::createParameters: could not instantiate class " << parclass << '\n';
exit(1);
}
instance_ = new Parameters(resParam);
} else {
std::cerr << "Parameters::createParameters: unknown mode '" << mode << "'\n";
exit(1);
}
instance_->init(config);
return instance_;
}
// -----------------------------------------------------------------
Parameters::Parameters()
: k_(0),parErrors_(0),parGCorr_(0),parCov_(0),trackEff_(0),
s_(gsl_root_fsolver_alloc(gsl_root_fsolver_brent)) {
gsl_set_error_handler_off();
}
// -----------------------------------------------------------------
Parameters::Parameters(Parametrization* p)
: k_(0),parErrors_(0),parGCorr_(0),parCov_(0),trackEff_(0),
s_(gsl_root_fsolver_alloc(gsl_root_fsolver_brent)) {
if(p) p_ = p;
gsl_set_error_handler_off();
if( !resParam_ ) resParam_ = new ResolutionEmpty(0);
}
// -----------------------------------------------------------------
Parameters::Parameters(ResolutionParametrization *resParam)
: k_(0),parErrors_(0),parGCorr_(0),parCov_(0),trackEff_(0),
s_(gsl_root_fsolver_alloc(gsl_root_fsolver_brent)) {
if( resParam ) resParam_ = resParam;
if( !p_ ) p_ = new EmptyParametrization(resParam_->nPtBins()*resParam_->nParPerPtBin());
gsl_set_error_handler_off();
}
// -----------------------------------------------------------------
Parameters& Parameters::operator=(const Parameters& p) {
return *instance_;
}
// -----------------------------------------------------------------
void Parameters::init(const ConfigFile& config)
{
eta_ntwr_used_ = config.read<unsigned>("maximum eta twr used",82);
eta_granularity_ = config.read<unsigned>("granularity in eta",1);
phi_granularity_ = config.read<unsigned>("granularity in phi",1);
eta_symmetry_ = config.read<bool>("symmetry in eta",false);
eta_granularity_jet_ = config.read<unsigned>("jet granularity in eta",1);
phi_granularity_jet_ = config.read<unsigned>("jet granularity in phi",1);
eta_granularity_track_ = config.read<unsigned>("track granularity in eta",1);
phi_granularity_track_ = config.read<unsigned>("track granularity in phi",1);
if (eta_ntwr_used_%2 !=0){
cerr << "WARNING: Use even number of eta towers! Forced exit."<< endl;
exit(1);
}
if (phi_ntwr_%phi_granularity_!=0) {
cerr << "WARNING: Check phi granularity! Forced exit."<< endl;
exit(1);
}
if (eta_symmetry_ && (eta_granularity_!=1 && eta_granularity_!=3 &&eta_granularity_!=4 &&eta_granularity_!=5&&eta_granularity_!=11&&
eta_granularity_!=21 && eta_granularity_!=41 )){
cerr << "WARNING: Check eta granularity! Should be 1, 3, 4, 5, 11, 21, or 41: Forced exit."<< endl;
exit(1);
}
if (!eta_symmetry_ && (eta_granularity_!=2 && eta_granularity_!=6 &&eta_granularity_!=10&&eta_granularity_!=22&&
eta_granularity_!=42 && eta_granularity_!=82 )){
cerr << "WARNING: Check eta granularity! Should be 2, 6, 10, 22, 42 or 82: Forced exit."<< endl;
exit(1);
}
start_values_ = bag_of<double>(config.read<string>("start values",""));
if ( start_values_.size()< p_->nTowerPars()){
cerr<< "ERROR: Number of start values and free parameters does not match!"<<endl
<< " There must be at least " << p_->nTowerPars() << " parameters!" << endl;
exit(2);
}
jet_start_values_ = bag_of<double>(config.read<string>("jet start values",""));
if ( jet_start_values_.size()< p_->nJetPars()){
if( jet_start_values_.size() == resParam_->nParPerPtBin() ) { // for resolution parametrisation; add better condition
for(unsigned int i = 1; i < resParam_->nPtBins(); ++i) {
for(unsigned int j = 0; j < resParam_->nParPerPtBin(); ++j) {
jet_start_values_.push_back(jet_start_values_[0+j]);
}
}
} else {
cerr<< "ERROR: Number of jet start values and free jet parameters does not match!"<<endl
<< " There must be at least " << p_->nJetPars() << " parameters!" << endl;
exit(3);
}
}
track_start_values_ = bag_of<double>(config.read<string>("track start values",""));
if ( track_start_values_.size()< p_->nTrackPars()){
cerr<< "ERROR: Number of track start values and free track parameters does not match!"<<endl
<< " There must be at least " << p_->nTrackPars() << " parameters!" << endl;
exit(3);
}
global_jet_start_values_ = bag_of<double>(config.read<string>("global jet start values",""));
if( global_jet_start_values_.size() < p_->nGlobalJetPars() ) {
cerr<< "ERROR: Number of global jet start values and free global jet parameters does not match!"<<endl
<< " There must be at least " << p_->nGlobalJetPars() << " parameters!" << endl;
exit(3);
}
// Initialize storage for parameter values and errors
k_ = new double[numberOfParameters()];
parErrors_ = new double[numberOfParameters()];
parGCorr_ = new double[numberOfParameters()];
parCov_ = new double[numberOfCovCoeffs()];
for(int i = 0; i < numberOfParameters(); i++) {
k_[i] = 0.;
parErrors_[i] = 0.;
parGCorr_[i] = 0.;
}
for(int i = 0; i < numberOfCovCoeffs(); ++i) {
parCov_[i] = 0.;
}
trackEff_ = new double[169];
isFixedPar_ = std::vector<bool>(numberOfParameters(),false);
for (unsigned int bin=0; bin<eta_granularity_*phi_granularity_; ++bin){
for (unsigned int tp=0; tp < p_->nTowerPars(); ++tp){
k_[ bin*p_->nTowerPars() + tp ] = start_values_[ tp ];
}
}
for (unsigned int bin=0; bin<eta_granularity_jet_*phi_granularity_jet_; ++bin){
for (unsigned int jp=0; jp < p_->nJetPars(); ++jp){
int i = numberOfTowerParameters() + bin*p_->nJetPars() + jp;
k_[i] = jet_start_values_[jp];
}
}
for (unsigned int bin=0; bin<eta_granularity_track_*phi_granularity_track_; ++bin){
for (unsigned int trp=0; trp < p_->nTrackPars(); ++trp){
int i = numberOfTowerParameters() + numberOfJetParameters() + bin*p_->nTrackPars() + trp;
k_[i] = track_start_values_[trp];
}
}
for(int etabin=0; etabin<13; ++etabin)
{
for(int ptbin=0; ptbin<13; ++ptbin)
{
trackEff_[13*etabin+ptbin] = 1;
}
}
for (unsigned int gjp = 0 ; gjp < p_->nGlobalJetPars() ; ++gjp){
int i = numberOfTowerParameters() + numberOfJetParameters() + numberOfTrackParameters() + gjp;
k_[i] = global_jet_start_values_[gjp];
}
// read predefined calibration contants from cfi
// or txt file depending on the ending of the name
std::vector<std::string> inputCalibration = bag_of_string(config.read<string>("input calibration",";"));
// Check whether start values for calibration are to be read
// from file
if( inputCalibration.empty() ) {
cout << "Using calibration start values from config file.\n";
} else {
cout << "Using calibration start values from file ";
// Check whether calibration constants are given in one of the
// Kalibri formats or in official JetMET format and prepare
// vector for further processing
std::string inputFormat = "UNKNOWN";
if( inputCalibration.front() == "Kalibri" ) {
inputFormat = "Kalibri";
inputCalibration.erase(inputCalibration.begin());
}
else if( inputCalibration.front() == "JetMET" ) {
inputFormat = "JetMET";
inputCalibration.erase(inputCalibration.begin());
}
else if( inputCalibration.size() == 1 ) { // For backward compatibility
inputFormat = "Kalibri";
}
// Read calibration constants
if( inputCalibration.size() == 0 ) {
std::cerr << "\nWARNING: No file name specified to read start values from.\n";
std::cerr << " Using start values from config file.\n";
}
else {
if( inputFormat == "Kalibri" ) {
// Read predefined calibration contants from cfi
// or txt file depending on the ending of the name
std::string inputFileName = inputCalibration.front();
if( !inputFileName.substr(inputFileName.rfind(".")+1).compare("cfi") ) {
cout << inputFileName << std::endl;
readCalibrationCfi(inputFileName);
}
else if( !inputFileName.substr(inputFileName.rfind(".")+1).compare("txt") ) {
cout << inputFileName << std::endl;
readCalibrationTxt(inputFileName);
}
else {
cerr << "\nERROR: Unknown file format: '" ;
cerr << inputFileName.substr(inputFileName.rfind(".")) << "'\n";
cerr << " Using start values from config file.\n";
}
}
else if( inputFormat == "JetMET" ) {
cout << ":\n";
readCalibrationJetMET(inputCalibration);
}
else {
std::cerr << "\nWARNING: Unknown input format.\n";
std::cerr << " Using start values from config file.\n";
}
}
}
std::string trackEffFileName = config.read<string>("track efficiency","");
if(!trackEffFileName.empty()){
cout << "Reading Track Efficiency from file '" << trackEffFileName << endl;
readTrackEffTxt(trackEffFileName);
}
// Specific to resolution fit
// PtBinning
ptBinEdges_ = bag_of<double>(config.read<std::string>("PtAve bin edges","-2 -1"));
if( ptBinEdges_.front() == -2. ) {
ptBinEdges_.at(0) = config.read<double>("Et min cut on dijet",-1.);
ptBinEdges_.at(1) = config.read<double>("Et max cut on dijet",-1.);
}
ptBinCenters_ = std::vector<double>(nPtBins());
ptTrueMin_ = std::vector<double>(nPtBins());
ptTrueMax_ = std::vector<double>(nPtBins());
for(unsigned int i = 0; i < nPtBins(); i++) {
ptBinCenters_[i] = 0.5*( ptBinEdges_[i] + ptBinEdges_[i+1] ); // Just to have a scale, use mean
ptTrueMin_[i] = 0.4*ptBinEdges_[i];
ptTrueMax_[i] = 1.8*ptBinEdges_[i+1];
}
if( nPtBins() == 1 ) {
ptTrueMin_[0] = config.read<double>("Et genJet min",ptTrueMin_[0]);
ptTrueMax_[0] = config.read<double>("Et genJet max",ptTrueMax_[0]);
}
//TODO: Should be made available via config
parNames_ = std::vector<std::string>(numberOfParameters(),"");
}
Parameters::~Parameters() {
delete [] k_;
delete [] parErrors_;
delete [] trackEff_;
delete [] parGCorr_;
delete [] parCov_;
for(FunctionMap::const_iterator i = funcmap_.begin() ;
i != funcmap_.end() ; ++i) {
delete i->second;
}
gsl_root_fsolver_free(s_);
if((this == instance_) && ncalls_) {
std::cout << "Inversion statistics for Jet::expectedEt:\n";
std::cout << "calls: " << ncalls_ << " average number of iterations:"
<< (double)ntries_/ncalls_ << " failures:" << (double)nfails_/ncalls_*100
<< "% warnings:" << (double)nwarns_/ntries_*100 << "%" <<std::endl;
}
}
Parameters* Parameters::clone() const {
assert(this == instance_);
Parameters* c = new Parameters();
c->eta_ntwr_used_ = eta_ntwr_used_;
c->eta_symmetry_ = eta_symmetry_;
c->eta_granularity_ = eta_granularity_;
c->phi_granularity_ = phi_granularity_;
c->eta_granularity_jet_ = eta_granularity_jet_;
c->phi_granularity_jet_ = phi_granularity_jet_;
c->eta_granularity_track_ = eta_granularity_track_;
c->phi_granularity_track_ = phi_granularity_track_;
// do not copy entries that are not needed...
//std::vector<double> start_values_, jet_start_values_, track_start_values_, global_jet_start_values_;
//std::vector<std::string> parNames_;
int n = numberOfParameters();
c->k_ = new double[n];
c->parErrors_ = new double[n];
c->parGCorr_ = new double[n];
for(int i = 0 ; i < n ; ++i) {
c->k_[i] = k_[i];
c->parErrors_[i] = parErrors_[i];
c->parGCorr_[i] = parGCorr_[i];
}
c->parCov_ = new double[numberOfCovCoeffs()];
for(int i = 0; i < numberOfCovCoeffs(); ++i) {
c->parCov_[i] = parCov_[i];
}
c->trackEff_ = new double[169];
c->isFixedPar_ = isFixedPar_;
c->ptBinEdges_ = ptBinEdges_;
c->ptBinCenters_ = ptBinCenters_;
c->ptTrueMin_ = ptTrueMin_;
c->ptTrueMax_ = ptTrueMax_;
//clone function map
for(FunctionMap::const_iterator i = funcmap_.begin() ;
i != funcmap_.end() ; ++i) {
Function* f = i->second->clone();
// std::cout << "cloning..." << i->second << ", " << f << '\n';
f->changeParBase(k_,c->k_);
c->funcmap_[i->first] = f;
}
clones_.push_back(c);
return c;
}
void Parameters::removeClone(Parameters* p) {
std::vector<Parameters*>::iterator i = find(clones_.begin(),
clones_.end(),p);
if(i != clones_.end()) {
delete *i;
clones_.erase(i);
}
}
// -----------------------------------------------------------------
std::string Parameters::trim(std::string const& source, char const* delims)
{
std::string result(source);
std::string::size_type index = result.find_last_not_of(delims);
if(index != std::string::npos)
result.erase(++index);
index = result.find_first_not_of(delims);
if(index != std::string::npos)
result.erase(0, index);
else
result.erase();
//replace all "," by " " :
std::string::size_type pos = result.find(",");
while(pos != string::npos) {
result.replace(pos,1," ");
pos = result.find(",",pos);
}
return result;
}
//! \brief Read predefined calibration constants from txt file
//!
//! fills start parameters for fit when read from txt file; expects
//! 72 lines for 72 bins in phi for each eta bin ranging from -41
//! to 41 (skipping the 0) and the following parameter format:
//! maxEta minEta nPar towerParameters jetParameters separated by
//! blanks
// ---------------------------------------------------------------
void Parameters::readCalibrationTxt(std::string const& configFile)
{
std::ifstream file(configFile.c_str());
std::string line; // buffer line
int ietaBin=-42;
// unsigned iLines= 0;
while( std::getline(file,line) ){
unsigned int iphiBin = 1; // Only 1 phibin is written to the file
// determine phi bin on the fly
// phiBin=(iLines%72)+1; // phi counts from 1...72 for each eta bin
// ++iLines;
// determine eta bin on the fly
if(iphiBin==1) ++ietaBin; // increas etaValue by for the first phi bin
if(ietaBin==0) ++ietaBin; // and don't forget to skip the 0
// cout << "etaBin: " << etaBin << " :: " << "phiBin: " << phiBin << endl;
// buffers for input parameters
unsigned nPar=0; //this is not needed but read out for control reasons
double etaMax=0; //this is not needed but read out for control reasons
double etaMin=0; //this is not needed but read out for control reasons
double etMin=0;
double etMax=0;
std::vector<double> twrPars, jetPars, trkPars,globaljetPars;
unsigned entry=0; // controls which parameter is to filled
while( line.length()>line.substr(0, line.find(" ")).size() ){
if( 0<line.find(" ")){
// extract value
switch(++entry){
case 1 : etaMin = std::atof( line.substr(0, line.find(" ")).c_str() );
break;
case 2 : etaMax = std::atof( line.substr(0, line.find(" ")).c_str() );
break;
case 3 : nPar = std::atoi( line.substr(0, line.find(" ")).c_str() );
break;
case 4 : etMin = std::atoi( line.substr(0, line.find(" ")).c_str() );
break;
case 5 : etMax = std::atoi( line.substr(0, line.find(" ")).c_str() );
break;
default:
if((entry-5)<=p_->nTowerPars()){
twrPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
}
else if((entry-5)<=p_->nTowerPars()+p_->nJetPars()){
jetPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
}
else if((entry-5)<=p_->nTowerPars()+p_->nJetPars()+p_->nTrackPars()) {
trkPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
} else {
globaljetPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
}
break;
}
// cut string
line = line.substr(line.find(" "));
}
else{
//cut string
if(line.find(" ")<std::string::npos){
line = line.substr(line.find(" ")+1);
}
}
}
// catch last character
//trkPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
if((entry-5)<=p_->nTowerPars()){
twrPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
}
else if((entry-5)<=p_->nTowerPars()+p_->nJetPars()){
jetPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
}
else if((entry-5)<=p_->nTowerPars()+p_->nJetPars()+p_->nTrackPars()) {
trkPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
} else {
globaljetPars.push_back(std::atof( line.substr(0, line.find(" ")).c_str() ));
}
// fill parameters
for(iphiBin = 1; iphiBin <= 72; iphiBin++) {
int towerIdx = bin(etaBin(ietaBin),phiBin(iphiBin));
if( towerIdx<0 ) continue;
for (unsigned n=0; n< p_->nTowerPars(); ++n) {
k_[towerIdx*p_->nTowerPars()+n] = twrPars[n];
//e[towerIdx*p_->nTowerPars()+n] = NOT_READ_OUT;
}
int jetIdx = jetBin(jetEtaBin(ietaBin),jetPhiBin(iphiBin));
if( jetIdx<0 ) continue;
for (unsigned n=0; n<p_->nJetPars(); ++n) {
k_[numberOfTowerParameters()+jetIdx*p_->nJetPars()+n] = jetPars[n];
//e[GetNumberOfTowerParameters()+jetIdx*p_->nJetPars()+n] = NOT_READ_OUT;
}
int trackIdx = trackBin(trackEtaBin(ietaBin),trackPhiBin(iphiBin));
if( trackIdx<0 ) continue;
for (unsigned n=0; n<p_->nTrackPars(); ++n) {
k_[numberOfTowerParameters()+numberOfJetParameters()+trackIdx*p_->nTrackPars()+n] = trkPars[n];
//e[numberOfTowerParameters()+jetIdx*p_->nJetPars()+n] = NOT_READ_OUT;
}
for (unsigned n=0; n<p_->nGlobalJetPars(); ++n) {
k_[numberOfTowerParameters()+numberOfJetParameters()+numberOfTrackParameters() +n] = globaljetPars[n];
}
}
}
}
//! \brief Read predefined calibration constants from cfi file
// -----------------------------------------------------------------
void Parameters::readCalibrationCfi(std::string const& configFile)
{
std::ifstream file(configFile.c_str());
std::string line, name;
char * dummy = new char[28];
std::vector<int> eta, phi;
std::vector<double> param[p_->nTowerPars()], error[p_->nTowerPars()];
std::vector<int> eta_jet, phi_jet;
std::vector<double> param_jet[p_->nJetPars()], error_jet[p_->nJetPars()];
std::vector<int> eta_track, phi_track;
std::vector<double> param_track[p_->nTrackPars()], error_track[p_->nTrackPars()];
std::vector<double> param_globaljet, error_globaljet;
int posEqual;
while (std::getline(file,line)) {
if (! line.length()) continue;
if( line.find("#") != string::npos) continue;
//Read Tower Calibration: ---------------------------------------------------
//if ( line.find("module ccctm = CalibratedCaloTowerMaker") != string::npos ) {
if ( line.find("block TowerCalibConstants = {") != string::npos ) {
while( std::getline(file,line) ) {
if( line.find("block") != string::npos) break;
posEqual=line.find('=');
name = line.substr(0,posEqual);
if( name.find("TowMapEta") != string::npos)
eta = bag_of<int>(trim(line.substr(posEqual+1)));
if( name.find("TowMapPhi") != string::npos)
phi = bag_of<int>(trim(line.substr(posEqual+1)));
for (unsigned i=0; i < p_->nTowerPars() ; ++i) {
sprintf(dummy,"TowerParam%d ",i);
if( name.find(dummy) != string::npos)
param[i] = bag_of<double>(trim(line.substr(posEqual+1)));
sprintf(dummy,"TowerError%d ",i);
if( name.find(dummy) != string::npos)
error[i] = bag_of<double>(trim(line.substr(posEqual+1)));
}
}
}
//Read Jet Calibration --------------------------------------------------------
if ( line.find("block JetCalibConstants = {") != string::npos ) {
while (std::getline(file,line)) {
if( line.find("block") != string::npos) break;
posEqual=line.find('=');
name = line.substr(0,posEqual);
//std::cout << name << ".\n";
if( name.find("JetMapEta") != string::npos)
eta_jet = bag_of<int>(trim(line.substr(posEqual+1)));
if( name.find("JetMapPhi") != string::npos)
phi_jet = bag_of<int>(trim(line.substr(posEqual+1)));
for (unsigned i=0; i<p_->nJetPars(); ++i) {
sprintf(dummy,"JetParam%d ",i);
if( name.find(dummy) != string::npos)
param_jet[i] = bag_of<double>(trim(line.substr(posEqual+1)));
sprintf(dummy,"JetError%d ",i);
if( name.find(dummy) != string::npos)
error_jet[i] = bag_of<double>(trim(line.substr(posEqual+1)));
}
if( name.find("GlobalJetParams") != string::npos)
param_globaljet = bag_of<double>(trim(line.substr(posEqual+1)));
if( name.find("GlobalJetErrors") != string::npos)
error_globaljet = bag_of<double>(trim(line.substr(posEqual+1)));
}
}
//Read Track Calibration --------------------------------------------------------
if ( line.find("block TrackCalibConstants = {") != string::npos ) {
while (std::getline(file,line)) {
if( line.find("block") != string::npos) break;
posEqual=line.find('=');
name = line.substr(0,posEqual);
//std::cout << name << ".\n";
if( name.find("TrackMapEta") != string::npos)
eta_track = bag_of<int>(trim(line.substr(posEqual+1)));
if( name.find("TrackMapPhi") != string::npos)
phi_track = bag_of<int>(trim(line.substr(posEqual+1)));
for (unsigned i=0; i<p_->nTrackPars(); ++i) {
sprintf(dummy,"TrackParam%d ",i);
if( name.find(dummy) != string::npos)
param_track[i] = bag_of<double>(trim(line.substr(posEqual+1)));
sprintf(dummy,"TrackError%d ",i);
if( name.find(dummy) != string::npos)
error_track[i] = bag_of<double>(trim(line.substr(posEqual+1)));
}
}
}
}
//check if the read calibration is ok:
bool ok=eta.size()==phi.size();
for (unsigned i=0; i < p_->nTowerPars(); ++i){
ok &= eta.size()==param[i].size();
ok &= eta.size()==error[i].size();
}
//fill tower parameters and errors:
if (ok) {
for (unsigned i=0; i<eta.size(); ++i){
int index = bin(etaBin(eta[i]),phiBin(phi[i]));
if (index<0) continue;
for (unsigned n=0; n< p_->nTowerPars(); ++n) {
k_[index*p_->nTowerPars()+n] = param[n][i];
parErrors_[index*p_->nTowerPars()+n] = error[n][i];
}
}
}
//check if the read calibration is ok:
ok=eta_jet.size()==phi_jet.size();
for (unsigned i=0; i<p_->nJetPars(); ++i){
ok &= eta_jet.size()==param_jet[i].size();
ok &= eta_jet.size()==error_jet[i].size();
}
//fill Jet parameters and errors:
if (ok) {
for (unsigned i=0; i<eta_jet.size(); ++i){
int index = jetBin(jetEtaBin(eta_jet[i]),jetPhiBin(phi_jet[i]));
if (index<0) continue;
for (unsigned n=0; n<p_->nJetPars(); ++n) {
k_[numberOfTowerParameters() + index*p_->nJetPars()+n] = param_jet[n][i];
parErrors_[numberOfTowerParameters() + index*p_->nJetPars()+n] = error_jet[n][i];
}
}
}
//check if the read calibration is ok:
ok=eta_track.size()==phi_track.size();
for (unsigned i=0; i<p_->nTrackPars(); ++i){
ok &= eta_track.size()==param_track[i].size();
ok &= eta_track.size()==error_track[i].size();
}
//fill Track parameters and errors:
if (ok) {
for (unsigned i=0; i<eta_track.size(); ++i){
int index = trackBin(trackEtaBin(eta_track[i]),trackPhiBin(phi_track[i]));
if (index<0) continue;
for (unsigned n=0; n<p_->nTrackPars(); ++n) {
k_[numberOfTowerParameters() + numberOfJetParameters () + index*p_->nTrackPars()+n] = param_track[n][i];
parErrors_[numberOfTowerParameters() + numberOfJetParameters () + index*p_->nTrackPars()+n] = error_track[n][i];
}
}
}
ok = (param_globaljet.size() == p_->nGlobalJetPars());
//fill global Jet parameters and errors:
if (ok) {
for (unsigned n=0; n<p_->nGlobalJetPars(); ++n) {
k_[numberOfTowerParameters() + numberOfJetParameters () + numberOfTrackParameters()+n] = param_globaljet[n];
parErrors_[numberOfTowerParameters() + numberOfJetParameters () + numberOfTrackParameters()+n] = error_globaljet[n];
}
}
delete[] dummy;
}
//! \brief Read correction factors in CondDB format
// -----------------------------------------------------------------
void Parameters::readCalibrationJetMET(const std::vector<std::string>& inputFileNames) {
std::string corrL2FileName;
std::string corrL3FileName;
std::string corrLResFileName;
for(size_t i = 0; i < inputFileNames.size(); i++) {
if( inputFileNames.at(i).find("L2Relative") != std::string::npos )
corrL2FileName = inputFileNames.at(i);
else if( inputFileNames.at(i).find("L3Absolute") != std::string::npos )
corrL3FileName = inputFileNames.at(i);
else if( inputFileNames.at(i).find("L2L3Residual") != std::string::npos )
corrLResFileName = inputFileNames.at(i);
}
if( !corrL2FileName.empty() ) {
std::cout << " L2: " << corrL2FileName << std::endl;
readCalibrationJetMETL2(corrL2FileName);
}
if( !corrL3FileName.empty() ) {
std::cout << " L3: " << corrL3FileName << std::endl;
readCalibrationJetMETL3(corrL3FileName);
}
if( !corrLResFileName.empty() ) {
std::cout << " LRes: " << corrLResFileName << std::endl;
readCalibrationJetMETLRes(corrLResFileName);
}
}
//! \brief Read L2 correction factors in CondDB format
//!
//! Read parameters of L2 correction from
//! txt file in CondDB format i.e.
//! <tt>etaMin etaMax nPar EtMin EtMax Par1 Par2 Par3 Par4 Par5 Par6</tt>.
//! If there are more than 6 L2 parameters in the file,
//! they are ignored.
//!
//! The pt ranges of validity are not considered.
//!
//! In case some eta bins are missing, default parameter
//! values 1 0 0 are assumed.
//!
//! \note There are scaling factors for the L2 parameters
//! in the \p L2L3JetParametrization . The parameter
//! values read by this method are scaled accordingly.
// -----------------------------------------------------------------
void Parameters::readCalibrationJetMETL2(const std::string& inputFileName) {
// There are scaling factors in "L2L3JetParametrization"
std::vector<double> scale(6,1.);
scale.at(1) = 10.;
scale.at(2) = 100.;
scale.at(3) = 100.;
scale.at(4) = 100.;
std::vector< std::vector<double> > parL2;
std::ifstream file;
file.open(inputFileName.c_str());
int etaBin = -41;
float etaMin = 0.;
float etaMax = 0.;
int nPar = 0;
float val = -1.; // Needs float precision as etaEdge() has
// float precision; do we need it there?
if( file.is_open() ) {
while( !file.eof() && etaBin < 42 ) {
if( etaBin == 0 ) etaBin++; // No bin index 0
file >> etaMin; // Eta min
file >> etaMax; // Eta max
val = 0.;
file >> val; // Number of values following
if( val != 0 ) { // Avoid reading of empty last line
nPar = static_cast<int>(val - 2); // Number of L2 parameters in file
std::vector<double> par(numberOfJetParametersPerBin(),0.); // Storage of the L2 parameters in this bin
file >> val; // Et min
file >> val; // Et max
// Store L2 parameters
for(int i = 0; i < numberOfJetParametersPerBin(); i++) {
file >> val;
par.at(i) = scale.at(i) * val;
}
// In case of different numbers of parameters in
// JetMET and Kalibri L2 parametrization
for(int i = 0; i < nPar - numberOfJetParametersPerBin(); i++) {
file >> val;
}
// In case some eta bin is missing,
// add default parameters
while( etaBin < 42 &&
etaMin != etaLowerEdge(etaBin) &&
etaMax != etaUpperEdge(etaBin) ) {
std::cout << " WARNING: No parameters for eta bin " << etaBin;
std::cout << "; using default parameters instead.\n";
std::vector<double> defaultPar(numberOfJetParametersPerBin(),0.);
for(int i = 0; i < numberOfJetParametersPerBin(); i++) {
if( i == 0 ) defaultPar.at(i) = 1.;
else defaultPar.at(i) = 0.;
}
parL2.push_back(defaultPar);
etaBin++;
}
if( etaBin < 42 ) {
parL2.push_back(par);
}
etaBin++;
}
}
}
file.close();
// In case last eta bins are missing,
// add default parameters
while( etaBin < 42 ) {
if( etaBin == 0 ) etaBin++; // No bin index 0
std::cout << " WARNING: No parameters for eta bin " << etaBin;
std::cout << "; using default parameters instead.\n";
std::vector<double> defaultPar(numberOfJetParametersPerBin(),0.);
for(int i = 0; i < numberOfJetParametersPerBin(); i++) {
if( i == 0 ) defaultPar.at(i) = 1.;
else defaultPar.at(i) = 0.;
}
parL2.push_back(defaultPar);
etaBin++;
}
// Write read constants to array
int etaIdx = 0;
for(etaBin = -41; etaBin <= 41; etaBin++, etaIdx++) {
if( etaBin == 0 ) etaBin++;
for(int phiBin = 1; phiBin <= 72; phiBin++) {
int jetIdx = jetBin(jetEtaBin(etaBin),jetPhiBin(phiBin));
if( jetIdx<0 ) continue;
for(int i = 0; i < numberOfJetParametersPerBin(); i++) {
k_[numberOfTowerParameters() +
jetIdx*numberOfJetParametersPerBin() + i] = parL2.at(etaIdx).at(i);
}
}
}
}
//! \brief Read L3 correction factors in CondDB format
//!
//! Read parameters of L3 correction from
//! txt file in CondDB format i.e.
//! <tt>etaMin etaMax nPar EtMin EtMax Par1 Par2 Par3 Par4</tt>
//!
//! The pt ranges of validity are not considered.
// -----------------------------------------------------------------
void Parameters::readCalibrationJetMETL3(const std::string& inputFileName) {
std::ifstream file;
file.open(inputFileName.c_str());
std::vector<double> parL3; // Storage for the L3 parameters
double val = -1.;
int n = 0;
if( file.is_open() ) {
file >> val; // Eta min
file >> val; // Eta max
file >> val; // Number of values following
n = static_cast<int>(val - 2); // Number of L3 parameters
file >> val; // Et min