11package org .jlab .service .alert ;
22
3+ import ai .djl .repository .zoo .ZooModel ;
4+ import ai .djl .translate .TranslateException ;
35import java .io .File ;
46import java .util .ArrayList ;
57import java .util .concurrent .atomic .AtomicInteger ;
1416import org .jlab .io .hipo .HipoDataSource ;
1517import org .jlab .io .hipo .HipoDataSync ;
1618import org .jlab .rec .alert .TrackMatchingAI .ModelTrackMatching ;
19+ import org .jlab .rec .alert .AIPID .ModelPrePID ;
1720import org .jlab .rec .alert .banks .RecoBankWriter ;
1821import org .jlab .rec .alert .projections .TrackProjector ;
1922import org .jlab .rec .atof .hit .ATOFHit ;
2427import org .jlab .rec .ahdc .Track .Track ;
2528import org .jlab .clas .pdg .PDGDatabase ;
2629import org .jlab .clas .pdg .PDGParticle ;
30+ import java .util .logging .Logger ;
2731
2832
2933
3034import ai .djl .util .Pair ;
35+ import org .jlab .rec .alert .AIPID .PrePIDResult ;
3136
3237
3338/**
@@ -51,7 +56,7 @@ public class ALERTEngine extends ReconstructionEngine {
5156 *
5257 */
5358 private RecoBankWriter rbc ;
54-
59+ static final Logger LOGGER = Logger . getLogger ( ModelPrePID . class . getName ());
5560 Detector ATOF ; // ALERT ATOF detector
5661 private AlertDCDetector AHDC ; // ALERT AHDC detector
5762
@@ -64,6 +69,7 @@ public class ALERTEngine extends ReconstructionEngine {
6469 private double b ; //Magnetic field
6570
6671 private ModelTrackMatching modelTrackMatching ;
72+ private ModelPrePID modelPrePID ;
6773
6874 public void setB (double B ) {
6975 this .b = B ;
@@ -90,6 +96,7 @@ public boolean init() {
9096 rbc = new RecoBankWriter ();
9197
9298 modelTrackMatching = new ModelTrackMatching ();
99+ modelPrePID = new ModelPrePID ();
93100
94101 AlertTOFFactory factory = new AlertTOFFactory ();
95102 DatabaseConstantProvider cp = new DatabaseConstantProvider (11 , "default" );
@@ -222,7 +229,92 @@ public boolean processDataEvent(DataEvent event) {
222229 }
223230 }
224231 rbc .appendTrackMatchingAIBank (event , matched_ATOF_hit_id );
232+
233+ // ---------------------------------------------------------------------------------------
234+ // PrePID using AI (AHDC::track + ATOF::clusters matched via ALERT::ai:projections)
235+ // ---------------------------------------------------------------------------------------
236+ if (event .hasBank ("ALERT::ai:projections" ) && event .hasBank ("AHDC::track" ) && event .hasBank ("ATOF::hits" )) {
237+
238+ DataBank bankProj = event .getBank ("ALERT::ai:projections" );
239+ DataBank bankTrk = event .getBank ("AHDC::track" );
240+ DataBank bankHit = event .getBank ("ATOF::hits" );
241+
242+ ArrayList <PrePIDResult > prepid_results = new ArrayList <>();
243+
244+ for (int i = 0 ; i < bankProj .rows (); i ++) {
245+
246+ int trackid = bankProj .getInt ("trackid" , i );
247+ int hitid = bankProj .getInt ("matched_atof_hit_id" , i ); // TODO: Fix to hit_id instead of clusterid
248+
249+ // TODO: refactor this to replace this with single line
250+ int trkRow = -1 ;
251+ for (int r = 0 ; r < bankTrk .rows (); r ++) {
252+ if (bankTrk .getInt ("trackid" , r ) == trackid ) { trkRow = r ; break ; }
253+ }
254+ if (trkRow < 0 ) continue ;
225255
256+ int hitRow = -1 ;
257+ for (int r = 0 ; r < bankHit .rows (); r ++) {
258+ if (bankHit .getInt ("id" , r ) == hitid ) { hitRow = r ; break ; }
259+ }
260+ if (hitRow < 0 ) continue ;
261+
262+ // Build feature vector float[23] in the exact training order
263+ float [] x = new float [23 ];
264+
265+ // AHDC::track (13)
266+ x [0 ] = bankTrk .getFloat ("x" , trkRow );
267+ x [1 ] = bankTrk .getFloat ("y" , trkRow );
268+ x [2 ] = bankTrk .getFloat ("z" , trkRow );
269+ x [3 ] = bankTrk .getFloat ("px" , trkRow );
270+ x [4 ] = bankTrk .getFloat ("py" , trkRow );
271+ x [5 ] = bankTrk .getFloat ("pz" , trkRow );
272+ x [6 ] = bankTrk .getInt ("n_hits" , trkRow );
273+ x [7 ] = bankTrk .getInt ("sum_adc" , trkRow );
274+ x [8 ] = bankTrk .getFloat ("path" , trkRow );
275+ x [9 ] = bankTrk .getFloat ("dEdx" , trkRow );
276+ x [10 ] = bankTrk .getFloat ("p_drift" , trkRow );
277+ x [11 ] = bankTrk .getFloat ("chi2" , trkRow );
278+ x [12 ] = bankTrk .getFloat ("sum_residuals" , trkRow );
279+
280+ /*// ATOF::clusters (10)
281+ x[13] = bankClu.getInt("n_bar", cluRow);
282+ x[14] = bankClu.getInt("n_wedge", cluRow);
283+ x[15] = bankClu.getFloat("time", cluRow);
284+ x[16] = bankClu.getFloat("x", cluRow);
285+ x[17] = bankClu.getFloat("y", cluRow);
286+ x[18] = bankClu.getFloat("z", cluRow);
287+ x[19] = bankClu.getFloat("energy", cluRow);
288+ x[20] = bankClu.getFloat("pathlength", cluRow);
289+ x[21] = bankClu.getFloat("inpathlength", cluRow);
290+ x[22] = bankClu.getInt("projID", cluRow);*/
291+
292+ // ATOF::Hits (Temporarily updating to the same 10 slots as ATOF Clusters would have if it worked)
293+ x [13 ] = 0f ;
294+ x [14 ] = 0f ;
295+ x [15 ] = bankHit .getFloat ("time" , hitRow );
296+ x [16 ] = bankHit .getFloat ("x" , hitRow );
297+ x [17 ] = bankHit .getFloat ("y" , hitRow );
298+ x [18 ] = bankHit .getFloat ("z" , hitRow );
299+ x [19 ] = bankHit .getFloat ("energy" , hitRow );
300+ x [20 ] = 0f ;
301+ x [21 ] = 0f ;
302+ x [22 ] = 0f ;
303+
304+ try {
305+ float [] pred = modelPrePID .prediction (x );
306+ int prepid = (int ) pred [0 ];
307+ prepid_results .add (new PrePIDResult (trackid , hitid , prepid , pred [1 ], pred [2 ], pred [3 ], pred [4 ], pred [5 ]));
308+ } catch (TranslateException ex ) {
309+ LOGGER .warning (() -> "Exception in ALERTEngine PrePID: " + ex );
310+ }
311+ }
312+
313+ rbc .appendPrePIDBank (event , prepid_results );
314+ }
315+
316+
317+
226318 ///////////////////////////////////////////
227319 /// Kalmam Filter
228320 /// ///////////////////////////////////////
0 commit comments