26 #include "TParameter.h"
29 #ifndef __FSLIB_NO_BACKWARD_COMPAT__
30 #define LIB_VERSION_OLD 3
35 inline int calcKey(
float eta,
float dist) {
36 return (
int)(eta*1000000)+(
int)(dist*1000);
41 double stepy =
m_step * sqrt(3.)/2.;
45 double dx0 = (ny % 2 )*
m_step/2.;
61 ver = (TParameter<int>*)
source->Get(
"version");
64 #ifndef __FSLIB_NO_BACKWARD_COMPAT__
69 TTree* TTreeMeta = (TTree*)
source->Get(
"meta");
70 TTree* TTreeLib = (TTree*)
source->Get(
"library");
72 if ((TTreeMeta ==
nullptr) || (TTreeLib ==
nullptr))
return nullptr;
74 std::cout <<
"FCALDistEtaEnergyShowerLib header found." << std::endl;
77 #ifndef __FSLIB_NO_BACKWARD_COMPAT__
84 if (!(newlib->
readMeta(TTreeMeta)) || !(newlib->
read(TTreeLib))) {
86 std::cout <<
"FCALDistEtaEnergyShowerLib read unsuccessful." << std::endl;
115 std::ifstream filestr(
inputFile.c_str(),std::ios::in);
118 if (!filestr.is_open()) {
119 std::cout <<
"FCALDistEtaEnergyShowerLib " <<
inputFile <<
": bad file!" << std::endl;
124 std::getline(filestr,instr);
131 std::stringstream
ss(instr);
135 #ifndef __FSLIB_NO_BACKWARD_COMPAT__
137 std::cout <<
"FCALDistEtaEnergyShowerLib: you are trying to create the old version of this library. Use the new one." << std::endl;
148 std::getline(filestr,instr);
149 double xrodcent, yrodcent,
step;
151 std::stringstream
ss(instr);
152 ss >> xrodcent >> yrodcent >>
step;
155 std::map<float,std::vector<float> > libstruct;
158 std::getline(filestr,instr);
159 while (instr !=
"END") {
161 std::stringstream
ss(instr);
168 std::cout <<
"First eta should be 3.0 (have " << eta <<
")" << std::endl;
172 }
else if (eta <= etaold) {
173 std::cout <<
"eta is not correct: " << eta <<
"<=" << etaold << std::endl;
178 std::vector<float> * distlist = &(libstruct[eta]);
186 std::cout <<
"file reading failed! (not enough data)" << std::endl;
192 std::cout <<
"no negative dists allowed (" << eta <<
")" << std::endl;
195 distlist->push_back(0.);
198 distlist->push_back(dist);
201 float maxdist =
step/sqrt(3);
205 if ((
ss.fail()) || (dist <= distold)) {
206 std::cout <<
"screwed dists! (" << dist <<
"<=" << distold <<
")" << std::endl;
209 if (dist < maxdist) {
210 distlist->push_back(dist);
211 }
else if (dist >= maxdist) {
212 std::cout <<
"dist can't be bigger than " << maxdist <<
" (" << dist <<
"). ignored" << std::endl;
216 std::getline(filestr,instr);
217 if (filestr.fail()) {
218 std::cout <<
"file reading failed! (not enough data)" << std::endl;
227 std::cout <<
"this structure is not valid" << std::endl;
241 std::getline(filestr,instr);
250 std::cout <<
"Library is not created for production use" << std::endl;
256 double x =
track->GetPosition().getX();
257 double y =
track->GetPosition().getY();
260 double eta =
track->GetPosition().eta();
265 G4int particleCode =
track->GetDefinition()->GetPDGEncoding();
266 if ( particleCode < 0 ) particleCode = -particleCode;
269 std::cout <<
"wrong particle: " << particleCode <<
"!=" <<
m_particle << std::endl;
273 std::cout <<
"wrong eta: |eta|=" << eta <<
" is not inside FCAL" << std::endl;
279 library::const_iterator libit =
m_libData.upper_bound(eta);
282 std::cout <<
"Something is wrong with eta: |eta|=" << eta << std::endl;
287 etabin::const_iterator etait = (*libit).second.upper_bound(dist);
289 if (etait == (*libit).second.begin()) {
291 std::cout <<
"Something is wrong with dist: x=" <<
x <<
" y=" <<
y <<
" dist=" << dist << std::endl;
298 if ((*etait).second.empty()) {
299 std::cout <<
"The bin corresponding to the eta/dist pair is empty" << std::endl;
302 double trenergy =
track->GetKineticEnergy();
304 distbin::const_iterator distit = (*etait).second.lower_bound(trenergy);
305 if (distit == (*etait).second.end()) --distit;
306 else if (distit != (*etait).second.begin()) {
307 distbin::const_iterator distitch = distit;
309 if (((*distit).first - trenergy) > (trenergy - (*distitch).first)) {
314 if (randomShift > 0) {
315 double upperEnergy = (*distit).first * 1.01;
316 for (
int i = 0;
i < randomShift;
i++) {
318 if (distit == (*etait).second.end()) {
322 if ((*distit).first > upperEnergy)
break;
325 if ((randomShift < 0)&&(distit != (*etait).second.begin())) {
326 double lowerEnergy = (*distit).first * 0.99;
327 for (
int i = 0;
i > randomShift;
i--) {
329 if (distit == (*etait).second.begin()) {
333 if (lowerEnergy > (*distit).first)
break;
338 std::vector<EnergySpot>* outshower =
new std::vector<EnergySpot>();
339 Shower::const_iterator iter;
342 float energyScale =
track->GetKineticEnergy() / (*distit).first;
345 for (iter = (*distit).second.begin() ; iter != (*distit).second.end() ; ++iter) {
346 EnergySpot tmp( (*iter)->GetPosition(), (*iter)->GetEnergy(), (*iter)->GetTime() );
347 tmp.SetEnergy(
tmp.GetEnergy() * energyScale);
348 outshower->push_back(
tmp);
352 if (
stats !=
nullptr) {
353 stats->recordShowerLibUse(
calcKey((*libit).first,(*etait).first));
362 std::cout <<
"Library is not created for production use" << std::endl;
368 double x =
track->GetPosition().getX();
369 double y =
track->GetPosition().getY();
372 double eta =
track->GetPosition().eta();
377 G4int particleCode =
track->GetDefinition()->GetPDGEncoding();
378 if ( particleCode < 0 ) particleCode = -particleCode;
381 std::cout <<
"wrong particle: " << particleCode <<
"!=" <<
m_particle << std::endl;
385 std::cout <<
"wrong eta: |eta|=" << eta <<
" is not inside FCAL" << std::endl;
389 library::const_iterator libit =
m_libData.upper_bound(eta);
392 std::cout <<
"Something is wrong with eta: |eta|=" << eta << std::endl;
397 etabin::const_iterator etait = (*libit).second.upper_bound(dist);
398 if (etait == (*libit).second.begin()) {
400 std::cout <<
"Something is wrong with dist: x=" <<
x <<
" y=" <<
y <<
" dist=" << dist << std::endl;
407 if ((*etait).second.empty()) {
408 std::cout <<
"The etabin corresponding to the eta is empty" << std::endl;
411 double trenergy =
track->GetKineticEnergy();
413 distbin::const_iterator distit = (*etait).second.lower_bound(trenergy);
414 if (distit == (*etait).second.end()) --distit;
415 else if (distit != (*etait).second.begin()) {
416 distbin::const_iterator distitch = distit;
418 if (((*distit).first - trenergy) > (trenergy - (*distitch).first)) {
423 double rezZ = (*distit).second.getZSize();
424 distbin::const_iterator distiter = distit;
427 double upperEnergy = (*distit).first * 1.01;
428 for (
int i = 0;
i < spread;
i++) {
430 if (distiter == (*etait).second.end()) {
433 if (upperEnergy < (*distiter).first)
break;
435 rezZ += (*distiter).second.getZSize();
439 if (distiter != (*etait).second.begin()) {
440 double lowerEnergy = (*distit).first * 0.99;
441 for (
int i = 0;
i < spread;
i++) {
443 if (lowerEnergy > (*distiter).first)
break;
445 rezZ += (*distiter).second.getZSize();
447 if (distiter == (*etait).second.begin()) {
452 return rezZ/actualNumFS;
458 std::cout <<
"Library is not created for production use" << std::endl;
464 double x =
track->GetPosition().getX();
465 double y =
track->GetPosition().getY();
468 double eta =
track->GetPosition().eta();
473 G4int particleCode =
track->GetDefinition()->GetPDGEncoding();
474 if ( particleCode < 0 ) particleCode = -particleCode;
477 std::cout <<
"wrong particle: " << particleCode <<
"!=" <<
m_particle << std::endl;
481 std::cout <<
"wrong eta: |eta|=" << eta <<
" is not inside FCAL" << std::endl;
485 library::const_iterator libit =
m_libData.upper_bound(eta);
488 std::cout <<
"Something is wrong with eta: |eta|=" << eta << std::endl;
493 etabin::const_iterator etait = (*libit).second.upper_bound(dist);
494 if (etait == (*libit).second.begin()) {
496 std::cout <<
"Something is wrong with dist: x=" <<
x <<
" y=" <<
y <<
" dist=" << dist << std::endl;
503 if ((*etait).second.empty()) {
504 std::cout <<
"The etabin corresponding to the eta is empty" << std::endl;
507 double trenergy =
track->GetKineticEnergy();
509 distbin::const_iterator distit = (*etait).second.lower_bound(trenergy);
510 if (distit == (*etait).second.end()) --distit;
511 else if (distit != (*etait).second.begin()) {
512 distbin::const_iterator distitch = distit;
514 if (((*distit).first - trenergy) > (trenergy - (*distitch).first)) {
519 double rezR = (*distit).second.getRSize();
520 distbin::const_iterator distiter = distit;
523 double upperEnergy = (*distit).first * 1.01;
524 for (
int i = 0;
i < spread;
i++) {
526 if (distiter == (*etait).second.end()) {
529 if (upperEnergy < (*distiter).first)
break;
531 rezR += (*distiter).second.getRSize();
535 if (distiter != (*etait).second.begin()) {
536 double lowerEnergy = (*distit).first * 0.99;
537 for (
int i = 0;
i < spread;
i++) {
539 if (lowerEnergy > (*distiter).first)
break;
541 rezR += (*distiter).second.getRSize();
543 if (distiter == (*etait).second.begin()) {
548 return rezR/actualNumFS;
554 std::cout <<
"ERROR: filled" << std::endl;
558 double x = genParticle->production_vertex()->position().x();
559 double y = genParticle->production_vertex()->position().y();
562 double eta = genParticle->production_vertex()->position().eta();
568 std::cout <<
"ERROR: wrong pdgcode: " <<
m_particle <<
" != " << genParticle->pdg_id() << std::endl;
572 std::cout <<
"wrong eta: |eta|=" << eta <<
" is not inside FCAL" << std::endl;
579 std::cout <<
"Something is wrong with eta: |eta|=" << eta << std::endl;
585 if (etait == (*libit).second.begin()) {
587 std::cout <<
"Something is wrong with dist: x=" <<
x <<
" y=" <<
y <<
" dist=" << dist << std::endl;
591 (*etait).second.insert(distbin::value_type(genParticle->momentum().e(),(*shower)));
600 dest->WriteObject(&
ver,
"version");
608 dest->WriteObject(&TTreeLib,
"library");
609 dest->WriteObject(&TTreeMeta,
"meta");
635 int nentr =
source->GetEntriesFast();
636 if (nentr < 3)
return false;
637 Float_t
x,
y,
z,
e,time;
638 source->SetBranchAddress(
"x",&
x);
639 source->SetBranchAddress(
"y",&
y);
640 source->SetBranchAddress(
"z",&
z);
641 source->SetBranchAddress(
"e",&
e);
642 source->SetBranchAddress(
"time",&time);
645 #ifndef __FSLIB_NO_BACKWARD_COMPAT__
652 #ifndef __FSLIB_NO_BACKWARD_COMPAT__
664 int ndists = (
int)(
x+0.1);
668 for (
int i = 0;
i < ndists;
i++) {
670 int nsh = (
int)(
x+0.1);
672 distbin * curbin = &((*curetabin)[curDist]);
673 for(
int j = 0; j < nsh; j++) {
676 int nhits = (
int)(
x+0.1);
678 Shower * shower = &((*curbin)[curEnergy]);
681 for(
int k = 0;
k < nhits;
k++) {
687 }
while (entr < nentr);
717 Float_t
x,
y,
z,
e,time;
718 dest->Branch(
"x",&
x);
719 dest->Branch(
"y",&
y);
720 dest->Branch(
"z",&
z);
721 dest->Branch(
"e",&
e);
722 dest->Branch(
"time",&time);
731 library::const_iterator libit;
733 x = (*libit).second.size();
739 etabin::const_iterator etait;
740 for (etait = (*libit).second.begin(); etait != (*libit).second.end(); ++etait) {
741 x = (*etait).second.size();
747 distbin::const_iterator distit;
748 for (distit = (*etait).second.begin(); distit != (*etait).second.end(); ++distit) {
749 x = (*distit).second.size();
750 y = (*distit).second.getRSize();
751 z = (*distit).second.getZSize();
755 Shower::const_iterator iter;
756 for (iter = (*distit).second.begin(); iter != (*distit).second.end(); ++iter) {
757 x = (*iter)->GetPosition().x();
758 y = (*iter)->GetPosition().y();
759 z = (*iter)->GetPosition().z();
760 e = (*iter)->GetEnergy();
761 time = (*iter)->GetTime();
773 std::stringstream
ss;
774 ss << std::fixed << std::setprecision(3);
781 std::map<float, std::vector<float> >::const_iterator iter;
783 for (iter = structure.begin(); iter != structure.end(); ++iter) {
785 std::vector<float>::const_iterator inneriter;
786 for (inneriter = (*iter).second.begin(); inneriter != (*iter).second.end(); ++inneriter) {
787 (
m_libData[(*iter).first])[(*inneriter)];
796 std::map<int, std::string>
names;
797 std::map<int, int> sizes;
799 for(etabin::const_iterator etait = (*it).second.begin(); etait != (*it).second.end(); ++etait) {
800 sizes[
calcKey(
it->first, etait->first)]=etait->second.size();
801 float etalow =
it->first;
802 float distlow = etait->first;
805 library::const_iterator it_copy =
it;
810 etahigh = it_copy->first;
812 etabin::const_iterator etait_copy = etait;
814 if (etait_copy == (*it).second.end()) {
817 disthigh = etait_copy->first;
819 std::stringstream
ss;
820 ss << std::showpos << std::fixed << std::setprecision(2);
821 ss <<
"ETA: " << etalow <<
" .. " << etahigh <<
" DIST: " << distlow <<
" .. " << disthigh;