#ifndef RXDATAPLAYER_CPP #define RXDATAPLAYER_CPP #include "RXDataPlayer.hpp" #include "HelperSvc.hpp" #include "vec_extends.h" #include "TTreeFormula.h" #include "boost/program_options.hpp" #include #include #include #include #include #include "zipfor.h" #include "core.h" #include "RXDataPlayer.hpp" void RXDataPlayer::Book1DHist( TString _varExpression, TString _VarLabel, int nBins, double min, double max ,TString Units , bool isoBin , bool useLogy ){ m_bookedVariable[m_nVars] = RXVarPlot( m_nVars, _varExpression, _VarLabel , nBins, min, max , Units, isoBin , useLogy); m_nVars++; }; void RXDataPlayer::Book1DHist( const RooRealVar & var){ TString _varExpression = var.GetName(); TString _VarLabel = var.GetTitle(); TString Units = TString(var.getUnit()); int nBins = var.numBins(); double min = var.getMin(); double max = var.getMax(); Book1DHist( _varExpression, _VarLabel, nBins, min, max, Units, false, false); //delegate bookkeping }; void RXDataPlayer::BookRXSelection( TCut _selection , TString Label){ m_bookedRXSelections[m_nRXSelections] = RXSelection(m_nRXSelections, _selection, Label); m_nRXSelections++; }; void RXDataPlayer::BookRXWeight( TString _weight , TString RXWeightLabel){ m_bookedRXWeights[m_nRXWeights] = RXWeight(m_nRXWeights, _weight, RXWeightLabel); m_nRXWeights++; }; std::vector< std::tuple > RXDataPlayer::BuildCombination(){ // map< int, std::tuple > RXDataPlayer::BuildCombination(){ std::vector< std::tuple > Final ; if( GetNSelections() * GetNWeightExp() * GetNVarPlot() != 0){ // const map & _RXSelections, const map & _RXWeights, const map & _Variables){ for( const auto & vv : m_bookedVariable){ if( vv.first/MAXSLOTS > 0){ MessageSvc::Error("ERROR, VARAIBLE INDEX ", "", "EXIT_FAILURE");} for( const auto & ww : m_bookedRXWeights){ if( ww.first/MAXSLOTS > 0){ MessageSvc::Error("ERROR, VARAIBLE INDEX ", "", "EXIT_FAILURE");} for( const auto & zz : m_bookedRXSelections){ if( zz.first/MAXSLOTS > 0){ MessageSvc::Error("ERROR, VARAIBLE INDEX ", "", "EXIT_FAILURE");} int idx_combo = vv.first + MAXSLOTS * ww.first + zz.first*MAXSLOTS*MAXSLOTS; Final.push_back( std::make_tuple( zz.second, ww.second, vv.second) ) ; } } } return Final; }else{ abort(); } return Final; }; void RXDataPlayer::Process( TChain & Chain , double frac) noexcept{ //--- We have the TChain here, we have our map< int, std::tuple , we have now to build all Weight*Cut TFormulas To use Long64_t _nEntries = Chain.GetEntries(); if (frac > 1 && frac < _nEntries) { _nEntries = frac; } if (frac > 0 && frac < 1) { _nEntries = floor(_nEntries * frac); } auto combo = BuildCombination(); vector ALLBRANCHESNEEDED = {}; ALLBRANCHESNEEDED.reserve(100); for( auto & el : combo){ auto Selection = std::get<(int)Order::Selection>( el); auto Weight = std::get<(int)Order::Weight>( el); auto Var = std::get<(int)Order::Variable>( el); Selection.Expression(); auto branches = GetBranchesFromExpression( Chain, Selection.Expression() ) + GetBranchesFromExpression( Chain, Weight.Expression()) + GetBranchesFromExpression( Chain, Var.Expression()) ; ALLBRANCHESNEEDED.insert( ALLBRANCHESNEEDED.end(), branches.begin(), branches.end()); } Chain.SetBranchStatus("*",0); std::map enabled; for (auto & b : ALLBRANCHESNEEDED) { if( enabled[b]) continue; // if (debug) { // MessageSvc::Warning("ENABLING BRANCH ", b); // } enabled[b] = true; Chain.SetBranchStatus(b, 1); } //--- Now the formula Parsing --- std::map< RXSelection, TTreeFormula* > SelectionFormulas; std::map< RXSelection, vector > SelectionFlag; std::map< RXWeight , TTreeFormula* > WeightFormulas; std::map< RXWeight, vector > WeightValues; std::map< RXVarPlot , TTreeFormula* > VarFormulas; std::map< RXVarPlot, vector > VarValues; for( auto & selection : m_bookedRXSelections){ SelectionFormulas[selection.second] = new TTreeFormula(selection.second.Label() , selection.second.Expression(), &Chain); Chain.SetNotify( SelectionFormulas[selection.second] ); SelectionFlag[selection.second].reserve( _nEntries); } for( auto & weight : m_bookedRXWeights){ WeightFormulas[weight.second] = new TTreeFormula(weight.second.Label() , weight.second.Expression(), &Chain); Chain.SetNotify( WeightFormulas[weight.second] ); WeightValues[weight.second].reserve( _nEntries); } for( auto & var : m_bookedVariable){ VarFormulas[var.second] = new TTreeFormula(var.second.Label() , var.second.Expression(), &Chain); Chain.SetNotify( VarFormulas[var.second] ); VarValues[var.second].reserve( _nEntries); } Chain.LoadTree(0); MessageSvc::Info("Processing Ntuple");//, (TString)fmt::format("ET KEY {0}, Entries = {1} ", _eventType.GetKey(), _nEntries)); boost::progress_display show_progress_evtloop(_nEntries); for (Long64_t entry = 0; entry < _nEntries; ++entry) { ++show_progress_evtloop; Chain.LoadTree(entry); Chain.GetEntry(entry); for( auto & selection : m_bookedRXSelections){ SelectionFormulas[selection.second]->UpdateFormulaLeaves(); m_bookedSelectionResults[selection.second].push_back( SelectionFormulas[selection.second]->EvalInstance(0) ); } for( auto & weight : m_bookedRXWeights){ // cout<<"eval 2" <UpdateFormulaLeaves(); m_bookedWeightsResults[weight.second].push_back( WeightFormulas[weight.second]->EvalInstance(0) ); } for( auto & selection : m_bookedVariable){ // cout<<"eval 3" <UpdateFormulaLeaves(); m_bookedVariableValues[selection.second].push_back( VarFormulas[selection.second]->EvalInstance(0) ); } } Chain.LoadTree(0); Chain.SetBranchStatus("*", 1); // std::cout<<"Deleting"; // for( auto & ss : SelectionFormulas){ // delete ss.second; // } // for( auto & ss : SelectionFormulas){ // delete ss.second; // } // for( auto & ss : VarFormulas){ // delete ss.second; // } std::cout<<"EXIT"<< std::endl; } TH1D * RXDataPlayer::GetHistogram( int _idx_var, int _idx_cut, int _idx_weight){ if( _idx_var >= GetNVarPlot()){ MessageSvc::Error("Not existing Variab", "","EXIT_FAILURE"); } if( _idx_cut >= GetNSelections() ){ MessageSvc::Error("Not existing Cuts", "","EXIT_FAILURE"); } if( _idx_cut >= GetNWeightExp() ){ MessageSvc::Error("Not existing Cuts", "","EXIT_FAILURE"); } auto to_fill_values = m_bookedVariableValues[ m_bookedVariable[_idx_var]]; auto to_fill_selection = m_bookedSelectionResults[ m_bookedRXSelections[_idx_cut]]; auto to_fill_weight = m_bookedWeightsResults[ m_bookedRXWeights[_idx_weight] ]; double min = m_bookedVariable[_idx_var].min(); double max = m_bookedVariable[_idx_var].max(); int nUnderflow = 0; int nOverFlow = 0; TString _nameVar = m_bookedVariable[_idx_var].Expression(); TString _nameCut = m_bookedRXSelections[_idx_cut].Label(); TString _nameWeight = m_bookedRXWeights[_idx_weight].Label(); TString nameHisto = _nameVar + " [ "+_nameCut +","+_nameWeight+" ]"; TString titleHisto = _nameCut+_nameWeight; TH1D * h1 = new TH1D(nameHisto, nameHisto , m_bookedVariable[_idx_var].nBins() ,m_bookedVariable[_idx_var].min() , m_bookedVariable[_idx_var].max() ); std::cout< max ){ nOverFlow++; continue;} h1->Fill(VALUE, WEIGHT); } if( nUnderflow >0){ std::cout<<" n nUnderflow = "<< nUnderflow << std::endl; } if( nOverFlow >0){ std::cout<<" n nOverFlow = "<< nOverFlow << std::endl; } TString xAxis = m_bookedVariable[_idx_var].Label(); if( m_bookedVariable[_idx_var].Units() != ""){ xAxis+=" ["+m_bookedVariable[_idx_var].Units()+"]"; } h1->GetXaxis()->SetTitle(xAxis); TString yAxis = Form("Counts/%.2f", abs(max-min)/m_bookedVariable[_idx_var].nBins() ); if( m_bookedVariable[_idx_var].Units() != ""){ yAxis += " ["+m_bookedVariable[_idx_var].Units()+"]"; } h1->GetYaxis()->SetTitle(yAxis); // MessageSvc::Info() return h1; }; //GetNSelections // GetNWeightExp // GetNVarPlot vector RXDataPlayer::GetValues(int _idx_var){ if(_idx_var > GetNVarPlot() ){ MessageSvc::Error("Requesting IDX > stored ones","","EXIT_FAILURE"); } return m_bookedVariableValues[ m_bookedVariable[_idx_var]]; } vector RXDataPlayer::GetSelectionValues(int _idx_sel){ if(_idx_sel > GetNSelections() ){ MessageSvc::Error("Requesting IDX > stored ones","","EXIT_FAILURE"); } return m_bookedSelectionResults[ m_bookedRXSelections[_idx_sel]]; } vector RXDataPlayer::GetWeightValues( int _idx_weight){ if(_idx_weight > GetNWeightExp() ){ MessageSvc::Error("Requesting IDX > stored ones","","EXIT_FAILURE"); } return m_bookedWeightsResults[ m_bookedRXWeights[_idx_weight]]; } #endif