-
Notifications
You must be signed in to change notification settings - Fork 0
/
Applications.cpp
executable file
·7578 lines (5888 loc) · 293 KB
/
Applications.cpp
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: Mircho Rodozov [email protected]
* Date 08.01.2013
* Info added at SVN revision 76
*
*/
//#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <new>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <map>
#include <boost/concept_check.hpp>
#include "core/DataObject.h"
#include "core/ExtendedStrip.h"
#include "core/ExtendedRoll.h"
#include "core/Chip.h"
#include "Applications.h"
#include "ROOT/tdrStyle.h"
#include <algorithm>
#include <list>
//#include "ServiceClasses/Service.h"
using namespace std;
void PrintGeometryTable(string InputWithRollNames, string Ouput, DataObject& Areas, DataObject& RawIDFile) {
ifstream IFS; // intput file stream
ofstream OFS; // output file stream
IFS.open(InputWithRollNames.c_str());
OFS.open(Ouput.c_str());
string LineOfInput;//,WheelRing,ID,Opt_ID;
//int WRN,Sector;
while (getline(IFS,LineOfInput)) {
// ParseOnlineRollTypeString(LineOfInput,WheelRing,WRN,Sector,ID,Opt_ID);
// ExRoll * aRoll = new ExRoll(WheelRing,WRN,Sector,ID,Opt_ID);
ExRoll * aRoll = new ExRoll(LineOfInput);
aRoll->setRollRawIDfromSource(RawIDFile);
aRoll->setStripsAreaFromSource_cmsswResource(Areas);
aRoll->WriteGeomTable(OFS);
delete aRoll;
IFS.clear();
}
//cout << "its before the closure" << endl; // debug output
IFS.close();
OFS.close();
}
TH2F * getHVvsRatePlotForOneRollForRunList(string & rollName,const string & resourcePath,const string & suffix,DataObject & RunHVTempDataObject,DataObject & areas) {
ExRoll * aroll ;
string * bufferRunNumber = new string;
double * buffHV = new double;
double * refPress = new double;
double * refTemp = new double;
*buffHV = 0;
*bufferRunNumber = "" ,*refPress = 1010, *refTemp=293;
// check the range of the rate and the HV ... fo the HV is easy , for the rate owful ...
TH2F * histo = new TH2F("h1","HV vs Rate",100,9500,10000,100,0,0.3);
for (int i = 0 ; i < RunHVTempDataObject.getLenght() ; i ++) {
aroll = new ExRoll(rollName);
aroll->setStripsAreaFromSource_cmsswResource(areas);
*bufferRunNumber=RunHVTempDataObject.getElementFromPropertyContainer(i+1,1); // not to use the long name++++++
aroll->setStripsRateFromRootFileSource(resourcePath+suffix+*bufferRunNumber+".root");
if (aroll->isInBarrel()) {
*buffHV = atof(RunHVTempDataObject.getElementFromPropertyContainer(i+1,2).c_str());
}
else {
*buffHV = atof(RunHVTempDataObject.getElementFromPropertyContainer(i+1,2).c_str());
}
*buffHV = *buffHV*((*refPress/atof(RunHVTempDataObject.getElementFromPropertyContainer(i+1,4).c_str()))*(atof(RunHVTempDataObject.getElementFromPropertyContainer(i+1,5).c_str())/(*refTemp)));
if (aroll->getAvgRatePSCWithoutCorrections() > 0) {
histo->Fill(*buffHV,aroll->getAvgRatePSCWithoutCorrections(),3);
}
delete aroll;
}
delete bufferRunNumber,buffHV,refPress,refTemp;
return histo;
}
void GetSingleDoubleGapRatios(string MapOfSingleGapRolls, string regex, string DBfilesContainer, string runlist,string LumiFile,string histoTitle,int histoBins,int histocolor,int histoLineColor) {
// result -> root files with the distribution
// new histogram added 30.12.11 -> Single/Double gap ratio Vs luminosity
// function done , most of the unnecessary sortings has been removed , works really fast and efficient
int divider = 1;
ifstream IFS,IFS2;
string tmpW,tmpID,tmpOID,tmp_r_name
,nextIfEcap,previousIfEcap,
LINE,RUN,theRoll,
bufferMainRollName,bufferPrevRollName,bufferNextRollName;
int tmpWORD,tempSector,nextIfBarrel,previousIfBarrel,FilteredRollsNumber;
IFS.open(MapOfSingleGapRolls.c_str());
istringstream ISS;
stringstream SS; // SS , change this name , you nazi :D
double theRate,tempVal,ratiosSumBuffer=0;
map<string,string> MapOFSGrolls;
map <string,map<string,double> > RateByRuns;
map <string,map<string,double> >::iterator RateByRuns_iter;
map<string,double*> RunNum_LS_Lumi;
map<string, string *> RollContainer;
TCanvas * can1 = new TCanvas ("can1","the title",1200,700);
TCanvas * can2 = new TCanvas ("can2","RatiosAverage",1200,700);
TCanvas * can3 = new TCanvas ("can3","RatiosMean",1200,700);
TH1F * h1 = new TH1F("h1","DG/SG ratio",histoBins,0,10);
TH2F * RatioAverageVsLumi = new TH2F("h2"," Ratios average Vs luminosity",100,0,3600,100,0,10);
TH2F * RatiosDistrubutionMeanVsLumi = new TH2F("h3"," Ratios mean Vs luminosity",100,0,3600,100,0,10);
//RatioAverageVsLumi->AndersonDarlingTest();
//h1->AndersonDarlingTest();
// load in map all SG roll names
ratiosSumBuffer = 0;
while (getline(IFS,LINE)) {
MapOFSGrolls[LINE] = LINE;
IFS.clear();
}
IFS.close();
IFS.open(runlist.c_str());
// SG names loaded
cout << "SG roll names loaded " << endl;
// load the rate for each run
while (getline(IFS,RUN)) {
IFS2.open((DBfilesContainer+"online_database_"+RUN+".txt").c_str());
map <string,double> roll_rate_map;
if (IFS2.is_open()) {
while (getline(IFS2,LINE)) {
ISS.str(LINE);
ISS >> theRoll >> theRate ;
roll_rate_map[theRoll] = theRate;
IFS2.clear();
ISS.clear();
}
IFS2.close();
RateByRuns[RUN] = roll_rate_map;
}
}
// rate loaded
cout << "Rate loaded" << endl;
// load the luminosity as map for each run
DataObject TheLumiDO(LumiFile,3);
for (int i=1;i <= TheLumiDO.getLenght();i++) {
double * pairs = new double[2];
pairs[0] = TheLumiDO.getElementAsInt(i,2);
pairs[1] = TheLumiDO.getElementAsDouble(i,3)/TheLumiDO.getElementAsDouble(i,2);
pairs[1] = pairs[1]/23.31;
RunNum_LS_Lumi[TheLumiDO.getElement(i,1)] = pairs;
}
// luminosity loaded
cout << "Lumi loaded" << endl;
// load a map with all possible roll names where each key is the SG and the two values are the neighbours . Do it once - use the map of the names to search for a rate entries
for (map<string,string>::iterator SingleGapIter = MapOFSGrolls.begin();SingleGapIter != MapOFSGrolls.end();SingleGapIter++) { // loop the rolls
if (SingleGapIter->first.find(regex) != string::npos) {
tmp_r_name = SingleGapIter->first;
// filter whats not in the current substring
ExRoll * aRoll = new ExRoll(tmp_r_name);
string * name_pairs = new string[2];
bufferMainRollName = aRoll->getFullOnlineRollID();
bufferNextRollName = aRoll->getNextNeighbourName();
bufferPrevRollName = aRoll->getPreviousNeighbourName();
name_pairs[0] = bufferPrevRollName;
name_pairs[1] = bufferNextRollName;
RollContainer[bufferMainRollName] = name_pairs;
// debug check , what would happen if the rolls are deleted , does this will lead to destruction of the map values
delete aRoll;
//cout << bufferMainRollName << " " << bufferPrevRollName << " " << bufferNextRollName << endl;
} // end of the filter if conditions
}
// end of loading map with SG and their neighbours
cout << "Map of SG rolls and their neighbours loaded" << endl;
FilteredRollsNumber = RollContainer.size();
cout << "SG rolls map contains " << FilteredRollsNumber << " elements"<< endl;
TH1F * SingleRunRatiosDistributionMean;
for (RateByRuns_iter = RateByRuns.begin() ; RateByRuns_iter != RateByRuns.end() ; RateByRuns_iter++) {
// loop the runs
ratiosSumBuffer = 0;
SingleRunRatiosDistributionMean = new TH1F(RateByRuns_iter->first.c_str(),"",100,0,100); // this histogram is to take the mean for the SG ratio values for each run
for (map<string,string*>::iterator SGMapIter = RollContainer.begin();SGMapIter != RollContainer.end();SGMapIter++) { // loop the rolls
if (RateByRuns_iter->second.find(SGMapIter->first)->second == 0) {
//cout << " The roll"<< aRoll->getFullOnlineRollID() << " for run " << RateByRuns_iter->first << " has rate value 0 , not possible to calculate ratio ..." << endl;
}
tempVal = (RateByRuns_iter->second.find(SGMapIter->second[0])->second+RateByRuns_iter->second.find(SGMapIter->second[1])->second)/ divider; // the average of the neighbours
tempVal = tempVal / RateByRuns_iter->second.find(SGMapIter->first)->second ;
h1->Fill(tempVal);
ratiosSumBuffer+=tempVal;
SingleRunRatiosDistributionMean->Fill(tempVal);
}
// try to find the run number
if (RunNum_LS_Lumi.find(RateByRuns_iter->first) != RunNum_LS_Lumi.end()) {
RatioAverageVsLumi->Fill(RunNum_LS_Lumi.find(RateByRuns_iter->first)->second[1], (ratiosSumBuffer/FilteredRollsNumber) , 2 );
RatiosDistrubutionMeanVsLumi->Fill(RunNum_LS_Lumi.find(RateByRuns_iter->first)->second[1], SingleRunRatiosDistributionMean->GetMean(), 2);
}
delete SingleRunRatiosDistributionMean;
}
setTDRStyle();
can1->SetFillColor(0);
can1->cd();
//h1->SetFillColor(histocolor);
h1->GetYaxis()->SetTitle("Number of entries");
h1->GetXaxis()->SetTitle("DG/SG average rate ratio values");
h1->SetLineColor(histoLineColor);
h1->SetTitle(histoTitle.c_str());
h1->Draw();
can1->SaveAs((histoTitle+".root").c_str());
can1->SaveAs((histoTitle+".png").c_str());
h1->SaveAs((histoTitle+"_histo.root").c_str());
delete h1;
can2->cd();
// put some style for this histos
RatioAverageVsLumi->GetYaxis()->SetTitle("Average Ratio Value");
RatioAverageVsLumi->GetXaxis()->SetTitle("Instantaneous luminosity (10^{30} cm^{-2} s^{-1})");
RatioAverageVsLumi->SetMarkerStyle(kCircle);
RatioAverageVsLumi->SetMarkerColor(kRed);
RatioAverageVsLumi->SetTitle((histoTitle+" Average of ratio values vs luminosity").c_str());
RatioAverageVsLumi->SetStats(NO);
RatioAverageVsLumi->Draw();
can2->SaveAs((histoTitle+"_scatterAverage.root").c_str());
can2->SaveAs((histoTitle+"_scatterAverage.png").c_str());
delete RatioAverageVsLumi;
can3->cd();
RatiosDistrubutionMeanVsLumi->GetYaxis()->SetTitle("Mean value of SG/DG values distribution");
RatiosDistrubutionMeanVsLumi->GetXaxis()->SetTitle("Instantaneous luminosity (10^{30} cm^{-2} s^{-1})");
RatiosDistrubutionMeanVsLumi->SetMarkerStyle(kCircle);
RatiosDistrubutionMeanVsLumi->SetMarkerColor(kBlue);
RatiosDistrubutionMeanVsLumi->SetTitle((histoTitle+" Mean value of each run SG/DG ratio values distribution Vs luminosity").c_str());
RatiosDistrubutionMeanVsLumi->SetStats(NO);
RatiosDistrubutionMeanVsLumi->Draw();
can3->SaveAs((histoTitle+"_scatterMeans.root").c_str());
can3->SaveAs((histoTitle+"_scatterMeans.png").c_str());
delete RatiosDistrubutionMeanVsLumi;
// voila ;)
}
void extractAverageRatesFromRootFilesForListOfFiles(const std::string& runlist, const std::string& pathToRateResources,const string & suffix ,const string & pathTo_TM_Resources,const std::string& areaFileString ,const std::string& outputPath,const int & singleStripCutValue) {
DataObject area;
area.getDataFromFile(areaFileString,2);
// but this one need new method with which the roll could assign its strip rate values from a TH1F object !!! - ok then -> Its done
std::ofstream OFS;
ifstream IFS;
string LINE_IN_RUNLIST,rootFileName,histoCurrentName;
IFS.open(runlist.c_str());
int currentNumberOfNotCountedStrips=0,currentNumberOfCountedStrips;
TFile * file;
while (getline(IFS,LINE_IN_RUNLIST)) {
// open the root file with that name in that directory , loop on the names inside and construct the rolls according to the name
rootFileName = pathToRateResources+suffix+LINE_IN_RUNLIST+".root";
DataObject ToMaskObj;
ToMaskObj.getDataFromFile(pathTo_TM_Resources+LINE_IN_RUNLIST+".txt",2);
OFS.open((outputPath+LINE_IN_RUNLIST).c_str());
TH1F * h1;
file = new TFile(rootFileName.c_str(),"READ","in");
TIter nextkey( file->GetListOfKeys() );
TKey *key;
TObject *obj;
while (key = (TKey*)nextkey()) {
obj = key->ReadObj();
h1 = (TH1F*)obj;
histoCurrentName = h1->GetName();
// populate the rolls here
ExRoll * singleRoll;
if (histoCurrentName.substr(0,1) == "W" || histoCurrentName.substr(0,2) == "RE") {
singleRoll = new ExRoll(histoCurrentName);
singleRoll->setStripsAreaFromSource_cmsswResource(area);
singleRoll->setStripsRatesFromTH1FObject(h1);
// add here logic , if the strip should be masked its rate must go zero
//singleRoll->setStripsToBeMaskedFromSource(ToMaskObj);
currentNumberOfNotCountedStrips = 0;
currentNumberOfCountedStrips = 0;
for (int i=1;i<97;i++) {
//if(singleRoll->getStrip(i)->isAboutToBeMasked()){
if (singleRoll->getStrip(i)->getRate() > singleStripCutValue) {
singleRoll->getStrip(i)->setRate(0);
currentNumberOfNotCountedStrips++;
}
if (singleRoll->getStrip(i)->getRate() > 0) {
currentNumberOfCountedStrips++;
}
}
// ok print here
OFS << singleRoll->getFullOnlineRollID() <<" "<< singleRoll->getAvgRatePSCWithoutCorrections() <<" "<<currentNumberOfNotCountedStrips <<" "<<currentNumberOfCountedStrips+currentNumberOfNotCountedStrips <<"\n";
delete singleRoll;
OFS.clear();
}
OFS.clear();
delete h1,obj;
}
//file->Close();
delete file;
OFS.close();
}
}
void getMeanValuesFromStripsDistributionsForRunlist(const std::string& runlist, const std::string& resourcePath, const std::string& sufix, const std::string& Out) {
ofstream OFS;
OFS.open(Out.c_str());
string LINE,currentRootFile,histoCurrentName;
ifstream IFS;
IFS.open(runlist.c_str());
TFile * rootFile;
while (getline(IFS,LINE)) {
currentRootFile = resourcePath+sufix+LINE+".root";
rootFile = new TFile(currentRootFile.c_str(),"READ","in");
TIter nextkey( rootFile->GetListOfKeys() );
TH1F *h1;
TKey *key;
TObject *obj;
while (key = (TKey*)nextkey()) {
obj = key->ReadObj();
h1 = (TH1F*)obj;
histoCurrentName = h1->GetName();
if (histoCurrentName.find("Strips - WIN") != std::string::npos) {
OFS << LINE <<" "<< h1->GetMean()<<"\n";
}
OFS.clear();
delete h1,obj;
}
rootFile->Delete();
rootFile->Close("R");
delete rootFile;
IFS.clear();
}
IFS.close();
OFS.close();
}
void WriteRollInactiveStripsRatios( string Output, DataObject& Masked, DataObject& Dead, DataObject& Areas, DataObject& RawIDFile, string rollNames,string runNumber){
ofstream ResultsOFS;
ResultsOFS.open(Output.c_str());
string onlineRollName;
ifstream RollNamesIFstream;
RollNamesIFstream.open(rollNames.c_str());
while (getline(RollNamesIFstream,onlineRollName)){
if (onlineRollName.find("RE") != string::npos) continue; // get barrel only
ExRoll * aRoll = new ExRoll(onlineRollName);
aRoll->allocStrips();
aRoll->initStrips();
aRoll->setMaskedStripsFromSource(Masked);
aRoll->setDeadStripsFromSource(Dead);
aRoll->setRollRawIDfromSource(RawIDFile);
int clones = 0;
clones = aRoll->getClones();
if (aRoll->getMasked() > 0 || aRoll->getDead() > 0 ){
for (int clone = 0 ; clone < clones ; clone++ ){
if (aRoll->getClones() == 6 && clone < 2) continue;
double totalStripInClone = (96/clones) - aRoll->getUnplugedFromClone(clone+1);
double maskedInClone = aRoll->getMaskedFromClone(clone+1);
double deadInClone = aRoll->getDeadFromClone(clone+1);
double inactiveInClone = maskedInClone + deadInClone;
ResultsOFS << runNumber << " " << aRoll->getRollIDofCloneWithNewIdentifiers(clone+1) << " ";
ResultsOFS << aRoll->getRawIDofClone(clone+1) << " ";
ResultsOFS << double(maskedInClone/totalStripInClone) << " " << double(deadInClone/totalStripInClone) << " " << double(inactiveInClone/totalStripInClone) << " ";
ResultsOFS << maskedInClone << " " << deadInClone << " " << inactiveInClone << " " << totalStripInClone << "\n";
ResultsOFS.clear();
}
}
delete aRoll;
}
ResultsOFS.close();
RollNamesIFstream.close();
}
void WriteRollsAndStripsFilesForDB_usingRootFile(string rootFile, string OutputRolls,string OutputStrips,string ErrorFile, DataObject& Masked, DataObject& Dead, DataObject& ToMask, DataObject& ToUnmask, DataObject& Areas, DataObject& RawIDFile,string rollNames) {
int exit_code = 0;
ofstream StripsOFS;
ofstream RollsOFS,ErrorOFS;
StripsOFS.open(OutputStrips.c_str());
RollsOFS.open(OutputRolls.c_str());
ErrorOFS.open(ErrorFile.c_str());
ifstream check;
// fill the map with all online names
map <string,bool> onlineNameMap;
map <string,bool>::iterator ONM_iterator;
string onlineRollName;
check.open(rollNames.c_str());
while (getline(check,onlineRollName)) {
onlineNameMap[onlineRollName] = false;
//cout << onlineRollName << endl;
check.clear();
}
check.close();
// end of fill
TFile * rFile = new TFile(rootFile.c_str(),"READ","in");
TIter nextkey(rFile->GetListOfKeys());
TH1F *h1;
TKey *key;
TObject *obj;
while (key = (TKey*)nextkey()) {
h1 = (TH1F*)(key->ReadObj());
string rollName = h1->GetName();
//cout << rollName << endl;
if (onlineNameMap.find(rollName) != onlineNameMap.end()) {
// cout << "--" << rollName << endl;
ExRoll * aRoll = new ExRoll(rollName);
aRoll->allocStrips();
aRoll->initStrips();
aRoll->setMaskedStripsFromSource(Masked);
aRoll->setDeadStripsFromSource(Dead);
aRoll->setStripsToBeMaskedFromSource(ToMask);
aRoll->setStripsToBeUnmaskedFromSource(ToUnmask);
aRoll->setStripsAreaFromSource_cmsswResource(Areas);
for (int cl=1 ; cl <= aRoll->getClones() ; cl++){
//cout << aRoll->getRollIDofClone(cl) << endl;
//cout << aRoll->getRollOfflineIDofClone(cl) << endl;
//cout << aRoll->getRollIDofClone_withEtaPartSeparated(cl) << endl;
// cout << aRoll->getRollIDofCloneWithNewIdentifiers(cl) << endl;
// cout << "---" << endl;
}
aRoll->setRollRawIDfromSource(RawIDFile);
aRoll->setStripsRatesFromTH1FObject(h1);
aRoll->WriteResultForDB(RollsOFS);
aRoll->WriteDetailedResultsForDB(StripsOFS,ErrorOFS);
onlineNameMap[rollName] = true;
delete aRoll;
}
delete h1;
}
rFile->Delete();
rFile->Close("R");
// check the missing names and put the records for them in the ofstreams
for (ONM_iterator = onlineNameMap.begin();ONM_iterator != onlineNameMap.end();ONM_iterator++) {
if (!ONM_iterator->second) {
string r_name;
r_name=ONM_iterator->first;
ExRoll * chamber = new ExRoll(r_name);
chamber->allocStrips();
chamber->initStrips();
chamber->setRollRawIDfromSource(RawIDFile);
for (int k=0;k < chamber->getClones() ; k++) {
RollsOFS << chamber->getRawIDofClone(k+1) <<" "<<chamber->getRollIDofClone(k+1)<<" "<<" -99 -99 -99 -99 -99 -99\n";
RollsOFS.clear();
for (int j=k*(96/chamber->getClones());j < (k+1)*(96/chamber->getClones());j++) {
StripsOFS << chamber->getRawIDofClone(k+1) <<" "<< chamber->getStrip(j+1)->getOnlineNumber() <<" "<< chamber->getStrip(j+1)->getOfflineNumber() <<" -99 -99 -99\n";
StripsOFS.clear();
}
}
delete chamber;
}
}
RollsOFS.close();
StripsOFS.close();
ErrorOFS.close();
}
void WriteResultsUsingIOtextFilesAndDataSources(std::string inputWithRollNames, std::string outputFileName, DataObject & objectWithMasked, DataObject & objectWithDead, DataObject & objectWithRAW) {
DataObject * objWithRollNames = new DataObject;
Roll * aRoll ;
ifstream File;
ofstream OutFile;
string LINE;
File.open(inputWithRollNames.c_str());
OutFile.open(outputFileName.c_str());
objWithRollNames->getDataFromFile(inputWithRollNames,1);
//std::cout << objWithRollNames->getLenght() << std::endl;
for (int i=0;i < objWithRollNames->getLenght() ; i++) {
std::getline(File,LINE);
aRoll = new Roll(LINE);
aRoll->setMaskedStripsFromSource(objectWithMasked);
aRoll->setDeadStripsFromSource(objectWithDead);
aRoll->setRollRawIDfromSource(objectWithRAW);
aRoll->WriteResults(OutFile);
delete aRoll;
}
File.clear();
File.close();
OutFile.clear();
OutFile.close();
delete objWithRollNames;
}
void writeIntrinsicForCMSSW_Rolls(DataObject & lumiResource,DataObject & area,string rootCont, string outCont,string IntrinsicFile,bool PlotOffline,bool PlotOnline) {//,double (*getRateCallBackFunc)(int)){
int count = 0;
ofstream OFS;
ifstream IFS;
string LINE;
// write 'run instLumi' file subroutine
OFS.open("fileCHE");
for (int i = 0; i < lumiResource.getLenght();i++) {
OFS << lumiResource.getElement(i+1,1) << " " << double((atof(lumiResource.getElement(i+1,3).c_str()))/atof(lumiResource.getElement(i+1,2).c_str()))/23.31 <<"\n";
OFS.clear();
}
OFS.close();
DataObject newLumi;
newLumi.getDataFromFile("fileCHE",3);
system("rm fileCHE");
// end of subroutine
map<int,map<string,double* > > runnumberMap; // the int is the runnumber
map<int,map<string,double* > >::iterator runIterator;
map<string,double* >::iterator rollIterator; // the string is the CMSSW id for the roll
map<int,map<string,double* > > runnumberMap_online;
map<string,double* >::iterator rollIterator_online;
map<int,map<string,double* > >::iterator runIterator_online;
for (int i = 1 ; i < newLumi.getLenght()+1 ; i++) { // loop on all runs in newLumi object
ExRoll * roll;
TH1F * h1;
TFile * file = new TFile((rootCont+"total_"+newLumi.getElement(i,1)+".root").c_str(),"READ","in");
cout << "reading file " <<newLumi.getElement(i,1) << endl;
TIter nextkey(file->GetListOfKeys());
TKey * key;
TObject * obj1;
map <string,double*> rollPairs;
map <string,double*> rollPairs_online;
while (key = (TKey*)nextkey()) { // for each file open , loop on the histograms inside
// obj1 = key->ReadObj();
h1 = (TH1F*)key->ReadObj();
string * rollName = new string;
*rollName = h1->GetName();
if (rollName->substr(0,1) == "W" || rollName->substr(0,2) == "RE") { // TODO rewrite this condition
roll = new ExRoll(*rollName);
roll->setStripsRatesFromTH1FObject(h1);
roll->setStripsAreaFromSource_cmsswResource(area);
//cout << " reading file "<<newLumi.getElement(i,1) ;
int * j = new int;
*j = 0;
double ** ptr;
//roll->getRollIDofClone()
ptr=new double * [roll->getClones()]; // should mean two new objects of type "pointer to double"
//cout << "enter while" << endl; // debug
for (*j=0;*j < roll->getClones() ; *j=*j+1) {
ptr[*j]=new double[2];
ptr[*j][0] = 0 ;
ptr[*j][1] = 0 ;
ptr[*j][0] = newLumi.getElementAsDouble(i,2);
ptr[*j][1] = roll->getRollRatePSCFromClone(*j+1); // change this with appropriate callback :D
rollPairs[roll->getRollIDofClone(*j+1)] = ptr[*j];
}
/** the ptr object hold the values to which the rollPairs points to. if we delete it , we delete the values as well*/
//for( *j=0;*j <roll->getClones();*j=*j+1)
// delete [] ptr[*j];
//delete [] ptr;
double * a_ptr = new double[2];
a_ptr[0] = newLumi.getElementAsDouble(i,2);
a_ptr[1] = roll->getAvgRatePSCWithoutCorrections();
rollPairs_online[roll->getFullOnlineRollID()] = a_ptr;
delete roll,j;
}
//h1->Delete();
delete h1;
delete rollName;
}
//
runnumberMap[newLumi.getElementAsInt(i,1)] = rollPairs;
runnumberMap_online[newLumi.getElementAsInt(i,1)] = rollPairs_online;
file->Delete();
file->Close("R");
delete file;
}
/** the data is stored in two maps , loop on it to find each roll rate*/
int iter=0;
map<string,double*> roll_with_values;
map<int,map<string,double*> >::iterator runIterator2;
map<string,double> rollName_intrinsicRate_map;
if (PlotOffline) {
IntrinsicFile = outCont+IntrinsicFile;
OFS.open(IntrinsicFile.c_str());
for (runIterator = runnumberMap.begin() ; (runIterator != runnumberMap.end() && iter == 0); runIterator++) {
//now second loop to get the rolls one by one and to fill histograms
for (rollIterator=runIterator->second.begin();rollIterator!=runIterator->second.end();rollIterator++) {
// for each roll search for it in the runmap by name
//cout <<rollIterator->first ;//<<" ";// << endl;
double * d_ptr;
TCanvas * can = new TCanvas(rollIterator->first.c_str(),rollIterator->first.c_str(),1000,700);
TH2F *h = new TH2F(rollIterator->first.c_str(),"",1000,0,3500,1000,0,10);
TF1 * f1 = new TF1("f1","[0]*pol1",0,3500);
for (runIterator2 = runnumberMap.begin();runIterator2 != runnumberMap.end();runIterator2++) {
roll_with_values = runIterator2->second;
d_ptr = roll_with_values.find(rollIterator->first)->second;//lumi
//cout <<" " << runIterator2->first <<" "<<d_ptr[0] <<" "<<d_ptr[1];
h->Fill(d_ptr[0],d_ptr[1]);
}
can->cd();
h->Fit(f1);
h->Draw();
OFS << rollIterator->first << " " << f1->GetMinimum() <<"\n";
can->SaveAs((outCont+rollIterator->first+".root").c_str());
//h->Delete();can->Delete(); f1->Delete();// delete cause segfault
delete h,can,f1;
cout << endl;
OFS.clear();
}
OFS.close();
iter++;
}
}
iter = 0;
if (PlotOnline) {
//if plotoffline flag is passed as true
IntrinsicFile = outCont+IntrinsicFile+"_online";
OFS.open(IntrinsicFile.c_str());
for (runIterator_online = runnumberMap_online.begin();runIterator_online != runnumberMap_online.end() ;runIterator_online++) {
if (iter == 0) {
int itr=0;
for (rollIterator_online = runIterator_online->second.begin();rollIterator_online != runIterator_online->second.end() && itr < 1232;rollIterator_online++) {
TCanvas * can2 = new TCanvas(rollIterator_online->first.c_str(),rollIterator_online->first.c_str(),1000,700);
TH2F * h2 = new TH2F(rollIterator_online->first.c_str(),rollIterator_online->first.c_str(),1000,0,3500,1000,0,10);
TF1 * f2 = new TF1("f1","[0]*pol1",0,3500);
double * ptr_to_values;
for (runIterator_online = runnumberMap_online.begin();runIterator_online != runnumberMap_online.end() ;runIterator_online++) {
ptr_to_values = runIterator_online->second.find(rollIterator_online->first)->second;
// ptr_to_values = runIterator_online->value.find(rollIterator_online->key)->value;
h2->Fill(ptr_to_values[0],ptr_to_values[1]);
}
itr++;
can2->cd();
h2->Fit(f2);
h2->SetMarkerStyle(kFullCircle);
h2->Draw();
OFS << rollIterator_online->first << " " << f2->GetMinimum() << "\n";
can2->SaveAs((outCont+rollIterator_online->first+".root").c_str());
delete h2,f2,can2;
OFS.clear();
//cout << "inner loop " << rollIterator_online->first << endl;
}
OFS.close();
//cout << " outer loop " << endl;
iter=1;
}
}
//cout << "out of if" << endl;
}
// segmentation fault in the end of the execution , seems rollIterator_online contains more then 1232 elements , use complete map of online rolls for that
}
void plotRateVsLumi_using_the_database_files_rollLevel_Ecap_Offline(DataObject& LumiFile, string filesCont, string outCont, string IntrinsicFile, bool WriteNewIntrinsic,bool subtractIntrinsic,bool averageEntries,string GoodRunsMap,QueryObject * query) {
// TODO add also the Rate vs Runnum as another histogram , it should use the same filters from the QueryObject
// JSON object as a query ? :P
// TODO
//gStyle->SetOptStat(1111110);
int divider = 1;//2; // in case we want the rate to be divided
string gapInTitle = "gap/"; // in case we divide on two ,
map <string,double> run_lumi_map,intrinsic_map;
map <string,double>::iterator run_lumi_map_iter,intrinsic_map_iter,roll_rate_map_iter;
map <string,map<string,double> > run_rollsMap_map;
map <string,map<string,double> >::iterator run_rollsMap_map_iter;
int min_lumi_sections = 0; // minimum lumisections
istringstream iss;
string a_line;
ifstream i_f;
map <string,string> good_runs;
i_f.open(GoodRunsMap.c_str());
while (getline(i_f,a_line)) {
good_runs[a_line] = a_line;
i_f.clear();
}
i_f.close();
for (int i = 0 ; i < LumiFile.getLenght() ; i++) {
if (LumiFile.getElementAsInt(i+1,2) > min_lumi_sections && good_runs.find(LumiFile.getElement(i+1,1)) != good_runs.end()) {// more than 100 lumisections
run_lumi_map[LumiFile.getElement(i+1,1)] = (LumiFile.getElementAsDouble(i+1,3)/LumiFile.getElementAsDouble(i+1,2))/23.31;
}
}
string buffStr;
ifstream IFS;
string roll,RAW_ID;
double rate,rate_pcsq;
int dead,masked,toU,toM;
for (run_lumi_map_iter = run_lumi_map.begin();run_lumi_map_iter != run_lumi_map.end();run_lumi_map_iter++) {
//cout << run_lumi_map_iter->first <<" "<<run_lumi_map_iter->second << endl;
buffStr = filesCont+"offline_database_"+run_lumi_map_iter->first+".txt";
IFS.open(buffStr.c_str());
if (IFS.is_open()) {
map<string,double> roll_rate_map;
//cout << run_lumi_map_iter->first << endl;
// do for each file
int countLines = 0;
while (getline(IFS,buffStr)) {
if (countLines > 0) {
iss.str(buffStr);
rate_pcsq = 0 ;
//iss >> RAW_ID >> roll >> dead >> masked >>toU >> toM >> rate >> rate_pcsq;
iss >> roll >> rate_pcsq;
// temp if condition
if (roll.find("RE") != string::npos && roll.find("CH") != string::npos) {
roll_rate_map[roll] = rate_pcsq;
//cout << roll << " " << rate_pcsq << endl;
}
}
countLines++;
iss.clear();
IFS.clear();
}
run_rollsMap_map[run_lumi_map_iter->first] = roll_rate_map;
}
IFS.close();
}
//
if (WriteNewIntrinsic) {
// fill the histos for the intrinsic here , also create a file to store the results
for (run_rollsMap_map_iter = run_rollsMap_map.begin() ; run_rollsMap_map_iter != run_rollsMap_map.end();run_rollsMap_map_iter++) {
}
}
//if(!WriteNewIntrinsic){
// if the flag is 0 , open the existing intrinsic file
else {
DataObject intrinsicObj(IntrinsicFile,2);
for (int i=0;i< intrinsicObj.getLenght();i++) {
intrinsic_map[intrinsicObj.getElement(i+1,1)] = intrinsicObj.getElementAsDouble(i+1,2);
}
}
double currentIntrinsicValue=0,currentLumi=0;
double rate_sum = 0;
int count_recs =0;
setTDRStyle(); // hell yeah , it works
TLegend * leg;
leg = new TLegend(0.218063,0.606759,0.404552,0.93226);
TCanvas * a_can = new TCanvas("Can_vas","Title",1200,700);
TCanvas * function_canvas = new TCanvas("FC","FC",1200,700);
a_can->cd();
for (int i=0 ; i < query->getHistoCounter() ; i++) {
TH2F * tmpHist = new TH2F(query->getNameForRecord(i+1).c_str(),""/*query->getTitleForRecord(i+1).c_str()*/,1000,query->getOptMapForRecord(i+1).histoMinX,
query->getOptMapForRecord(i+1).histoMaxX,1000,query->getOptMapForRecord(i+1).histoMinY,query->getOptMapForRecord(i+1).histoMaxY
);
double minLumi=50000,maxLumi=0;
// the other loops goes here
for (run_rollsMap_map_iter = run_rollsMap_map.begin() ; run_rollsMap_map_iter != run_rollsMap_map.end();run_rollsMap_map_iter++) {
currentLumi = run_lumi_map.find(run_rollsMap_map_iter->first)->second;
for (roll_rate_map_iter = run_rollsMap_map_iter->second.begin();roll_rate_map_iter != run_rollsMap_map_iter->second.end(); roll_rate_map_iter++) {
currentIntrinsicValue = intrinsic_map.find(roll_rate_map_iter->first)->second;
if (!subtractIntrinsic) {
currentIntrinsicValue = 0;
}
// here all the check for the name will goes
//if (roll_rate_map_iter->first.find(query->getOptMapForRecord(i+1).histoRegexOption) != string::npos){
if (rollFilter(roll_rate_map_iter->first,query->getOptMapForRecord(i+1).disk,query->getOptMapForRecord(i+1).ring,query->getOptMapForRecord(i+1).orientation,query->getOptMapForRecord(i+1).chamberStart,query->getOptMapForRecord(i+1).chamberEnd,query->getOptMapForRecord(i+1).partOfChamber) ) {
//cout << roll_rate_map_iter->first << endl;
if (!averageEntries) {
if (query->getOptMapForRecord(i+1).cutByRunRange
&& atoi(run_rollsMap_map_iter->first.c_str()) >= query->getOptMapForRecord(i+1).runStart
&& atoi(run_rollsMap_map_iter->first.c_str()) <= query->getOptMapForRecord(i+1).runEnd
)
{
/** @brief last point */
tmpHist->Fill(currentLumi,(rate_sum/count_recs)/divider,3);
}
if (query->getOptMapForRecord(i+1).cutByRunRange == false) {
// if there is no cutByRunRange - just fill the dots
//cout << "normal execution " << endl;
tmpHist->Fill(currentLumi,(rate_sum/count_recs)/divider,3);
}
}
rate_sum += roll_rate_map_iter->second - currentIntrinsicValue;
count_recs++;
}
}
if (averageEntries) {
if (query->getOptMapForRecord(i+1).cutByRunRange
&& atoi(run_rollsMap_map_iter->first.c_str()) >= query->getOptMapForRecord(i+1).runStart
&& atoi(run_rollsMap_map_iter->first.c_str()) <= query->getOptMapForRecord(i+1).runEnd
)
{
tmpHist->Fill(currentLumi,(rate_sum/count_recs)/divider,3);
}
if (query->getOptMapForRecord(i+1).cutByRunRange == false) {
tmpHist->Fill(currentLumi,(rate_sum/count_recs)/divider,3);
}
}
if (query->getOptMapForRecord(i+1).cutByRunRange
&& atoi(run_rollsMap_map_iter->first.c_str()) >= query->getOptMapForRecord(i+1).runStart
&& atoi(run_rollsMap_map_iter->first.c_str()) <= query->getOptMapForRecord(i+1).runEnd
)
{
if (minLumi > currentLumi) {
minLumi = currentLumi;
}
if (currentLumi > maxLumi) {
maxLumi = currentLumi;
}
}
rate_sum = 0;
count_recs = 0;
/** @brief find the luminosity range */
}
tmpHist->SetMarkerStyle(query->getOptMapForRecord(i+1).histoMarkerStyle);
tmpHist->SetMarkerColor(query->getOptMapForRecord(i+1).histoColor);
tmpHist->SetTitleSize(0.05);
tmpHist->GetXaxis()->SetTitle(query->getOptMapForRecord(i+1).histoXtitle.c_str());
tmpHist->GetYaxis()->SetTitle(query->getOptMapForRecord(i+1).histoYtitle.c_str());
tmpHist->SetStats(false);
tmpHist->GetCorrelationFactor();
TF1 * func = new TF1(tmpHist->GetName(),"[0]+x*[1]",minLumi,maxLumi);
double par1,par2;
func->SetParName(0,"par0");
func->SetParName(1,"par1");
//func->SetRange(minLumi,maxLumi);
// private case , remove when not needed
//func->SetRange(0,10000); // in case of extrapolation
func->SetLineColor(tmpHist->GetMarkerColor());
func->SetLineWidth(3);
//cout << func->GetLineWidth() << endl;
//tmpHist->Fit(func,"R");
//tmpHist->RebinX();
//func->GetMaximumX()
//cout << func->GetMaximum() << endl;
// tmpHist->GetYaxis()->SetLabelOffset((tmpHist->GetYaxis()->GetLabelOffset())*0.2);
tmpHist->GetYaxis()->SetTitleOffset(tmpHist->GetXaxis()->GetTitleOffset()*0.8);
tmpHist->GetXaxis()->SetTitleOffset((tmpHist->GetXaxis()->GetTitleOffset())*0.9);
tmpHist->GetXaxis()->SetTitleSize(tmpHist->GetYaxis()->GetTitleSize());
tmpHist->GetXaxis()->SetLabelSize(0.04);
tmpHist->GetYaxis()->SetLabelSize(tmpHist->GetXaxis()->GetLabelSize());
leg->AddEntry(tmpHist,tmpHist->GetName(),"p");
if (i+1==1) {
a_can->cd();
tmpHist->Draw();
function_canvas->cd() ;
func->Draw();
}
else {
a_can->cd() ;
tmpHist->Draw("same");
function_canvas->cd() ;
func->Draw("same");
}
//delete func;
//delete tmpHist;
}
// a_can->
a_can->cd();
leg->SetFillColor(0);
leg->SetTextSize(0.06);
leg->SetBorderSize(0);
leg->Draw();
function_canvas->cd();
leg->Draw();
a_can->SaveAs((query->getCanvasTitle()+".root").c_str());
a_can->SaveAs((query->getCanvasTitle()+".png").c_str());
}
void plotEcap_RateVsPhi(string rateFile, bool subtractIntrinsic, string fileWithIntrinsic,DataObject & area ,double cutValueSingleStrip,string SGmapFile,DataObject & LumiFile,QueryObject* query) {
int divider = 1;
TCanvas *c1 = new TCanvas("c1","multigraph",10,10,1200,700);
c1->SetFillColor(0);
TH1F *hr = c1->DrawFrame(0,0,6.282,48);
//TH1F *hr = c1->DrawFrame(0,0,6.282,1.2);
hr->SetXTitle(" #phi (rad) ");
hr->GetXaxis()->SetTitleSize(hr->GetXaxis()->GetTitleSize()*1.5);
hr->GetYaxis()->SetTitleSize(hr->GetYaxis()->GetTitleSize()*1.5);
hr->GetXaxis()->SetTitleOffset(hr->GetXaxis()->GetTitleOffset()*0.8);
hr->GetYaxis()->SetTitleOffset(hr->GetYaxis()->GetTitleOffset()*0.8);
hr->GetXaxis()->SetLabelFont(42);
hr->GetYaxis()->SetLabelFont(42);
hr->SetYTitle("Rate (Hz/cm^{2})");
hr->SetFillColor(0);
TLegend * leg;
//leg->SetTextSize(leg->GetTextSize()*1.2);
leg = new TLegend(0.127019,0.573407,0.312775,0.896122);
TPaveText * pt,* pt2,* secondText,* rateText;
pt = new TPaveText(0.0994983,0.924332,0.253344,0.989614,"NDC"); // NDC sets coords
pt2 = new TPaveText(0.492475,0.81454,0.899666,0.897626,"NDC");
pt->SetTextFont(42);
pt2->SetTextFont(42);
secondText = new TPaveText(0.704112,0.904432,0.906021,0.975069,"NDC");
secondText->SetFillColor(0);
secondText->SetBorderSize(0);
secondText->SetTextSize(0.08);
secondText->AddText("CMS Preliminary");
secondText->SetTextFont(42);