--- /dev/null
+#!/usr/bin/perl -w
+
+BEGIN {push @INC, '/home/hadaq/trbsoft/hadesdaq/hmon/'}
+
+
+use warnings;
+use strict;
+use HADES::TrbNet;
+use Hmon;
+use Time::HiRes qw( gettimeofday usleep time );
+use Data::Dumper;
+Hmon::TraceDBLoad();
+
+trb_init_ports() or die trb_strerror();
+print "WARNING: This script is not yet tested, but might already be working... \n";
+# my $dummy = <STDIN>;
+
+sub getboard {
+ my ($h,$ports) = @_;
+ my @result;
+ for(my $i=1; $i<13;$i++) {
+ if ($ports & (1<<$i)) {
+ my $cmd = sprintf("cd /home/hadaq/trbsoft/daq/nettools/ && ./nettracedb.pl 0x%04x %i",$h,$i);
+ my @o = qx($cmd);
+ my $addr = $o[0] =~ /:\s+0x(\w{4})/;
+ $addr = hex($addr);
+ push(@result,$addr);
+ printf("Looked for %04x %i, got %04x\n",$h,$i,$addr);
+ }
+ }
+ return @result;
+ }
+
+
+
+my $msg = "";
+my @boardstoloadthresh;
+
+#stop trigger
+print "Checking for time-outs\n";
+#Get timed out ports
+my $t_timeout = trb_register_read(0xfffe, 0x8b);
+print "Stopping trigger\n";
+system("trbcmd setbit 0x0003 0xa0c0 0x400");
+
+print "Checking for busy DiRich front-ends\n";
+my $t_busy = trb_register_read(0xfe51, 0x0);
+
+foreach my $h (keys %$t_busy) {
+# my $h = 0x7196;
+ my $lvl1cnt = ($t_busy->{$h}) & 0x10;
+ if($lvl1cnt) {
+ my $boards = "";
+ my $s = sprintf("Lvl1 error on Dirich %04x",$h);
+ print $s."\n";
+# system("logger -p local1.info -t DAQ 'Resync <I> $s'");
+ $msg .= sprintf("%04x",$h);
+ $msg .= ", ";
+ #send local reset
+ my @port = `trbcmd nettrace $h | tail -n1`;
+ print Dumper @port;
+ my ($b,$p) = $port[0] =~ /0x(\w{4})\s+0x(\w{8})/;
+ printf("Sending local reset signal %04x %08x\n",hex($b),1 << hex($p));
+ trb_register_write(hex($b),0xc6,1 << hex($p));
+# trb_register_write(hex($b),0xc6,0);
+ push(@boardstoloadthresh,$h);
+ }
+ }
+
+
+foreach my $h (keys %$t_timeout) {
+ if($h >= 0x8200 && $h < 0x8300) {
+ my $ports = ($t_timeout->{$h} >> 16) & 0x1ffe;
+ if($ports) {
+ my $boards = "";
+ foreach my $i (1..16) {
+ if ($ports & (1<<($i))) {
+ $boards .= sprintf(" %04x",Hmon::TraceDBGet($h,$i));
+ push(@boardstoloadthresh,Hmon::TraceDBGet($h,$i));
+ }
+ }
+
+ my $s = sprintf("Timeout on hub %04x, port mask %04x (%s ) found.",$h,$ports,$boards);
+ print $s."\n";
+# system("logger -p local1.info -t DAQ 'Resync <I> $s'");
+ $msg .= sprintf("%04x-%04x (%s )",$h,$ports,$boards);
+ $msg .= ", ";
+ #Check if ports are still up
+ my $portup = trb_register_read($h,0x84);
+ my $t = $portup->{$h} & $ports;
+ if($t != $ports) {
+ printf ("A link on hub %04x (%04x) is not active any more. A power cycle is needed.\n",$h,$ports);
+ goto END;
+ }
+ #send local reset
+ trb_register_clearbit($h,0xc0,$ports);
+ printf("Sending local power cycle %04x %08x\n",$h,$ports << 15);
+ trb_register_setbit($h,0xd580,$ports << 15);
+ usleep(30000);
+ trb_register_clearbit($h,0xd580,$ports << 15);
+ usleep(30000);
+ printf("Sending local trigger signal %04x %08x\n",$h,$ports);
+
+ trb_register_write($h,0xc6,$ports);
+# trb_register_write($h,0xc6,0);
+
+# push(@boardstoloadthresh,getboard($h,$ports));
+
+ }
+ }
+ }
+
+
+print "Checking for busy DiRich front-ends\n";
+$t_busy = trb_register_read(0xfffe, 0x80);
+foreach my $h (keys %$t_busy) {
+ next unless ($h >= 0x8200 && $h < 0x8300);
+
+ my $ports = ($t_busy->{$h}) & 0x1ffe;
+ if($ports) {
+ my $boards = "";
+ foreach my $i (0..16) {
+ if ($ports & (1<<($i))) {
+ $boards .= sprintf(" %04x",Hmon::TraceDBGet($h,$i));
+ push(@boardstoloadthresh,Hmon::TraceDBGet($h,$i));
+ }
+ }
+
+ my $s = sprintf("Busy on hub %04x, port mask %04x (%s ) found.",$h,$ports,$boards);
+ print $s."\n";
+# system("logger -p local1.info -t DAQ 'Resync <I> $s'");
+ $msg .= sprintf("%04x-%04x (%s )",$h,$ports,$boards);
+ $msg .= ", ";
+ #Check if ports are still up
+ my $portup = trb_register_read($h,0x84);
+ my $t = $portup->{$h} & $ports;
+ if($t != $ports) {
+ printf ("A link on hub %04x (%04x) is not active any more. A power cycle is needed.\n",$h,$ports);
+ goto END;
+ }
+ #send local reset
+ printf("Sending local power cycle %04x %08x\n",$h,$ports << 15);
+ trb_register_setbit($h,0xd580,$ports << 15);
+ usleep(30000);
+ trb_register_clearbit($h,0xd580,$ports << 15);
+ usleep(30000);
+ printf("Sending local reset signal %04x %08x\n",$h,$ports);
+
+ trb_register_write($h,0xc6,$ports);
+# trb_register_write($h,0xc6,0);
+
+
+ }
+ }
+# # print("Sending local reset signal\n");
+# #trb_register_write(0x1034,0xc6,0x40);
+
+print("Waiting for links to get active...\n");
+sleep(1);
+# foreach my $h (keys %$t_timeout) {
+# if($h >= 0x1000 && $h < 0x2000 && $h%16) {
+# my $ports = ($t_timeout->{$h} >> 16) & 0x1fe;
+# if($ports) {
+# my $portup = trb_register_read($h,0x84);
+# my $t = $portup->{$h} & $ports;
+# if($t != $ports) {
+# printf ("A link on hub %04x (%04x) is not active any more. A power cycle is needed.\n",$h,$ports & ~$portup->{$h});
+# goto END;
+# }
+# }
+# }
+# }
+
+#load startup-settings
+print("Loading configuration for DiRich\n");
+system("ssh -X lxhadesdaqp 'cd /home/hadaq/trbsoft/daq/main/;./startup.pl -f ../rich/startup.script -c 0 -eb off'");
+
+
+foreach my $h (@boardstoloadthresh) {
+ my $cmd = sprintf("ssh lxhadesdaq 'sleep 2 && /home/hadaq/trbsoft/hadesdaq/utils/RICH_Thr_Call.sh --loading-file /home/hadaq/hadesdaq/rich/trb_dirich_threshold/threshold_files/standard_baselines.thr -l 0 -t 0x%04x 70' &",$h);
+ printf("Loading thresholds for %04x\n",$h);
+ }
+
+
+#start trigger
+print("Done. Starting trigger again\n");
+trb_register_write(0xfe52,0xc0,0xffff);
+
+system("trbcmd clearbit 0x0003 0xa0c0 0x400");
+
+ chop $msg; chop $msg;
+ system("logger -p local1.info -t DAQ 'RICH <I> Re-Included DiRich to DAQ: $msg'");
+
+END:
+# sleep(10);