-
Notifications
You must be signed in to change notification settings - Fork 26
ALERT: Improvement made to the algorithm for generating all track candidates. #508
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
Merged
baltzell
merged 6 commits into
JeffersonLab:development
from
mathieuouillon:development
Mar 27, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26d799e
Change the option to select the track finding to a enum.
mathieuouillon 0bc8384
Change the default options for DJL:
mathieuouillon 3861767
Modification of the track candidates generation
mathieuouillon 6b9afce
Improve the function to get all the track candidates.
mathieuouillon 28dd7f8
Update AHDCEngine to initialize model conditionally and adjust predic…
mathieuouillon 377f726
Merge branch 'development' into development
mathieuouillon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,38 @@ | ||
| package org.jlab.rec.ahdc.AI; | ||
|
|
||
| import org.apache.commons.lang3.mutable.MutableBoolean; | ||
| import org.jlab.rec.ahdc.Hit.Hit; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.*; | ||
|
|
||
| /** | ||
| * The TrackConstruction class is responsible for constructing all possible track | ||
| * candidates from a set of superpreclusters. | ||
| */ | ||
| public class TrackConstruction { | ||
| private int max_number_of_track_candidates = 10000; | ||
| private double max_angle = Math.toRadians(60); | ||
|
|
||
| /** | ||
| * Default constructor. | ||
| */ | ||
| public TrackConstruction() {} | ||
|
|
||
| /** | ||
| * Computes the modulo operation, which returns the remainder of the division | ||
| * of one number by another. This method handles floating-point edge cases | ||
| * to ensure accurate results within the expected range. | ||
| * | ||
| * @param x The dividend. | ||
| * @param y The divisor. If y is 0, the method returns x. | ||
| * @return The result of x modulo y. The result is in the range: | ||
| * - [0..y) if y > 0 | ||
| * - (y..0] if y < 0 | ||
| * Special cases are handled to avoid floating-point inaccuracies. | ||
| */ | ||
| private double mod(double x, double y) { | ||
|
|
||
| if (0. == y) return x; | ||
|
|
@@ -33,74 +56,135 @@ private double mod(double x, double y) { | |
| return m; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Wraps an angle to the range [0, 2π). | ||
| * | ||
| * @param angle The angle to wrap. | ||
| * @return The angle wrapped to the range [0, 2π). | ||
| */ | ||
| private double warp_zero_two_pi(double angle) { return mod(angle, 2. * Math.PI); } | ||
|
|
||
| /** | ||
| * Checks if an angle is within a specified range. | ||
| * | ||
| * @param angle The angle to check. | ||
| * @param lower The lower bound of the range. | ||
| * @param upper The upper bound of the range. | ||
| * @return {@code true} if the angle is within the range, {@code false} otherwise. | ||
| */ | ||
| private boolean angle_in_range(double angle, double lower, double upper) { return warp_zero_two_pi(angle - lower) <= warp_zero_two_pi(upper - lower); } | ||
|
|
||
| /** | ||
| * Computes the Cartesian product of two lists of integers, ensuring the number of track candidates | ||
| * does not exceed the maximum allowed limit. | ||
| * | ||
| * @param v1 The first list of integer combinations. | ||
| * @param v2 The second list of integers to combine with the first list. | ||
| * @param too_much_track_candidates A mutable boolean that is set to {@code true} if the number of track candidates exceeds the maximum limit. | ||
| * @param number_of_track_candidates The current count of track candidates. | ||
| * @return A list of all possible combinations of integers from {@code v1} and {@code v2}. | ||
| */ | ||
| private ArrayList<ArrayList<Integer>> cartesian_product(ArrayList<ArrayList<Integer>> v1, ArrayList<Integer> v2, MutableBoolean too_much_track_candidates, int number_of_track_candidates) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably simplifieable, maybe warrants optimization |
||
| ArrayList<ArrayList<Integer>> result = new ArrayList<>(); | ||
| for (ArrayList<Integer> i : v1) { | ||
| if (too_much_track_candidates.booleanValue()) break; | ||
| for (int j : v2) { | ||
| if (too_much_track_candidates.booleanValue()) break; | ||
| ArrayList<Integer> n = new ArrayList<>(i); | ||
| n.add(j); | ||
| result.add(n); | ||
|
|
||
| if (number_of_track_candidates + result.size() >= max_number_of_track_candidates) { | ||
| too_much_track_candidates.setValue(true); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public ArrayList<ArrayList<PreclusterSuperlayer>> get_all_possible_track(ArrayList<PreclusterSuperlayer> preclusterSuperlayers) { | ||
|
|
||
| // Get seeds to start the track finding algorithm | ||
| ArrayList<PreclusterSuperlayer> seeds = new ArrayList<>(); | ||
| for (PreclusterSuperlayer precluster : preclusterSuperlayers) { | ||
| if (precluster.getPreclusters().get(0).get_hits_list().get(0).getSuperLayerId() == 1) seeds.add(precluster); | ||
| } | ||
| seeds.sort(new Comparator<PreclusterSuperlayer>() { | ||
| @Override | ||
| public int compare(PreclusterSuperlayer a1, PreclusterSuperlayer a2) { | ||
| return Double.compare(Math.atan2(a1.getY(), a1.getX()), Math.atan2(a2.getY(), a2.getX())); | ||
| return result; | ||
| } | ||
|
|
||
| public boolean get_all_possible_track(ArrayList<PreclusterSuperlayer> preclusterSuperlayers, ArrayList<ArrayList<PreclusterSuperlayer>> all_track_candidates) { | ||
|
|
||
| /* | ||
| Identify all superpreclusters located in the first superlayer. | ||
| These superpreclusters serve as seeds for constructing track candidates. | ||
| A track candidate always starts from a seed. | ||
| */ | ||
| ArrayList<Integer> seed_index = new ArrayList<>(); | ||
| for (int i = 0; i < preclusterSuperlayers.size(); i++) { | ||
| if (!preclusterSuperlayers.get(i).getPreclusters().isEmpty() && | ||
| preclusterSuperlayers.get(i).getSuperlayer() == 1) { | ||
| seed_index.add(i); | ||
| } | ||
| }); | ||
| // System.out.println("seeds: " + seeds); | ||
| } | ||
|
|
||
| // Get all possible tracks ---------------------------------------------------------------- | ||
| double max_angle = Math.toRadians(60); | ||
|
|
||
| ArrayList<ArrayList<PreclusterSuperlayer>> all_combinations = new ArrayList<>(); | ||
| for (PreclusterSuperlayer seed : seeds) { | ||
| double phi_seed = warp_zero_two_pi(Math.atan2(seed.getY(), seed.getX())); | ||
| boolean sucess = true; | ||
| int number_of_track_candidates = 0; | ||
|
|
||
| ArrayList<PreclusterSuperlayer> track = new ArrayList<>(); | ||
| for (PreclusterSuperlayer p : preclusterSuperlayers) { | ||
| double phi_p = warp_zero_two_pi(Math.atan2(p.getY(), p.getX())); | ||
| if (angle_in_range(phi_p, phi_seed - max_angle, phi_seed + max_angle)) track.add(p); | ||
| } | ||
| // System.out.println("track: " + track.size()); | ||
|
|
||
| ArrayList<ArrayList<PreclusterSuperlayer>> combinations = new ArrayList<>(List.of(new ArrayList<>(List.of(seed)))); | ||
| // System.out.println("combinations: " + combinations); | ||
|
|
||
| for (int i = 1; i < 5; ++i) { | ||
| ArrayList<ArrayList<PreclusterSuperlayer>> new_combinations = new ArrayList<>(); | ||
| for (ArrayList<PreclusterSuperlayer> combination : combinations) { | ||
|
|
||
| for (PreclusterSuperlayer precluster : track) { | ||
| if (precluster.getPreclusters().get(0).get_hits_list().get(0).getSuperLayerId() == seed.getPreclusters().get(0).get_hits_list().get(0).getSuperLayerId() + i) { | ||
| // System.out.printf("Good Precluster x: %.2f, y: %.2f, r: %.2f%n", precluster.getX(), precluster.getY(), Math.hypot(precluster.getX(), precluster.getY())); | ||
| // System.out.println("combination: " + combination); | ||
|
|
||
| ArrayList<PreclusterSuperlayer> new_combination = new ArrayList<>(combination); | ||
| new_combination.add(precluster); | ||
| // System.out.println("new_combination: " + new_combination); | ||
| new_combinations.add(new_combination); | ||
| } | ||
| } | ||
| for (ArrayList<PreclusterSuperlayer> c : new_combinations) { | ||
| // System.out.println("c.size: " + c.size() + ", c: " + c); | ||
| } | ||
| // Loop over all seeds to construct track candidates | ||
| for (int s : seed_index) { | ||
| // Check if the number of track candidates exceeds the maximum limit if so, stop the loop | ||
| if (!sucess) break; | ||
|
|
||
| // Find all superpreclusters that have a phi angle within phi angle of the seed +/- 60 degrees | ||
| // The goal is to reduce the number of superpreclusters to loop over | ||
| double phi_seed = warp_zero_two_pi(Math.atan2(preclusterSuperlayers.get(s).getY(), preclusterSuperlayers.get(s).getX())); // phi angle of the seed | ||
| ArrayList<Integer> all_superpreclusters = new ArrayList<>(); // all superpreclusters that are within phi angle of the seed | ||
| for (int i = 0; i < preclusterSuperlayers.size(); ++i) { | ||
| double phi_p = warp_zero_two_pi(Math.atan2(preclusterSuperlayers.get(i).getY(), preclusterSuperlayers.get(i).getX())); | ||
| if (angle_in_range(phi_p, phi_seed - max_angle, phi_seed + max_angle)) { | ||
| all_superpreclusters.add(i); | ||
| } | ||
| combinations = new_combinations; | ||
| if (combinations.size() > 10000) break; | ||
| } | ||
| for (ArrayList<PreclusterSuperlayer> combination : combinations) { | ||
| if (combination.size() == 5) { | ||
| all_combinations.add(combination); | ||
|
|
||
|
|
||
| // Sort the superpreclusters by superlayer to have a simpler loops after | ||
| ArrayList<Integer> superpreclusters_s1 = new ArrayList<>(List.of(s)); | ||
| ArrayList<Integer> superpreclusters_s3 = new ArrayList<>(); | ||
| ArrayList<Integer> superpreclusters_s4 = new ArrayList<>(); | ||
| ArrayList<Integer> superpreclusters_s2 = new ArrayList<>(); | ||
| ArrayList<Integer> superpreclusters_s5 = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < all_superpreclusters.size(); i++) { | ||
| if (preclusterSuperlayers.get(all_superpreclusters.get(i)).getPreclusters().get(0).get_Super_layer() == 2) | ||
| superpreclusters_s2.add(all_superpreclusters.get(i)); | ||
| else if (preclusterSuperlayers.get(all_superpreclusters.get(i)).getPreclusters().get(0).get_Super_layer() == 3) | ||
| superpreclusters_s3.add(all_superpreclusters.get(i)); | ||
| else if (preclusterSuperlayers.get(all_superpreclusters.get(i)).getPreclusters().get(0).get_Super_layer() == 4) | ||
| superpreclusters_s4.add(all_superpreclusters.get(i)); | ||
| else if (preclusterSuperlayers.get(all_superpreclusters.get(i)).getPreclusters().get(0).get_Super_layer() == 5) | ||
| superpreclusters_s5.add(all_superpreclusters.get(i)); | ||
| } | ||
|
|
||
| MutableBoolean too_much_track_candidates = new MutableBoolean(); // Need to be a mutable boolean to be able to change it in the cartesian_product method | ||
| too_much_track_candidates.setFalse(); | ||
|
|
||
| // Find all possible combinations of superpreclusters on different superlayers | ||
| ArrayList<ArrayList<Integer>> combinations_s1_s2 = cartesian_product(new ArrayList<>(List.of(superpreclusters_s1)), superpreclusters_s2, too_much_track_candidates, number_of_track_candidates); | ||
| ArrayList<ArrayList<Integer>> combinations_s1_s2_s3 = cartesian_product(combinations_s1_s2, superpreclusters_s3, too_much_track_candidates, number_of_track_candidates); | ||
| ArrayList<ArrayList<Integer>> combinations_s1_s2_s3_s4 = cartesian_product(combinations_s1_s2_s3, superpreclusters_s4, too_much_track_candidates, number_of_track_candidates); | ||
| ArrayList<ArrayList<Integer>> combinations_s1_s2_s3_s4_s5 = cartesian_product(combinations_s1_s2_s3_s4, superpreclusters_s5, too_much_track_candidates, number_of_track_candidates); | ||
|
|
||
| // Keep track of the number of track candidates | ||
| number_of_track_candidates += combinations_s1_s2_s3_s4_s5.size(); | ||
| if (too_much_track_candidates.booleanValue()) sucess = false; // If the number of track candidates exceeds the maximum limit, set success to false | ||
|
|
||
| // Add all track candidates to the list of all track candidates | ||
| // And switch back from index to superprecluster | ||
| for (ArrayList<Integer> combination : combinations_s1_s2_s3_s4_s5) { | ||
| ArrayList<PreclusterSuperlayer> track_candidate = new ArrayList<>(); | ||
| for (int index : combination) { | ||
| track_candidate.add(preclusterSuperlayers.get(index)); | ||
| } | ||
| all_track_candidates.add(track_candidate); | ||
| } | ||
| } | ||
|
|
||
| return all_combinations; | ||
| return sucess; | ||
| } | ||
|
|
||
| } | ||
5 changes: 5 additions & 0 deletions
5
reconstruction/alert/src/main/java/org/jlab/rec/ahdc/Mode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package org.jlab.rec.ahdc; | ||
|
|
||
| public enum Mode { | ||
| AI_Track_Finding, CV_Track_Finding; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably should be IEEERemainder