HighLAND
AnalysisUtils.cxx
1 #include "AnalysisUtils.hxx"
2 #include <unistd.h>
3 
4 //**************************************************
5 Float_t anaUtils::GetSeparationSquared(const Float_t* pos1, const Float_t* pos2){
6 //**************************************************
7  return ((pos1[0] - pos2[0])*(pos1[0] - pos2[0]) + (pos1[1] - pos2[1])*(pos1[1] - pos2[1]) + (pos1[2] - pos2[2])*(pos1[2] - pos2[2]));
8 }
9 
10 //********************************************************************
11 std::vector<Float_t> anaUtils::ComputeWeights(const AnaEventB& event){
12 //********************************************************************
13 
14  std::vector<Float_t> weights;
15 
16  // Only beam flux reweight for the moment
17  weights.push_back(event.Weight);
18 
19  return weights;
20 }
21 
22 //********************************************************************
23 std::string anaUtils::GetSoftwareVersionFromPath(const std::string& path){
24 //********************************************************************
25 
26  std::string version="";
27  size_t lastSlash = path.find_last_of("/");
28  version = path.substr(lastSlash+1);
29 
30  return version;
31 }
32 
33 //********************************************************************
34 std::string anaUtils::GetPackageNameFromProgram(const std::string& program){
35 //********************************************************************
36 
37  // Imagine we run all an executable (../../../numuCCAnalysis/v1r12/Darwin/RunNumuCCAnalysis.exe) from
38  // another package (PWD=/hep/T2K/nd280rep/ANALYSIS/HIGHLAND2/SIFS2/highland2/numuCCMultiPiAnalysis/v1r12/cmt)
39 
40  char the_path[256];
41 
42  // Get the current working directory
43  // i.e. /hep/T2K/nd280rep/ANALYSIS/HIGHLAND2/SIFS2/highland2/numuCCMultiPiAnalysis/v1r12/cmt
44  getcwd(the_path, 255);
45 
46  // Add the relative path to the executable
47  // i.e. /hep/T2K/nd280rep/ANALYSIS/HIGHLAND2/SIFS2/highland2/numuCCMultiPiAnalysis/v1r12/cmt/../../../numuCCAnalysis/v1r12/Darwin/RunNumuCCAnalysis.exe
48  strcat(the_path, "/");
49  strcat(the_path, program.c_str());
50 
51  return GetPackageNameFromPath(std::string(the_path));
52 }
53 
54 //********************************************************************
55 std::string anaUtils::GetPackageNameFromPath(const std::string& full_path){
56 //********************************************************************
57 
58  // another package (PWD=/hep/T2K/nd280rep/ANALYSIS/HIGHLAND2/SIFS2/highland2/numuCCMultiPiAnalysis/v1r12/cmt)
59 
60  char real_path[256];
61 
62  // convert into a canonical path (resolving .., env variables and symbolic links)
63  // i.e. /hep/T2K/nd280rep/ANALYSIS/HIGHLAND2/SIFS2/highland2/numuCCAnalysis/v1r12/Darwin/RunNumuCCAnalysis.exe
64  realpath(full_path.c_str(), real_path);
65 
66  // convert into string
67  std::string path(real_path);
68 
69  // Get all path up to the binary directory (excluded)
70  // i.e. /hep/T2K/nd280rep/ANALYSIS/HIGHLAND2/SIFS2/highland2/numuCCAnalysis/v1r12
71  path = path.substr(0, path.rfind(getenv("CMTCONFIG"))-1);
72 
73  // Check that the next substring is a package version and not a package name
74  // This is needed because in some configurations (i.e. jenkins validation) the package version
75  // is not present
76  std::string version = path.substr(path.find_last_of("/")+1);
77 
78  if (version[0]=='v' && (version[2]=='r' || version[3]=='r') &&
79  version.find("Analysis")==std::string::npos &&
80  version.find("highland")==std::string::npos &&
81  version.find("psyche") ==std::string::npos){
82 
83  // Get all path up to the last "/" (excluded)
84  // i.e. /hep/T2K/nd280rep/ANALYSIS/HIGHLAND2/SIFS2/highland2/numuCCAnalysis
85  path = path.substr(0,path.find_last_of("/"));
86  }
87 
88  // Get all path from the last "/" (excluded)
89  // i.e. numuCCAnalysis
90  path = path.substr(path.find_last_of("/")+1);
91 
92  return path;
93 }
94 
95 
96 //********************************************************************
97 std::vector<std::string> anaUtils::GetPackageHierarchy(){
98 //********************************************************************
99 
100  std::vector<std::string> packages;
101 
102  std::string PATHENV = getenv("PATH");
103 
104  // Get a maximum of 50 packages
105  for (Int_t i=0;i<50;i++){
106  size_t first = PATHENV.find_first_of(":");
107  std::string path= PATHENV.substr(0,first);
108  if (path.find("nd280AnalysisTools") ==std::string::npos){
109  std::string package = GetPackageNameFromPath(path);
110  packages.push_back(package);
111  if (package == "psycheCore") break;
112  }
113  PATHENV= PATHENV.substr(first+1);
114  }
115  return packages;
116 }
117 
118 //********************************************************************
120  //********************************************************************
121 
122  for (int i = 0; i < event.nTrueParticles; i++) {
123  if (event.TrueParticles[i]->ID == ID) {
124  return event.TrueParticles[i];
125  }
126  }
127 
128  return NULL;
129 }
130 
131 //**************************************************
132 std::vector<AnaTrackB*> anaUtils::MergeUniqueTracks(const std::vector<AnaTrackB*>& tracks1, const std::vector<AnaTrackB*>& tracks2) {
133 //**************************************************
134  std::vector<AnaTrackB*> merged;
135 
136  for (std::vector<AnaTrackB*>::const_iterator it = tracks1.begin(); it != tracks1.end(); it++) {
137  if (std::find(merged.begin(), merged.end(), *it) == merged.end()) {
138  merged.push_back(*it);
139  }
140  }
141 
142  for (std::vector<AnaTrackB*>::const_iterator it = tracks2.begin(); it != tracks2.end(); it++) {
143  if (std::find(merged.begin(), merged.end(), *it) == merged.end()) {
144  merged.push_back(*it);
145  }
146  }
147 
148  return merged;
149 }
std::vector< AnaTrackB * > MergeUniqueTracks(const std::vector< AnaTrackB *> &tracks1, const std::vector< AnaTrackB *> &tracks2)
Merge the two vectors of AnaTrackBs into a single vector, with no duplicated tracks.
std::string GetPackageNameFromPath(const std::string &path)
Get the package name provided a path containing the package name.
std::string GetPackageNameFromProgram(const std::string &program)
Get the package name provided the name of the executable being run.
float GetSeparationSquared(const Float_t *pos1, const Float_t *pos2)
Calculate the distance between two points.
Int_t ID
The ID of the trueObj, which corresponds to the ID of the TTruthParticle that created it...
std::string GetSoftwareVersionFromPath(const std::string &path)
Get The software version from the path of the package.
Representation of a true Monte Carlo trajectory/particle.
Float_t Weight
Run, subrun, event, time stamp, etc.
AnaTrueParticleB * GetTrueParticleByID(const AnaEventB &event, int ID)
Get the AnaTrueParticleB in the current spill with the given ID. Return NULL if it can&#39;t be found...
AnaTrueParticleB ** TrueParticles
The true MC particles used in this spill.
std::vector< std::string > GetPackageHierarchy()
Get the vector of packages ordered following the package hierarchy.
std::vector< Float_t > ComputeWeights(const AnaEventB &event)
Compute all event weights (FluxWeight, etc. not the systematic ones !!!) and return the vector...