22 #include <calobase/RawTower.h>
23 #include <calobase/RawTowerContainer.h>
24 #include <calobase/RawTowerGeom.h>
25 #include <calobase/RawTowerGeomContainer.h>
26 #include <calobase/TowerInfoContainer.h>
27 #include <calobase/TowerInfo.h>
31 #include "fastjet/AreaDefinition.hh"
32 #include "fastjet/ClusterSequenceArea.hh"
33 #include "fastjet/Selector.hh"
34 #include "fastjet/tools/BackgroundEstimatorBase.hh"
35 #include "fastjet/tools/JetMedianBackgroundEstimator.hh"
36 #include <fastjet/ClusterSequence.hh>
37 #include <fastjet/JetDefinition.hh>
38 #include <fastjet/PseudoJet.hh>
54 #include <TTimeStamp.h>
59 , m_outputfilename(outputfilename)
60 , _doEventSelection(
false)
61 , m_eventSelection(-1, 100000)
88 TTimeStamp* ts =
new TTimeStamp();
89 m_seed =
static_cast<unsigned int>(ts->GetNanoSec());
97 m_tree =
new TTree(
"T",
"RandomConeAna");
146 std::vector<Jet *> inputs;
149 std::vector<Jet *> parts = _input->get_input(topNode);
150 for (
auto &part : parts)
152 inputs.push_back(part);
153 inputs.back()->set_id(inputs.size() - 1);
157 std::vector<Jet *> iter_inputs;
160 std::vector<Jet *> parts = _iter_input->get_input(topNode);
161 for (
auto &part : parts)
163 iter_inputs.push_back(part);
164 iter_inputs.back()->set_id(iter_inputs.size() - 1);
171 if(std::isnan(leading_truth_eta_phi.first) || std::isnan(leading_truth_eta_phi.second)){
172 std::cout <<
"RandomConeAna::process_event(PHCompositeNode *topNode) - leading truth jet eta phi is nan" << std::endl;
178 std::vector<fastjet::PseudoJet> iter_pseudojets =
jets_to_pseudojets(iter_inputs,
false);
181 std::vector<fastjet::PseudoJet> randomize_pseudojets =
jets_to_pseudojets(inputs,
true);
182 std::vector<fastjet::PseudoJet> randomize_iter_pseudojets =
jets_to_pseudojets(iter_inputs,
true);
186 float cone_eta =
m_random->Uniform(-0.7, 0.7);
187 float cone_phi =
m_random->Uniform(-M_PI, M_PI);
188 float cone_phi_avoid_lead_jet = 0;
190 float dR_from_leading = 1.4;
191 float current_deta = cone_eta - leading_truth_eta_phi.first;
195 test_phi =
m_random->Uniform(-M_PI, M_PI);
196 float test_dphi = test_phi - leading_truth_eta_phi.second;
197 if (test_dphi > M_PI) test_dphi -= 2 * M_PI;
198 if (test_dphi < -M_PI) test_dphi += 2 * M_PI;
199 float test_dr = sqrt((current_deta*current_deta) + (test_dphi*test_dphi));
200 if(test_dr > dR_from_leading)
202 cone_phi_avoid_lead_jet = test_phi;
210 for (
unsigned int ipart = 0; ipart < pseudojets.size(); ++ipart)
213 float deta = cone_eta - pseudojets[ipart].eta();
214 float dphi = cone_phi - pseudojets[ipart].phi_std();
215 if (dphi > M_PI) dphi -= 2 * M_PI;
216 if (dphi < -M_PI) dphi += 2 * M_PI;
217 float dr = sqrt(deta * deta + dphi * dphi);
227 for (
unsigned int ipart = 0; ipart < iter_pseudojets.size(); ++ipart)
230 float deta = cone_eta - iter_pseudojets[ipart].eta();
231 float dphi = cone_phi - iter_pseudojets[ipart].phi_std();
232 if (dphi > M_PI) dphi -= 2 * M_PI;
233 if (dphi < -M_PI) dphi += 2 * M_PI;
234 float dr = sqrt(deta * deta + dphi * dphi);
243 for (
unsigned int ipart = 0; ipart < randomize_pseudojets.size(); ++ipart)
246 float deta = cone_eta - randomize_pseudojets[ipart].eta();
247 float dphi = cone_phi - randomize_pseudojets[ipart].phi_std();
248 if (dphi > M_PI) dphi -= 2 * M_PI;
249 if (dphi < -M_PI) dphi += 2 * M_PI;
250 float dr = sqrt(deta * deta + dphi * dphi);
260 for (
unsigned int ipart = 0; ipart < randomize_iter_pseudojets.size(); ++ipart)
263 float deta = cone_eta - randomize_iter_pseudojets[ipart].eta();
264 float dphi = cone_phi - randomize_iter_pseudojets[ipart].phi_std();
265 if (dphi > M_PI) dphi -= 2 * M_PI;
266 if (dphi < -M_PI) dphi += 2 * M_PI;
267 float dr = sqrt(deta * deta + dphi * dphi);
277 for (
unsigned int ipart = 0; ipart < pseudojets.size(); ++ipart)
280 float deta = cone_eta - pseudojets[ipart].eta();
281 float dphi = cone_phi_avoid_lead_jet - pseudojets[ipart].phi_std();
282 if (dphi > M_PI) dphi -= 2 * M_PI;
283 if (dphi < -M_PI) dphi += 2 * M_PI;
284 float dr = sqrt(deta * deta + dphi * dphi);
293 for (
unsigned int ipart = 0; ipart < iter_pseudojets.size(); ++ipart)
296 float deta = cone_eta - iter_pseudojets[ipart].eta();
297 float dphi = cone_phi_avoid_lead_jet - iter_pseudojets[ipart].phi_std();
298 if (dphi > M_PI) dphi -= 2 * M_PI;
299 if (dphi < -M_PI) dphi += 2 * M_PI;
300 float dr = sqrt(deta * deta + dphi * dphi);
318 std::cout <<
"RandomConeAna::End - Output to " <<
m_outputfilename << std::endl;
324 std::cout <<
"RandomConeAna::End(PHCompositeNode *topNode) This is the End..." << std::endl;
348 JetMap *truthjets = findNode::getClass<JetMap>(topNode,
"AntiKt_Truth_r04");
351 std::cout <<
"JetTree::LeadingR04TruthJet(PHCompositeNode *topNode) Could not find truth jet nodes" << std::endl;
355 float leading_truth_pt = -1;
358 Jet *jet = iter->second;
359 if(jet->
get_pt() > leading_truth_pt) leading_truth_pt = jet->
get_pt();
362 return leading_truth_pt;
373 CentralityInfo* cent_node = findNode::getClass<CentralityInfo>(topNode,
"CentralityInfo");
376 std::cout <<
"RandomConeAna::process_event() ERROR: Can't find CentralityInfo" << std::endl;
387 std::vector<fastjet::PseudoJet> calo_pseudojets;
390 std::vector<Jet *> parts = _input->get_input(topNode);
391 for (
unsigned int i = 0;
i < parts.size(); ++
i)
393 auto& part = parts[
i];
394 float this_e = part->get_e();
395 if (this_e == 0.)
continue;
396 float this_px = parts[
i]->get_px();
397 float this_py = parts[
i]->get_py();
398 float this_pz = parts[
i]->get_pz();
402 float e_ratio = 0.001 / this_e;
403 this_e = this_e * e_ratio;
404 this_px = this_px * e_ratio;
405 this_py = this_py * e_ratio;
406 this_pz = this_pz * e_ratio;
408 fastjet::PseudoJet pseudojet(this_px, this_py, this_pz, this_e);
409 calo_pseudojets.push_back(pseudojet);
411 for (
auto &
p : parts)
delete p;
415 return calo_pseudojets;
428 std::vector<fastjet::PseudoJet> calo_pseudojets =
get_psuedojets(topNode);
430 fastjet::AreaDefinition area_def(fastjet::active_area_explicit_ghosts,
431 fastjet::GhostedAreaSpec(1.5, 1, 0.01));
438 fastjet::Selector jet_selector = (jet_eta_selector && jet_pt_selector);
443 fastjet::JetMedianBackgroundEstimator bge {bkgd_selector, jet_def, area_def};
444 bge.set_particles(calo_pseudojets);
450 fastjet::ClusterSequenceArea cs(calo_pseudojets, jet_def, area_def);
451 std::vector<fastjet::PseudoJet> jets =
fastjet::sorted_by_pt( bkgd_selector( cs.inclusive_jets(1.0) ) );
452 fastjet::Selector not_pure_ghost = !fastjet::SelectorIsPureGhost();
454 std::vector<float> pt_over_nConstituents;
456 int n_empty_jets = 0;
457 float total_constituents = 0;
458 for (
auto jet : jets) {
459 float jet_pt = jet.pt();
460 std::vector<fastjet::PseudoJet> constituents = not_pure_ghost(jet.constituents());
461 int jet_size = constituents.size();
462 total_constituents += jet_size;
467 pt_over_nConstituents.push_back(jet_pt/jet_size);
471 int total_jets = nfj_jets + n_empty_jets;
472 float mean_pT = total_constituents/total_jets;
473 if (mean_pT < 0) mean_pT = 0;
475 float med_tmp, std_tmp;
476 _median_stddev(pt_over_nConstituents, n_empty_jets, med_tmp, std_tmp);
484 const float percentile,
485 const float nempty)
const
487 assert(percentile >= 0. && percentile <= 1.);
489 int njets = sorted_vec.size();
490 if(njets == 0)
return 0;
492 float total_njets = njets + nempty;
493 float perc_pos = (total_njets)*percentile - nempty - 0.5;
496 if(perc_pos >= 0 && njets > 1){
497 int pindex = int(perc_pos);
499 if(pindex + 1 > njets-1){
501 perc_pos = njets - 1;
504 result = sorted_vec.at(pindex)*(pindex+1-perc_pos)
505 + sorted_vec.at(pindex+1)*(perc_pos-pindex);
507 else if(perc_pos > -0.5 && njets >=1){
508 result = sorted_vec.at(0);
521 float & std_dev)
const
529 std::vector<float> sorted_vec =
vec;
530 std::sort(sorted_vec.begin(), sorted_vec.end());
532 int njets = sorted_vec.size();
533 if(n_empty_jets > njets/4.0){
534 std::cout <<
"WARNING: n_empty_jets = " << n_empty_jets <<
" is too large, setting to " << njets/4.0 << std::endl;
535 n_empty_jets = njets/4.0;
539 float posn[2] = {0.5, (1.0-0.6827)/2.0};
541 for (
int i = 0;
i < 2;
i++) {
546 std_dev = res[0] - res[1];
553 JetMap *truthjets = findNode::getClass<JetMap>(topNode,
"AntiKt_Truth_r04");
556 std::cout <<
"JetTree::LeadingR04TruthJet(PHCompositeNode *topNode) Could not find truth jet nodes" << std::endl;
560 float leading_truth_pt = -1;
561 float leading_truth_eta = NAN;
562 float leading_truth_phi = NAN;
565 Jet *jet = iter->second;
566 if(jet->
get_pt() > leading_truth_pt)
568 leading_truth_pt = jet->
get_pt();
569 leading_truth_eta = jet->
get_eta();
570 leading_truth_phi = jet->
get_phi();
575 return std::make_pair(leading_truth_eta, leading_truth_phi);
582 std::vector<fastjet::PseudoJet> pseudojets;
583 for (
unsigned int ipart = 0; ipart < particles.size(); ++ipart)
585 if (!std::isfinite(particles[ipart]->get_px()) ||
586 !std::isfinite(particles[ipart]->get_py()) ||
587 !std::isfinite(particles[ipart]->get_pz()) ||
588 !std::isfinite(particles[ipart]->get_e()))
590 std::cout <<
PHWHERE <<
" invalid particle kinematics:"
591 <<
" px: " << particles[ipart]->get_px()
592 <<
" py: " << particles[ipart]->get_py()
593 <<
" pz: " << particles[ipart]->get_pz()
594 <<
" e: " << particles[ipart]->get_e() << std::endl;
598 fastjet::PseudoJet pseudojet(particles[ipart]->get_px(),
599 particles[ipart]->get_py(),
600 particles[ipart]->get_pz(),
601 particles[ipart]->get_e());
604 if(pseudojet.eta() < -0.7 || pseudojet.eta() > 0.7)
continue;
605 if(randomize_etaphi){
611 float E = pseudojet.e();
612 float pT = E/cosh(eta);
613 float px = pT * cos(phi);
614 float py = pT * sin(phi);
615 float pz = pT * sinh(eta);
616 pseudojet.reset(px, py, pz, E);
621 if (particles[ipart]->get_e() == 0.)
continue;
623 pseudojet.set_user_index(ipart);
624 pseudojets.push_back(pseudojet);