-
Notifications
You must be signed in to change notification settings - Fork 5
/
serverext.cc
5655 lines (4957 loc) · 202 KB
/
serverext.cc
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
// Shut up warning about _POSIX_C_SOURCE mismatch, caused by a
// harmless conflict between _GNU_SOURCE and python header files.
//#define NDEBUG 1
#include <unistd.h>
#if _POSIX_C_SOURCE != 200112L
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#endif
#define VISDQM_NO_ROOT 1
#define DEBUG(n, x)
#include "DQM/DQMNet.h"
#include "DQM/NatSort.h"
#include "DQM/Objects.h"
#include "DQM/StringAtom.h"
#include "DQM/VisDQMCache.h"
#include "DQM/VisDQMFile.h"
#include "DQM/VisDQMIndex.h"
#include "DQM/VisDQMTools.h"
#include "classlib/iobase/File.h"
#include "classlib/iobase/Filename.h"
#include "classlib/iobase/IOStatus.h"
#include "classlib/iobase/LocalSocket.h"
#include "classlib/iobase/SubProcess.h"
#include "classlib/utils/Argz.h"
#include "classlib/utils/DebugAids.h"
#include "classlib/utils/Regexp.h"
#include "classlib/utils/RegexpMatch.h"
#include "classlib/utils/Signal.h"
#include "classlib/utils/StringFormat.h"
#include "classlib/utils/StringOps.h"
#include "classlib/utils/TimeInfo.h"
#include "boost/gil/image.hpp"
#include "boost/gil/typedefs.hpp"
#include "boost/python.hpp"
#include "boost/python/stl_iterator.hpp"
#include "boost/shared_ptr.hpp"
#undef HAVE_PROTOTYPES // conflict between python and libjpeg
#undef HAVE_STDLIB_H // conflict between python and libjpeg
#include "boost/algorithm/string.hpp"
#include "boost/gil/extension/io/jpeg_io.hpp"
#include "boost/gil/extension/io/png_io.hpp"
#include "boost/gil/extension/numeric/convolve.hpp"
#include "boost/gil/extension/numeric/kernel.hpp"
#include "boost/gil/extension/numeric/resample.hpp"
#include "boost/gil/extension/numeric/sampler.hpp"
#include "boost/lambda/algorithm.hpp"
#include "boost/lambda/casts.hpp"
#include "rtgu/image/filters.hpp"
#include "rtgu/image/rescale.hpp"
#include <dlfcn.h>
#include <inttypes.h>
#include <math.h>
#include <cfloat>
#include <ext/hash_map>
#include <fstream>
#include <set>
#include <stdexcept>
#ifndef __GLIBC__
#include <stdio.h>
// Use unique buffer for read and write.
struct membuf {
char *data;
char **addr;
size_t *len;
size_t size;
size_t pos;
};
int fmemread(void *cookie, char *into, int len) {
membuf *mbuf = (membuf *)cookie;
if (mbuf->pos > mbuf->size) return 0;
size_t nmax = std::min<size_t>(len, mbuf->size - mbuf->pos);
memcpy(into, mbuf->data + mbuf->pos, nmax);
mbuf->pos += nmax;
return nmax;
}
int fmemwrite(void *cookie, const char *from, int len) {
/* Copy content from from into cookie's internal buf for
len bytes. Allocate space in case it is needed. Keeps
allocating it just to the exact boundary. Maybe a bulk
allocation could be more performing? Keep track of the
new allocated memory and save it back into the pointer
passed to open_memstream. */
membuf *mbuf = (membuf *)cookie;
if (mbuf->pos + len > mbuf->size) {
if (!(mbuf->data = (char *)realloc((void *)mbuf->data,
mbuf->pos + len - mbuf->size)))
return 0;
*mbuf->addr = mbuf->data;
mbuf->size = mbuf->pos + len;
}
memcpy(&(mbuf->data[mbuf->pos]), from, len);
mbuf->pos += len;
*mbuf->len = mbuf->pos;
return len;
}
fpos_t fmemseek(void *cookie, fpos_t pos, int whence) {
membuf *mbuf = (membuf *)cookie;
if (whence == SEEK_SET)
mbuf->pos = pos;
else if (whence == SEEK_END)
mbuf->pos = mbuf->size;
else if (whence == SEEK_CUR)
mbuf->pos += pos;
return mbuf->pos;
}
int fmemclose(void *cookie) {
/* Demand deletion of allocated memory to the calling
function!! We basically delete the cookie but not
the allocated memory. The pointer to the allocated
memory is passed back to the calling function in
write mode which is responsible to delete it. This
is to keep the same logic used with the open_memstream
function under linux. */
free(cookie);
return 0;
}
FILE *fmemopen(char *buf, size_t n, const char *) {
membuf *mbuf = (membuf *)malloc(sizeof(membuf));
mbuf->data = buf;
mbuf->size = n;
mbuf->pos = 0;
return funopen(mbuf, fmemread, 0, fmemseek, fmemclose);
}
FILE *open_memstream(char **buf, size_t *len, const char * = 0) {
membuf *mbuf = (membuf *)malloc(sizeof(membuf));
mbuf->data = (char *)malloc(1024 * 100); // 100K default
mbuf->addr = buf;
*mbuf->addr = mbuf->data;
mbuf->size = 1024 * 100;
mbuf->pos = 0;
mbuf->len = len;
*mbuf->len = mbuf->pos;
return funopen(mbuf, 0, fmemwrite, fmemseek, fmemclose);
}
#endif
namespace boost {
namespace gil {
/** Kernel for sinc filter of radius 2.0. NB: Some say sinc(x) is
sin(x)/x. Others say it's sin(PI*x)/(PI*x), a horizontal
compression of the former which is zero at integer values. We
use the latter. */
struct sinc_filter {
static float width(void) { return 2.0f; }
static float filter(float x) {
if (x == 0.0) return 1.0;
return sinf(M_PI * x) / (M_PI * x);
}
};
/** Kernel for Lanczos filter of radius 3.0. */
struct lanczos_filter {
static float width(void) { return 3.0f; }
static float filter(float x) {
if (x < 0.0f) x = -x;
if (x < 3.0f) return sinc_filter::filter(x) * sinc_filter::filter(x / 3.0);
return 0.0f;
}
};
template <typename DstPixel, typename SrcPixel>
class blendop {
typedef typename channel_type<DstPixel>::type DstChannel;
typedef typename channel_type<SrcPixel>::type SrcChannel;
typedef channel_traits<DstChannel> DstInfo;
typedef channel_traits<SrcChannel> SrcInfo;
float f_;
public:
blendop(float f) : f_(f) {}
DstChannel operator()(SrcChannel a, SrcChannel b) const {
return channel_convert<DstChannel>(
std::min(SrcInfo::max_value(),
std::max(SrcInfo::min_value(), SrcChannel(a + f_ * (b - a)))));
}
};
/** Blend two source views @a and @b to a destination view @a dst
with a blending coefficient @a f. The result destination image
will be $a + f * (b - a)$. Hence f=0 results in source a, f=1
results in source b, f values (0, 1) blend the sources a and b,
and f>1 results in boosting source b and subtracting some part
of source a. */
template <typename SrcView1, typename SrcView2, typename Scalar,
typename DstView>
inline void blend(const SrcView1 &a, const SrcView2 &b, Scalar f,
DstView &dst) {
typedef typename SrcView1::value_type SrcPixel;
typedef typename DstView::value_type DstPixel;
assert(a.dimensions() == dst.dimensions());
assert(b.dimensions() == dst.dimensions());
int height = a.height();
for (int rr = 0; rr < height; ++rr) {
typename SrcView1::x_iterator ai = a.row_begin(rr);
typename SrcView1::x_iterator ae = a.row_end(rr);
typename SrcView2::x_iterator bi = b.row_begin(rr);
typename DstView::x_iterator di = dst.row_begin(rr);
for (; ai != ae; ++ai, ++bi, ++di)
static_transform(*ai, *bi, *di, blendop<DstPixel, SrcPixel>(f));
}
}
/** Sharpen source image @a src by @a amount and write the result
into destination image @a dst. Performs the sharpening using an
operation known as unsharp masking: subtracting a slightly
smoothed version of the image from the image itself.
Intermediate results are in 32-bit float pixel format. */
inline void sharpen(rgb8_view_t &src, rgb8_view_t &dst, float amount) {
static const float SMOOTH[] = {1. / 7., 5. / 7., 1. / 7.};
kernel_1d_fixed<float, 3> smooth(SMOOTH, 1);
rgb32f_image_t smoothed(src.width(), src.height());
copy_and_convert_pixels(src, view(smoothed));
convolve_rows_fixed<rgb32f_pixel_t>(const_view(smoothed), smooth,
view(smoothed));
convolve_cols_fixed<rgb32f_pixel_t>(const_view(smoothed), smooth,
view(smoothed));
blend(const_view(smoothed), color_converted_view<rgb32f_pixel_t>(src), amount,
dst);
}
} // namespace gil
} // namespace boost
using namespace lat;
using namespace boost;
namespace ext = __gnu_cxx;
namespace py = boost::python;
typedef std::pair<std::string, std::string> StringPair;
typedef std::set<StringAtom> StringAtomSet;
typedef std::vector<StringAtom> StringAtomList;
typedef std::map<StringAtom, StringAtomList> StringAtomParentList;
static pthread_rwlock_t parentslock;
static StringAtomParentList parents;
static StringAtomTree stree(1024 * 1024);
static Regexp RX_THOUSANDS("(\\d)(\\d{3}($|\\D))");
static Regexp RX_OUTER_WHITE("^\\s+|\\s+$");
static Regexp RX_CMSSW_VERSION("CMSSW(?:_[0-9])+(?:_pre[0-9]+)?");
namespace __gnu_cxx {
template <>
struct hash<StringAtom> {
size_t operator()(const StringAtom &s) const { return s.index(); }
};
template <>
struct hash<std::string> {
size_t operator()(const std::string &s) const {
return __stl_hash_string(s.c_str());
}
};
} // namespace __gnu_cxx
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
class PyReleaseInterpreterLock {
PyThreadState *save_;
public:
PyReleaseInterpreterLock(void) { save_ = PyEval_SaveThread(); }
~PyReleaseInterpreterLock(void) { PyEval_RestoreThread(save_); }
};
class Lock {
pthread_mutex_t *lock_;
public:
Lock(pthread_mutex_t *lock) { pthread_mutex_lock(lock_ = lock); }
~Lock(void) { pthread_mutex_unlock(lock_); }
};
class RDLock {
pthread_rwlock_t *lock_;
public:
RDLock(pthread_rwlock_t *lock) { pthread_rwlock_rdlock(lock_ = lock); }
~RDLock(void) { pthread_rwlock_unlock(lock_); }
};
class WRLock {
pthread_rwlock_t *lock_;
public:
WRLock(pthread_rwlock_t *lock) { pthread_rwlock_wrlock(lock_ = lock); }
~WRLock(void) { pthread_rwlock_unlock(lock_); }
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
struct VisDQMAxisOptions;
struct VisDQMDrawOptions;
struct VisDQMLayoutItem;
struct VisDQMLayoutRow;
struct VisDQMItem;
struct VisDQMStatus;
struct VisDQMShownItem;
struct VisDQMShownRow;
struct VisDQMStatus;
struct VisDQMEventNum;
struct VisDQMSample;
struct VisDQMRegexp;
class VisDQMSource;
typedef std::vector<shared_ptr<VisDQMLayoutItem> > VisDQMLayoutItems;
typedef std::vector<shared_ptr<VisDQMLayoutRow> > VisDQMLayoutRows;
typedef std::vector<shared_ptr<VisDQMShownItem> > VisDQMShownItems;
typedef std::vector<shared_ptr<VisDQMShownRow> > VisDQMShownRows;
typedef ext::hash_map<StringAtom, shared_ptr<VisDQMItem> > VisDQMItems;
typedef ext::hash_map<StringAtom, VisDQMStatus> VisDQMStatusMap;
typedef std::map<StringAtom, VisDQMDrawOptions> VisDQMDrawOptionMap;
typedef std::list<VisDQMEventNum> VisDQMEventNumList;
typedef std::vector<VisDQMSample> VisDQMSamples;
typedef std::map<std::string, VisDQMRegexp> VisDQMRegexps;
struct VisDQMAxisOptions {
std::string min;
std::string max;
std::string type;
};
struct VisDQMDrawOptions {
VisDQMAxisOptions xaxis;
VisDQMAxisOptions yaxis;
VisDQMAxisOptions zaxis;
std::string drawopts;
std::string withref;
};
struct VisDQMLayoutItem {
StringAtom path;
std::string desc;
VisDQMDrawOptions drawopts;
StringAtomSet overlays;
};
struct VisDQMLayoutRow {
VisDQMLayoutItems columns;
};
struct VisDQMItem {
uint32_t flags;
uint64_t version;
StringAtom name;
StringAtomList parents;
std::string data;
VisDQMSource *plotter;
shared_ptr<VisDQMLayoutRows> layout;
};
struct VisDQMShownItem {
uint64_t version;
int nalarm;
int nlive;
StringAtom name;
std::string desc;
VisDQMSource *plotter;
VisDQMDrawOptions drawopts;
StringAtomSet overlays;
};
struct VisDQMShownRow {
VisDQMShownItems columns;
};
struct VisDQMStatus {
int nleaves;
int nalarm;
int nlive;
};
struct VisDQMEventNum {
std::string subsystem;
long runnr;
long luminr;
long eventnr;
long runstart;
};
enum VisDQMSampleType {
SAMPLE_LIVE,
SAMPLE_ONLINE_DATA,
SAMPLE_OFFLINE_DATA,
SAMPLE_OFFLINE_RELVAL,
SAMPLE_OFFLINE_MC,
SAMPLE_ANY
};
struct VisDQMSample {
VisDQMSampleType type;
long runnr;
uint32_t importversion;
std::string dataset;
std::string version;
VisDQMSource *origin;
uint64_t time;
VisDQMSample(VisDQMSampleType t, long r, const std::string &d)
: type(t),
runnr(r),
importversion(0),
dataset(d),
version(""),
origin(0),
time(0) {}
VisDQMSample(VisDQMSampleType t, long r)
: type(t),
runnr(r),
importversion(0),
dataset(""),
version(""),
origin(0),
time(0) {}
VisDQMSample() { VisDQMSample(SAMPLE_ANY, 0); }
};
static const char *sampleTypeLabel[] = {"live", "online_data",
"offline_data", "offline_relval",
"offline_mc", "any"};
struct VisDQMRegexp {
static const size_t REGEXPS_PER_LOCK = 1024;
shared_ptr<Regexp> rx;
std::vector<bool> matched;
std::vector<bool> tried;
std::vector<pthread_rwlock_t> locks;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
static std::ostream &lograw(const char *label) {
Time now = Time::current();
return std::cout << now.format(true, "[%d/%b/%Y:%H:%M:%S.")
<< now.nanoformat(3, 3) << ']' << label;
}
static std::ostream &loginfo(void) { return lograw(" INFO: "); }
static std::ostream &logerr(void) { return lograw(" ERROR: "); }
static std::ostream &logwarn(void) { return lograw(" WARNING: "); }
static std::string thousands(const std::string &arg) {
std::string result;
std::string tmp(arg);
result.reserve(arg.size() + arg.size() / 3 + 1);
while (true) {
result = StringOps::replace(tmp, RX_THOUSANDS, "\\1'\\2");
if (result == tmp) break;
tmp = result;
}
return result;
}
static void copyopts(const py::dict &opts,
std::map<std::string, std::string> &options) {
for (py::stl_input_iterator<std::string> i(opts), e; i != e; ++i) {
std::string key(*i);
py::extract<std::string> value(opts.get(key));
if (value.check()) options[key] = value();
}
}
/// Check whether the @a path is a subdirectory of @a ofdir. Returns
/// true both for an exact match and any nested subdirectory.
static bool isSubdirectory(const std::string &ofdir, const std::string &path) {
return (ofdir.empty() ||
(path.size() >= ofdir.size() &&
path.compare(0, ofdir.size(), ofdir) == 0 &&
(path.size() == ofdir.size() || path[ofdir.size()] == '/')));
}
static void splitPath(std::string &dir, std::string &name,
const std::string &path) {
size_t slash = path.rfind('/');
if (slash != std::string::npos) {
dir.append(path, 0, slash);
name.append(path, slash + 1, std::string::npos);
} else
name = path;
}
static std::string stringToJSON(const std::string &x,
bool emptyIsNone = false) {
if (emptyIsNone && x.empty()) return "None";
char buf[8];
std::string result;
result.reserve(2 * x.size());
result += '"';
for (size_t i = 0, e = x.size(); i < e; ++i) {
unsigned int ch = (unsigned char)x[i];
if (ch == '\\') {
result += '\\';
result += '\\';
} else if (ch == '"') {
result += '\\';
result += (char)ch;
} else if (ch == '\n') {
result += '\\';
result += 'n';
} else if (ch == '\r') {
result += '\\';
result += 'r';
} else if (ch == '\t') {
result += '\\';
result += 't';
} else if (!isprint(ch)) {
sprintf(buf, "\\u%04x", ch);
result += buf;
} else
result += (char)ch;
}
result += '"';
return result;
}
static std::string stringsToJSON(const StringAtomSet &overlays,
bool emptyIsNone = false) {
StringAtomSet::const_iterator mi = overlays.begin();
StringAtomSet::const_iterator me = overlays.end();
std::string result = "[";
for (; mi != me; ++mi) {
if (result.length() > 1) result += ", ";
result += stringToJSON(mi->string());
}
result += "]";
return result;
}
static shared_ptr<Regexp> rx(const std::string &match, int options = 0) {
shared_ptr<Regexp> r;
if (!match.empty()) {
r.reset(new Regexp(match, options));
if (!r->valid())
throw std::runtime_error(
StringFormat("Invalid regular expression syntax at"
" character %2 of '%1': %3")
.arg(match)
.arg(r->errorOffset())
.arg(r->errorMessage()));
r->study();
}
return r;
}
static void makerx(const std::string &rxstr, shared_ptr<Regexp> &rxobj,
std::string &rxerr, int options = 0) {
try {
if (!rxstr.empty()) rxobj = rx(rxstr, options);
} catch (Error &e) {
rxerr = e.explainSelf();
} catch (std::exception &e) {
rxerr = e.what();
}
}
static void fastrx(VisDQMRegexp &rxobj, const std::string &rxstr) {
rxobj.rx = rx(rxstr);
rxobj.tried.resize(stree.capacity(), false);
rxobj.matched.resize(stree.capacity(), false);
rxobj.locks.resize(stree.capacity() / VisDQMRegexp::REGEXPS_PER_LOCK);
for (size_t i = 0, e = rxobj.locks.size(); i < e; ++i)
pthread_rwlock_init(&rxobj.locks[i], 0);
}
static bool fastmatch(VisDQMRegexp *rx, const StringAtom &str) {
if (!rx || !rx->rx) return true;
size_t nlock = str.index() / VisDQMRegexp::REGEXPS_PER_LOCK;
pthread_rwlock_rdlock(&rx->locks[nlock]);
assert(str.index() < rx->tried.size());
bool tried = rx->tried[str.index()];
bool matched = rx->matched[str.index()];
pthread_rwlock_unlock(&rx->locks[nlock]);
if (tried) return matched;
pthread_rwlock_wrlock(&rx->locks[nlock]);
size_t maxtree = stree.size();
size_t minidx = nlock * VisDQMRegexp::REGEXPS_PER_LOCK;
size_t maxidx = minidx + VisDQMRegexp::REGEXPS_PER_LOCK;
for (size_t i = minidx; i < maxidx && i < maxtree; ++i)
if (!rx->tried[i]) {
bool match = rx->rx->match(stree.key(i));
rx->tried[i] = true;
rx->matched[i] = match;
if (i == str.index()) matched = match;
}
pthread_rwlock_unlock(&rx->locks[nlock]);
return matched;
}
static void buildParentNames(StringAtomList &to, const StringAtom &from) {
pthread_rwlock_rdlock(&parentslock);
StringAtomParentList::iterator pos = parents.find(from);
if (pos == parents.end()) {
pthread_rwlock_unlock(&parentslock);
pthread_rwlock_wrlock(&parentslock);
pos = parents.find(from);
}
if (pos == parents.end()) {
std::string parentstr;
const std::string &fromstr = from.string();
to.reserve(std::count(fromstr.begin(), fromstr.end(), '/') + 1);
parentstr.reserve(fromstr.size());
for (size_t slash = 0; slash < fromstr.size();
slash = fromstr.find('/', ++slash)) {
parentstr.assign(fromstr, 0, slash);
to.push_back(StringAtom(&stree, parentstr));
}
parents[from] = to;
} else
to = pos->second;
pthread_rwlock_unlock(&parentslock);
}
static uint32_t getType(uint32_t flags) {
return flags & VisDQMIndex::SUMMARY_PROP_TYPE_MASK;
}
static bool isROOTType(uint32_t flags) {
uint32_t type = getType(flags);
return (type >= VisDQMIndex::SUMMARY_PROP_TYPE_TH1F &&
type <= VisDQMIndex::SUMMARY_PROP_TYPE_TPROF2D);
}
static bool isScalarType(uint32_t flags) {
uint32_t type = getType(flags);
return (type >= VisDQMIndex::SUMMARY_PROP_TYPE_INT &&
type <= VisDQMIndex::SUMMARY_PROP_TYPE_STRING);
}
static bool isBLOBType(uint32_t flags) {
return getType(flags) == DQMNet::DQM_PROP_TYPE_DATABLOB;
}
static bool isIntegerType(uint32_t flags) {
return getType(flags) == VisDQMIndex::SUMMARY_PROP_TYPE_INT;
}
static bool isRealType(uint32_t flags) {
return getType(flags) == VisDQMIndex::SUMMARY_PROP_TYPE_REAL;
}
static void translateDrawOptions(py::dict opts, VisDQMDrawOptions &o) {
py::stl_input_iterator<std::string> ki(opts), ke;
for (; ki != ke; ++ki) {
std::string key(*ki);
std::string value(py::extract<std::string>(opts.get(key)));
if (key == "withref")
o.withref = value;
else if (key == "drawopts")
o.drawopts = value;
else if (key == "xmin")
o.xaxis.min = value;
else if (key == "xmax")
o.xaxis.max = value;
else if (key == "xtype")
o.xaxis.type = value;
else if (key == "ymin")
o.yaxis.min = value;
else if (key == "ymax")
o.yaxis.max = value;
else if (key == "ytype")
o.yaxis.type = value;
else if (key == "zmin")
o.zaxis.min = value;
else if (key == "zmax")
o.zaxis.max = value;
else if (key == "ztype")
o.zaxis.type = value;
}
}
static std::string formatStartTime(long curStartTime) {
Time now = Time::current();
Time runstart = (curStartTime <= 0 ? now : Time(curStartTime, 0));
tm nowtm = now.utc();
tm runtm = runstart.utc();
bool sameyear = nowtm.tm_year == runtm.tm_year;
bool samemonth = (sameyear && nowtm.tm_mon == runtm.tm_mon);
bool sameday = (samemonth && nowtm.tm_mday == runtm.tm_mday);
const char *fmt =
(curStartTime <= 0
? "(Not recorded)"
: sameday ? "Today %H:%M"
: samemonth ? "%a %d, %H:%M"
: sameyear ? "%a %b %d, %H:%M"
: "%a %b %d '%y, %H:%M");
return runstart.format(false, fmt);
}
static void getEventInfoNum(const std::string &name, const char *data,
VisDQMEventNumList &eventnums) {
long value = -1;
double real;
size_t len;
if ((len = 15) > 0 && name.size() > len &&
name.compare(name.size() - len, len, "/EventInfo/iRun") == 0)
value = strtol(data, 0, 10);
else if ((len = 23) > 0 && name.size() > len &&
name.compare(name.size() - len, len, "/EventInfo/iLumiSection") == 0)
value = strtol(data, 0, 10);
else if ((len = 17) && name.size() > len &&
name.compare(name.size() - len, len, "/EventInfo/iEvent") == 0)
value = strtol(data, 0, 10);
else if ((len = 28) && name.size() > len &&
name.compare(name.size() - len, len,
"/EventInfo/runStartTimeStamp") == 0)
if ((real = atof(data)) > 3600) value = long(real + 0.5);
if (value >= 0) {
bool found = false;
std::string subsys(name, 0, name.size() - len);
VisDQMEventNumList::iterator li = eventnums.begin();
VisDQMEventNumList::iterator le = eventnums.end();
for (; li != le; ++li)
if ((found = (li->subsystem == subsys))) break;
VisDQMEventNum *evnum;
if (!found) {
eventnums.push_front(VisDQMEventNum());
evnum = &eventnums.front();
evnum->subsystem = subsys;
evnum->runnr = -1;
evnum->luminr = -1;
evnum->eventnr = -1;
evnum->runstart = -1;
} else
evnum = &*li;
if (len == 15)
evnum->runnr = value;
else if (len == 23)
evnum->luminr = value;
else if (len == 17)
evnum->eventnr = value;
else if (len == 28)
evnum->runstart = value;
}
}
// Pick greatest run/lumi/event number combo seen.
static void setEventInfoNums(const VisDQMEventNumList &eventnums,
VisDQMEventNum ¤t) {
VisDQMEventNumList::const_iterator li = eventnums.begin();
VisDQMEventNumList::const_iterator le = eventnums.end();
for (; li != le; ++li) {
if (li->runnr > current.runnr) current = *li;
if (li->runnr == current.runnr && li->luminr > current.luminr)
current.luminr = li->luminr;
if (li->runnr == current.runnr && li->eventnr > current.eventnr)
current.eventnr = li->eventnr;
if (li->runnr == current.runnr && li->runstart >= 0 &&
li->runstart < current.runstart)
current.runstart = li->runstart;
}
}
inline bool orderSamplesByDataset(const VisDQMSample &a,
const VisDQMSample &b) {
// - Order by sample type (ascending).
if (a.type < b.type)
return true;
else if (a.type > b.type)
return false;
// - Order by dataset (descending, natural string comparison).
if (natcmpstr(a.dataset, b.dataset))
return true;
else if (natcmpstr(b.dataset, a.dataset))
return false;
// - Order by run (descending).
if (a.runnr > b.runnr)
return true;
else if (a.runnr < b.runnr)
return false;
// - Order by version (descending, natural string comparison).
if (natcmpstr(b.version, a.version))
return true;
else if (natcmpstr(a.version, b.version))
return false;
// Should not come here, unless the samples are indeed identical, in
// which case we return false
return false;
}
inline bool orderSamplesByRun(const VisDQMSample &a, const VisDQMSample &b) {
// - Order by sample type (ascending).
if (a.type < b.type)
return true;
else if (a.type > b.type)
return false;
// - Order by run (descending).
if (a.runnr > b.runnr)
return true;
else if (a.runnr < b.runnr)
return false;
// - Order by version (descending, natural string comparison).
if (natcmpstr(b.version, a.version))
return true;
else if (natcmpstr(a.version, b.version))
return false;
// - Order by dataset (descending, natural string comparison).
if (natcmpstr(a.dataset, b.dataset))
return true;
else if (natcmpstr(b.dataset, a.dataset))
return false;
// Should not come here, unless the samples are indeed identical, in
// which case we return false
return false;
}
static std::string axisStatsToJSON(uint32_t nbins[3], double mean[3],
double rms[3], double bounds[3][2],
int axis) {
if (std::isfinite(mean[axis]) && std::isfinite(rms[axis]))
return StringFormat(
"{ \"nbins\": %1, \"mean\": %2, \"rms\": %3,"
" \"min\": %4, \"max\": %5 }")
.arg(nbins[axis])
.arg(mean[axis])
.arg(rms[axis])
.arg(bounds[axis][0])
.arg(bounds[axis][1]);
if (!std::isfinite(mean[axis])) {
if (!std::isfinite(rms[axis]))
return StringFormat(
"{ \"nbins\": %1, \"mean\": \"NaN or Inf\", \"rms\": \"Nan or "
"Inf\","
" \"min\": %2, \"max\": %3 }")
.arg(nbins[axis])
.arg(bounds[axis][0])
.arg(bounds[axis][1]);
return StringFormat(
"{ \"nbins\": %1, \"mean\": \"NaN or Inf\", \"rms\": %2,"
" \"min\": %3, \"max\": %4 }")
.arg(nbins[axis])
.arg(rms[axis])
.arg(bounds[axis][0])
.arg(bounds[axis][1]);
}
return StringFormat(
"{ \"nbins\": %1, \"mean\": %2, \"rms\": \"Nan or Inf\","
" \"min\": %3, \"max\": %4 }")
.arg(nbins[axis])
.arg(mean[axis])
.arg(bounds[axis][0])
.arg(bounds[axis][1]);
}
// Format objects to json, with full data if requested.
static void objectToJSON(const std::string &name, const std::string &path,
const char *value, const char *qdata,
DQMNet::DataBlob &rawdata, uint64_t lumisect,
uint32_t flags, uint32_t tag, double nentries,
uint32_t nbins[3], double mean[3], double rms[3],
double bounds[3][2], DQMNet::QReports &qreports,
std::string &qstr, std::string &result) {
if (!result.empty()) result += ", ";
DQMNet::QReports::const_iterator qi, qe;
uint32_t type = flags & DQMNet::DQM_PROP_TYPE_MASK;
uint32_t report = flags & DQMNet::DQM_PROP_REPORT_MASK;
uint32_t qflags = 0;
const char *qsep = "";
qreports.clear();
qstr.clear();
DQMNet::unpackQualityData(qreports, qflags, qdata);
for (qi = qreports.begin(), qe = qreports.end(); qi != qe; ++qi) {
char buf[64];
sprintf(buf, "%.*g", DBL_DIG + 2, qi->qtresult);
qstr += StringFormat(
"%1 {\"status\": %2,\"result\": %3,"
" \"name\": %4, \"algorithm\": %5,"
" \"message\": %6}")
.arg(qsep)
.arg(qi->code)
.arg(buf)
.arg(stringToJSON(qi->qtname))
.arg(stringToJSON(qi->algorithm))
.arg(stringToJSON(qi->message));
qsep = ", ";
}
result +=
StringFormat(
"{\"obj\": %1, \"path\": %2, \"properties\": {\"kind\": \"%3\","
" \"type\": \"%4\", \"lumisect\": \"%5\", \"report\": { \"alarm\": "
"%6,"
" \"error\": %7, \"warn\": %8, \"other\": %9 },"
" \"hasref\": %10, \"isEff\": %11, \"tagged\": %12 },\"tag\": %13,"
" \"qresults\": [%14], \"nentries\": %15,"
" \"stats\": { \"x\": %16, \"y\": %17, \"z\": %18 },"
" \"%19\": \"%20\"}\n")
.arg(stringToJSON(name))
.arg(stringToJSON(path))
.arg(type == DQMNet::DQM_PROP_TYPE_INVALID
? "INVALID"
: type <= DQMNet::DQM_PROP_TYPE_SCALAR ? "SCALAR" : "ROOT")
.arg(
type == DQMNet::DQM_PROP_TYPE_INT
? "INT"
: type == DQMNet::DQM_PROP_TYPE_REAL
? "REAL"
: type == DQMNet::DQM_PROP_TYPE_STRING
? "STRING"
: type == DQMNet::DQM_PROP_TYPE_TH1F
? "TH1F"
: type == DQMNet::DQM_PROP_TYPE_TH1S
? "TH1S"
: type == DQMNet::DQM_PROP_TYPE_TH1D
? "TH1D"
: type == DQMNet::
DQM_PROP_TYPE_TH2F
? "TH2F"
: type == DQMNet::
DQM_PROP_TYPE_TH2S
? "TH2S"
: type == DQMNet::
DQM_PROP_TYPE_TH2D
? "TH2D"
: type == DQMNet::
DQM_PROP_TYPE_TH3F
? "TH3F"
: type == DQMNet::
DQM_PROP_TYPE_TH3S
? "TH3S"
: type == DQMNet::
DQM_PROP_TYPE_TH3D
? "TH3D"
: type == DQMNet::
DQM_PROP_TYPE_TPROF
? "TPROF"
: type == DQMNet::
DQM_PROP_TYPE_TPROF2D
? "TPROF2D"
: type == DQMNet::
DQM_PROP_TYPE_DATABLOB
? "DATABLOB"
: "OTHER")
.arg((unsigned long)lumisect)
.arg((report & DQMNet::DQM_PROP_REPORT_ALARM) ? 1 : 0)
.arg((report & DQMNet::DQM_PROP_REPORT_ERROR) ? 1 : 0)
.arg((report & DQMNet::DQM_PROP_REPORT_WARN) ? 1 : 0)
.arg((report & DQMNet::DQM_PROP_REPORT_OTHER) ? 1 : 0)
.arg((flags & DQMNet::DQM_PROP_HAS_REFERENCE) ? 1 : 0)
.arg((flags & DQMNet::DQM_PROP_EFFICIENCY_PLOT) ? 1 : 0)
.arg((flags & DQMNet::DQM_PROP_TAGGED) ? 1 : 0)
.arg(tag)
.arg(qstr)
.arg(nentries)
.arg(axisStatsToJSON(nbins, mean, rms, bounds, 0))
.arg(axisStatsToJSON(nbins, mean, rms, bounds, 1))
.arg(axisStatsToJSON(nbins, mean, rms, bounds, 2))
.arg(isScalarType(flags) ? "value" : "rootobj")
.arg(isScalarType(flags)
? StringOps::replace(std::string(value), "\"", "\\\"")
: hexlify(rawdata));
}
static void sampleToJSON(const VisDQMSample &sample, std::string &result) {
result += "{\"type\":\"";
result += sampleTypeLabel[sample.type];