00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "NtupleReader/MyTools.h"
00014
00015
00016
00017
00018
00019
00020 using namespace std;
00021
00022
00023
00024
00025
00026
00027
00028
00029 void MyTools::init() {
00030
00031 m_nb_multiple_matches = 0;
00032 }
00033
00034
00035
00036
00037
00038
00039
00040 void MyTools::destruct(void) {
00041
00042 return;
00043
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 MyParticle*
00055 MyTools::find_matching_particle( TLorentzVector particle_tlv,
00056 std::vector<MyParticle*> & particle_vector,
00057 double deltaR_cut,
00058 bool bool_matchingflag,
00059 bool bool_usematching){
00060
00061
00062
00063 double deltaR = 0.;
00064 double deltaR_min = 10.5;
00065
00066 int k_min = -1;
00067 int nb_foundparticles = 0;
00068
00069
00070
00071
00072
00073 for (unsigned int k=0; k<particle_vector.size(); k++) {
00074
00075
00076
00077 if(bool_usematching) {
00078 if( !(particle_vector[k]->matchingflag() == 0) ) continue;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 deltaR = particle_tlv.DeltaR( particle_vector[k]->tlv() );
00088
00089
00090
00091
00092
00093
00094
00095
00096 if( deltaR < deltaR_cut && deltaR < deltaR_min){
00097 k_min = k;
00098 deltaR_min = deltaR;
00099 nb_foundparticles++;
00100 }
00101
00102
00103 }
00104
00105
00106
00107
00108 if(nb_foundparticles>1){
00109 m_nb_multiple_matches++;
00110 }
00111
00112 if(nb_foundparticles==0){
00113 return NULL;
00114 }
00115 else{
00116 if(bool_matchingflag) particle_vector[k_min]->set_matchingflag(1);
00117 return particle_vector[k_min];
00118 }
00119
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 MyParticle*
00129 MyTools::find_matching_particle( MyParticle* particle,
00130 std::vector<MyParticle*> & particle_vector,
00131 double deltaR_cut,
00132 bool bool_matchingflag,
00133 bool bool_usematching){
00134 return find_matching_particle(particle->tlv(),
00135 particle_vector,
00136 deltaR_cut,
00137 bool_matchingflag,
00138 bool_usematching);
00139 }
00140
00141
00142
00143 Int_t
00144 MyTools::nb_multiple_matches(void){
00145
00146 return m_nb_multiple_matches;
00147
00148 }
00149
00150
00151 void
00152 MyTools::clear_vector(std::vector<MyParticle*> & particle_vector){
00153
00154 for(unsigned int k =0;k<particle_vector.size(); k++ ){
00155 particle_vector[k]->set_matchingflag(0);
00156 }
00157
00158 }
00159
00160
00161 void
00162 MyTools::clear_nb_multiple_matches(){
00163
00164 m_nb_multiple_matches = 0;
00165
00166 }
00167
00168
00169
00170
00171
00172
00173
00174 std::vector<MyParticle*>
00175 MyTools::remove_overlap( std::vector<MyParticle*> vec_part_tb_rm,
00176 std::vector<MyParticle*> vec_part_rm_with,
00177 double deltaR_cut) {
00178
00179 std::vector<MyParticle*> vec_tmp;
00180
00181 MyParticle* tmp_part_overlaps = NULL;
00182
00183
00184 for (unsigned int k=0; k<vec_part_tb_rm.size(); k++){
00185
00186 tmp_part_overlaps =
00187 find_matching_particle( vec_part_tb_rm[k], vec_part_rm_with,
00188 deltaR_cut, false, false);
00189
00190
00191 if(tmp_part_overlaps!=NULL){
00192 continue;
00193 }
00194
00195 vec_tmp.push_back( vec_part_tb_rm[k] );
00196 }
00197
00198 return vec_tmp;
00199
00200 }
00201
00202
00203
00204
00205
00206
00207
00208 std::vector<MyParticle*>
00209 MyTools::sort_particles( std::vector<MyParticle*> p_vector ){
00210
00211
00212 std::vector<MyParticle*> sorted_p_vector(0);
00213
00214 unsigned int orig_size = p_vector.size();
00215
00216 for (unsigned int cycle=0; cycle<orig_size; cycle++){
00217
00218 int k_max=-1;
00219 double abspt_max=-1;
00220
00221 for (unsigned int k=0; k<p_vector.size(); k++) {
00222
00223 double abspt = TMath::Abs(p_vector[k]->tlv().Pt());
00224
00225 if( abspt > abspt_max ){
00226 k_max = k;
00227 abspt_max = abspt;
00228 }
00229
00230 }
00231
00232
00233
00234
00235 sorted_p_vector.push_back( p_vector[k_max] );
00236 p_vector.erase(p_vector.begin()+k_max);
00237
00238 }
00239
00240 return sorted_p_vector;
00241
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 void
00251 MyTools::check_order( std::vector<MyParticle*> p_vector ){
00252
00253 double abspt_min=0;
00254
00255 if (p_vector.size()==0) return;
00256
00257 for (unsigned int k=0; k<p_vector.size(); k++) {
00258
00259 double abspt = TMath::Abs(p_vector[k]->tlv().Pt());
00260
00261 if(k!=0 && abspt>abspt_min){
00262
00263
00264 cerr << endl
00265 << "Class MyTools, method check_order: ERROR!\n"
00266 << "vector: " << p_vector[0]->type() << " not sorted" << endl;
00267
00268 }
00269
00270
00271 abspt_min = abspt;
00272 }
00273
00274 return;
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284 std::vector<MyParticle*>
00285 MyTools::pt_eta_cut( const vector<MyParticle*> p_vector,
00286 const double pt_min, const double pt_max,
00287 const double eta_min, const double eta_max) {
00288
00289 std::vector<MyParticle*> tmp_v;
00290 tmp_v.clear();
00291
00292 for (unsigned int i=0; i<p_vector.size(); i++){
00293
00294 double pt = (p_vector[i])->tlv().Pt();
00295 double eta = TMath::Abs((p_vector[i])->tlv().Eta());
00296
00297 if(pt_min>-1){
00298 if(pt < pt_min) continue;
00299 }
00300 if(pt_max>-1){
00301 if(pt > pt_max) continue;
00302 }
00303 if(eta_min>-1){
00304 if(eta < eta_min) continue;
00305 }
00306 if(eta_max>-1){
00307 if(eta > eta_max) continue;
00308 }
00309
00310 tmp_v.push_back(p_vector[i]);
00311
00312 }
00313
00314 return tmp_v;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323 std::vector<MyParticle*>
00324 MyTools::et_eta_cut( const vector<MyParticle*> p_vector,
00325 const double et_min, const double et_max,
00326 const double eta_min, const double eta_max) {
00327
00328 std::vector<MyParticle*> tmp_v;
00329 tmp_v.clear();
00330
00331 for (unsigned int i=0; i<p_vector.size(); i++){
00332
00333 double et = (p_vector[i])->tlv().Et();
00334 double eta = TMath::Abs((p_vector[i])->tlv().Eta());
00335
00336 if(et_min>-1){
00337 if(et < et_min) continue;
00338 }
00339 if(et_max>-1){
00340 if(et > et_max) continue;
00341 }
00342 if(eta_min>-1){
00343 if(eta < eta_min) continue;
00344 }
00345 if(eta_max>-1){
00346 if(eta > eta_max) continue;
00347 }
00348
00349 tmp_v.push_back(p_vector[i]);
00350
00351 }
00352
00353 return tmp_v;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362 std::vector<MyParticle*>
00363 MyTools::append_vector(std::vector<MyParticle*> first_vector,
00364 std::vector<MyParticle*> second_vector) {
00365
00366 std::vector<MyParticle*> added_vector(0);
00367
00368 for (unsigned int j=0;j<first_vector.size();j++) {
00369 added_vector.push_back(first_vector[j]);
00370 }
00371
00372 for (unsigned int j=0;j<second_vector.size();j++) {
00373 added_vector.push_back(second_vector[j]);
00374 }
00375
00376 return added_vector;
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 vector<MyParticle*>
00425 MyTools::delete_doubles( vector<MyParticle*> p_vector, double delta_r){
00426 vector<MyParticle*> tmp_v;
00427 tmp_v = p_vector;
00428
00429 double dpt;
00430 double dr;
00431 dpt = 0;
00432 int n_doubles;
00433 bool double_exists;
00434 double_exists = false;
00435 n_doubles = 0;
00436 TLorentzVector tlv1;
00437 TLorentzVector tlv2;
00438
00439 for (int j=tmp_v.size()-1; j>=0; j--){
00440 double_exists = false;
00441 tlv1 = (tmp_v[j])->tlv();
00442
00443
00444 for (int k=0; k<j; k++){
00445 if(j==k)cerr<<"j==k in MyTools::delete_doubles"<<endl;
00446 tlv2 = (tmp_v[k])->tlv();
00447 dr = tlv1.DeltaR(tlv2);
00448 dpt = tlv1.Pt() - tlv2.Pt();
00449 if(dr<delta_r && dpt<0){
00450
00451 n_doubles++;
00452 double_exists = true;
00453
00454
00455 }
00456 }
00457
00458 if(double_exists){
00459
00460 tmp_v.erase(tmp_v.begin()+j);
00461 }
00462 }
00463
00464 return tmp_v;
00465
00466 }
00467
00468
00469
00470
00471
00472
00473 double MyTools::tauRec_ETcorr(double et_in, double eta_in, int ntrk){
00474
00475 double m_ptetacorrectionsntr1[9] = { 0.8339, 0.8125, 0.8254,
00476 0.8747, 0.8583, 0.8594,
00477 0.9088, 0.9013, 0.8922};
00478 double m_ptetacorrectionsntr23[9]= { 0.9000, 0.8593, 0.9034,
00479 0.9208, 0.8791, 0.9132,
00480 0.9359, 0.9231, 0.9033};
00481
00482 const int m_nptbins = 3;
00483 const int m_netabins = 3;
00484
00485 double m_ptpoints[m_nptbins+1] = {15., 35., 150., 0.};
00486
00487 double m_etapoints[m_netabins+1] = {0.25, 1.0, 2.0, 0.0};
00488
00489 double et = et_in;
00490 double eta = fabs( eta_in );
00491 double corr = 1, fudge = 1.011;
00492
00493 unsigned int lowpt_idx = 0;
00494 unsigned int loweta_idx = 0;
00495
00496 double lowpt_frac = 0;
00497 double loweta_frac = 0;
00498
00499 while ( lowpt_idx < m_nptbins-1 && et > m_ptpoints[lowpt_idx+1] ) lowpt_idx++;
00500
00501 lowpt_frac = ( m_ptpoints[lowpt_idx+1] - et ) / ( m_ptpoints[lowpt_idx+1] -
00502 m_ptpoints[lowpt_idx] );
00503
00504
00505 if ( lowpt_frac > 1 ) lowpt_frac = 1;
00506 if( lowpt_frac < 0 ) {
00507 std::cerr << "FIXME: lowpt_frac < 0 !!" << std::endl;
00508
00509
00510
00511
00512
00513
00514
00515 }
00516
00517 while ( loweta_idx < m_netabins-1 && eta > m_etapoints[loweta_idx+1] ) loweta_idx++;
00518
00519 loweta_frac = ( m_etapoints[loweta_idx+1] - eta ) / (
00520 m_etapoints[loweta_idx+1] -
00521 m_etapoints[loweta_idx] );
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 if ( loweta_frac > 1) loweta_frac = 1;
00538 if( loweta_frac < 0 ) {
00539 std::cerr << "FIXME: loweta_frac < 0 !!" << std::endl;
00540
00541
00542
00543
00544
00545
00546
00547 }
00548
00549 double coeff_matrix[2][2] = { {0, 0}, {0, 0} };
00550
00551 if ( ntrk <= 1 ) {
00552 coeff_matrix[0][0] =
00553 m_ptetacorrectionsntr1[lowpt_idx*m_netabins+loweta_idx];
00554 if( lowpt_idx < m_nptbins-1 ){
00555 coeff_matrix[1][0] =
00556 m_ptetacorrectionsntr1[(lowpt_idx+1)*m_netabins+loweta_idx];
00557 }
00558 if( loweta_idx < m_netabins-1 ){
00559 coeff_matrix[0][1] =
00560 m_ptetacorrectionsntr1[lowpt_idx*m_netabins+(loweta_idx+1)];
00561 }
00562 if( lowpt_idx < m_nptbins-1 && loweta_idx < m_netabins-1 ){
00563 coeff_matrix[1][1] =
00564 m_ptetacorrectionsntr1[(lowpt_idx+1)*m_netabins+(loweta_idx+1)];
00565 }
00566 } else {
00567 coeff_matrix[0][0] =
00568 m_ptetacorrectionsntr23[lowpt_idx*m_netabins+loweta_idx];
00569 if( lowpt_idx < m_nptbins-1 ){
00570 coeff_matrix[1][0] =
00571 m_ptetacorrectionsntr23[(lowpt_idx+1)*m_netabins+loweta_idx];
00572 }
00573 if( loweta_idx < m_netabins-1 ){
00574 coeff_matrix[0][1] =
00575 m_ptetacorrectionsntr23[lowpt_idx*m_netabins+(loweta_idx+1)];
00576 }
00577 if( lowpt_idx < m_nptbins-1 && loweta_idx < m_netabins-1 ){
00578 coeff_matrix[1][1] =
00579 m_ptetacorrectionsntr23[(lowpt_idx+1)*m_netabins+(loweta_idx+1)];
00580 }
00581 }
00582
00583 corr = ( coeff_matrix[0][0] * lowpt_frac * loweta_frac ) +
00584 ( coeff_matrix[1][0] * (1-lowpt_frac) * loweta_frac );
00585 corr += ( coeff_matrix[0][1] * lowpt_frac * (1-loweta_frac) ) +
00586 ( coeff_matrix[1][1] * (1-lowpt_frac) * (1-loweta_frac) );
00587
00588 et *= corr*fudge;
00589
00590 return et;
00591
00592 }
00593
00594
00595
00596
00597
00598 vector<MyParticle*> MyTools::correct_tau_et( std::vector<MyParticle*> tau_vector){
00599
00600 vector<MyParticle*> new_vector;
00601 new_vector.clear();
00602 MyTauJet *tmp_tau = new MyTauJet;
00603 for (unsigned int k=0; k<tau_vector.size();k++) {
00604
00605 double tau_et=((MyTauJet*)(tau_vector[k]))->ethadcalib()
00606 +((MyTauJet*)(tau_vector[k]))->etemcalib();
00607 double tau_eta=(tau_vector[k])->tlv().Eta();
00608 double tau_phi=(tau_vector[k])->tlv().Phi();
00609 int tau_ntrk=((MyTauJet*)(tau_vector[k]))->nb_tracks();
00610
00611
00612 double tau_et_recalib=tauRec_ETcorr(tau_et, tau_eta, tau_ntrk);
00613
00614 double tau_e_recalib=tau_et_recalib*cosh(tau_eta);
00615 double tau_p2_recalib=tau_e_recalib*tau_e_recalib-1.77699*1.77699;
00616 double tau_p_recalib=sqrt(tau_p2_recalib);
00617 double tau_pt_recalib=tau_p_recalib/cosh(tau_eta);;
00618
00619 TLorentzVector new_tlv;
00620 new_tlv.SetPtEtaPhiE(tau_pt_recalib,tau_eta,tau_phi,tau_e_recalib);
00621
00622 tmp_tau=((MyTauJet*)tau_vector[k])->Clone();
00623 tmp_tau->set_tlv(new_tlv);
00624 new_vector.push_back(tmp_tau);
00625
00626 }
00627 return new_vector;
00628
00629 }
00630
00631
00632
00633
00634
00635
00636
00637 void
00638 MyTools::PrintVector( vector<MyParticle*> p_vector, std::string option){
00639
00640 for (unsigned int k=0; k<p_vector.size(); k++) {
00641 (p_vector[k])->Print(option);
00642 }
00643
00644 }
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654 vector<MyParticle*>
00655 MyTools::particles_from_vertex(const vector<MyParticle*> & p_vector, const int vertex) {
00656
00657 vector<MyParticle*> particles;
00658
00659 for (unsigned int k=0; k<p_vector.size(); k++) {
00660
00661 if(p_vector[k]->vertexIndex() == vertex){
00662 particles.push_back(p_vector[k]);
00663 }
00664 }
00665
00666 return particles;
00667
00668 }
00669
00670
00671
00672
00673
00674 double MyTools::change_btag_weight(double current_btag_weight, double delta_weight){
00675
00676 double new_weight;
00677 new_weight = current_btag_weight + delta_weight;
00678
00679 return new_weight;
00680
00681 }
00682
00683
00684
00685
00686
00687 void MyTools::scale_b_tags(std::vector<MyParticle*> * jets, int flavour, double delta_bweight){
00688
00689 for (unsigned int il=0;il<jets->size();il++){
00690
00691 double bweight=((MyParticleJet*)((*jets)[il]))->b_tag_weight();
00692 int pdgId=((MyParticleJet*)((*jets)[il]))->truth_flavour();
00693 double new_bweight=bweight;
00694
00695 if (pdgId==flavour){
00696 new_bweight=change_btag_weight(bweight,delta_bweight);
00697 }
00698
00699 ((MyParticleJet*)((*jets)[il]))->set_b_tag_weight(new_bweight);
00700
00701
00702
00703
00704 }
00705
00706 }