forked from xination/ROOTSCOPE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyDialog_2D.h
1507 lines (996 loc) · 39.9 KB
/
myDialog_2D.h
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
//
// Author : Pei-Luan Tai
// Contact: pt10f@my.fsu.edu
// Last update: Feb 7 2018
//***************************************/
#include <vector>
#include <TH1.h>
#include <TH2.h>
#include <TF1.h>
#include <TF2.h>
#include <TFile.h>
#include <TObjArray.h>
#include <TString.h>
#include <TGFrame.h>
#include <TGLabel.h>
#include <TGButton.h>
#include <TGTextEntry.h>
#include <TGWindow.h>
#include <TKey.h>
#include <TVirtualX.h>
#include <KeySymbols.h>
#include <TObjString.h>
class Dlg_Operation_histo2ds: public TGMainFrame {
private:
TH2* fHisto2d;
vector<TH2*>* fHisto2ds;
TString* fHisto2d_operated;
TString fHisto2d_operated_backup;
TString* fMessage;
TString fEntry_out;
TGVerticalFrame* fVF;
TGHorizontalFrame* fHF_1;
TGHorizontalFrame* fHF_2;
TGVerticalFrame* fHisto_frame;
TGVerticalFrame* fHisto_frame2;
TGLabel* fHisto_nameLabel[200]; // set a upper limit = 200
TGLabel* fLabel;
TGTextEntry* fTextEntries;
TGTextButton* fBtn_close;
TGTextButton* fBtn_cancel;
public:
// class constructor
Dlg_Operation_histo2ds(
const TGWindow* , /* base windows */
UInt_t , /* width */
UInt_t , /* height */
TString* histo2d_list, /* value passing by reference */
vector<TH2*>* histo2ds,
TH2* histo2d,
TString* message );
void To_response_key( Event_t* );
void To_focus_on_entry( Event_t* );
void CloseWindow();
void Cancel();
void Update_entry_output(char*);
private:
void Parse_expression( );
bool Special_case( TString epr );
void General_case( TString epr );
void combo_case( TString epr );
void del_spectra( TString epr );
void to_display_a_TH2( TString epr );
void to_overlap_two_TH2( TString epr);
void to_use_TCutG( TString epr);
void to_exchange_axis(TString epr);
void to_apply_formula( TString epr );
};
void Dlg_Operation_histo2ds::To_response_key(Event_t* e) {
if ( e->fType == kGKeyPress )
{
// using LookuString to get the key mapping.
UInt_t key_symbol;
char tmp[2];
gVirtualX->LookupString( e, tmp,sizeof(tmp), key_symbol );
const unsigned key_enter = 36;
if( key_symbol == kKey_Enter ) { Parse_expression(); }
// the enter key near num pad
else if ( e->fCode == key_enter ) { Parse_expression(); }
// the enter key in regular position.
else if( key_symbol == kKey_Escape ) { CloseWindow(); }
}
}
// this function parse the mathematical exprssion for the histogram
// operations. We will validate the input before apply the expression.
void Dlg_Operation_histo2ds::Parse_expression( ){
TString epr = fEntry_out; // epr = expression
// define the variables for process
Bool_t go_special_case = false; // 99 = 1 (just one equal sign)
Bool_t go_general_case = false; // 99 = 3 * 2 ( one equal and operator sign)
Bool_t hasSingleEqualSign = false;
Bool_t hasEqualSign = false;
Bool_t hasPlusSign = false;
Bool_t hasMinusSign = false;
Bool_t hasTimesSign = false;
Bool_t IsNoOtherOperator = true;
TObjArray* objarray;
Int_t substringN;
// to check single equal sign
objarray = epr.Tokenize( "=" );
substringN = objarray->GetEntries();
if( substringN ==2 ) { hasSingleEqualSign=true; }
// to check "=, +, -, * " sign
Int_t idxEqual = epr.Index("=");
Int_t idxPlus = epr.Index("+");
Int_t idxMinus = epr.Index("-");
Int_t idxTimes = epr.Index("*");
if ( idxEqual != -1 ) hasEqualSign = true;
if ( idxPlus != -1 ) hasPlusSign = true;
if ( idxMinus != -1 ) hasMinusSign = true;
if ( idxTimes != -1 ) hasTimesSign = true;
if( hasSingleEqualSign && !hasPlusSign && !hasMinusSign && !hasTimesSign ) {
// ( just one equal sign )
go_special_case = true;
}
if( hasSingleEqualSign && ( hasPlusSign || hasMinusSign || hasTimesSign) ){
// ( one equal sign + one operator )
if( !epr.Contains("+=") ) go_general_case = true;
}
if( epr.IsDigit() ) { to_display_a_TH2(epr); }
else if( epr.Contains("exchange axis") ) { to_exchange_axis(epr); }
else if( epr.Contains("sum") ) { to_use_TCutG(epr); }
else if( epr.Contains("crop") ) { to_use_TCutG(epr); }
else if( epr.Contains("exclude") ) { to_use_TCutG(epr); }
else if( epr.Contains("overlap") ) { to_overlap_two_TH2(epr); }
else if( epr.Contains("let")) { to_apply_formula( epr); }
else if( epr.Contains("profile") ) { *fMessage = epr; }
else if( go_special_case ) { Special_case( epr ); }
else if( go_general_case ) { General_case( epr ); }
else if( epr.SubString("+=") != "" ) { combo_case( epr ); }
else if( epr.SubString("del") != "" ) { del_spectra( epr); }
CloseWindow();
}
void Dlg_Operation_histo2ds::Cancel(){
// cancel the change, and use the original setting.
*fHisto2d_operated = fHisto2d_operated_backup.Data();
CloseWindow();
}
void Dlg_Operation_histo2ds::CloseWindow(){
fBtn_close->SetState(kButtonDisabled); // to avoid double click.
fBtn_cancel->SetState(kButtonDisabled);
DeleteWindow();
}
void Dlg_Operation_histo2ds::To_focus_on_entry( Event_t* e) {
fTextEntries->SetFocus();
fTextEntries->SelectAll();
}
void Dlg_Operation_histo2ds::Update_entry_output( char* entry_output ) {
fEntry_out = entry_output;
}
void Dlg_Operation_histo2ds::del_spectra( TString epr ){
// del 1..3 means to delete h1, h2, and h3 ( continuous range )
// del 1 5 7 means to delete h1, h5, and h7
Int_t idx1stDot = -1;
Int_t idx2ndDot = -1;
Int_t idxDel = epr.Index("l") + 1;
TString s2 = epr.SubString( ".." );
if( s2 != "" ) { idx1stDot = epr.Index("."); idx2ndDot = idx1stDot + 1; }
// STEP 2 Get operands
TString tmp;
Int_t hIdxRange1 = 0;
Int_t hIdxRange2 = 0;
Bool_t check1;
Bool_t check2;
Bool_t IsValidEpr = false;
if( epr.Contains("..") ) {
tmp = epr( idxDel,idx1stDot);
hIdxRange1 = tmp.Atoi();
hIdxRange1--; // since in the panel, we label histo[0] as spec no.1
tmp = epr(idx2ndDot+1, epr.Length() );
hIdxRange2 = tmp.Atoi();
hIdxRange2--;
check1 = ( hIdxRange1>=0 ) && ( hIdxRange1< fHisto2ds->size() );
check2 = ( hIdxRange2>=hIdxRange1 ) && ( hIdxRange2< fHisto2ds->size() );
if( check1 && check2 ) IsValidEpr = true;
}
if( IsValidEpr ) {
// delete spectra
Int_t del_operation_N = hIdxRange2 - hIdxRange1 + 1;
Int_t starting_idx = hIdxRange1;
for( Int_t i=0; i<del_operation_N; i++ ) {
// make sure we have at least one histogram.
if( fHisto2ds->size() >= 2 ){
fHisto2ds->erase( fHisto2ds->begin() + starting_idx );
}
}
*fMessage = Form("delte spec from %d to %d", hIdxRange1+1,hIdxRange2+1 ) ;
}
if( s2=="" ){
TObjArray* objarray = epr.Tokenize(" "); // separate by space
Int_t substringN = objarray->GetEntries();
vector<Int_t> idx_to_del;
vector<Int_t> idx_to_del_temp;
for( Int_t i = 1; i < substringN; i++ ) {
TString tmp = ( (TObjString*)objarray->At(i) ) -> GetString();
Int_t idx = tmp.Atoi() - 1; // note: we label histo[0] as spec no.1
if( idx < fHisto2ds->size() ) {
idx_to_del_temp.push_back( idx );
}
}
// to sort idx_to_del
while( 1 ){
Int_t min_idx = 999;
Int_t location = -1;
for( Int_t i = 0; i<idx_to_del_temp.size(); i++ )
{
if( idx_to_del_temp.at(i) < min_idx ) {
min_idx = idx_to_del_temp.at(i);
location = i;
}
}
idx_to_del.push_back( min_idx );
idx_to_del_temp.erase( idx_to_del_temp.begin() + location );
if( idx_to_del_temp.size() == 0 ){ break; }
} // ---- end of while loop.
// now we erase the selected histogram.
Int_t starting_idx;
TString msg = "";
for( Int_t i = 0; i<idx_to_del.size(); i++ ){
msg += Form("%d ", idx_to_del.at(i) + 1 );
starting_idx = idx_to_del.at(i) - i;
fHisto2ds->erase( fHisto2ds->begin() + starting_idx );
}
*fMessage = Form("delte spec %s", msg.Data() ) ;
}
}
void Dlg_Operation_histo2ds::combo_case( TString epr ) {
// this block is to deal with "99 += 1..32" and " 99 += * "
// we have checked "+=" sign, before go in to this method.
Int_t idxEqual= epr.Index("=");
Int_t idxPlus = epr.Index("+");
Int_t idxAsterisk = epr.Index("*");
Int_t idx1stDot = -1;
Int_t idx2ndDot = -1;
TString s2 = epr.SubString( ".." );
if( s2 != "" ) { idx1stDot = epr.Index("."); idx2ndDot = idx1stDot + 1; }
// STEP 1 Get operands
TString tmp;
Int_t hOutIdx = 0;
Int_t hIdxRange1 = 0;
Int_t hIdxRange2 = 0;
Bool_t IsValidEpr = false;
Bool_t IsValidEpr2 = false;
if( !epr.Contains("..") )
{
// for: 99 += *
tmp = epr(0,idxEqual);
hOutIdx = tmp.Atoi();
hOutIdx--; // since in the panel, we label histo[0] as spec no.1
if( hOutIdx >=0 ) IsValidEpr = true;
}
else {
// for: 99 += 1..3
tmp = epr(0,idxEqual);
hOutIdx = tmp.Atoi();
hOutIdx--; // since in the panel, we label histo[0] as spec no.1
tmp = epr(idxEqual+1,idx1stDot);
hIdxRange1 = tmp.Atoi();
hIdxRange1--;
tmp = epr(idx2ndDot+1, epr.Length() );
hIdxRange2 = tmp.Atoi();
hIdxRange2--;
bool check1 = ( hOutIdx >=0 );
bool check2 = ( hIdxRange1>=0 ) && ( hIdxRange1< fHisto2ds->size() );
bool check3 = ( hIdxRange2>=hIdxRange1 ) && ( hIdxRange2< fHisto2ds->size() );
if( check1 && check2 && check3 ) IsValidEpr2 = true;
}
Bool_t Is_operation_ok;
TH2* tempHisto;
if( IsValidEpr ){
// then we add up all the spectra
// note: clone is not a good idea
Int_t nbinsx = fHisto2ds->at(0)-> GetXaxis()-> GetNbins();
Double_t xlow = fHisto2ds->at(0)-> GetXaxis()-> GetXmin();
Double_t xup = fHisto2ds->at(0)-> GetXaxis()-> GetXmax();
Int_t nbinsy = fHisto2ds->at(0)-> GetYaxis()-> GetNbins();
Double_t ylow = fHisto2ds->at(0)-> GetYaxis()-> GetXmin();
Double_t yup = fHisto2ds->at(0)-> GetYaxis()-> GetXmax();
TString tempTitle = Form("tempHisto%f", gRandom->Uniform() );
tempHisto = new TH2F( tempTitle.Data(), "tempHisto",
nbinsx, xlow, xup,
nbinsy, ylow, yup );
for( Int_t i=0; i<fHisto2ds->size(); i++ )
{
Is_operation_ok = tempHisto->Add( fHisto2ds->at(i) );
if( !Is_operation_ok ) break;
}
}
if( Is_operation_ok && IsValidEpr ) {
tempHisto->SetTitle("Adding Up all spectra");
Bool_t createNewHisto = false;
if( hOutIdx > 0 && hOutIdx >= fHisto2ds->size() ) createNewHisto = true;
if( createNewHisto )
{
fHisto2ds->push_back( tempHisto );
*fHisto2d_operated = Form( "%d", static_cast<int>(fHisto2ds->size()-1) ) ;
*fMessage =
Form( "\nCreate a new spectrum at %d\n", static_cast<int>(fHisto2ds->size()) );
}
else
{
fHisto2ds->at(hOutIdx) = tempHisto;
*fHisto2d_operated = Form( "%d", hOutIdx) ;
*fMessage = Form( "\nupdate the spectrum at %d\n", hOutIdx+1 );
}
}
else {
*fMessage = Form( "\nInvalid operation.\n" );
}
if( IsValidEpr2 ){
// then we add up spectra from the selected range.
// note: clone is not a good idea
Int_t nbinsx = fHisto2ds->at(0)-> GetXaxis()-> GetNbins();
Double_t xlow = fHisto2ds->at(0)-> GetXaxis()-> GetXmin();
Double_t xup = fHisto2ds->at(0)-> GetXaxis()-> GetXmax();
Int_t nbinsy = fHisto2ds->at(0)-> GetYaxis()-> GetNbins();
Double_t ylow = fHisto2ds->at(0)-> GetYaxis()-> GetXmin();
Double_t yup = fHisto2ds->at(0)-> GetYaxis()-> GetXmax();
TString tempTitle = Form("tempHisto%f", gRandom->Uniform() );
tempHisto = new TH2F( tempTitle.Data(), "tempHisto",
nbinsx, xlow, xup,
nbinsy, ylow, yup );
for( Int_t i=hIdxRange1; i<=hIdxRange2; i++ )
{
Is_operation_ok = tempHisto->Add( fHisto2ds->at(i) );
if( !Is_operation_ok ) break;
}
}
if( Is_operation_ok && IsValidEpr2 ) {
tempHisto->
SetTitle(Form("Adding Up from spec %d to %d", hIdxRange1+1,hIdxRange2+1 ) );
Bool_t createNewHisto = false;
if( hOutIdx > 0 && hOutIdx >= fHisto2ds->size() ) createNewHisto = true;
if( createNewHisto )
{
fHisto2ds->push_back( tempHisto );
*fHisto2d_operated = Form( "%d", static_cast<int>(fHisto2ds->size()-1) ) ;
*fMessage =
Form( "\nCreate a new spectrum at %d\n", static_cast<int>(fHisto2ds->size()) );
}
else
{
fHisto2ds->at(hOutIdx) = tempHisto;
*fHisto2d_operated = Form( "%d", hOutIdx) ;
*fMessage = Form( "\nupdate the spectrum at %d\n", hOutIdx+1 );
}
}
else {
*fMessage = Form( "\nInvalid operation.\n" );
}
}
void Dlg_Operation_histo2ds::to_display_a_TH2( TString epr) {
*fMessage = "to show TH2: ";
TObjArray* tmp_array = epr.Tokenize(" ");
Int_t substringN = tmp_array->GetEntries();
// we will add the valid histo idx to the fHisto2d_operated ( TString )
for( Int_t i = 0; i < substringN; i++ ) {
TString s_tmp = ( (TObjString*)tmp_array->At(i) )->GetString();
int histo_idx = s_tmp.Atoi() - 1;
// validate ( to avoid out the index range )
if( histo_idx >= 0 && histo_idx<fHisto2ds->size() ) {
*fHisto2d_operated += Form( "%d ", histo_idx );
}
}
}
void Dlg_Operation_histo2ds::to_overlap_two_TH2( TString epr) {
TObjArray* tmp_array = epr.Tokenize(" ");
if( tmp_array->GetEntries() >= 3 ) {
//
// to deal with input as "overlap 1 2"
//
*fHisto2d_operated = ( (TObjString*)tmp_array->At(1) )->GetString();
*fHisto2d_operated += " ";
*fHisto2d_operated += ( (TObjString*)tmp_array->At(2) )->GetString();
*fMessage = "to overlap TH2: ";
}
}
void Dlg_Operation_histo2ds::to_exchange_axis( TString epr ) {
*fMessage = "to exchange axis TH2: ";
// to parse the input, and assign the fHisto2d_operated
// ex. "keyword 2" then "2" will be assigned to fHisto2d_operated
// if no TH2 obj number, ex "keyword", then
// we will exchange current TH2 object x <-->y
// the epr = "exchange axis 2" or "exchange axis"
TObjArray* tmp_array = epr.Tokenize(" "); // separate by space
if( tmp_array->GetEntries() >= 3 ) {
*fHisto2d_operated = ( (TObjString*)tmp_array->At(2) )->GetString();
}
else if( tmp_array->GetEntries() == 2 ) {
*fHisto2d_operated = "-99";
}
}
void Dlg_Operation_histo2ds::to_use_TCutG( TString epr ) {
*fMessage = "use_TCutG ";
// to deal with different keywords
if( epr.Contains("sum") ) { *fMessage += "sum"; }
if( epr.Contains("crop") ) { *fMessage += "crop"; }
if( epr.Contains("exclude") ) { *fMessage += "exclude"; }
// to parse the input, and assign the fHisto2d_operated
// ex. "keyword 2" then "2" will be assigned to fHisto2d_operated
// if no TH2 obj number, ex "keyword", then
// we will make a TCutG at the current TH2 object, and do operations.
TObjArray* tmp_array = epr.Tokenize(" "); // separate by space
if( tmp_array->GetEntries() >= 2 ) {
*fHisto2d_operated = ( (TObjString*)tmp_array->At(1) )->GetString();
}
else if( tmp_array->GetEntries() == 1 ) {
*fHisto2d_operated = "-99";
}
}
void Dlg_Operation_histo2ds::to_apply_formula( TString epr ) {
//============================================
// parse epr and create formula
// epr will be
// " let x = x + 1 " subject = x, formula = x + 1
// " let y = x * y + sin( 2*y) " etc...
TObjArray* tmp_array;
tmp_array= epr.Tokenize( "=" );
int sizeN = tmp_array->GetEntries();
TString formula = ( (TObjString*)tmp_array->At( sizeN-1) ) -> GetString();
TString subject = ( (TObjString*)tmp_array->At(0) ) -> GetString();
// when we contain 'y' in the formula, ROOT will treat it as TF2.
// ex. let y = y + 1.
// the actual one is 0*x + y + 1
bool isTF2 = ( formula.Contains( "y") );
//============================================
// to adjust the TH2 according to formula
//
int binxN = fHisto2d->GetXaxis()->GetNbins();
int binyN = fHisto2d->GetYaxis()->GetNbins();
unique_ptr<TH2> histo2d_copy( (TH2*) fHisto2d->Clone() );
bool toX = ( subject.Contains("x") );
if( isTF2 ) {
// formula: x = x + y + 1
// or x = y
// or y = y + 1
unique_ptr<TF2> func( new TF2("func", formula.Data() ) );
if( func->IsValid() )
{
*fMessage = "apply formula";
fHisto2d->Reset();
for( int ix=1; ix<=binxN; ++ix ) {
for( int iy=1; iy<=binyN; ++iy ) {
int counts = histo2d_copy->GetBinContent(ix, iy);
if( counts == 0 ) continue;
double x = fHisto2d->GetXaxis()->GetBinCenter( ix );
double y = fHisto2d->GetYaxis()->GetBinCenter( iy );
if( toX ){
double xNew = func->Eval( x, y );
int ixNew = fHisto2d->GetXaxis()->FindBin( xNew );
fHisto2d->SetBinContent( ixNew, iy, counts );
} else {
double yNew = func->Eval( x, y );
int iyNew = fHisto2d->GetYaxis()->FindBin( yNew );
fHisto2d->SetBinContent( ix , iyNew, counts );
}
}}
}
}
else {
// formula ex. x = x + 1
unique_ptr<TF1> func( new TF1("func", formula.Data() ) );
if( func->IsValid() )
{
*fMessage = "apply formula";
fHisto2d->Reset();
for( int ix=1; ix<=binxN; ++ix ) {
for( int iy=1; iy<=binyN; ++iy ) {
int counts = histo2d_copy->GetBinContent(ix, iy);
if( counts == 0 ) continue;
double x = fHisto2d->GetXaxis()->GetBinCenter( ix );
double y = fHisto2d->GetYaxis()->GetBinCenter( iy );
if( toX ){
double xNew = func->Eval( x );
int ixNew = fHisto2d->GetXaxis()->FindBin( xNew );
fHisto2d->SetBinContent( ixNew, iy, counts );
}
}}
}
}
}
bool Dlg_Operation_histo2ds::Special_case( TString epr ){
// we just have one "=", and no other operators.
TObjArray* objarray;
Int_t substringN;
// STEP 1 Get operands
Bool_t IsValidEpr = true;
// 0 1 2 3 4
// ex. 3 = 2 ( histo[2] = histo[1] )
Int_t hOutIdx = 0;
Int_t hAIdx = 0;
TString tmp;
Int_t idxEqual= epr.Index("=");
tmp = epr(0,idxEqual);
hOutIdx = tmp.Atoi();
hOutIdx--; // since in the panel, we label histo[0] as spec no.1
tmp = epr( idxEqual+1, epr.Length() );
hAIdx = tmp.Atoi();
hAIdx--;
// check hAIdx
if( hAIdx >= fHisto2ds->size() || hAIdx < 0 ) IsValidEpr = false;
if( hOutIdx <0 ) IsValidEpr = false;
if( IsValidEpr ) {
Bool_t createNewHisto = false;
if( hOutIdx > 0 && hOutIdx >= fHisto2ds->size() ) createNewHisto = true;
if( createNewHisto )
{
fHisto2ds->push_back( (TH2*) fHisto2ds->at(hAIdx)->Clone() );
*fHisto2d_operated = Form( "%d", static_cast<int>(fHisto2ds->size()-1) ) ;
*fMessage = Form( "\nCreate a new spectrum at %d\n",
static_cast<int>(fHisto2ds->size()) );
}
else
{
fHisto2ds->at(hOutIdx) = (TH2*) fHisto2ds->at(hAIdx)->Clone();
*fHisto2d_operated = Form( "%d", hOutIdx) ;
*fMessage = Form( "\nupdate the spectrum at %d\n", hOutIdx+1 );
}
return 1;
}
else
{
*fMessage = Form( "\nInvalid operation.\n" );
}
return 0;
}
void Dlg_Operation_histo2ds::General_case( TString epr ){
// 0 1 2 3 4
// ex. 2 = 2 + 1 ( histo[1] = histo[1] + histo[0]
// ex 99 = 3 * 2 ( last one = 3 * histo[1] )
// STEP1: Get operands
Int_t hOutIdx = 0;
Int_t hAIdx = 0;
Int_t hBIdx = 0;
Double_t mul = 0;
TString tmp;
Bool_t hasEqualSign = false;
Bool_t hasPlusSign = false;
Bool_t hasMinusSign = false;
Bool_t hasTimesSign = false;
Int_t idxEqual = epr.Index("=");
Int_t idxPlus = epr.Index("+");
Int_t idxMinus = epr.Index("-");
Int_t idxTimes = epr.Index("*");
if ( idxEqual != -1 ) hasEqualSign = true;
if ( idxPlus != -1 ) hasPlusSign = true;
if ( idxMinus != -1 ) hasMinusSign = true;
if ( idxTimes != -1 ) hasTimesSign = true;
tmp = epr(0,idxEqual); // slice it from index=0 to index=idxEqual
hOutIdx = tmp.Atoi();
hOutIdx--; // since in the panel, we label histo[0] as spec no.1
if( hasPlusSign ) {
// hout = hA + hB
tmp = epr( idxEqual+1, idxPlus);
hAIdx = tmp.Atoi();
hAIdx--;
tmp = epr( idxPlus+1, epr.Length() );
hBIdx = tmp.Atoi();
hBIdx--;
}
else if ( hasMinusSign && !hasTimesSign ) {
// hout = hA - hB
tmp = epr( idxEqual+1, idxMinus);
hAIdx = tmp.Atoi();
hAIdx--;
tmp = epr( idxMinus+1, epr.Length() );
hBIdx = tmp.Atoi();
hBIdx--;
}
else if( hasTimesSign ) {
// hout = mul * hB
tmp = epr( idxEqual+1, idxTimes);
mul = tmp.Atof();
tmp = epr( idxTimes+1, epr.Length() );
hBIdx = tmp.Atoi();
hBIdx--;
}
bool createNewHisto = false;
bool IsValidEpr = true;
// check hAIdx and hBIdx is within the range of histo2ds.
if( hAIdx > fHisto2ds->size() || hAIdx < 0 ) IsValidEpr = false;
if( hBIdx > fHisto2ds->size() || hBIdx < 0 ) IsValidEpr = false;
if( hOutIdx <0 ) IsValidEpr = false;
if( IsValidEpr ) {
Bool_t Is_operation_ok = false;
// note: clone is not a good idea
Int_t nbinsx = fHisto2ds->at( hBIdx)-> GetXaxis()-> GetNbins();
Double_t xlow = fHisto2ds->at( hBIdx)-> GetXaxis()-> GetXmin();
Double_t xup = fHisto2ds->at( hBIdx)-> GetXaxis()-> GetXmax();
Int_t nbinsy = fHisto2ds->at( hBIdx)-> GetYaxis()-> GetNbins();
Double_t ylow = fHisto2ds->at( hBIdx)-> GetYaxis()-> GetXmin();
Double_t yup = fHisto2ds->at( hBIdx)-> GetYaxis()-> GetXmax();
TH2* tempHisto = new TH2F( "tempHisto", "tempHisto",
nbinsx, xlow, xup,
nbinsy, ylow, yup );
if( hasPlusSign )
{
// hout = hA + hB
Is_operation_ok
= tempHisto->Add( fHisto2ds->at(hAIdx) , fHisto2ds->at(hBIdx) );
if( Is_operation_ok )
{
tempHisto -> SetTitle( Form("spec%02d + spec%02d", hAIdx+1, hBIdx+1 ));
tempHisto -> SetName ( Form("spec%02d + spec%02d", hAIdx+1, hBIdx+1 ));
}
}
else if ( hasMinusSign && !hasTimesSign )
{
// hout = hA - hB
Is_operation_ok
= tempHisto->Add( fHisto2ds->at(hAIdx), fHisto2ds->at(hBIdx), 1 , -1 );
if( Is_operation_ok )
{
tempHisto -> SetTitle( Form("spec%0d - spec%0d", hAIdx+1, hBIdx+1 ));
tempHisto -> SetName ( Form("spec%0d - spec%0d", hAIdx+1, hBIdx+1 ));
}
}
else if ( hasTimesSign )
{
// hout = mul * hB
Is_operation_ok
= tempHisto->Add( fHisto2ds->at(hBIdx), mul );
if( Is_operation_ok )
{
tempHisto -> SetTitle( Form("spec%02d * (%.1f)", hBIdx+1, mul ) );
tempHisto -> SetName ( Form("spec%02d * (%.1f)", hBIdx+1, mul ) );
}
}
if( hOutIdx > 0 && hOutIdx >= fHisto2ds->size() ) createNewHisto = true;
if( Is_operation_ok )
{
if( createNewHisto )
{
fHisto2ds->push_back( tempHisto );
*fHisto2d_operated = Form( "%d", static_cast<int>(fHisto2ds->size()-1) ) ;
*fMessage = Form( "\nCreate a new spectrum at %d\n",
static_cast<int>(fHisto2ds->size()) );
}
else
{
fHisto2ds->at(hOutIdx) = tempHisto;
*fHisto2d_operated = Form( "%d", hOutIdx) ;
*fMessage = Form( "\nupdate the spectrum at %d\n", hOutIdx+1 );
}
}
}
else
{
*fMessage = Form( "\nInvalid operation.\n" );
}
}
// class constructor
Dlg_Operation_histo2ds::Dlg_Operation_histo2ds(
const TGWindow* p,
UInt_t w,
UInt_t h,
TString* histo2d_list,
vector<TH2*>* histo2ds,
TH2* histo2d,
TString* message )
: TGMainFrame(p, w, h) {
SetCleanup(kDeepCleanup); // important step for closing windows properly.
fHisto2d_operated = histo2d_list;
fHisto2d_operated_backup= histo2d_list->Copy();
fHisto2d = histo2d; // the current active TH2.
fHisto2ds = histo2ds;
fMessage = message;
TGLayoutHints* Layout1 = new TGLayoutHints( kLHintsCenterY, 2, 2, 2, 2);
TGLayoutHints* Layout2 = new TGLayoutHints( kLHintsExpandX, 2, 2, 2, 2);
TGLayoutHints* Layout3 = new TGLayoutHints( kLHintsLeft, 2, 2, 2, 2);
fHisto_frame =new TGVerticalFrame( this, 10, 10);
Int_t nRow = 10;
Int_t nCol = 0;
fHisto_frame->SetLayoutManager ( new TGMatrixLayout( fHisto_frame , nRow, nCol) );
fHisto_frame2 =new TGVerticalFrame( this, 10, 10);
fHisto_frame2->SetLayoutManager ( new TGMatrixLayout( fHisto_frame2, nRow, nCol) );
//-------------------------------------------------------| Labels
TString outstr;
TString histo2d_title;
// to set 10 entries for each column.
// for 101 to 200 entry we will put them into the secon layer.
int n0 = histo2ds->size() ;
int n1 = histo2ds->size() / 10;
int n2 = ( n1 + 1) *10;
if (n2 <= 10 ) n2 =20;
if( n0 <= 100 ) {