### Example PDF Usage in Fortran Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/examples.dox A basic example of how to use LHAPDF from Fortran. ```fortran program fexample1 use LHAPDF implicit none integer :: i real(8) :: x, Q real(8), allocatable :: pdf_values(:) call init_LHAPDF('NNPDF31_nnlo_as_0118_nnlo_1') print *, "PDF name: ", get_description() x = 0.01d0 Q = 100.0d0 call xfx_LHAPDF(x, Q, pdf_values) print *, "PDF values at x=", x, ", Q=", Q do i = 1, size(pdf_values) print *, " ", i, ": ", pdf_values(i) end do end program fexample1 ``` -------------------------------- ### Install LHAPDF from Source Source: https://context7.com/hepcedar/lhapdf/llms.txt Commands to download, build, and configure the LHAPDF library, including setting necessary environment variables and installing PDF data sets. ```bash # Download and extract wget https://lhapdf.hepforge.org/downloads/?f=LHAPDF-6.5.6.tar.gz -O LHAPDF-6.5.6.tar.gz tar xf LHAPDF-6.5.6.tar.gz cd LHAPDF-6.5.6 # Configure and build ./configure --prefix=/path/for/installation make make install # Set runtime environment variables export PATH=$PATH:/path/for/installation/bin export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/for/installation/lib export PYTHONPATH=$PYTHONPATH:/path/for/installation/lib/python3.9/site-packages # Install PDF data sets lhapdf install CT10nlo ``` -------------------------------- ### Advanced Installation Options (lhapdf CLI) Source: https://context7.com/hepcedar/lhapdf/llms.txt Control installation behavior with options like dry run, force upgrade, keeping downloaded files, specifying custom directories, or using custom mirror URLs. ```bash lhapdf install --dryrun CT10nlo ``` ```bash lhapdf install --upgrade CT10nlo ``` ```bash lhapdf install --keep CT10nlo ``` ```bash lhapdf --pdfdir /custom/path install CT10nlo ``` ```bash lhapdf --source http://mirror.example.com/lhapdf/ install CT10nlo ``` -------------------------------- ### LHAPDF Fortran Interface Example Source: https://context7.com/hepcedar/lhapdf/llms.txt Example demonstrating the legacy Fortran interface for LHAPDF. Initializes a PDF set, retrieves information, and evolves PDFs. ```fortran program example implicit double precision (a-h,o-z) character name*64 double precision f(-6:6) ! Initialize PDF set by name name = 'CT10nlo' call InitPDFsetByName(name) ! Initialize member 0 (central value) call InitPDF(0) ! Get number of members in set call numberPDF(N) print *, 'Number of PDF members:', N ! Get validity ranges call GetXmin(0, xmin) call GetXmax(0, xmax) call GetQ2min(0, q2min) call GetQ2max(0, q2max) print *, 'x range: [', xmin, ',', xmax, ']' print *, 'Q2 range: [', q2min, ',', q2max, ']' ! Get alpha_s at M_Z QMZ = 91.18d0 alphas = alphasPDF(QMZ) print *, 'alpha_s(M_Z) =', alphas ! Evaluate PDFs x = 0.1d0 Q = 100.0d0 call evolvePDF(x, Q, f) ! f(-6:6) contains xf values: ! f(-6) = anti-top, ..., f(-1) = anti-down ! f(0) = gluon ! f(1) = down, ..., f(6) = top print *, 'At x =', x, ', Q =', Q, 'GeV:' print *, ' gluon:', f(0) print *, ' up:', f(2) print *, ' down:', f(1) ! Loop over x values do ix = 1, 10 x = 0.1d0 * ix call evolvePDF(x, Q, f) print '(A,F5.2,A,E12.4)', ' x=', x, ' gluon=', f(0) enddo end program example ``` -------------------------------- ### List Available PDF Sets (lhapdf CLI) Source: https://context7.com/hepcedar/lhapdf/llms.txt List all available PDF sets from the LHAPDF index. Use --installed to see only locally installed sets. ```bash lhapdf list ``` ```bash lhapdf list --installed ``` ```bash lhapdf list --outdated ``` ```bash lhapdf list "CT*" ``` ```bash lhapdf list "NNPDF*" ``` -------------------------------- ### Example PDF Usage in Python Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/examples.dox Demonstrates how to use LHAPDF from Python. ```python import lhapdf # Initialize the PDF pdf = lhapdf.PDF("NNPDF31_nnlo_as_0118_nnlo_1") # Get PDF values at x=0.01, Q=100 GeV x = 0.01 Q = 100.0 pdf_values = pdf.xfx(x, Q) print(f"PDF name: {pdf.description()}") print(f"PDF values at x={x}, Q={Q}:") for i, val in pdf_values.items(): print(f" {i}: {val}") ``` -------------------------------- ### Update and Install PDF Sets (lhapdf CLI) Source: https://context7.com/hepcedar/lhapdf/llms.txt Update the local index of available PDF sets and install new sets. Supports installing single sets, multiple sets, or sets matching a pattern. ```bash lhapdf update ``` ```bash lhapdf install CT10nlo ``` ```bash lhapdf install NNPDF30_nlo_as_0118 ``` ```bash lhapdf install CT10nlo MMHT2014nlo68cl ``` ```bash lhapdf install "NNPDF31*" ``` -------------------------------- ### Query PDFs and Calculate Uncertainties in Python Source: https://context7.com/hepcedar/lhapdf/llms.txt Shows how to query PDF values, manage PDF sets, and perform uncertainty calculations using the Python interface. Includes an example of NumPy integration for grid generation. ```python #!/usr/bin/env python3 import lhapdf import numpy as np # Version and path information print(f"LHAPDF version: {lhapdf.version()}") print(f"Search paths: {lhapdf.paths()}") # Add custom search path # lhapdf.pathsPrepend("/path/to/extra/pdfsets") # Create a single PDF member pdf = lhapdf.mkPDF("CT10nlo", 0) # Alternative: pdf = lhapdf.mkPDF("CT10nlo/0") # Query PDF values x = 0.01 Q = 91.2 # Z mass # Single flavor query xf_gluon = pdf.xfxQ(21, x, Q) # gluon xf_up = pdf.xfxQ(2, x, Q) # up quark print(f"xf(gluon) at x={x}, Q={Q}: {xf_gluon:.4f}") print(f"xf(up) at x={x}, Q={Q}: {xf_up:.4f}") # All flavors for pid in pdf.flavors(): print(f" PID {pid:3d}: {pdf.xfxQ(pid, x, Q):.4e}") # Alpha_s print(f"\nalpha_s(M_Z) = {pdf.alphasQ(91.2):.4f}") # Working with PDF sets pset = lhapdf.getPDFSet("CT10nlo") print(f"\nSet: {pset.name}") print(f"Description: {pset.description[:80]}...") print(f"Error type: {pset.errorType}") print(f"Number of members: {pset.size}") # Create all members pdfs = pset.mkPDFs() # Calculate observable for all members xg_values = [p.xfxQ(21, x, Q) for p in pdfs] xu_values = [p.xfxQ(2, x, Q) for p in pdfs] # Calculate uncertainties unc = pset.uncertainty(xg_values) print(f"\nGluon uncertainty: {unc.central:.4f} +{unc.errplus:.4f} -{unc.errminus:.4f}") # Correlation corr = pset.correlation(xg_values, xu_values) print(f"Gluon-up correlation: {corr:.4f}") # NumPy integration: fill 2D array with xf values xs = np.logspace(-5, 0, 50) # x from 1e-5 to 1 Qs = np.logspace(1, 4, 40) # Q from 10 to 10000 GeV gluon_grid = np.zeros((len(xs), len(Qs))) for ix, x in enumerate(xs): for iq, Q in enumerate(Qs): gluon_grid[ix, iq] = pdf.xfxQ(21, x, Q) print(f"\nGluon grid shape: {gluon_grid.shape}") print(f"Max value: {gluon_grid.max():.4f}") ``` ```python #!/usr/bin/env python3 import lhapdf # Load a PDF set pset = lhapdf.getPDFSet("CT10nlo") pdfs = pset.mkPDFs() x = 0.1 Q = 100.0 # Collect values for all members xg_all = [pdf.xfxQ(21, x, Q) for pdf in pdfs] xu_all = [pdf.xfxQ(2, x, Q) for pdf in pdfs] print(f"Error type: {pset.errorType}") print(f"Error conf level: {pset.errorConfLevel}") ``` -------------------------------- ### Make a New PDF Subclass in C++ Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/examples.dox Provides an example of how to create a new PDF subclass in C++. This is for advanced users who want to implement custom PDF models. ```cpp // analyticpdf.cc // This is a conceptual example. A real implementation would involve // defining a class derived from LHAPDF::PDF and implementing its methods. #include "LHAPDF/LHAPDF.h" #include namespace LHAPDF { // Define a simple analytic PDF model class AnalyticPDF : public PDF { public: AnalyticPDF(int id) : PDF(id) {} // Implement the core xfx method virtual std::vector> xfx(double x, double Q) const { // Example: A very simple analytic form (not physically realistic) double u = x * (1.0 - x); double d = x * (1.0 - x) * 0.5; return {{2, u}, {1, d}, {-2, u}, {-1, d}}; // Example quark and antiquark PDFs } // Implement other required virtual methods as needed // e.g., description(), numberPDFs(), alphas(), etc. virtual std::string description() const override { return "AnalyticPDF Example"; } virtual int numberPDFs() const override { return 4; } // Example: u, d, anti-u, anti-d virtual double alphas(double Q) const override { return 0.3; } // Example constant alpha_s }; // Register the new PDF subclass REGISTER_PDF_TYPE(AnalyticPDF); } // Example usage in a main function (would typically be in a separate file) int main() { try { // Initialize using a dummy ID, assuming AnalyticPDF is registered // In a real scenario, you might have a specific ID or name for this analytic PDF. LHAPDF::initPDF("AnalyticPDF"); // Assuming 'AnalyticPDF' is registered name std::cout << "Initialized custom PDF: " << LHAPDF::getDescription() << std::endl; double x = 0.1; double Q = 10.0; auto pdf_values = LHAPDF::xfx(x, Q); std::cout << "PDF values at x=" << x << ", Q=" << Q << ":" << std::endl; for (const auto& p : pdf_values) { std::cout << " " << p.first << ": " << p.second << std::endl; } } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; } ``` -------------------------------- ### Create Custom Analytic PDF in C++ Source: https://context7.com/hepcedar/lhapdf/llms.txt Implement a custom PDF class by inheriting from LHAPDF::PDF. This example shows a simple power-law toy model for parton distribution functions. ```cpp #include "LHAPDF/PDF.h" #include #include // Custom analytic PDF class class MyAnalyticPDF : public LHAPDF::PDF { public: MyAnalyticPDF() { // Define available flavors info().set_entry("Flavors", "-5,-4,-3,-2,-1,21,1,2,3,4,5"); } // Required: implement the core xf calculation double _xfxQ2(int id, double x, double q2) const override { // Simple toy model: power-law behavior if (std::abs(id) > 5 && id != 21) return 0; double Q = std::sqrt(q2); double alpha = 0.5 + 0.1 * std::log(Q / 10.0); // Running exponent if (id == 21) { // Gluon return 3.0 * std::pow(1.0 - x, 5.0) * std::pow(x, -alpha); } else if (id > 0) { // Quarks return 0.5 * std::pow(1.0 - x, 3.0) * std::pow(x, -0.3); } else { // Antiquarks return 0.2 * std::pow(1.0 - x, 5.0) * std::pow(x, -0.5); } } // Required: vector fill version (for efficiency) void _xfxQ2(double x, double q2, std::vector& ret) const override { ret.resize(13); // Standard 13 flavors for (int id = -6; id <= 6; ++id) { if (id == 0) { ret[6] = _xfxQ2(21, x, q2); // Gluon at index 6 } else { ret[id + 6] = _xfxQ2(id, x, q2); } } } // Required: range checks (define as always valid for analytic PDF) bool inRangeX(double x) const override { return x > 0 && x <= 1.0; } bool inRangeQ2(double q2) const override { return q2 > 0; } }; int main() { MyAnalyticPDF myPDF; LHAPDF::PDF& pdf = myPDF; std::cout << "Custom analytic PDF values:" << std::endl; for (double x = 0.01; x < 1.0; x += 0.1) { double Q2 = 10000.0; // Q = 100 GeV std::cout << "x=" << x << " gluon=" << pdf.xfxQ2(21, x, Q2) << " up=" << pdf.xfxQ2(2, x, Q2) << std::endl; } return 0; } ``` -------------------------------- ### Compile Fortran code against LHAPDF Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/main.dox Use `lhapdf-config` to get linker flags for Fortran and explicitly link the C++ standard library. ```bash gfortran mycode.f90 -o myexe `lhapdf-config --ldflags` -lstdc++ ``` -------------------------------- ### Generate Random Values from Hessian Sets (Python) Source: https://context7.com/hepcedar/lhapdf/llms.txt Generate random PDF values from Hessian sets. This example uses zeros for random numbers for demonstration purposes. ```python if pset.errorType.startswith("hessian"): nmem = pset.size - 1 neigen = nmem // 2 # Generate random numbers (here just zeros for demo) randoms = [0.0] * neigen xg_random = pset.randomValueFromHessian(xg_all, randoms) xu_random = pset.randomValueFromHessian(xu_all, randoms) print(f"\nRandom values: xg={xg_random:.4f}, xu={xu_random:.4f}") ``` -------------------------------- ### Get Available Parton Flavors and Check Existence Source: https://context7.com/hepcedar/lhapdf/llms.txt Retrieves a list of available parton IDs and checks for the presence of specific partons. Useful for PDFs with non-standard content like photons. Uses PDG numbering and LHAPDF::PIDs enum. ```cpp #include "LHAPDF/LHAPDF.h" #include #include int main() { std::unique_ptr pdf(LHAPDF::mkPDF("CT10nlo", 0)); // Get list of available parton flavors const std::vector& flavors = pdf->flavors(); std::cout << "Available parton flavors:" << std::endl; for (int pid : flavors) { std::string name; switch (pid) { case -6: name = "anti-top"; break; case -5: name = "anti-bottom"; break; case -4: name = "anti-charm"; break; case -3: name = "anti-strange"; break; case -2: name = "anti-up"; break; case -1: name = "anti-down"; break; case 0: case 21: name = "gluon"; break; case 1: name = "down"; break; case 2: name = "up"; break; case 3: name = "strange"; break; case 4: name = "charm"; break; case 5: name = "bottom"; break; case 6: name = "top"; break; case 22: name = "photon"; break; default: name = "unknown"; } std::cout << " PID " << pid << ": " << name << std::endl; } // Check for specific partons std::cout << "\nParton availability:" << std::endl; std::cout << " Has gluon (21): " << (pdf->hasFlavor(21) ? "yes" : "no") << std::endl; std::cout << " Has up (2): " << (pdf->hasFlavor(2) ? "yes" : "no") << std::endl; std::cout << " Has photon (22): " << (pdf->hasFlavor(22) ? "yes" : "no") << std::endl; std::cout << " Has top (6): " << (pdf->hasFlavor(6) ? "yes" : "no") << std::endl; // Use parton ID enum for clarity using namespace LHAPDF::PIDs; std::cout << "\nUsing PIDs enum:" << std::endl; std::cout << " Has GLUON: " << (pdf->hasFlavor(GLUON) ? "yes" : "no") << std::endl; std::cout << " Has UP: " << (pdf->hasFlavor(UP) ? "yes" : "no") << std::endl; std::cout << " Has CHARM: " << (pdf->hasFlavor(CHARM) ? "yes" : "no") << std::endl; return 0; } ``` -------------------------------- ### Create and Query PDF Objects in C++ Source: https://context7.com/hepcedar/lhapdf/llms.txt Demonstrates using the mkPDF factory function to instantiate PDF objects and querying parton density values using xfxQ and xfxQ2 methods. ```cpp #include "LHAPDF/LHAPDF.h" #include #include int main() { // Create PDF by set name and member number LHAPDF::PDF* pdf = LHAPDF::mkPDF("CT10nlo", 0); // Alternative: create by combined string "setname/member" LHAPDF::PDF* pdf2 = LHAPDF::mkPDF("CT10nlo/0"); // Alternative: create by LHAPDF ID code LHAPDF::PDF* pdf3 = LHAPDF::mkPDF(11000); // Use smart pointers for automatic memory management std::unique_ptr smartpdf(LHAPDF::mkPDF("CT10nlo", 0)); // Query PDF values double x = 0.1; // momentum fraction double Q = 100.0; // energy scale in GeV double Q2 = Q * Q; // squared energy scale // Get gluon xf(x,Q) value (PID 21 = gluon) double xf_gluon = smartpdf->xfxQ(21, x, Q); std::cout << "xf(gluon) at x=" << x << ", Q=" << Q << " GeV: " << xf_gluon << std::endl; // Get up quark xf(x,Q2) value (PID 2 = up quark) double xf_up = smartpdf->xfxQ2(2, x, Q2); std::cout << "xf(up) at x=" << x << ", Q2=" << Q2 << " GeV2: " << xf_up << std::endl; // Clean up raw pointers delete pdf; delete pdf2; delete pdf3; return 0; } ``` -------------------------------- ### Query PDF values with xfxQ and xfxQ2 Source: https://context7.com/hepcedar/lhapdf/llms.txt Demonstrates retrieving PDF values for specific flavors or all flavors using energy scales Q or Q2. Includes optimized methods for filling pre-allocated containers to avoid repeated memory allocation. ```cpp #include "LHAPDF/LHAPDF.h" #include #include #include int main() { std::unique_ptr pdf(LHAPDF::mkPDF("CT10nlo", 0)); double x = 0.01; double Q = 91.2; // Z boson mass double Q2 = Q * Q; // Query single flavor by PDG ID // Common IDs: -6=anti-top, -5=anti-bottom, ..., -1=anti-down, // 0 or 21=gluon, 1=down, 2=up, 3=strange, 4=charm, 5=bottom, 6=top double xf_gluon = pdf->xfxQ(21, x, Q); // Using Q double xf_up = pdf->xfxQ2(2, x, Q2); // Using Q2 (more efficient) double xf_down = pdf->xfxQ(1, x, Q); double xf_strange = pdf->xfxQ(3, x, Q); std::cout << "At x=" << x << ", Q=" << Q << " GeV:" << std::endl; std::cout << " gluon: " << xf_gluon << std::endl; std::cout << " up: " << xf_up << std::endl; std::cout << " down: " << xf_down << std::endl; std::cout << " strange: " << xf_strange << std::endl; // Query all flavors at once - returns map std::map all_xf = pdf->xfxQ(x, Q); for (const auto& kv : all_xf) { std::cout << " PID " << kv.first << ": " << kv.second << std::endl; } // More efficient: fill pre-allocated map (avoids allocation per call) std::map xf_map; pdf->xfxQ(x, Q, xf_map); // Even more efficient: fill pre-allocated vector (13 standard flavors) // Vector index: pid+6 for quarks, index 6 for gluon // Order: [-6, -5, -4, -3, -2, -1, 21, 1, 2, 3, 4, 5, 6] std::vector xf_vec(13); pdf->xfxQ(x, Q, xf_vec); std::cout << " gluon (from vector): " << xf_vec[6] << std::endl; std::cout << " up (from vector): " << xf_vec[8] << std::endl; // index = 2+6 return 0; } ``` -------------------------------- ### Manage PDF sets and calculate uncertainties Source: https://context7.com/hepcedar/lhapdf/llms.txt Shows how to instantiate PDF sets, access metadata, generate PDF members, and compute uncertainties using the built-in error calculation methods. ```cpp #include "LHAPDF/LHAPDF.h" #include #include #include int main() { // Get PDF set by name LHAPDF::PDFSet set("CT10nlo"); // Access set metadata std::cout << "Set name: " << set.name() << std::endl; std::cout << "Description: " << set.description() << std::endl; std::cout << "Number of members: " << set.size() << std::endl; std::cout << "Error type: " << set.errorType() << std::endl; std::cout << "LHAPDF ID: " << set.lhapdfID() << std::endl; // Create all PDF members - raw pointers (caller manages memory) std::vector pdfs = set.mkPDFs(); // Or use smart pointers for automatic cleanup typedef std::unique_ptr PDFPtr; std::vector smartpdfs = set.mkPDFs(); // Create a single member LHAPDF::PDF* central = set.mkPDF(0); // Central value is usually member 0 // Evaluate observable for all members double x = 0.1; double Q = 100.0; std::vector gluon_values; for (size_t i = 0; i < set.size(); ++i) { gluon_values.push_back(smartpdfs[i]->xfxQ(21, x, Q)); } // Calculate PDF uncertainty (automatically uses correct formula for error type) LHAPDF::PDFUncertainty unc = set.uncertainty(gluon_values); std::cout << "\nGluon PDF uncertainty at x=" << x << ", Q=" << Q << " GeV:" << std::endl; std::cout << " Central: " << unc.central << std::endl; std::cout << " Error+: " << unc.errplus << std::endl; std::cout << " Error-: " << unc.errminus << std::endl; std::cout << " Symmetric error: " << unc.errsymm << std::endl; // Scale uncertainty to different confidence level (e.g., 90%) LHAPDF::PDFUncertainty unc90 = set.uncertainty(gluon_values, 90.0); std::cout << "\nAt 90% CL:" << std::endl; std::cout << " Symmetric error: " << unc90.errsymm << std::endl; // Clean up raw pointers for (auto* p : pdfs) delete p; delete central; return 0; } ``` -------------------------------- ### Test PDF Grids in C++ Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/examples.dox Demonstrates how to use and test PDF grids in C++. This is useful for performance-critical applications. ```cpp #include "LHAPDF/LHAPDF.h" #include int main(int argc, char* argv[]) { // Initialize with a PDF that supports grids LHAPDF::initPDF("NNPDF31_nnlo_as_0118_nnlo_1"); // Check if grids are available if (LHAPDF::hasGrid()) { std::cout << "PDF grids are available." << std::endl; // Example: Accessing grid data (specifics depend on grid implementation) // This is a conceptual example; actual grid access might differ. // For instance, you might iterate through grid points or query specific values. double x = 0.005; double Q = 50.0; auto pdf_values = LHAPDF::xfx(x, Q); std::cout << "PDF values at x=" << x << ", Q=" << Q << " (using grids if available):" << std::endl; for (const auto& p : pdf_values) { std::cout << " " << p.first << ": " << p.second << std::endl; } } else { std::cout << "PDF grids are not available for this PDF." << std::endl; } return 0; } ``` -------------------------------- ### Compile C++ code against LHAPDF Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/main.dox Use `lhapdf-config` to get compiler and linker flags for C++. ```bash g++ mycode.cc -o myexe `lhapdf-config --cflags --ldflags` ``` -------------------------------- ### Access LHAPDF Metadata and Configuration in C++ Source: https://context7.com/hepcedar/lhapdf/llms.txt Demonstrates how to access global configuration, PDF-specific metadata, set-level information, and physical parameters like quark masses and flavor thresholds. ```cpp #include "LHAPDF/LHAPDF.h" #include #include int main() { // Access global configuration LHAPDF::Info& config = LHAPDF::getConfig(); // Get verbosity level (0=quiet, 1=normal, 2=debug) int verbosity = config.get_entry_as("Verbosity", 1); std::cout << "Verbosity level: " << verbosity << std::endl; // Create PDF and access its metadata std::unique_ptr pdf(LHAPDF::mkPDF("CT10nlo", 0)); // Access PDF-level info LHAPDF::PDFInfo& pdfinfo = pdf->info(); // Query metadata values std::cout << "\nPDF metadata:" << std::endl; std::cout << " Format: " << pdfinfo.get_entry("Format") << std::endl; std::cout << " QCD order: " << pdf->orderQCD() << std::endl; std::cout << " Data version: " << pdf->dataversion() << std::endl; std::cout << " Member type: " << pdf->type() << std::endl; // Get quark masses std::cout << "\nQuark masses (GeV):" << std::endl; std::cout << " Charm: " << pdf->quarkMass(4) << std::endl; std::cout << " Bottom: " << pdf->quarkMass(5) << std::endl; std::cout << " Top: " << pdf->quarkMass(6) << std::endl; // Get flavor thresholds std::cout << "\nFlavor thresholds (GeV):" << std::endl; std::cout << " Charm: " << pdf->quarkThreshold(4) << std::endl; std::cout << " Bottom: " << pdf->quarkThreshold(5) << std::endl; // Access set-level info LHAPDF::PDFSet& pset = pdf->set(); std::cout << "\nSet-level info:" << std::endl; std::cout << " Set name: " << pset.name() << std::endl; // Check for specific metadata keys if (pdfinfo.has_key("XMin")) { std::cout << " XMin: " << pdfinfo.get_entry("XMin") << std::endl; } // List all available keys std::cout << "\nAll metadata keys:" << std::endl; for (const std::string& key : pdfinfo.keys()) { std::cout << " " << key << std::endl; } return 0; } ``` -------------------------------- ### Create tarball for PDF set integration Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/main.dox Create a `.tar.gz` archive that expands to the set directory and its contents. ```bash tar czf MySet.tar.gz MySet/ ``` -------------------------------- ### Test PDFs in C++ Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/examples.dox Demonstrates how to use and test PDFs in C++. Includes basic PDF operations and checks. ```cpp #include "LHAPDF/LHAPDF.h" #include int main(int argc, char* argv[]) { LHAPDF::initPDF("NNPDF31_nnlo_as_0118_nnlo_1"); std::cout << "PDF name: " << LHAPDF::getDescription() << std::endl; // Get PDF values at x=0.01, Q=100 GeV double x = 0.01; double Q = 100.0; auto pdf_values = LHAPDF::xfx(x, Q); std::cout << "PDF values at x=" << x << ", Q=" << Q << ":" << std::endl; for (const auto& p : pdf_values) { std::cout << " " << p.first << ": " << p.second << std::endl; } return 0; } ``` -------------------------------- ### Querying PDF Values with xfxQ and xfxQ2 Source: https://context7.com/hepcedar/lhapdf/llms.txt Demonstrates how to retrieve PDF values xf(x,Q) or xf(x,Q2) for specific parton IDs, or all flavors at once, using the `xfxQ` and `xfxQ2` methods. ```APIDOC ## xfxQ and xfxQ2 - Query PDF Values ### Description The `xfxQ` and `xfxQ2` methods return the PDF value xf(x,Q) or xf(x,Q2) for a given parton ID, momentum fraction x, and energy scale Q or Q2. Multiple overloads exist for querying single flavors or all flavors at once. ### Method `double xfxQ(int pid, double x, double Q)` `double xfxQ2(int pid, double x, double Q2)` `std::map xfxQ(double x, double Q)` `void xfxQ(double x, double Q, std::map& map)` `void xfxQ(double x, double Q, std::vector& vec)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include "LHAPDF/LHAPDF.h" #include #include #include int main() { std::unique_ptr pdf(LHAPDF::mkPDF("CT10nlo", 0)); double x = 0.01; double Q = 91.2; // Z boson mass double Q2 = Q * Q; // Query single flavor by PDG ID // Common IDs: -6=anti-top, -5=anti-bottom, ..., -1=anti-down, // 0 or 21=gluon, 1=down, 2=up, 3=strange, 4=charm, 5=bottom, 6=top double xf_gluon = pdf->xfxQ(21, x, Q); // Using Q double xf_up = pdf->xfxQ2(2, x, Q2); // Using Q2 (more efficient) double xf_down = pdf->xfxQ(1, x, Q); double xf_strange = pdf->xfxQ(3, x, Q); std::cout << "At x=" << x << ", Q=" << Q << " GeV:" << std::endl; std::cout << " gluon: " << xf_gluon << std::endl; std::cout << " up: " << xf_up << std::endl; std::cout << " down: " << xf_down << std::endl; std::cout << " strange: " << xf_strange << std::endl; // Query all flavors at once - returns map std::map all_xf = pdf->xfxQ(x, Q); for (const auto& kv : all_xf) { std::cout << " PID " << kv.first << ": " << kv.second << std::endl; } // More efficient: fill pre-allocated map (avoids allocation per call) std::map xf_map; pdf->xfxQ(x, Q, xf_map); // Even more efficient: fill pre-allocated vector (13 standard flavors) // Vector index: pid+6 for quarks, index 6 for gluon // Order: [-6, -5, -4, -3, -2, -1, 21, 1, 2, 3, 4, 5, 6] std::vector xf_vec(13); pdf->xfxQ(x, Q, xf_vec); std::cout << " gluon (from vector): " << xf_vec[6] << std::endl; std::cout << " up (from vector): " << xf_vec[8] << std::endl; // index = 2+6 return 0; } ``` ### Response #### Success Response (200) - **double** (return value): The PDF value xf(x,Q) or xf(x,Q2). - **std::map** (return value): A map where keys are PDG IDs and values are the corresponding PDF values for all flavors. - **void** (for map/vector overloads): The provided map or vector is populated with PDF values. #### Response Example ```json { "xf_gluon": 0.12345, "xf_up": 0.09876, "xf_down": 0.05432, "xf_strange": 0.00123 } ``` ``` -------------------------------- ### Calculate PDF Uncertainties in C++ Source: https://gitlab.com/hepcedar/lhapdf/-/blob/main/doc/examples.dox Demonstrates how to calculate PDF uncertainties in C++. This involves using multiple PDF replicas. ```cpp #include "LHAPDF/LHAPDF.h" #include #include #include int main(int argc, char* argv[]) { // Initialize a PDF set with multiple members for uncertainty calculation LHAPDF::initPDF("NNPDF31_nnlo_as_0118_nnlo_1"); double x = 0.01; double Q = 100.0; // Calculate the central value (e.g., member 0) LHAPDF::setPDFMember(0); double central_value = LHAPDF::xfx(x, Q)[2]; // Example: gluon PDF (ID 21 is often used, but here using index 2) // Calculate values for all members to determine uncertainty std::vector member_values; for (int i = 0; i < LHAPDF::numberOftypes(); ++i) { LHAPDF::setPDFMember(i); member_values.push_back(LHAPDF::xfx(x, Q)[2]); // Example: gluon PDF } // Calculate standard deviation (uncertainty) double sum = 0.0; for (double val : member_values) { sum += val; } double mean = sum / member_values.size(); double sum_sq_diff = 0.0; for (double val : member_values) { sum_sq_diff += std::pow(val - mean, 2); } double std_dev = std::sqrt(sum_sq_diff / (member_values.size() - 1)); // Sample standard deviation std::cout << "PDF (gluon) at x="<< x << ", Q="<< Q << ":" << std::endl; std::cout << " Central value (member 0): " << central_value << std::endl; std::cout << " Mean value: " << mean << std::endl; std::cout << " Standard deviation: " << std_dev << std::endl; // Note: For NNLO PDFs, specific uncertainty definitions (e.g., symmetric vs asymmetric) // might be used. This is a basic standard deviation calculation. return 0; } ```