--- /dev/null
+#!/usr/bin/perl
+use warnings;
+use strict;
+use HADES::TrbNet;
+use Time::HiRes qw(usleep);
+use Data::Dumper;
+
+use lib "/home/hadaq/trbsoft/daqtools/dmon/code";
+use Dmon;
+
+my $dirich = 0x1234;
+my $std_thresh = 0x6800;
+
+$dirich = $ARGV[0];
+if($ARGV[1]) {
+ $std_thresh = $ARGV[1];
+ $std_thresh = hex($std_thresh);
+}
+
+unless ($ARGV[0]) {
+ print "usage: $0 <DiRICH--TrbNet-Address> [std_threshold]]\n";
+ exit;
+}
+
+$dirich = hex($dirich);
+
+my $throffset = 0xa000;
+#my $monitor = 0xdfc0;
+my $monitor = 0xc001;
+
+my $first_channel = 0;
+my $last_channel = 47;
+
+my $default_threshold = 0x6000;
+
+#my $absolute_max_threshold = 0x8000;
+my $absolute_min_threshold = 0x1000;
+
+my @res; my $res; my $rh_res;
+
+trb_init_ports() or die trb_strerror();
+
+# enable monitor counters
+$res = trb_register_write($dirich, 0xdf80 , 0xffffffff);
+if(!defined $res) {
+ $res = trb_strerror();
+ print "error output: $res\n";
+}
+
+
+my $command;
+my $chain=0;
+
+my $READ = 0x0<<20; # bits to set for a read command
+my $WRITE = 0x8<<20; # bits to set for a write command
+my $REGNR = 24; # number of bits to shift for the register number
+
+my $rh_thresh;
+
+for my $channel (0 .. $last_channel) {
+# if($channel%2 == 0) {
+# next;
+# }
+ $chain = int($channel/16);
+ $command = ($channel&0xf)<<$REGNR | $READ ;
+ #print "$command\n";
+ my $res = Dmon::PadiwaSendCmd($command,$dirich, $chain);
+ foreach my $cur_dirich (keys %$res) {
+ #print "cur_dirich: $cur_dirich\n";
+ #my $value = $res->{$dirich} & 0xffff;
+ $rh_thresh->{$cur_dirich}->[$channel] = $res->{$cur_dirich} &0xffff;
+ #usleep(30E1);
+ #print Dumper $;
+ }
+}
+
+#print Dumper $rh_thresh->{0x6024};
+#print Dumper $rh_thresh;
+#exit;
+
+for my $channel (0 .. $last_channel) {
+ if($channel%2 == 0) {
+ next;
+ }
+
+ #foreach my $cur_dirich (keys %$rh_thresh) {
+ #my $ra_channels = $rh_thresh->{$cur_dirich};
+ #my $channel = 0;
+ #foreach my $cur_value (@$ra_channels) {
+ $chain = int($channel/16);
+ $command = ($channel&0xf)<<$REGNR | $WRITE | 0x1ff;
+ #print "write: chain: $chain, Channel: $channel, value: $cur_value\n";
+ Dmon::PadiwaSendCmd($command,$dirich, $chain);
+
+# usleep(30E4);
+# $command = ($channel&0xf)<<$REGNR | $WRITE | 0x0;
+# Dmon::PadiwaSendCmd($command,$dirich, $chain);
+ #}
+
+}
+
+usleep(30E3);
+#sleep;
+
+for my $channel (0 .. $last_channel) {
+ if($channel%2 == 0) {
+ next;
+ }
+
+ #print "channel: $channel\n";
+ foreach my $cur_dirich (keys %$rh_thresh) {
+ #my $ra_channels = $rh_thresh->{$cur_dirich};
+ #my $channel = 0;
+ my $cur_value = $rh_thresh->{$cur_dirich}->[$channel];
+ #foreach my $cur_value (@$ra_channels) {
+ $chain = int($channel/16);
+
+ #printf "want to write: $cur_value to ecal: %x, chain %d\n",$cur_dirich,$chain;
+ $command = ($channel&0xf)<<$REGNR | $WRITE | $cur_value;
+ #print "write: chain: $chain, Channel: $channel, value: $cur_value\n";
+ Dmon::PadiwaSendCmd($command,$cur_dirich, $chain);
+ #usleep(30E1);
+ }
+
+}
+
+
+
+exit;
+