use Time::HiRes qw(usleep);
use Data::Dumper;
-my $dirich = 0x1208;
+use lib "/home/hadaq/trbsoft/daqtools/dmon/code";
+use Dmon;
+
+my $dirich = 0x1229;
+
+$dirich = $ARGV[0];
+
+unless ($ARGV[0]) {
+ print "usage: $0 <DiRICH--TrbNet-Address>\n";
+ exit;
+}
+
+$dirich = hex($dirich);
+
my $throffset = 0xa000;
-my $monitor = 0xdfc0;
+#my $monitor = 0xdfc0;
+my $monitor = 0xc001;
-my $last_channel = 8;
+my $first_channel = 0;
+my $last_channel = 31;
my $default_threshold = 0x3000;
trb_init_ports() or die trb_strerror();
+# enable monitor counters
$res = trb_register_write($dirich, 0xdf80 , 0xffffffff);
if(!defined $res) {
$res = trb_strerror();
}
+my $fixed_bits = 0x00800000;
+my $shift_bits = 0;
+my $channel_shift = 24;
+my $command;
+my $chain=0;
+
+
+
for my $channel (0 .. 31) {
- trb_register_write($dirich, $throffset + $channel , $default_threshold);
- #$rh_res = trb_register_read($dirich, $throffset + $channel);
+ $chain = ($channel <16) ? 0 : 1;
+ $command = $fixed_bits | ($channel << $channel_shift) | ($default_threshold << $shift_bits);
+ Dmon::PadiwaSendCmd($command,$dirich, $chain);
+ #trb_register_write($dirich, $throffset + $channel , $default_threshold);
+ #$rh_res = trb_register_read($dirich, $throffset + $channel);
}
usleep (1E5);
my $boundaries = {};
-#for my $channel (0 .. $last_channel) {
-for my $channel (28 .. 31) {
+for my $channel ($first_channel .. $last_channel) {
+#for my $channel (30 .. 31) {
my $hit_zero_diff_flag = 0;
- my $lower_threshold = 0x6e00;
+ my $lower_threshold = 0x6a80;
my $upper_threshold = 0xd000;
my $reasonable_upper_threshold = 0x8000;
- my $thresh_increment = 0x04;
-
+ my $thresh_increment = 0x8;
+
THRESH_LOOP: for (my $thresh = $lower_threshold ; $thresh <= $upper_threshold; $thresh += $thresh_increment) {
- trb_register_write($dirich, $throffset + $channel , $thresh);
+ $chain = ($channel <16) ? 0 : 1;
+ $command = $fixed_bits | ($channel << $channel_shift) | ($thresh << $shift_bits);
+ Dmon::PadiwaSendCmd($command,$dirich, $chain);
+ ##trb_register_write($dirich, $throffset + $channel , $thresh);
undef $rh_res;
my @hits = ();
foreach (1..2) {
- usleep(50E3);
$rh_res = trb_register_read($dirich, $monitor + $channel);
#$res = trb_strerror();
#print "error output: $res\n";
#print Dumper $rh_res;
push @hits ,$rh_res->{$dirich};
- }
-
+ #if ($_==1) {
+ usleep(40E3);
+ #}
+ }
+
my $diff = $hits[1] - $hits[0];
+ #printf "cur thresh: %.4x diff: $diff\n",$thresh ;
+ #sleep 0.2;
$hit_zero_diff_flag = 1 if($diff == 0);
-
- if($diff > 1 && !$hit_zero_diff_flag ) {
+
+ if($diff != 0 && !$hit_zero_diff_flag ) {
print "channel: $channel, backup threshold a bit (by 0x800)..., thresh: "; printf "0x%x\n",$thresh;
if($thresh <= $absolute_min_threshold) {
print "reached abs min threshold\n";
}
$thresh_increment *= 4 if($thresh_increment <= 0x100 && $thresh >= $reasonable_upper_threshold );
-
+
#my $thrstr = sprintf("0x%x", $thresh);
#print "channel: $channel: thresh: $thrstr : diff: $diff, a=$hits[0] b=$hits[1]\n";
my $thrstr = sprintf("0x%x", $thresh);
- if($diff > 1) {
+ if($diff >= 50) {
if( ! exists $boundaries->{$channel}->{'lower'} ) {
print "channel: $channel, lower thresh: $thrstr\n";
$boundaries->{$channel}->{'lower'} = $thresh;
}
}
- elsif ($diff == 0 && exists $boundaries->{$channel}->{'lower'}) {
+ elsif ($diff == 0 && exists $boundaries->{$channel}->{'lower'} && ($thresh - $boundaries->{$channel}->{'lower'} ) > 0x40 ) {
print "channel: $channel, upper thresh: $thrstr\n";
$boundaries->{$channel}->{'upper'} = $thresh;
last THRESH_LOOP;
}
-
- }
+
+ }
+
if ( ! exists $boundaries->{$channel}->{'upper'}) {
- $boundaries->{$channel}->{'upper'} = $upper_threshold;
+ $boundaries->{$channel}->{'upper'} = $upper_threshold;
+ print "strange setting of upper thresh.\n";
}
-
- trb_register_write($dirich, $throffset + $channel , $default_threshold);
+
+ $chain = ($channel <16) ? 0 : 1;
+ $command = $fixed_bits | ($channel << $channel_shift) | ($default_threshold << $shift_bits);
+ Dmon::PadiwaSendCmd($command,$dirich, $chain);
+ #trb_register_write($dirich, $throffset + $channel , $default_threshold);
}
-print "\nresult:\n";
+printf "\nresult for 0x%.4x:\n",$dirich;
#print Dumper $boundaries;
print "channel | noiseband [mV]\n";
print "------------------------\n";
printf "%2d | %02.0f\n", $cur_channel , $width;
}
-print "\nsummary:\n";
+printf "\nsummary for 0x%.4x:\n", $dirich;
foreach my $cur_channel (sort {$a <=> $b} keys %$boundaries) {
my $diff = $boundaries->{$cur_channel}->{upper} - $boundaries->{$cur_channel}->{lower};
my $width = $diff * 38E-6 * 1000;
- printf "%2d ", $width;
+ printf "%02.0f ", $width;
}
print "\n";
-
// tdc->SetRefChannel(nch, nch-1, 0xffff, 10000, -90., 90.);
//}
- for (unsigned j=0; j<33; j++) {
+ for (unsigned j=10; j<33; j++) {
tdc->SetRefChannel(j, 1, 0xffff, 10000, -40., 40.);
}
+ tdc->SetRefChannel(6, 0, 0xffff, 10000, -100.,100.);
//tdc->SetRefChannel(0, 0, 0x1202, 10000, -20., 20.);
//tdc->SetRefChannel(4, 2, 0xffff, 10000, -20., 20.);
//tdc->SetRefChannel(1, tdc->NumChannels() -1 , 0xffff, 20000, -10., 10.);