Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Low q2 training #21

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
18cd472
Added NN training script
simonge Aug 30, 2023
0c5ed31
updated training script for clarity
simonge Sep 21, 2023
82fefd9
added simple first benchmarks
simonge Oct 4, 2023
a61cfae
benchmark change
simonge Jan 9, 2024
1a259d5
Extended hit benchmarks
simonge Jan 25, 2024
83eed26
Added deeper directory structure for benchmarks
simonge Jan 25, 2024
5c13f13
Added directory structure and moved geometry id definitions to a sepa…
simonge Jan 25, 2024
ed0a7a5
Added acceptance plots
simonge Jan 25, 2024
132dfbc
Fixed memory and sumw2 issues
simonge Jan 26, 2024
72496d2
Added reconstruction and fixed some bugs
simonge Jan 26, 2024
58a034f
Formatting and range corrections
simonge Jan 29, 2024
77e327d
Added postprocessing script to create and format histograms into canv…
simonge Jan 29, 2024
0bc51e4
Fixed typo in dataframe string
simonge Jan 29, 2024
7c2adb0
Fixed some bugs, added some new resolution plots and sped things up
simonge Jan 31, 2024
80933e9
Added .gitignore and moved plots into directory
simonge Jan 31, 2024
48d7d1a
Added calibrations and fieldmaps to .gitignore
simonge Jan 31, 2024
c8f49b8
Removed excess text from histogram titles and cleaned up integrated a…
simonge Jan 31, 2024
ea6bd3e
Merge remote-tracking branch 'origin/master' into add_lowq2_benchmarks
simonge Jan 31, 2024
eacf3c8
Improved formatted historgams and fixed some inconsistencies
simonge Feb 1, 2024
1608444
Added primary-secondary hits example plot
simonge Feb 1, 2024
057570b
Added Low-Q2 benchmarks to CI, does nothing currently
simonge Apr 30, 2024
e13ff34
Some changes
simonge May 6, 2024
067f01c
Added basic training components
simonge May 6, 2024
c319d83
Updated to work locally at least
simonge May 7, 2024
2d58b4f
Works locally and has settings to test on github
simonge May 7, 2024
7b158fa
Removed full local eicrecon path
simonge May 7, 2024
cbce8fd
Empty commit
veprbl May 8, 2024
861c47b
Updated to remove training from branch
simonge May 16, 2024
8569cbe
Merge remote-tracking branch 'origin/master' into add_lowq2_benchmarks
simonge May 16, 2024
29374e0
removed some local paths and generalised
simonge May 16, 2024
e3475b1
Changes to for remote running
simonge May 16, 2024
5b972e4
Removed other tests while investigating CI
simonge May 16, 2024
bf1ab67
Included analysis config.yml
simonge May 16, 2024
27d9a43
Some more simplification
simonge May 16, 2024
efd92c9
Remove other CI checks temporarily
simonge May 16, 2024
669ff1b
Added analysis gitignore
simonge May 16, 2024
285dfa0
Split and updated config.yml
simonge May 16, 2024
3ee84ed
Updated stage
simonge May 16, 2024
6af8b24
Merge branch 'Low-Q2_training' of github.com:eic/detector_benchmarks …
simonge May 16, 2024
90ff930
Merge remote-tracking branch 'origin/master' into add_lowq2_benchmarks
veprbl May 17, 2024
38dae45
Merge remote-tracking branch 'origin/master' into Low-Q2_training
veprbl May 17, 2024
dbd39e2
.gitlab-ci.yml: fix syntax
veprbl May 17, 2024
9183d93
.gitlab-ci.yml: fix syntax
veprbl May 17, 2024
dd29b98
.gitlab-ci.yml: full include path
veprbl May 17, 2024
79ed678
config.yml: this is not a snakemake config
veprbl May 17, 2024
fe37ac8
.gitlab-ci.yml: enable zdc back
veprbl May 17, 2024
21bbf74
.gitlab-ci.yml: just comment needs instead
veprbl May 17, 2024
cf8c5a4
Added gitignores
simonge May 21, 2024
d6dc119
Updated to (probably) work when the remote flie isn't available or not
simonge May 21, 2024
a27c89f
edited while epic-main isn't named right
simonge May 22, 2024
c796a3d
Merge branch 'add_lowq2_benchmarks' of github.com:eic/detector_benchm…
simonge May 22, 2024
117685e
removed config.yml file in LOWQ2 as analysis called directly
simonge May 22, 2024
6e6794e
Changed config path
simonge May 22, 2024
cc5e54c
Added gitignores for training directories
simonge May 22, 2024
ebe03d3
Added remote config from other branch, getting confused about what is…
simonge May 23, 2024
cadec47
Merge branch 'add_lowq2_benchmarks' into Low-Q2_training
simonge May 23, 2024
550cca5
Add training config to gitlab yml
simonge May 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ get_data:
- source .local/bin/env.sh
- ls -lrtha
- ln -s "${LOCAL_DATA_PATH}/sim_output" sim_output
- ln -s "${LOCAL_DATA_PATH}/datasets/data" data
- ln -s "${LOCAL_DATA_PATH}/datasets/data" data
# snakemake support
- mkdir "${DETECTOR_CONFIG}"
- ln -s "${LOCAL_DATA_PATH}/sim_output" "${DETECTOR_CONFIG}/sim_output"
Expand All @@ -130,21 +130,23 @@ get_data:

include:
# - local: 'benchmarks/backgrounds/config.yml'
- local: 'benchmarks/ecal_gaps/config.yml'
- local: 'benchmarks/tracking_detectors/config.yml'
- local: 'benchmarks/barrel_ecal/config.yml'
- local: 'benchmarks/barrel_hcal/config.yml'
- local: 'benchmarks/zdc/config.yml'
- local: 'benchmarks/material_maps/config.yml'
- local: 'benchmarks/material_scan/config.yml'
- local: 'benchmarks/pid/config.yml'
- local: 'benchmarks/timing/config.yml'
- local: 'benchmarks/b0_tracker/config.yml'
# - local: 'benchmarks/ecal_gaps/config.yml'
# - local: 'benchmarks/tracking_detectors/config.yml'
# - local: 'benchmarks/barrel_ecal/config.yml'
# - local: 'benchmarks/barrel_hcal/config.yml'
# - local: 'benchmarks/zdc/config.yml'
# - local: 'benchmarks/material_maps/config.yml'
# - local: 'benchmarks/material_scan/config.yml'
# - local: 'benchmarks/pid/config.yml'
# - local: 'benchmarks/timing/config.yml'
# - local: 'benchmarks/b0_tracker/config.yml'
# - local: 'benchmarks/LOWQ2/analysis/config.yml'
- local: 'benchmarks/LOWQ2/reconstruction_training/config.yml'

deploy_results:
stage: deploy
needs:
- ["collect_results:zdc","collect_results:barrel_ecal","collect_results:barrel_hcal","collect_results:material_scan"]
# - ["collect_results:zdc","collect_results:barrel_ecal","collect_results:barrel_hcal","collect_results:material_scan"]
script:
- echo "deploy results!"
- find results -print | sort | tee summary.txt
Expand Down Expand Up @@ -222,5 +224,3 @@ pages:
artifacts:
paths:
- public


5 changes: 5 additions & 0 deletions benchmarks/LOWQ2/analysis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plots*
calibrations*
fieldmaps*
gdml*
.snakemake*
289 changes: 289 additions & 0 deletions benchmarks/LOWQ2/analysis/LOWQ2Benchmarks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
#include "TString.h"
#include "TCanvas.h"
#include "ROOT/RDataFrame.hxx"
#include <vector>
#include "TH1.h"
#include "TFile.h"
#include "LOWQ2hits.h"
#include "LOWQ2acceptance.h"
#include "LOWQ2clusters.h"
#include "LOWQ2reconstruction.h"

// Define alias
using RNode = ROOT::RDF::RNode;
using H1ResultPtr = ROOT::RDF::RResultPtr<TH1D>;
using H2ResultPtr = ROOT::RDF::RResultPtr<TH2D>;
using H3ResultPtr = ROOT::RDF::RResultPtr<TH3D>;

using RVecS = ROOT::VecOps::RVec<string>;
using RVecI = ROOT::VecOps::RVec<int>;

std::map<TString,std::tuple<std::map<TString,H1ResultPtr>,std::map<TString,H2ResultPtr>,std::map<TString,H3ResultPtr>>> histMap;

//---------------------------------------------------------------------------------------------
// Create dataframe from input file(s)
//---------------------------------------------------------------------------------------------
RNode initialise( string fileName ){

ROOT::RDataFrame d0("events",fileName);
return d0;

}

//---------------------------------------------------------------------------------------------
// Create histograms showing the variation of flux in various system components
//---------------------------------------------------------------------------------------------
void WriteFluxPlots(H2ResultPtr hist, std::string parts){
// Make projection of 2D pixel binned histogram
auto nBins = hist->GetNcells();
TString pixelFluxName = TString(parts)+"Flux";
TString logPixelFluxName = TString(parts)+"LogFlux";

//Get maximum bin content to set range
double maxBinContent = hist->GetMaximum();
double logMax = log10(maxBinContent);

//Create pixel flux histogram
TH1D* pixelFlux = new TH1D(pixelFluxName,pixelFluxName,100,0,maxBinContent);

//Create log pixel flux histogram
TH1D* logPixelFlux = new TH1D(logPixelFluxName,logPixelFluxName,100,0,logMax);
for(int i=0; i<nBins; i++){
pixelFlux->Fill(hist->GetBinContent(i));
logPixelFlux->Fill(log10(hist->GetBinContent(i)));
}
pixelFlux->GetXaxis()->SetTitle("N_{hits}");
logPixelFlux->GetXaxis()->SetTitle("log(N_{hits})");
pixelFlux->GetYaxis()->SetTitle("N_{pixels}");
logPixelFlux->GetYaxis()->SetTitle("N_{pixels}");

pixelFlux->Write();
logPixelFlux->Write();

}

//---------------------------------------------------------------------------------------------
//Create histograms showing the variation of resolution accross the kinematic phase space
//---------------------------------------------------------------------------------------------
void WriteResolutionPlots(H2ResultPtr hist, std::string parts){

hist->FitSlicesX(nullptr,0,-1,5);
TH1D* h1 = (TH1D*)gDirectory->Get(TString(hist->GetName())+"_2");
h1->GetXaxis()->SetTitle(hist->GetXaxis()->GetTitle());
h1->GetYaxis()->SetTitle(TString(hist->GetYaxis()->GetTitle())+"Resolution");

h1->Write();

}


//---------------------------------------------------------------------------------------------
// Format and write plots
//---------------------------------------------------------------------------------------------
void writePlots( TString outName ){

TFile* rootfile = new TFile(outName,"RECREATE");
auto benchmarkDir = rootfile->mkdir("LOWQ2");

//Loop over 1D histograms saving
for(auto &[bmkey,hists]: histMap){

//Split mapped distributions name into parts
std::stringstream ss(bmkey.Data());
std::string part;
std::vector<std::string> parts;
while (std::getline(ss, part, '/')) {
parts.push_back(part);
}

TDirectory* dir = benchmarkDir;
for (size_t i = 0; i < parts.size(); ++i) {
// This is not the last part, create or get the directory
if (!dir->GetDirectory(parts[i].c_str())) {
dir = dir->mkdir(parts[i].c_str());
} else {
dir = dir->GetDirectory(parts[i].c_str());
}
dir->cd();
}

TDirectory* currentDir = gDirectory;

for(auto &[key,hist]: get<0>(hists)){

//Split histogram name into parts
std::stringstream ss2(key.Data());
std::string part2;
std::vector<std::string> parts2;
while (std::getline(ss2, part2, '/')) {
parts2.push_back(part2);
}

TDirectory* dir = currentDir;
for (size_t i = 0; i < parts2.size(); ++i) {
if (i == parts2.size() - 1) {
// This is the last part, write the histogram
hist->SetMinimum(0);
hist->Write(parts2[i].c_str());
} else {
// This is not the last part, create or get the directory
if (!dir->GetDirectory(parts2[i].c_str())) {
dir = dir->mkdir(parts2[i].c_str());
} else {
dir = dir->GetDirectory(parts2[i].c_str());
}
dir->cd();
}
}
currentDir->cd();
// hist->Write(key);
}

//Loop over 2D histograms saving and calculating fluxes as needed
for(auto &[key,hist]: get<1>(hists)){

TDirectory* currentDir = gDirectory;
std::stringstream ss(key.Data());
std::string part;
TDirectory* dir = currentDir;
std::vector<std::string> parts;
while (std::getline(ss, part, '/')) {
parts.push_back(part);
}
for (size_t i = 0; i < parts.size(); ++i) {
if (i == parts.size() - 1) {
// If histogram name contains rate or hits then calculate flux
if(parts[i].find("Rate") != std::string::npos || parts[i].find("Hits") != std::string::npos){
WriteFluxPlots(hist,parts[i].c_str());
}

// If histogram name contains Res then create a profile
if(parts[i].find("Res") != std::string::npos){
WriteResolutionPlots(hist,parts[i].c_str());
}

hist->Sumw2(false);
hist->Write();
} else {
// This is not the last part, create or get the directory
if (!dir->GetDirectory(parts[i].c_str())) {
dir = dir->mkdir(parts[i].c_str());
} else {
dir = dir->GetDirectory(parts[i].c_str());
}
dir->cd();
}
}
currentDir->cd();
}

//Loop over 3D histograms saving
for(auto &[key,hist]: get<2>(hists)){
TDirectory* currentDir = gDirectory;
std::stringstream ss(key.Data());
std::string part;
TDirectory* dir = currentDir;
std::vector<std::string> parts;
while (std::getline(ss, part, '/')) {
parts.push_back(part);
}
for (size_t i = 0; i < parts.size(); ++i) {
if (i == parts.size() - 1) {
// This is the last part, write the histogram
hist->Write(parts[i].c_str());

//Fit histogram z slices and save 2d histogram
hist->FitSlicesZ(nullptr,0,-1,5);
TH2D* h2 = (TH2D*)gDirectory->Get(TString(hist->GetName())+"_2");
h2->GetXaxis()->SetTitle(hist->GetXaxis()->GetTitle());
h2->GetYaxis()->SetTitle(hist->GetYaxis()->GetTitle());
h2->GetZaxis()->SetTitle(TString(hist->GetZaxis()->GetTitle())+"Resolution");
h2->SetTitle(hist->GetTitle());
h2->Write();

} else {
// This is not the last part, create or get the directory
if (!dir->GetDirectory(parts[i].c_str())) {
dir = dir->mkdir(parts[i].c_str());
} else {
dir = dir->GetDirectory(parts[i].c_str());
}
dir->cd();
}
}
currentDir->cd();
// hist->Write(key);
}

benchmarkDir->cd();

}

rootfile->Close();

}

//---------------------------------------------------------------------------------------------
// Create the benchmark plots
//---------------------------------------------------------------------------------------------
void LOWQ2Benchmarks( string inName, TString outName, dd4hep::Detector& detector, double eventRate ){

auto node = initialise( inName );

int events = *node.Count();
double eventWeight = eventRate / events;

RVecS colNames = node.GetColumnNames();

std::string readoutName = "TaggerTrackerHits";

if(Any(colNames==readoutName)){
//-----------------------------------------
// Hit detector IDs
//-----------------------------------------
auto ids = detector.readout(readoutName).idSpec().fields();

node = node.Define("hitParticleIndex","_TaggerTrackerHits_MCParticle.index")
.Define("generatorStat","MCParticles.generatorStatus")
.Define("primary",[](RVecI index, RVecI status){
RVecI result(index.size());
for (size_t i = 0; i < index.size(); ++i) {
result[i] = status[index[i]] == 1;
}
return result;
},{"hitParticleIndex","generatorStat"});

std::string filterName = "TaggerTrackerHitsFilter";
auto allnode = node.Define(filterName,"TaggerTrackerHits");
auto primarynode = node.Define(filterName,"TaggerTrackerHits[primary==1]");
auto secondarynode = node.Define(filterName,"TaggerTrackerHits[primary==0]");

for(auto &[key,id]: ids){
TString colName = key+"ID";
node = node.Define(colName,getSubID(key,detector),{readoutName});
primarynode = primarynode.Define(colName,getSubID(key,detector),{filterName});
secondarynode = secondarynode.Define(colName,getSubID(key,detector),{filterName});
}

//Create Rate Plots
histMap["Rates/AllHits"] = createHitPlots(node,eventWeight);
histMap["Rates/PrimaryHits"] = createHitPlots(primarynode,eventWeight);
histMap["Rates/SecondaryHits"] = createHitPlots(secondarynode,eventWeight);

}

if((Any(colNames==readoutName) || Any(colNames=="InclusiveKinematicsElectron")) && Any(colNames=="MCParticles")){
histMap["Acceptance"] = createAcceptancePlots(node);
}

if(Any(colNames=="TaggerTrackerClusterPositions")){
histMap["Clusters"] = createClusterPlots(node);
}

if(Any(colNames=="LowQ2TrackParameters") && Any(colNames=="MCParticles")){
histMap["Reconstruction"] = createReconstructionPlots(node);
}

writePlots( outName );

}
Loading