tof_dataclasses/
database.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
//! Database access & entities of the TOF
//!
//! A local .sqlite database is shipped with 
//! this packet and allows to access all
//! mapping relevant TOF information, e.g. 
//! paddle connection to LTBs anr RBs,
//! paddle information, paddle cordinates,
//! panel ids and so on.
//!

use std::fmt;
use std::collections::HashMap;

use glob::glob;
use regex::Regex;
use chrono::{
    DateTime,
    Utc,
};

//use rusqlite::Connection;
use diesel::prelude::*;
mod schema;
    
use schema::tof_db_rat::dsl::*;
use schema::tof_db_dsicard::dsl::*;

use crate::calibrations::RBCalibrations;
//use crate::constants::HUMAN_TIMESTAMP_FORMAT;
use crate::DsiLtbRBMapping;
pub use crate::RbChPidMapping;

// FIXME - probably we should make this nicer
pub type DsiJChPidMapping = DsiLtbRBMapping; 

/// Universal function to connect to the database
pub fn connect_to_db(database_url : String) -> Result<diesel::SqliteConnection, ConnectionError>  {
    //let database_url = "database.sqlite3";
    SqliteConnection::establish(&database_url)
}

/// Create a mapping of mtb link ids to rb ids
pub fn get_linkid_rbid_map(rbs : &Vec<ReadoutBoard>) -> HashMap<u8, u8>{
  let mut mapping = HashMap::<u8, u8>::new();
  for rb in rbs {
    mapping.insert(rb.mtb_link_id, rb.rb_id);
  }
  mapping
}

/// Create a mapping of rb id to mtb link ids
pub fn get_rbid_linkid_map(rbs : &Vec<ReadoutBoard>) -> HashMap<u8, u8> {
  let mut mapping = HashMap::<u8, u8>::new();
  for rb in rbs {
    mapping.insert(rb.rb_id, rb.mtb_link_id);
  }
  mapping
}

pub fn get_dsi_j_ch_pid_map(paddles : &Vec<Paddle>) -> DsiJChPidMapping {
  let mut mapping = DsiJChPidMapping::new();
  for dsi in 1..6 {
    let mut jmap = HashMap::<u8, HashMap<u8, (u8, u8)>>::new();
    for j in 1..6 {
      let mut rbidch_map : HashMap<u8, (u8,u8)> = HashMap::new();
      for ch in 1..17 {
        let rbidch = (0,0);
        rbidch_map.insert(ch,rbidch);
        //map[dsi] = 
      }
      jmap.insert(j,rbidch_map);
    }
    mapping.insert(dsi,jmap);
  }
  for pdl in paddles {
    let dsi  = pdl.dsi as u8;
    let   j  = pdl.j_ltb   as u8;
    let ch_b = pdl.ltb_chA as u8;
    let ch_a = pdl.ltb_chB as u8;
    let pid  = pdl.paddle_id as u8;
    let panel_id = pdl.panel_id as u8;
    mapping.get_mut(&dsi).unwrap().get_mut(&j).unwrap().insert(ch_a,(pid, panel_id));
    mapping.get_mut(&dsi).unwrap().get_mut(&j).unwrap().insert(ch_b,(pid, panel_id));
  }
  return mapping;
}

/// Create a map for rbid, ch -> paddle id. This is for both sides
/// and will always return a paddle id independent of A or B
pub fn get_rb_ch_pid_map(paddles : &Vec<Paddle>) -> RbChPidMapping {
  let mut mapping = RbChPidMapping::new();
  for rbid  in 1..51 {
    let mut chmap = HashMap::<u8, u8>::new();
    for ch in 1..9 {
      chmap.insert(ch,0);
    }
    mapping.insert(rbid,chmap);
  }
  for pdl in paddles {
    let rb_id = pdl.rb_id  as u8;
    let ch_a  = pdl.rb_chA as u8;
    let ch_b  = pdl.rb_chB as u8;
    let pid   = pdl.paddle_id as u8;
    //println!("rb_id {rb_id}, chA {ch_a}, chB {ch_b}");
    *mapping.get_mut(&rb_id).unwrap().get_mut(&ch_a).unwrap() = pid;
    *mapping.get_mut(&rb_id).unwrap().get_mut(&ch_b).unwrap() = pid;
  }
  mapping
}

/// Create a map for rbid, ch -> paddle id. This is only for the A 
/// side and will not have an entry in case the given RB channel
/// is connected to the B side of the paddle
pub fn get_rb_ch_pid_a_map(paddles : &Vec<Paddle>) -> RbChPidMapping {
  let mut mapping = RbChPidMapping::new();
  for rbid  in 1..51 {
    let mut chmap = HashMap::<u8, u8>::new();
    for ch in 1..9 {
      chmap.insert(ch,0);
    }
    mapping.insert(rbid,chmap);
  }
  for pdl in paddles {
    let rb_id = pdl.rb_id  as u8;
    let ch_a  = pdl.rb_chA as u8;
    let pid   = pdl.paddle_id as u8;
    *mapping.get_mut(&rb_id).unwrap().get_mut(&ch_a).unwrap() = pid;
  }
  mapping
}


/// Create a map for rbid, ch -> paddle id. This is only for the B 
/// side and will not have an entry in case the given RB channel
/// is connected to the A side of the paddle
pub fn get_rb_ch_pid_b_map(paddles : &Vec<Paddle>) -> RbChPidMapping {
  let mut mapping = RbChPidMapping::new();
  for rbid  in 1..51 {
    let mut chmap = HashMap::<u8, u8>::new();
    for ch in 1..9 {
      chmap.insert(ch,0);
    }
    mapping.insert(rbid,chmap);
  }
  for pdl in paddles {
    let rb_id = pdl.rb_id  as u8;
    let ch_b  = pdl.rb_chB as u8;
    let pid   = pdl.paddle_id as u8;
    *mapping.get_mut(&rb_id).unwrap().get_mut(&ch_b).unwrap() = pid;
  }
  mapping
}

/// A representation of a run 
#[derive(Debug, Clone, Queryable,Insertable, Selectable, serde::Serialize, serde::Deserialize)]
#[diesel(table_name = schema::tof_db_run)]
#[diesel(primary_key(run_id))]
pub struct Run {
  pub run_id                    : i64,
  pub runtime_secs              : Option<i64>,
  pub calib_before              : Option<bool>,
  pub shifter                   : Option<i16>,
  pub run_type                  : Option<i16>,
  pub run_path                  : Option<String>,
}

impl Run {
  pub fn new() -> Self {
    Self {
      run_id        : 0, 
      runtime_secs  : Some(0), 
      calib_before  : Some(true), 
      shifter       : Some(0), 
      run_type      : Some(0), 
      run_path      : Some(String::from("")), 
    }
  }

  pub fn get_last_run(conn: &mut SqliteConnection) -> Option<u32> {
    use schema::tof_db_run::dsl::*;
    match tof_db_run.load::<Run>(conn) {
      Err(err) => {
        error!("Unable to load DSICards from db! {err}");
        return None;
      }
      Ok(_runs) => {
        //return Some(runs);
      }
    }
    let _results = tof_db_run
      //.filter(published.eq(true))
      .limit(1)
      //.select(Run::as_select())
      .load::<Run>(conn)
      .expect("Error loading posts");
    None
  }
}

impl fmt::Display for Run {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr = String::from("<Run");
    repr += &(format!("\n  RunID         : {}", self.run_id));                   
    repr += &(format!("\n  - auto cali   : {}", self.calib_before.unwrap_or(false)));
    repr += &(format!("\n  runtime [sec] : {}", self.runtime_secs.unwrap_or(-1)));
    repr += &(format!("\n  shifter       : {}", self.shifter.unwrap_or(-1)));
    repr += &(format!("\n  run_type      : {}", self.run_type.unwrap_or(-1)));
    repr += &(format!("\n  run_path      : {}", self.run_path.clone().unwrap_or(String::from(""))));
    write!(f, "{}", repr)
  }
}

/// Representation of a local trigger board.
/// 
/// The individual LTB channels do not map directly to PaddleEnds. Rather two of them
/// map to a paddle and then the whole paddle should get read out.
/// To be more specific about this. The LTB has 16 channels, but we treat them as 8.
/// Each 2 LTB channels get "married" internally in the board and will then continue
/// on as 1 LTB channel, visible to the outside. The information about which end of 
/// the Paddle crossed which threshhold is lost.
/// How it works is that the two channels will be combined by the trigger logic:
/// - There are 4 states (2 bits)
///   - 0 - no hit
///   - 1 - Hit
///   - 2 - Beta
///   - 3 - Veto
/// 
/// Each defining an individual threshold. If that is crossed, the whole paddle
/// (ends A+B) will be read out by the ReadoutBoard
/// 
/// The LTB channels here are labeled 1-8. This is as it is in the TOF spreadsheet.
/// Also dsi is labeled as in the spreadsheet and will start from one.
/// 
/// It is NOT clear from this which ch on the rb is connected to which side, for that
/// the paddle/RB tables need to be consulted.
/// Again: rb_ch0 does NOT necessarily correspond to the A side!
/// 
#[derive(Debug,Queryable, Selectable, serde::Serialize, serde::Deserialize)]
#[diesel(table_name = schema::tof_db_rat)]
#[diesel(primary_key(rat_id))]
pub struct RAT {
  pub rat_id                    : i16, 
  pub pb_id                     : i16, 
  pub rb1_id                    : i16, 
  pub rb2_id                    : i16, 
  pub ltb_id                    : i16, 
  pub ltb_harting_cable_length  : i16, 
}

impl RAT {
  pub fn new() -> Self {
    Self {
      rat_id                    : 0, 
      pb_id                     : 0, 
      rb1_id                    : 0, 
      rb2_id                    : 0, 
      ltb_id                    : 0, 
      ltb_harting_cable_length  : 0, 
    }
  }
  
  /// Get the RAT where rb2id matched the argument
  pub fn where_rb2id(conn: &mut SqliteConnection, rb2id : u8) -> Option<Vec<RAT>> {
    let mut result = Vec::<RAT>::new();
    match RAT::all(conn) {
      Some(rats) => {
        for rat in rats {
          if rat.rb2_id == rb2id as i16 {
            result.push(rat);
          }
        }
        return Some(result);
      }
      None => ()
    }
    Some(result)
  }
  
  /// Get the RAT where rb1id (the rb id of rb"1" in the RAT) matched the argument
  pub fn where_rb1id(conn: &mut SqliteConnection, rb2id : u8) -> Option<Vec<RAT>> {
    let mut result = Vec::<RAT>::new();
    match RAT::all(conn) {
      Some(rats) => {
        for rat in rats {
          if rat.rb1_id == rb2id as i16 {
            result.push(rat);
          }
        }
        return Some(result);
      }
      None => ()
    }
    Some(result)
  }

  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<RAT>> {
    match tof_db_rat.load::<RAT>(conn) {
      Err(err) => {
        error!("Unable to load RATs from db! {err}");
        return None;
      }
      Ok(rats) => {
        return Some(rats);
      }
    }
  }

}

impl fmt::Display for RAT {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr = String::from("<RAT");
    repr += &(format!("\n  ID                : {}", self.rat_id));                   
    repr += &(format!("\n  PB                : {} ", self.pb_id));                    
    repr += &(format!("\n  RB1               : {}", self.rb1_id));                   
    repr += &(format!("\n  RB2               : {}", self.rb2_id));                   
    repr += &(format!("\n  LTB               : {}", self.ltb_id));                   
    repr += &(format!("\n  H. cable len [cm] : {}>", self.ltb_harting_cable_length)); 
    write!(f, "{}", repr)
  }
}


/// A DSI card which is plugged into one of five slots on the MTB
/// The DSI card provides the connection to RBs and LTBs and has 
/// a subdivision, which is called 'j'
#[derive(Queryable, Selectable)]
#[diesel(primary_key(dsi_id))]
#[diesel(table_name = schema::tof_db_dsicard)]
pub struct DSICard { 
  pub dsi_id    : i16,
  pub j1_rat_id : Option<i16>,
  pub j2_rat_id : Option<i16>,
  pub j3_rat_id : Option<i16>,
  pub j4_rat_id : Option<i16>,
  pub j5_rat_id : Option<i16>,
}
 

impl DSICard {
  pub fn new() -> Self {
    Self {
      dsi_id    : 0,
      j1_rat_id : None,
      j2_rat_id : None,
      j3_rat_id : None,
      j4_rat_id : None,
      j5_rat_id : None,
    }
  }
  
  /// True if this RAT box is plugged in to any of the j 
  /// connectors on this specific DSI card
  pub fn has_rat(&self, r_id : u8) -> bool {
    if let Some(rid) = self.j1_rat_id {
      if rid as u8 == r_id {
        return true;
      }
    }
    if let Some(rid) = self.j2_rat_id {
      if rid as u8 == r_id {
        return true;
      }
    }
    if let Some(rid) = self.j3_rat_id {
      if rid as u8 == r_id {
        return true;
      }
    }
    if let Some(rid) = self.j4_rat_id {
      if rid as u8 == r_id {
        return true;
      }
    }
    if let Some(rid) = self.j5_rat_id {
      if rid as u8 == r_id {
        return true;
      }
    }
    return false;
  }

  /// Get the j connetor for this specific RAT
  /// Raises ValueError if the RAT is not connected
  pub fn get_j(&self, r_id : u8) -> Option<u8> {
    if !self.has_rat(r_id) {
      return None;
    }
    if let Some(rid) = self.j1_rat_id {
      if rid as u8 == r_id {
        let _j = self.j1_rat_id.unwrap() as u8;
        return Some(_j);
      }
    }
    if let Some(rid) = self.j2_rat_id {
      if rid as u8 == r_id {
        let _j = self.j2_rat_id.unwrap() as u8;
        return Some(_j);
      }
    }
    if let Some(rid) = self.j3_rat_id {
      if rid as u8 == r_id {
        let _j = self.j3_rat_id.unwrap() as u8;
        return Some(_j);
      }
    }
    if let Some(rid) = self.j4_rat_id {
      if rid as u8 == r_id {
        let _j = self.j4_rat_id.unwrap() as u8;
        return Some(_j);
      }
    }
    if let Some(rid) = self.j5_rat_id {
      if rid as u8 == r_id {
        let _j = self.j5_rat_id.unwrap() as u8;
        return Some(_j);
      }
    }
  None
  }
  
  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<DSICard>> {
    match tof_db_dsicard.load::<DSICard>(conn) {
      Err(err) => {
        error!("Unable to load DSICards from db! {err}");
        return None;
      }
      Ok(dsis) => {
        return Some(dsis);
      }
    }
  }
}

impl fmt::Display for DSICard {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr  = String::from("<DSI Card:");
    repr += &(format!("\n  ID     : {}", self.dsi_id));     
    repr += "\n  -- -- -- --";
    if let Some(_j) = self.j1_rat_id {
        repr += &(format!("\n  J1 RAT : {}",_j));
    } else {
        repr += "\n  J1 RAT : Not connected";
    }
    if let Some(_j) = self.j2_rat_id {
        repr += &(format!("\n  J2 RAT : {}",_j));
    } else {
        repr += "\n  J2 RAT : Not connected";
    }
    if let Some(_j) = self.j3_rat_id {
        repr += &(format!("\n  J3 RAT : {}",_j));
    } else {
        repr += "\n  J3 RAT : Not connected";
    }
    if let Some(_j) = self.j4_rat_id {
        repr += &(format!("\n  J4 RAT : {}",_j));
    } else {
        repr += "\n  J4 RAT : Not connected";
    }
    if let Some(_j) = self.j5_rat_id {
        repr += &(format!("\n  J5 RAT : {}>",_j));
    } else {
        repr += "\n  J5 RAT : Not connected>";
    }
    write!(f, "{}", repr)
  }
}

/// A single TOF paddle with 2 ends 
/// comnected
#[derive(Debug,PartialEq, Clone,Queryable, Selectable, serde::Serialize, serde::Deserialize)]
#[diesel(table_name = schema::tof_db_paddle)]
#[diesel(primary_key(paddle_id))]
#[allow(non_snake_case)]
pub struct Paddle {
  pub paddle_id         : i16, 
  pub volume_id         : i64, 
  pub panel_id          : i16, 
  pub mtb_link_id       : i16, 
  pub rb_id             : i16, 
  pub rb_chA            : i16, 
  pub rb_chB            : i16, 
  pub ltb_id            : i16, 
  pub ltb_chA           : i16, 
  pub ltb_chB           : i16, 
  pub pb_id             : i16, 
  pub pb_chA            : i16, 
  pub pb_chB            : i16, 
  pub cable_len         : f32, 
  pub dsi               : i16, 
  pub j_rb              : i16, 
  pub j_ltb             : i16, 
  pub height            : f32, 
  pub width             : f32, 
  pub length            : f32, 
  pub normal_x          : f32,
  pub normal_y          : f32,
  pub normal_z          : f32,
  pub global_pos_x_l0   : f32, 
  pub global_pos_y_l0   : f32, 
  pub global_pos_z_l0   : f32, 
  pub global_pos_x_l0_A : f32, 
  pub global_pos_y_l0_A : f32, 
  pub global_pos_z_l0_A : f32, 
  pub global_pos_x_l0_B : f32, 
  pub global_pos_y_l0_B : f32, 
  pub global_pos_z_l0_B : f32, 
}

impl Paddle {
  pub fn new() -> Self {
    Self {
      paddle_id         : 0, 
      volume_id         : 0, 
      panel_id          : 0, 
      mtb_link_id       : 0, 
      rb_id             : 0, 
      rb_chA            : 0, 
      rb_chB            : 0, 
      ltb_id            : 0, 
      ltb_chA           : 0, 
      ltb_chB           : 0, 
      pb_id             : 0, 
      pb_chA            : 0, 
      pb_chB            : 0, 
      cable_len         : 0.0, 
      dsi               : 0, 
      j_rb              : 0, 
      j_ltb             : 0, 
      height            : 0.0, 
      width             : 0.0, 
      length            : 0.0, 
      normal_x          : 0.0,
      normal_y          : 0.0,
      normal_z          : 0.0,
      global_pos_x_l0   : 0.0, 
      global_pos_y_l0   : 0.0, 
      global_pos_z_l0   : 0.0, 
      global_pos_x_l0_A : 0.0, 
      global_pos_y_l0_A : 0.0, 
      global_pos_z_l0_A : 0.0, 
      global_pos_x_l0_B : 0.0, 
      global_pos_y_l0_B : 0.0, 
      global_pos_z_l0_B : 0.0, 
    }
  }

  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<Paddle>> {
    use schema::tof_db_paddle::dsl::*;
    match tof_db_paddle.load::<Paddle>(conn) {
      Err(err) => {
        error!("Unable to load paddles from db! {err}");
        return None;
      }
      Ok(pdls) => {
        return Some(pdls);
      }
    }
  }
}

impl fmt::Display for Paddle {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr = String::from("<Paddle:");
    repr += "\n** identifiers **";
    repr += &(format!("\n   pid                : {}", self.paddle_id));     
    repr += &(format!("\n   vid                : {}", self.volume_id));
    repr += &(format!("\n   panel id           : {}", self.panel_id));
    repr += "\n  ** connedtions **";
    repr += &(format!("\n   DSI/J/CH (LG) [A]  : {}  | {} | {:02}", self.dsi, self.j_ltb, self.ltb_chA));
    repr += &(format!("\n   DSI/J/CH (HG) [A]  : {}  | {} | {:02}", self.dsi, self.j_rb, self.rb_chA));
    repr += &(format!("\n   DSI/J/CH (LG) [B]  : {}  | {} | {:02}", self.dsi, self.j_ltb, self.ltb_chB));
    repr += &(format!("\n   DSI/J/CH (HG) [B]  : {}  | {} | {:02}", self.dsi, self.j_rb, self.rb_chB));
    repr += &(format!("\n   RB/CH         [A]  : {:02} | {}", self.rb_id, self.rb_chA));
    repr += &(format!("\n   RB/CH         [B]  : {:02} | {}", self.rb_id, self.rb_chB));
    repr += &(format!("\n   LTB/CH        [A]  : {:02} | {}", self.ltb_id, self.ltb_chA));
    repr += &(format!("\n   LTB/CH        [B]  : {:02} | {}", self.ltb_id, self.ltb_chB));
    repr += &(format!("\n   PB/CH         [A]  : {:02} | {}", self.pb_id, self.pb_chA));
    repr += &(format!("\n   PB/CH         [B]  : {:02} | {}", self.pb_id, self.pb_chB));
    repr += &(format!("\n   MTB Link ID        : {:02}", self.mtb_link_id));
    repr += "\n   cable len [cm] :";
    repr += &(format!("\n    \u{21B3} {:.2}", self.cable_len));
    repr += "\n    (Harting -> RB)";
    repr += "\n  ** Coordinates (L0) & dimensions **";
    repr += "\n   length, width, height [mm]";
    repr += &(format!("\n    \u{21B3} [{:.2}, {:.2}, {:.2}]", self.length, self.width, self.height));
    repr += "\n   center [mm]:";
    repr += &(format!("\n    \u{21B3} [{:.2}, {:.2}, {:.2}]", self.global_pos_x_l0, self.global_pos_y_l0, self.global_pos_z_l0));
    repr += "\n   normal vector:";
    repr += &(format!("\n    \u{21B3} [{:.2}, {:.2}, {:.2}]", self.normal_x, self.normal_y, self.normal_z));
    repr += "\n   A-side [mm]:";
    repr += &(format!("\n    \u{21B3} [{:.2}, {:.2}, {:.2}]>", self.global_pos_x_l0_A, self.global_pos_y_l0_A, self.global_pos_z_l0_A));
    repr += "\n   B-side [mm]:";
    repr += &(format!("\n    \u{21B3} [{:.2}, {:.2}, {:.2}]>", self.global_pos_x_l0_B, self.global_pos_y_l0_B, self.global_pos_z_l0_B));
    write!(f, "{}", repr)
  }
}
    
// Summary of DSI/J/LTBCH (0-319)
// This is not "official" but provides a way of indexing all
// the individual channels
#[derive(Debug,PartialEq,Queryable, Selectable)]
#[diesel(table_name = schema::tof_db_mtbchannel)]
#[diesel(primary_key(mtb_ch))]
#[allow(non_snake_case)]
pub struct MTBChannel {
  pub mtb_ch      : i64,         
  pub dsi         : Option<i16>, 
  pub j           : Option<i16>, 
  pub ltb_id      : Option<i16>, 
  pub ltb_ch      : Option<i16>, 
  pub rb_id       : Option<i16>, 
  pub rb_ch       : Option<i16>, 
  pub mtb_link_id : Option<i16>, 
  pub paddle_id   : Option<i16>, 
  pub paddle_isA  : Option<bool>,
  pub hg_ch       : Option<i16>, 
  pub lg_ch       : Option<i16>, 
}

impl MTBChannel {

  pub fn new() -> Self {
    Self {
      mtb_ch      : -1,         
      dsi         : None, 
      j           : None, 
      ltb_id      : None, 
      ltb_ch      : None, 
      rb_id       : None, 
      rb_ch       : None, 
      mtb_link_id : None, 
      paddle_id   : None, 
      paddle_isA  : None,
      hg_ch       : None, 
      lg_ch       : None, 
    }
  }
  
  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<MTBChannel>> {
    use schema::tof_db_mtbchannel::dsl::*;
    match tof_db_mtbchannel.load::<MTBChannel>(conn) {
      Err(err) => {
        error!("Unable to load RATs from db! {err}");
        return None;
      }
      Ok(mtbch) => {
        return Some(mtbch);
      }
    }
  }
}


impl fmt::Display for MTBChannel {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr = String::from("<MTBChannel");
    repr += &(format!("\n  Channel ID : {}", self.mtb_ch));
    repr += &(format!("\n  DSI/J/     : {}/{}", self.dsi.unwrap_or(-1), self.j.unwrap_or(-1)));
    repr += "\n  LTB ID/CH => RB ID/CH";
    repr += &(format!("\n   |-> {}/{} => {}/{}", self.ltb_id.unwrap_or(-1), self.ltb_ch.unwrap_or(-1), self.rb_id.unwrap_or(-1), self.rb_ch.unwrap_or(-1)));
    repr += &(format!("\n  MTB Link ID [RB] : {}", self.mtb_link_id.unwrap_or(-1)));
    repr += "\n  LG CH => HG CH";
    repr += &(format!("\n   |-> {} => {}", self.lg_ch.unwrap_or(-1), self.hg_ch.unwrap_or(-1)));
    repr += &(format!("\n  Paddle Id: {}", self.paddle_id.unwrap_or(-1)));
    let mut pend = "None";
    if !self.paddle_isA.is_none() {
      if self.paddle_isA.unwrap() {
          pend = "A";
      } else {
          pend = "B";
      }
    }
    repr += &(format!("\n  Paddle End: {}>", pend));
    write!(f, "{}", repr)
  }
}


///////////////////////////////////////////////////
//
// The following models exceed a bit the capabilities
// of Diesel, or my Diesel skill.
// These models contain multiple ForeignKeys, in all
// cases these link to the paddle table. 
//
// For each of LocalTriggerBoard, ReadoutBoard, Panel
// we have 2 structs:
// One called DB<entity> and the other <entity>. The
// first does have the ForeignKeys as SmallInt, and 
// the latter looks them up and fills in the blanks
//
//
//

/// The DB wrapper for the LocalTriggerBoard, for 
/// easy implementation there are no joins, we do 
/// them manually in the public implementation 
/// of the LocaltriggerBoard
#[derive(Queryable, Selectable, Identifiable, Associations)]
#[diesel(table_name = schema::tof_db_localtriggerboard)]
#[diesel(primary_key(board_id))]
#[diesel(belongs_to(Paddle, foreign_key=paddle1_id))]
pub struct DBLocalTriggerBoard {
    pub board_id      : i16,    
    pub dsi           : Option<i16>,
    pub j             : Option<i16>,
    pub rat           : Option<i16>,
    pub ltb_id        : Option<i16>, 
    pub cable_len     : f32,
    pub paddle1_id    : Option<i16>,
    pub paddle2_id    : Option<i16>,
    pub paddle3_id    : Option<i16>,
    pub paddle4_id    : Option<i16>,
    pub paddle5_id    : Option<i16>,
    pub paddle6_id    : Option<i16>,
    pub paddle7_id    : Option<i16>,
    pub paddle8_id    : Option<i16>,
}

impl DBLocalTriggerBoard {
  
  //pub fn new() -> Self {
  //  Self {
  //    board_id      : 0,    
  //    dsi           : None,
  //    j             : None,
  //    rat           : None,
  //    ltb_id        : None, 
  //    cable_len     : 0.0,
  //    paddle1_id    : None,
  //    paddle2_id    : None,
  //    paddle3_id    : None,
  //    paddle4_id    : None,
  //    paddle5_id    : None,
  //    paddle6_id    : None,
  //    paddle7_id    : None,
  //    paddle8_id    : None,
  //  }
  //}

  /// True if sane dsi and j values are 
  /// assigned to this board
  pub fn connected(&self) -> bool {
    self.dsi != None && self.j != None
  }

  /// True if all fields are filled with 
  /// reasonable values and not the default
  pub fn valid(&self) -> bool {
    self.board_id      > 0 &&    
    self.dsi       .is_some() && 
    self.j         .is_some() && 
    self.rat       .is_some() && 
    // right now, we explicitly don't care
    // about the ltb_id
    //self.ltb_id    .is_some() &&  
    self.cable_len     > 0.0  &&
    self.paddle1_id.is_some() &&
    self.paddle2_id.is_some() &&
    self.paddle3_id.is_some() &&
    self.paddle4_id.is_some() &&
    self.paddle5_id.is_some() &&
    self.paddle6_id.is_some() &&
    self.paddle7_id.is_some() &&
    self.paddle8_id.is_some()
  }
  
  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<DBLocalTriggerBoard>> {
    use schema::tof_db_localtriggerboard::dsl::*;
    match tof_db_localtriggerboard
        //.inner_join(tof_db_localtriggerboard.on(schema::tof_db_paddle::dsl::paddle_id.eq(schema::tof_db_localtriggerboard::dsl::paddle1_id)))
        .load::<DBLocalTriggerBoard>(conn) {
      Err(err) => {
        error!("Unable to load LocalTriggerBoards from db! {err}");
        return None;
      }
      Ok(ltbs) => {
        return Some(ltbs);
      }
    }
  }
}

impl fmt::Display for DBLocalTriggerBoard {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr : String;
    if !self.connected() {
      repr = format!("<DBLocalTriggerBoard: ID {}  - UNCONNECTED>", self.board_id);
    } else {
      repr = String::from("<DBLocalTriggerBoard:");
      repr += &(format!("\n  LTB ID  : {}", self.board_id));             
    }
    repr += &(format!("\n  DSI/J   : {}/{}", self.dsi.unwrap(), self.j.unwrap()));     
    repr += &(format!("\n  RAT ID  : {}", self.rat.unwrap()));
    repr += "\n  H. cable len (MTB connection):";
    repr += &(format!("\n    ->      {}", self.cable_len));
    repr += "\n  -- -- -- -- -- -- -- -- -- -- -- -- -- --";
    repr += "\n  Paddle IDs:";
    repr += &(format!("\n    {:02}", self.paddle1_id.unwrap_or(-1))); 
    repr += &(format!("\n    {:02}", self.paddle2_id.unwrap_or(-1)));  
    repr += &(format!("\n    {:02}", self.paddle3_id.unwrap_or(-1)));  
    repr += &(format!("\n    {:02}", self.paddle4_id.unwrap_or(-1)));  
    repr += &(format!("\n    {:02}", self.paddle5_id.unwrap_or(-1))); 
    repr += &(format!("\n    {:02}", self.paddle6_id.unwrap_or(-1))); 
    repr += &(format!("\n    {:02}", self.paddle7_id.unwrap_or(-1))); 
    repr += &(format!("\n    {:02}", self.paddle8_id.unwrap_or(-1))); 
    write!(f, "{}", repr)
  }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalTriggerBoard {
    pub board_id      : u8,    
    pub dsi           : u8,
    pub j             : u8,
    pub rat           : u8,
    pub ltb_id        : u8, 
    pub cable_len     : f32,
    pub paddle1       : Paddle,
    pub paddle2       : Paddle,
    pub paddle3       : Paddle,
    pub paddle4       : Paddle,
    pub paddle5       : Paddle,
    pub paddle6       : Paddle,
    pub paddle7       : Paddle,
    pub paddle8       : Paddle,
}

impl LocalTriggerBoard {
  
  pub fn new() -> Self {
    Self {
      board_id      : 0,    
      dsi           : 0,
      j             : 0,
      rat           : 0,
      ltb_id        : 0, 
      cable_len     : 0.0,
      paddle1       : Paddle::new(),
      paddle2       : Paddle::new(),
      paddle3       : Paddle::new(),
      paddle4       : Paddle::new(),
      paddle5       : Paddle::new(),
      paddle6       : Paddle::new(),
      paddle7       : Paddle::new(),
      paddle8       : Paddle::new(),
    }
  }
  
  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<LocalTriggerBoard>> {
    use schema::tof_db_localtriggerboard::dsl::*;
    let db_ltbs : Vec<DBLocalTriggerBoard>;
    match tof_db_localtriggerboard
        //.inner_join(tof_db_localtriggerboard.on(schema::tof_db_paddle::dsl::paddle_id.eq(schema::tof_db_localtriggerboard::dsl::paddle1_id)))
        .load::<DBLocalTriggerBoard>(conn) {
      Err(err) => {
        error!("Unable to load LocalTriggerBoards from db! {err}");
        return None;
      }
      Ok(ltbs) => {
        db_ltbs = ltbs;
      }
    }
    let paddles_op = Paddle::all(conn);
    match paddles_op {
      None => {
        return None;
      }
      Some(_) => ()
    }
    let paddles = paddles_op.unwrap();
    // This is not the best and fastest, but since our diesel skills 
    // are a merely 3, we can't do it right now.
    let mut ltbs = Vec::<LocalTriggerBoard>::new();
    //println!("Iterating over {} ltbs in the DB!", db_ltbs.len());
    for dbltb in db_ltbs {
      let mut ltb  = LocalTriggerBoard::new();
      for pdl in paddles.iter() {
        // this call ensures that the following unwraps
        // go through
        if !dbltb.valid() {
          error!("Got unpopulated LTB from DB for LTB {}", dbltb);
          continue;
        }
        if pdl.paddle_id == dbltb.paddle1_id.unwrap() {
          ltb.board_id  = dbltb.board_id as u8;        
          ltb.dsi       = dbltb.dsi.unwrap_or(0) as u8;
          ltb.j         = dbltb.j.unwrap_or(0) as u8;     
          ltb.rat       = dbltb.rat.unwrap_or(0) as u8;     
          ltb.ltb_id    = dbltb.ltb_id.unwrap_or(0) as u8;    
          ltb.cable_len = dbltb.cable_len;    
          ltb.paddle1   = pdl.clone();
        }
        if pdl.paddle_id == dbltb.paddle2_id.unwrap() {
          ltb.paddle2   = pdl.clone();
        }
        if pdl.paddle_id == dbltb.paddle3_id.unwrap() {
          ltb.paddle3   = pdl.clone();
        }
        if pdl.paddle_id == dbltb.paddle4_id.unwrap() {
          ltb.paddle4   = pdl.clone();
        }
        if pdl.paddle_id == dbltb.paddle5_id.unwrap() {
          ltb.paddle5   = pdl.clone();
        }
        if pdl.paddle_id == dbltb.paddle6_id.unwrap() {
          ltb.paddle6   = pdl.clone();
        }
        if pdl.paddle_id == dbltb.paddle7_id.unwrap() {
          ltb.paddle7   = pdl.clone();
        }
        if pdl.paddle_id == dbltb.paddle8_id.unwrap() {
          ltb.paddle8   = pdl.clone();
        }
      }
      ltbs.push(ltb);
    }
    Some(ltbs)
  }
}

impl fmt::Display for LocalTriggerBoard {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr : String;
    repr = String::from("<LocalTriggerBoard:");
    repr += &(format!("\n  LTB ID  : {}", self.board_id));             
    repr += &(format!("\n  DSI/J   : {}/{}", self.dsi, self.j));     
    repr += &(format!("\n  RAT ID  : {}", self.rat));
    repr += "\n  H. cable len (MTB connection):";
    repr += &(format!("\n    ->      {}", self.cable_len));
    repr += "\n  -- -- -- -- -- -- -- -- -- -- -- -- -- --";
    repr += "\n  LTB Ch -> RB Id, RB chn, Pdl ID, Pan ID:";
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}",  self.paddle1.rb_id, self.paddle1.rb_chA, self.paddle1.rb_chB, self.paddle1.paddle_id, self.paddle1.panel_id)); 
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}",  self.paddle2.rb_id, self.paddle2.rb_chA, self.paddle2.rb_chB, self.paddle2.paddle_id, self.paddle2.panel_id));  
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}",  self.paddle3.rb_id, self.paddle3.rb_chA, self.paddle3.rb_chB, self.paddle3.paddle_id, self.paddle3.panel_id));  
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}",  self.paddle4.rb_id, self.paddle4.rb_chA, self.paddle4.rb_chB, self.paddle4.paddle_id, self.paddle4.panel_id));  
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}",  self.paddle5.rb_id, self.paddle5.rb_chA, self.paddle5.rb_chB, self.paddle5.paddle_id, self.paddle5.panel_id)); 
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}",  self.paddle6.rb_id, self.paddle6.rb_chA, self.paddle6.rb_chB, self.paddle6.paddle_id, self.paddle6.panel_id)); 
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}",  self.paddle7.rb_id, self.paddle7.rb_chA, self.paddle7.rb_chB, self.paddle7.paddle_id, self.paddle7.panel_id)); 
    repr += &(format!("\n            {:02}   |   {},{} |  {:03} | {:02}>", self.paddle8.rb_id, self.paddle8.rb_chA, self.paddle8.rb_chB, self.paddle8.paddle_id, self.paddle8.panel_id)); 
    write!(f, "{}", repr)
  }
}

/// A Readoutboard with paddles connected
/// 
#[derive(Debug,PartialEq, Clone,Queryable, Selectable, serde::Serialize, serde::Deserialize)]
#[diesel(table_name = schema::tof_db_readoutboard)]
#[diesel(primary_key(rb_id_id))]
#[allow(non_snake_case)]
pub struct DBReadoutBoard {
  // FIXME - this HAS TO BE (MUST!) the same order
  // as in schema.rs !!
  pub rb_id        : i16, 
  pub dsi          : i16, 
  pub j            : i16, 
  pub mtb_link_id  : i16, 
  pub paddle12_chA : Option<i16>,
  pub paddle34_chA : Option<i16>,
  pub paddle56_chA : Option<i16>,
  pub paddle78_chA : Option<i16>,
  pub paddle12_id  : Option<i16>,
  pub paddle34_id  : Option<i16>,
  pub paddle56_id  : Option<i16>,
  pub paddle78_id  : Option<i16>,
}

impl DBReadoutBoard {
  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<DBReadoutBoard>> {
    use schema::tof_db_readoutboard::dsl::*;
    match tof_db_readoutboard
        //.inner_join(tof_db_localtriggerboard.on(schema::tof_db_paddle::dsl::paddle_id.eq(schema::tof_db_localtriggerboard::dsl::paddle1_id)))
        .load::<DBReadoutBoard>(conn) {
      Err(err) => {
        error!("Unable to load ReadoutBoards from db! {err}");
        return None;
      }
      Ok(rbs) => {
        return Some(rbs);
      }
    }
  }
}

impl fmt::Display for DBReadoutBoard {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr  = String::from("<ReadoutBoard:");
    repr += &(format!("\n  Board id    : {}",self.rb_id));            
    repr += &(format!("\n  MTB Link ID : {}",self.mtb_link_id));
    repr += &(format!("\n  DSI/J       : {}/{}",self.dsi,self.j));
    repr += "\n **Connected paddles**";
    repr += &(format!("\n  Ch0/1(1/2)  : {}", self.paddle12_id.unwrap_or(-1)));         
    repr += &(format!("\n  Ch1/2(2/3)  : {}", self.paddle34_id.unwrap_or(-1)));         
    repr += &(format!("\n  Ch2/3(3/4)  : {}", self.paddle56_id.unwrap_or(-1)));         
    repr += &(format!("\n  Ch3/4(4/5)  : {}>",self.paddle78_id.unwrap_or(-1)));         
    write!(f, "{}", repr)
  }
}

/// A Readoutboard with paddles connected
#[derive(Debug, Clone)]
#[allow(non_snake_case)]
pub struct ReadoutBoard {
  pub rb_id           : u8, 
  pub dsi             : u8, 
  pub j               : u8, 
  pub mtb_link_id     : u8, 
  pub paddle12        : Paddle,
  pub paddle12_chA    : u8,
  pub paddle34        : Paddle,
  pub paddle34_chA    : u8,
  pub paddle56        : Paddle,
  pub paddle56_chA    : u8,
  pub paddle78        : Paddle,
  pub paddle78_chA    : u8,
  // extra stuff, not from the db
  // or maybe in the future?
  pub calib_file_path : String,
  pub calibration     : RBCalibrations,       
}

impl ReadoutBoard {

  pub fn new() -> Self {
    Self {
      rb_id           : 0, 
      dsi             : 0, 
      j               : 0, 
      mtb_link_id     : 0, 
      paddle12        : Paddle::new(),
      paddle12_chA    : 0,
      paddle34        : Paddle::new(),
      paddle34_chA    : 0,
      paddle56        : Paddle::new(),
      paddle56_chA    : 0,
      paddle78        : Paddle::new(),
      paddle78_chA    : 0,
      calib_file_path : String::from(""),
      calibration     : RBCalibrations::new(0),
    }
  }

  /// Returns the ip address following a convention
  ///
  /// This does NOT GUARANTEE that the address is correct!
  pub fn guess_address(&self) -> String {
    format!("tcp://10.0.1.1{:02}:42000", self.rb_id)
  }
 
  pub fn get_paddle_ids(&self) -> [u8;4] {
    let pid0 = self.paddle12.paddle_id as u8;
    let pid1 = self.paddle34.paddle_id as u8;
    let pid2 = self.paddle56.paddle_id as u8;
    let pid3 = self.paddle78.paddle_id as u8;
    [pid0, pid1, pid2, pid3]
  }

  #[allow(non_snake_case)]
  pub fn get_A_sides(&self) -> [u8;4] {
    let pa_0 = self.paddle12_chA;
    let pa_1 = self.paddle34_chA;
    let pa_2 = self.paddle56_chA;
    let pa_3 = self.paddle78_chA;
    [pa_0, pa_1, pa_2, pa_3]
  }

  #[allow(non_snake_case)]
  pub fn get_pid_rbchA(&self, pid : u8) -> Option<u8> {
    if self.paddle12.paddle_id as u8 == pid {
      let rv = self.paddle12.rb_chA as u8;
      return Some(rv);
    } else if self.paddle34.paddle_id as u8 == pid {
      let rv = self.paddle34.rb_chA as u8;
      return Some(rv);
    } else if self.paddle56.paddle_id as u8 == pid {
      let rv = self.paddle56.rb_chA as u8;
      return Some(rv);
    } else if self.paddle78.paddle_id as u8== pid {
      let rv = self.paddle78.rb_chA as u8;
      return Some(rv);
    } else {
      return None;
    }
  }
  
  #[allow(non_snake_case)]
  pub fn get_pid_rbchB(&self, pid : u8) -> Option<u8> {
    if self.paddle12.paddle_id as u8 == pid {
      let rv = self.paddle12.rb_chB as u8;
      return Some(rv);
    } else if self.paddle34.paddle_id as u8== pid {
      let rv = self.paddle34.rb_chB as u8;
      return Some(rv);
    } else if self.paddle56.paddle_id as u8== pid {
      let rv = self.paddle56.rb_chB as u8;
      return Some(rv);
    } else if self.paddle78.paddle_id as u8 == pid {
      let rv = self.paddle78.rb_chB as u8;
      return Some(rv);
    } else {
      return None;
    }
  }

  pub fn get_paddle_length(&self, pid : u8) -> Option<f32> {
    if self.paddle12.paddle_id as u8 == pid {
      let rv = self.paddle12.length;
      return Some(rv);
    } else if self.paddle34.paddle_id as u8== pid {
      let rv = self.paddle34.length;
      return Some(rv);
    } else if self.paddle56.paddle_id as u8== pid {
      let rv = self.paddle56.length;
      return Some(rv);
    } else if self.paddle78.paddle_id as u8 == pid {
      let rv = self.paddle78.length;
      return Some(rv);
    } else {
      return None;
    }
  }

  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<ReadoutBoard>> {
    use schema::tof_db_readoutboard::dsl::*;
    let db_rbs : Vec<DBReadoutBoard>;
    match tof_db_readoutboard
        //.inner_join(tof_db_localtriggerboard.on(schema::tof_db_paddle::dsl::paddle_id.eq(schema::tof_db_localtriggerboard::dsl::paddle1_id)))
        .load::<DBReadoutBoard>(conn) {
      Err(err) => {
        error!("Unable to load ReadoutBoards from db! {err}");
        return None;
      }
      Ok(rbs) => {
        db_rbs = rbs;
      }
    }
    let paddles_op = Paddle::all(conn);
    match paddles_op {
      None => {
        return None;
      }
      Some(_) => ()
    }
    let paddles = paddles_op.unwrap();
    // This is not the best and fastest, but since our diesel skills 
    // are a merely 3, we can't do it right now.
    let mut rbs = Vec::<ReadoutBoard>::new();
    //println!("Iterating over {} rbs in the DB!", db_rbs.len());
    for dbrb in db_rbs {
      let mut rb  = ReadoutBoard::new();
      rb.rb_id        = dbrb.rb_id as u8;        
      rb.dsi          = dbrb.dsi as u8;
      rb.j            = dbrb.j  as u8;     
      rb.mtb_link_id  = dbrb.mtb_link_id  as u8;    
      rb.paddle12_chA = dbrb.paddle12_chA.unwrap() as u8;
      rb.paddle34_chA = dbrb.paddle34_chA.unwrap() as u8;
      rb.paddle56_chA = dbrb.paddle56_chA.unwrap() as u8;
      rb.paddle78_chA = dbrb.paddle78_chA.unwrap() as u8;
      for pdl in paddles.iter() {
        // this call ensures that the following unwraps
        // go through
        //if !dbltb.valid() {
        //  error!("Got unpopulated LTB from DB for LTB {}", dbltb);
        //  continue;
        //}
        if pdl.paddle_id == dbrb.paddle12_id.unwrap_or(0) {
          rb.paddle12     = pdl.clone();
        }
        if pdl.paddle_id == dbrb.paddle34_id.unwrap_or(0) {
          rb.paddle34   = pdl.clone();
        }
        if pdl.paddle_id == dbrb.paddle56_id.unwrap_or(0) {
          rb.paddle56   = pdl.clone();
        }
        if pdl.paddle_id == dbrb.paddle78_id.unwrap_or(0) {
          rb.paddle78   = pdl.clone();
        }
      }
      rbs.push(rb);
    }
    Some(rbs)
  }
  
  // FIXME - better query
  pub fn where_rbid(conn: &mut SqliteConnection, rb_id : u8) -> Option<ReadoutBoard> {
    let all = ReadoutBoard::all(conn)?;
    for rb in all {
      if rb.rb_id == rb_id {
        return Some(rb);
      }
    }
    None
  }

  pub fn to_summary_str(&self) -> String {
    let mut repr  = String::from("<ReadoutBoard:");
    repr += &(format!("\n  Board id    : {}",self.rb_id));            
    repr += &(format!("\n  MTB Link ID : {}",self.mtb_link_id));
    repr += &(format!("\n  RAT         : {}",self.paddle12.ltb_id));
    repr += &(format!("\n  DSI/J       : {}/{}",self.dsi,self.j));
    repr += "\n **Connected paddles**";
    repr += &(format!("\n  Channel 1/2 : {:02} (panel {:01})", self.paddle12.paddle_id, self.paddle12.panel_id));
    repr += &(format!("\n  Channel 3/4 : {:02} (panel {:01})", self.paddle34.paddle_id, self.paddle34.panel_id));
    repr += &(format!("\n  Channel 5/6 : {:02} (panel {:01})", self.paddle56.paddle_id, self.paddle56.panel_id));
    repr += &(format!("\n  Channel 7/8 : {:02} (panel {:01})", self.paddle78.paddle_id, self.paddle78.panel_id));
    repr
  }

  /// Load the newest calibration from the calibration file path
  pub fn load_latest_calibration(&mut self) -> Result<(), Box<dyn std::error::Error>> {
    //  files look like RB20_2024_01_26-08_15_54.cali.tof.gaps
    //let re = Regex::new(r"(\d{4}_\d{2}_\d{2}-\d{2}_\d{2}_\d{2})")?;
    let re = Regex::new(r"(\d{6}_\d{6})")?;
    // Define your file pattern (e.g., "logs/*.log" for all .log files in the logs directory)
    let pattern = format!("{}/RB{:02}_*", self.calib_file_path, self.rb_id); // Adjust this pattern to your files' naming convention
    let timestamp = DateTime::<Utc>::from_timestamp(0,0).unwrap(); // I am not sure what to do here
                                                                   // otherwise than unwrap. How is
                                                                   // this allowed to fail?
    //let mut newest_file = (String::from(""), NaiveDateTime::from_timestamp(0, 0));
    let mut newest_file = (String::from(""), timestamp);

    // Iterate over files that match the pattern
    let mut filename : String;
    for entry in glob(&pattern)? {
      if let Ok(path) = entry {
        // Get the filename as a string
        //let cpath = path.clone();
        match path.file_name() {
          None => continue,
          Some(fname) => {
              // the expect might be ok, since this is something done during initialization
              filename = fname.to_os_string().into_string().expect("Unwrapping filename failed!");
          }
        }
        if let Some(caps) = re.captures(&filename) {
          if let Some(timestamp_str) = caps.get(0).map(|m| m.as_str()) {
            //println!("timestamp_str {}, {}",timestamp_str, HUMAN_TIMESTAMP_FORMAT);
            //let timestamp = NaiveDateTime::parse_from_str(timestamp_str, "%Y_%m_%d-%H_%M_%S")?;
            //let timestamp = DateTime::<Utc>::parse_from_str(timestamp_str, "%Y_%m_%d-%H_%M_%S")?;
            let footzstring = format!("{}+0000", timestamp_str);
            let timestamp = DateTime::parse_from_str(&footzstring, "%y%m%d_%H%M%S%z")?;
            //let timestamp = DateTime::parse_from_str(&footzstring, HUMAN_TIMESTAMP_FORMAT)?;
            //println!("parse successful");
            //let _timestamp = DateTime
            if timestamp > newest_file.1 {
              // FIXME - into might panic?
              newest_file.1 = timestamp.into();
              newest_file.0 = filename.clone();
            }
          }
        }
      }
    }
    
    if newest_file.0.is_empty() {
      error!("No matching calibration available for board {}!", self.rb_id);
    } else {
      let file_to_load = format!("{}/{}", self.calib_file_path, newest_file.0);
      info!("Loading calibration from file: {}", file_to_load);
      self.calibration = RBCalibrations::from_file(file_to_load, true)?;
    }
    Ok(())
  }
}

impl fmt::Display for ReadoutBoard {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr  = String::from("<ReadoutBoard:");
    repr += &(format!("\n  Board id    : {}",self.rb_id));            
    repr += &(format!("\n  MTB Link ID : {}",self.mtb_link_id));
    repr += &(format!("\n  DSI/J       : {}/{}",self.dsi,self.j));
    repr += "\n **Connected paddles**";
    repr += &(format!("\n  Ch0/1(1/2)  : {}",self.paddle12)); 
    repr += &(format!("\n    A-side    : {}", self.paddle12_chA));
    repr += &(format!("\n  Ch1/2(2/3)  : {}",self.paddle34));         
    repr += &(format!("\n    A-side    : {}", self.paddle34_chA));
    repr += &(format!("\n  Ch2/3(3/4)  : {}",self.paddle56));         
    repr += &(format!("\n    A-side    : {}", self.paddle56_chA));
    repr += &(format!("\n  Ch3/4(4/5)  : {}>",self.paddle78));         
    repr += &(format!("\n    A-side    : {}", self.paddle78_chA));
    repr += "** calibration will be loaded from this path:";
    repr += &(format!("\n      \u{021B3} {}", self.calib_file_path));
    repr += &(format!("\n  calibration : {}>", self.calibration));
    write!(f, "{}", repr)
  }
}


/// A TOF Panel is a larger unit of paddles next to each other
///
/// TOF faces (e.g. Umbrella) can have multiple Panels
#[derive(Debug, Clone,Queryable, Selectable)]
#[diesel(table_name = schema::tof_db_panel)]
#[diesel(primary_key(panel_id))]
pub struct DBPanel {
  // ORDER OF THESE FIELDS HAS TO BE THE SAME AS IN schema.rs!!
  pub  panel_id    : i16        ,   
  pub  description : String     ,   
  pub  normal_x    : i16        ,   
  pub  normal_y    : i16        ,   
  pub  normal_z    : i16        ,   
  pub  dw_paddle   : Option<i16>,   
  pub  dh_paddle   : Option<i16>,   
  pub  paddle0_id  : Option<i16>,   
  pub  paddle1_id  : Option<i16>,   
  pub  paddle10_id : Option<i16>,   
  pub  paddle11_id : Option<i16>,   
  pub  paddle2_id  : Option<i16>,   
  pub  paddle3_id  : Option<i16>,   
  pub  paddle4_id  : Option<i16>,   
  pub  paddle5_id  : Option<i16>,   
  pub  paddle6_id  : Option<i16>,   
  pub  paddle7_id  : Option<i16>,   
  pub  paddle8_id  : Option<i16>,   
  pub  paddle9_id  : Option<i16>,   
}

impl DBPanel {

  pub fn valid(&self) -> bool {
    self.panel_id    > 0 &&    
    self.description != String::from("") &&   
    self.paddle0_id.is_some()   
  }

  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<DBPanel>> {
    use schema::tof_db_panel::dsl::*;
    match tof_db_panel
        //.inner_join(tof_db_localtriggerboard.on(schema::tof_db_paddle::dsl::paddle_id.eq(schema::tof_db_localtriggerboard::dsl::paddle1_id)))
        .load::<DBPanel>(conn) {
      Err(err) => {
        error!("Unable to load Panels from db! {err}");
        return None;
      }
      // dirty mind check
      Ok(pnls) => {
        return Some(pnls);
      }
    }
  }
  
  pub fn get_npaddles(&self) -> u8 {
    let mut npaddles = 0u8;
    if self.paddle0_id.is_some() {
      npaddles += 1;
    }
    if self.paddle1_id.is_some() {
      npaddles += 1;
    }
    if self.paddle2_id.is_some() {
      npaddles += 1;
    }
    if self.paddle3_id.is_some() {
      npaddles += 1;
    }
    if self.paddle4_id.is_some() {
      npaddles += 1;
    }
    if self.paddle5_id.is_some() {
      npaddles += 1;
    }
    if self.paddle6_id.is_some() {
      npaddles += 1;
    }
    if self.paddle7_id.is_some() {
      npaddles += 1;
    }
    if self.paddle8_id.is_some() {
      npaddles += 1;
    }
    if self.paddle9_id.is_some() {
      npaddles += 1;
    }
    if self.paddle10_id.is_some() {
      npaddles += 1;
    }
    if self.paddle11_id.is_some() {
      npaddles += 1;
    }
    npaddles
  }
}

impl fmt::Display for DBPanel {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr = String::from("<DBPanel");
    repr += &(format!("\n  id    : {}",self.panel_id));
    repr += &(format!("\n  descr : {}",self.description));
    repr += "\n  orientation:";
    repr += &(format!("\n   [{},{},{}]", self.normal_x, self.normal_y, self.normal_z));
    repr += &(format!("\n  paddle list ({}) paddles)", self.get_npaddles()));
    if self.paddle0_id.is_some() {
      repr += &(format!("\n   {}",self.paddle0_id.unwrap()));
    }
    if self.paddle1_id.is_some() {
      repr += &(format!("\n   {}",self.paddle1_id.unwrap()));
    }
    if self.paddle2_id.is_some() { 
      repr += &(format!("\n   {}",self.paddle2_id.unwrap()));
    }
    if self.paddle3_id.is_some() { 
      repr += &(format!("\n   {}",self.paddle3_id.unwrap()));
    }
    if self.paddle4_id.is_some() {
      repr += &(format!("\n   {}",self.paddle4_id.unwrap()));
    }
    if self.paddle5_id.is_some() {
      repr += &(format!("\n   {}",self.paddle5_id.unwrap()));
    }
    if self.paddle6_id.is_some()  {
      repr += &(format!("\n   {}",self.paddle6_id.unwrap()));
    }
    if self.paddle7_id.is_some() {
      repr += &(format!("\n   {}",self.paddle7_id.unwrap()));
    }
    if self.paddle8_id.is_some() {
      repr += &(format!("\n   {}",self.paddle8_id.unwrap()));
    }
    if self.paddle9_id.is_some() {
      repr += &(format!("\n   {}",self.paddle9_id.unwrap()));
    }
    if self.paddle10_id.is_some() {
      repr += &(format!("\n   {}",self.paddle10_id.unwrap()));
    }
    if self.paddle11_id.is_some() {
      repr += &(format!("\n   {}",self.paddle11_id.unwrap()));
    }
    repr += ">";
    write!(f, "{}", repr)
  }
}

pub struct Panel {
  pub  panel_id    : u8        ,   
  pub  description : String    ,   
  pub  normal_x    : u8        ,   
  pub  normal_y    : u8        ,   
  pub  normal_z    : u8        ,   
  pub  paddle0  : Paddle,   
  pub  paddle1  : Option<Paddle>,   
  pub  paddle2  : Option<Paddle>,   
  pub  paddle3  : Option<Paddle>,   
  pub  paddle4  : Option<Paddle>,   
  pub  paddle5  : Option<Paddle>,   
  pub  paddle6  : Option<Paddle>,   
  pub  paddle7  : Option<Paddle>,   
  pub  paddle8  : Option<Paddle>,   
  pub  paddle9  : Option<Paddle>,   
  pub  paddle10 : Option<Paddle>,   
  pub  paddle11 : Option<Paddle>,   
  // FIXME - these are for the future 
  // when we are buiding the geometry 
  // from the database
  //pub  dh_paddle   : Option<>,   
  //pub  dw_paddle   : Option<>,   
}

impl Panel {
 
  pub fn new() -> Self {
    Self {
      panel_id    : 0        ,   
      description : String::from(""),   
      normal_x    : 0        ,   
      normal_y    : 0        ,   
      normal_z    : 0        ,   
      paddle0     : Paddle::new(),   
      paddle1     : None,   
      paddle2     : None,   
      paddle3     : None,   
      paddle4     : None,   
      paddle5     : None,   
      paddle6     : None,   
      paddle7     : None,   
      paddle8     : None,   
      paddle9     : None,   
      paddle10    : None,   
      paddle11    : None,   
    }
  }


  pub fn get_npaddles(&self) -> u8 {
    let mut npaddles = 1u8;
    if self.paddle1.is_some() {
      npaddles += 1;
    }
    if self.paddle2.is_some() {
      npaddles += 1;
    }
    if self.paddle3.is_some() {
      npaddles += 1;
    }
    if self.paddle4.is_some() {
      npaddles += 1;
    }
    if self.paddle5.is_some() {
      npaddles += 1;
    }
    if self.paddle6.is_some() {
      npaddles += 1;
    }
    if self.paddle7.is_some() {
      npaddles += 1;
    }
    if self.paddle8.is_some() {
      npaddles += 1;
    }
    if self.paddle9.is_some() {
      npaddles += 1;
    }
    if self.paddle10.is_some() {
      npaddles += 1;
    }
    if self.paddle11.is_some() {
      npaddles += 1;
    }
    npaddles
  }
  
  pub fn all(conn: &mut SqliteConnection) -> Option<Vec<Panel>> {
    use schema::tof_db_panel::dsl::*;
    let db_panels : Vec<DBPanel>;
    match tof_db_panel
        //.inner_join(tof_db_localtriggerboard.on(schema::tof_db_paddle::dsl::paddle_id.eq(schema::tof_db_localtriggerboard::dsl::paddle1_id)))
        .load::<DBPanel>(conn) {
      Err(err) => {
        error!("Unable to load Panels from db! {err}");
        return None;
      }
      Ok(pnls) => {
        db_panels = pnls;
      }
    }
    let paddles_op = Paddle::all(conn);
    match paddles_op {
      None => {
        return None;
      }
      Some(_) => ()
    }
    let paddles = paddles_op.unwrap();
    // This is not the best and fastest, but since our diesel skills 
    // are a merely 3, we can't do it right now.
    let mut panels = Vec::<Panel>::new();
    println!("Iterating over {} panels in the DB!", db_panels.len());
    for dbpanel in db_panels {
      let mut pnl  = Panel::new();
      for pdl in paddles.iter() {
        // this call ensures that the following unwraps
        // go through
        if !dbpanel.valid() {
          error!("Got unpopulated Panel from DB for Panel {}", dbpanel);
          continue;
        }
        if pdl.paddle_id == dbpanel.paddle0_id.unwrap() {
          pnl.panel_id     = dbpanel.panel_id as u8;        
          pnl.description  = dbpanel.description.clone();
          pnl.normal_x     = dbpanel.normal_x as u8;     
          pnl.normal_y     = dbpanel.normal_y as u8;     
          pnl.normal_z     = dbpanel.normal_z as u8;    
          pnl.paddle0      = pdl.clone();
        }
        if pdl.paddle_id == dbpanel.paddle1_id.unwrap() {
          pnl.paddle1   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle2_id.unwrap() {
          pnl.paddle2   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle3_id.unwrap() {
          pnl.paddle3   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle4_id.unwrap() {
          pnl.paddle4   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle5_id.unwrap() {
          pnl.paddle5   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle6_id.unwrap() {
          pnl.paddle6   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle7_id.unwrap() {
          pnl.paddle7   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle8_id.unwrap() {
          pnl.paddle8   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle9_id.unwrap() {
          pnl.paddle9   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle10_id.unwrap() {
          pnl.paddle10   = Some(pdl.clone());
        }
        if pdl.paddle_id == dbpanel.paddle11_id.unwrap() {
          pnl.paddle11   = Some(pdl.clone());
        }
      }
      panels.push(pnl);
    }
    Some(panels)
  }
}

impl fmt::Display for Panel {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut repr = String::from("<Panel");
    repr += &(format!("\n  id    : {}",self.panel_id));
    repr += &(format!("\n  descr : {}",self.description));
    repr += "\n  orientation:";
    repr += &(format!("\n   [{},{},{}]", self.normal_x, self.normal_y, self.normal_z));
    repr += &(format!("\n  paddle list ({}) paddles)", self.get_npaddles()));
    repr += &(format!("\n   {}",self.paddle0));
    if self.paddle1.is_some() {
      repr += &(format!("\n   {}",self.paddle1.as_ref().unwrap()));
    }
    if self.paddle2.is_some() { 
      repr += &(format!("\n   {}",self.paddle2.as_ref().unwrap()));
    }
    if self.paddle3.is_some() { 
      repr += &(format!("\n   {}",self.paddle3.as_ref().unwrap()));
    }
    if self.paddle4.is_some() {
      repr += &(format!("\n   {}",self.paddle4.as_ref().unwrap()));
    }
    if self.paddle5.is_some() {
      repr += &(format!("\n   {}",self.paddle5.as_ref().unwrap()));
    }
    if self.paddle6.is_some()  {
      repr += &(format!("\n   {}",self.paddle6.as_ref().unwrap()));
    }
    if self.paddle7.is_some() {
      repr += &(format!("\n   {}",self.paddle7.as_ref().unwrap()));
    }
    if self.paddle8.is_some() {
      repr += &(format!("\n   {}",self.paddle8.as_ref().unwrap()));
    }
    if self.paddle9.is_some() {
      repr += &(format!("\n   {}",self.paddle9.as_ref().unwrap()));
    }
    if self.paddle10.is_some() {
      repr += &(format!("\n   {}",self.paddle10.as_ref().unwrap()));
    }
    if self.paddle11.is_some() {
      repr += &(format!("\n   {}",self.paddle11.as_ref().unwrap()));
    }
    repr += ">";
    write!(f, "{}", repr)
  }
}